Skip to content

Commit 367c5c6

Browse files
andreabediniangerman
authored andcommitted
feat: support generated cmm-sources
1 parent 98242d4 commit 367c5c6

File tree

10 files changed

+157
-87
lines changed

10 files changed

+157
-87
lines changed

Cabal-syntax/src/Distribution/PackageDescription/FieldGrammar.hs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ libraryFieldGrammar
174174
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
175175
, c (List CommaVCat (Identity Dependency) Dependency)
176176
, c (List CommaVCat (Identity Mixin) Mixin)
177-
, c (List VCat (Identity ExtraSource) ExtraSource)
177+
, c (List VCat (Identity (ExtraSource Pkg)) (ExtraSource Pkg))
178+
, c (List VCat (Identity (ExtraSource Build)) (ExtraSource Build))
178179
, c (List CommaVCat (Identity ModuleReexport) ModuleReexport)
179180
, c (List FSep (MQuoted Extension) Extension)
180181
, c (List FSep (MQuoted Language) Language)
@@ -225,7 +226,8 @@ foreignLibFieldGrammar
225226
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
226227
, c (List CommaVCat (Identity Dependency) Dependency)
227228
, c (List CommaVCat (Identity Mixin) Mixin)
228-
, c (List VCat (Identity ExtraSource) ExtraSource)
229+
, c (List VCat (Identity (ExtraSource Pkg)) (ExtraSource Pkg))
230+
, c (List VCat (Identity (ExtraSource Build)) (ExtraSource Build))
229231
, c (List FSep (Identity ForeignLibOption) ForeignLibOption)
230232
, c (List FSep (MQuoted Extension) Extension)
231233
, c (List FSep (MQuoted Language) Language)
@@ -265,7 +267,8 @@ executableFieldGrammar
265267
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
266268
, c (List CommaVCat (Identity Dependency) Dependency)
267269
, c (List CommaVCat (Identity Mixin) Mixin)
268-
, c (List VCat (Identity ExtraSource) ExtraSource)
270+
, c (List VCat (Identity (ExtraSource Pkg)) (ExtraSource Pkg))
271+
, c (List VCat (Identity (ExtraSource Build)) (ExtraSource Build))
269272
, c (List FSep (MQuoted Extension) Extension)
270273
, c (List FSep (MQuoted Language) Language)
271274
, c (List FSep Token String)
@@ -342,7 +345,8 @@ testSuiteFieldGrammar
342345
, c (List CommaFSep Token String)
343346
, c (List CommaVCat (Identity Dependency) Dependency)
344347
, c (List CommaVCat (Identity Mixin) Mixin)
345-
, c (List VCat (Identity ExtraSource) ExtraSource)
348+
, c (List VCat (Identity (ExtraSource Pkg)) (ExtraSource Pkg))
349+
, c (List VCat (Identity (ExtraSource Build)) (ExtraSource Build))
346350
, c (List FSep (MQuoted Extension) Extension)
347351
, c (List FSep (MQuoted Language) Language)
348352
, c (List FSep Token String)
@@ -487,7 +491,8 @@ benchmarkFieldGrammar
487491
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
488492
, c (List CommaVCat (Identity Dependency) Dependency)
489493
, c (List CommaVCat (Identity Mixin) Mixin)
490-
, c (List VCat (Identity ExtraSource) ExtraSource)
494+
, c (List VCat (Identity (ExtraSource Pkg)) (ExtraSource Pkg))
495+
, c (List VCat (Identity (ExtraSource Build)) (ExtraSource Build))
491496
, c (List FSep (MQuoted Extension) Extension)
492497
, c (List FSep (MQuoted Language) Language)
493498
, c (List FSep Token String)
@@ -590,7 +595,8 @@ buildInfoFieldGrammar
590595
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
591596
, c (List CommaVCat (Identity Dependency) Dependency)
592597
, c (List CommaVCat (Identity Mixin) Mixin)
593-
, c (List VCat (Identity ExtraSource) ExtraSource)
598+
, c (List VCat (Identity (ExtraSource Pkg)) (ExtraSource Pkg))
599+
, c (List VCat (Identity (ExtraSource Build)) (ExtraSource Build))
594600
, c (List FSep (MQuoted Extension) Extension)
595601
, c (List FSep (MQuoted Language) Language)
596602
, c (List FSep Token String)
@@ -637,6 +643,8 @@ buildInfoFieldGrammar =
637643
^^^ availableSince CabalSpecV3_0 []
638644
<*> monoidalFieldAla "cmm-sources" formatExtraSources L.cmmSources
639645
^^^ availableSince CabalSpecV3_0 []
646+
<*> monoidalFieldAla "autogen-cmm-sources" formatExtraSources L.autogenCmmSources
647+
-- FIXME ^^^ availableSince CabalSpecV3_0 []
640648
<*> monoidalFieldAla "c-sources" formatExtraSources L.cSources
641649
<*> monoidalFieldAla "cxx-sources" formatExtraSources L.cxxSources
642650
^^^ availableSince CabalSpecV2_2 []
@@ -840,7 +848,7 @@ formatOtherExtensions = alaList' FSep MQuoted
840848
formatOtherModules :: [ModuleName] -> List VCat (MQuoted ModuleName) ModuleName
841849
formatOtherModules = alaList' VCat MQuoted
842850

843-
formatExtraSources :: [ExtraSource] -> List VCat (Identity ExtraSource) ExtraSource
851+
formatExtraSources :: [ExtraSource pkg] -> List VCat (Identity (ExtraSource pkg)) (ExtraSource pkg)
844852
formatExtraSources = alaList' VCat Identity
845853

846854
-------------------------------------------------------------------------------

Cabal-syntax/src/Distribution/Types/BuildInfo.hs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,17 @@ data BuildInfo = BuildInfo
7272
-- ^ support frameworks for Mac OS X
7373
, extraFrameworkDirs :: [SymbolicPath Pkg (Dir Framework)]
7474
-- ^ extra locations to find frameworks
75-
, asmSources :: [ExtraSource]
75+
, asmSources :: [ExtraSource Pkg]
7676
-- ^ Assembly source files
77-
, cmmSources :: [ExtraSource]
77+
, cmmSources :: [ExtraSource Pkg]
7878
-- ^ C-- source files
79-
, cSources :: [ExtraSource]
79+
, autogenCmmSources :: [ExtraSource Build]
80+
-- ^ C-- generated source files
81+
, cSources :: [ExtraSource Pkg]
8082
-- ^ C source files
81-
, cxxSources :: [ExtraSource]
83+
, cxxSources :: [ExtraSource Pkg]
8284
-- ^ C++ source files
83-
, jsSources :: [ExtraSource]
85+
, jsSources :: [ExtraSource Pkg]
8486
-- ^ JavaScript source file
8587
, hsSourceDirs :: [SymbolicPath Pkg (Dir Source)]
8688
-- ^ where to look for the Haskell module hierarchy
@@ -172,6 +174,7 @@ instance Monoid BuildInfo where
172174
, extraFrameworkDirs = []
173175
, asmSources = []
174176
, cmmSources = []
177+
, autogenCmmSources = []
175178
, cSources = []
176179
, cxxSources = []
177180
, jsSources = []
@@ -225,6 +228,7 @@ instance Semigroup BuildInfo where
225228
, extraFrameworkDirs = combineNub extraFrameworkDirs
226229
, asmSources = combineNub asmSources
227230
, cmmSources = combineNub cmmSources
231+
, autogenCmmSources = combineNub autogenCmmSources
228232
, cSources = combineNub cSources
229233
, cxxSources = combineNub cxxSources
230234
, jsSources = combineNub jsSources

Cabal-syntax/src/Distribution/Types/BuildInfo/Lens.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Distribution.Types.BuildInfo (BuildInfo)
1616
import Distribution.Types.ExtraSource (ExtraSource)
1717
import Distribution.Types.Dependency (Dependency)
1818
import Distribution.Types.ExeDependency (ExeDependency)
19+
import Distribution.Types.ExtraSource (ExtraSource)
1920
import Distribution.Types.LegacyExeDependency (LegacyExeDependency)
2021
import Distribution.Types.Mixin (Mixin)
2122
import Distribution.Types.PkgconfigDependency (PkgconfigDependency)
@@ -80,23 +81,27 @@ class HasBuildInfo a where
8081
extraFrameworkDirs = buildInfo . extraFrameworkDirs
8182
{-# INLINE extraFrameworkDirs #-}
8283

83-
asmSources :: Lens' a [ExtraSource]
84+
asmSources :: Lens' a [ExtraSource Pkg]
8485
asmSources = buildInfo . asmSources
8586
{-# INLINE asmSources #-}
8687

87-
cmmSources :: Lens' a [ExtraSource]
88+
autogenCmmSources :: Lens' a [ExtraSource Build]
89+
autogenCmmSources = buildInfo . autogenCmmSources
90+
{-# INLINE autogenCmmSources #-}
91+
92+
cmmSources :: Lens' a [ExtraSource Pkg]
8893
cmmSources = buildInfo . cmmSources
8994
{-# INLINE cmmSources #-}
9095

91-
cSources :: Lens' a [ExtraSource]
96+
cSources :: Lens' a [ExtraSource Pkg]
9297
cSources = buildInfo . cSources
9398
{-# INLINE cSources #-}
9499

95-
cxxSources :: Lens' a [ExtraSource]
100+
cxxSources :: Lens' a [ExtraSource Pkg]
96101
cxxSources = buildInfo . cxxSources
97102
{-# INLINE cxxSources #-}
98103

99-
jsSources :: Lens' a [ExtraSource]
104+
jsSources :: Lens' a [ExtraSource Pkg]
100105
jsSources = buildInfo . jsSources
101106
{-# INLINE jsSources #-}
102107

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,79 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE DeriveDataTypeable #-}
23
{-# LANGUAGE DeriveGeneric #-}
3-
{-# LANGUAGE DataKinds #-}
4+
{-# LANGUAGE FlexibleInstances #-}
5+
{-# LANGUAGE TypeFamilies #-}
46

57
module Distribution.Types.ExtraSource
68
( ExtraSource (..)
7-
, extraSourceFromPath
9+
, ExtraSourceClass (..)
810
) where
911

1012
import Distribution.Compat.Prelude
1113
import Prelude ()
1214

1315
import Distribution.Parsec
1416
import Distribution.Pretty
15-
import Distribution.Utils.Path (SymbolicPath, FileOrDir(..), Pkg)
17+
import Distribution.Utils.Path (Build, FileOrDir (..), Pkg, RelativePath, SymbolicPath, relativeSymbolicPath, unsafeCoerceSymbolicPath)
1618

1719
import qualified Distribution.Compat.CharParsing as P
1820
import qualified Text.PrettyPrint as PP
19-
import Distribution.FieldGrammar.Newtypes (SymbolicPathNT(..))
2021

21-
data ExtraSource = ExtraSource
22-
{ extraSourceFile :: SymbolicPath Pkg File
23-
, extraSourceOpts :: [String]
24-
}
22+
data family ExtraSource pkg
23+
24+
data instance ExtraSource Pkg = ExtraSourcePkg (SymbolicPath Pkg File) [String]
25+
deriving (Generic, Show, Read, Eq, Ord, Typeable, Data)
26+
27+
data instance ExtraSource Build = ExtraSourceBuild (RelativePath Build File) [String]
2528
deriving (Generic, Show, Read, Eq, Ord, Typeable, Data)
2629

27-
instance Binary ExtraSource
28-
instance Structured ExtraSource
29-
instance NFData ExtraSource where rnf = genericRnf
30+
class ExtraSourceClass e where
31+
extraSourceOpts :: e -> [String]
32+
extraSourceFile :: e -> SymbolicPath Pkg 'File
33+
34+
instance ExtraSourceClass (ExtraSource Pkg) where
35+
extraSourceOpts (ExtraSourcePkg _ opts) = opts
36+
extraSourceFile (ExtraSourcePkg f _) = f
37+
38+
instance ExtraSourceClass (ExtraSource Build) where
39+
extraSourceOpts (ExtraSourceBuild _ opts) = opts
40+
41+
-- FIXME
42+
extraSourceFile (ExtraSourceBuild f _) = unsafeCoerceSymbolicPath (relativeSymbolicPath f)
3043

31-
instance Parsec ExtraSource where
44+
instance Binary (ExtraSource Pkg)
45+
instance Structured (ExtraSource Pkg)
46+
instance NFData (ExtraSource Pkg) where rnf = genericRnf
47+
48+
instance Binary (ExtraSource Build)
49+
instance Structured (ExtraSource Build)
50+
instance NFData (ExtraSource Build) where rnf = genericRnf
51+
52+
instance Parsec (ExtraSource Pkg) where
3253
parsec = do
33-
SymbolicPathNT path <- parsec <* P.spaces
34-
opts <- P.optional (parensLax (P.sepBy p P.spaces))
35-
return (ExtraSource path (fromMaybe mempty opts))
54+
path <- parsec <* P.spaces
55+
opts <- P.optional (parensLax (P.sepBy p P.spaces))
56+
return (ExtraSourcePkg path (fromMaybe mempty opts))
3657
where
3758
p :: P.CharParsing p => p String
38-
p = some $ P.satisfy (\c -> not (isSpace c) && not (c == ')'))
59+
p = some $ P.satisfy (\c -> not (isSpace c) && (c /= ')'))
3960

40-
parensLax :: (P.CharParsing m) => m a -> m a
41-
parensLax p = P.between (P.char '(' *> P.spaces) (P.char ')' *> P.spaces) p
61+
instance Parsec (ExtraSource Build) where
62+
parsec = do
63+
path <- parsec <* P.spaces
64+
opts <- P.optional (parensLax (P.sepBy p P.spaces))
65+
return (ExtraSourceBuild path (fromMaybe mempty opts))
66+
where
67+
p :: P.CharParsing p => p String
68+
p = some $ P.satisfy (\c -> not (isSpace c) && (c /= ')'))
4269

43-
instance Pretty ExtraSource where
44-
pretty (ExtraSource path opts) =
45-
pretty (SymbolicPathNT path) <<>> PP.parens (PP.hsep (map PP.text opts))
70+
instance Pretty (ExtraSource Pkg) where
71+
pretty (ExtraSourcePkg path opts) =
72+
pretty path <<>> PP.parens (PP.hsep (map PP.text opts))
4673

47-
extraSourceFromPath :: SymbolicPath Pkg File -> ExtraSource
48-
extraSourceFromPath fp = ExtraSource fp mempty
74+
instance Pretty (ExtraSource Build) where
75+
pretty (ExtraSourceBuild path opts) =
76+
pretty path <<>> PP.parens (PP.hsep (map PP.text opts))
77+
78+
parensLax :: P.CharParsing m => m a -> m a
79+
parensLax p = P.between (P.char '(' *> P.spaces) (P.char ')' *> P.spaces) p

Cabal-syntax/src/Distribution/Utils/Path.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ data CWD
460460
-- | Abstract directory: package directory (e.g. a directory containing the @.cabal@ file).
461461
--
462462
-- See Note [Symbolic paths] in Distribution.Utils.Path.
463-
data Pkg
463+
data Pkg deriving (Data)
464464

465465
-- | Abstract directory: dist directory (e.g. @dist-newstyle@).
466466
--
@@ -490,7 +490,7 @@ data Framework
490490
-- | Abstract directory: build directory.
491491
--
492492
-- See Note [Symbolic paths] in Distribution.Utils.Path.
493-
data Build
493+
data Build deriving (Data)
494494

495495
-- | Abstract directory: directory for build artifacts, such as documentation or @.hie@ files.
496496
--

Cabal/src/Distribution/Simple/Build.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -643,35 +643,35 @@ generateCode codeGens nm pdesc bi lbi clbi verbosity = do
643643
addExtraCSources :: BuildInfo -> [SymbolicPath Pkg File] -> BuildInfo
644644
addExtraCSources bi extras = bi{cSources = new}
645645
where
646-
new = ordNub (map extraSourceFromPath extras ++ cSources bi)
646+
new = ordNub (map (flip ExtraSourcePkg []) extras ++ cSources bi)
647647

648648
-- | Add extra C++ sources generated by preprocessing to build
649649
-- information.
650650
addExtraCxxSources :: BuildInfo -> [SymbolicPath Pkg File] -> BuildInfo
651651
addExtraCxxSources bi extras = bi{cxxSources = new}
652652
where
653-
new = ordNub (map extraSourceFromPath extras ++ cxxSources bi)
653+
new = ordNub (map (flip ExtraSourcePkg []) extras ++ cxxSources bi)
654654

655655
-- | Add extra C-- sources generated by preprocessing to build
656656
-- information.
657657
addExtraCmmSources :: BuildInfo -> [SymbolicPath Pkg File] -> BuildInfo
658658
addExtraCmmSources bi extras = bi{cmmSources = new}
659659
where
660-
new = ordNub (map extraSourceFromPath extras ++ cmmSources bi)
660+
new = ordNub (map (flip ExtraSourcePkg []) extras ++ cmmSources bi)
661661

662662
-- | Add extra ASM sources generated by preprocessing to build
663663
-- information.
664664
addExtraAsmSources :: BuildInfo -> [SymbolicPath Pkg File] -> BuildInfo
665665
addExtraAsmSources bi extras = bi{asmSources = new}
666666
where
667-
new = ordNub (map extraSourceFromPath extras ++ asmSources bi)
667+
new = ordNub (map (flip ExtraSourcePkg []) extras ++ asmSources bi)
668668

669669
-- | Add extra JS sources generated by preprocessing to build
670670
-- information.
671671
addExtraJsSources :: BuildInfo -> [SymbolicPath Pkg File] -> BuildInfo
672672
addExtraJsSources bi extras = bi{jsSources = new}
673673
where
674-
new = ordNub (map extraSourceFromPath extras ++ jsSources bi)
674+
new = ordNub (map (flip ExtraSourcePkg []) extras ++ jsSources bi)
675675

676676
-- | Add extra HS modules generated by preprocessing to build
677677
-- information.
@@ -717,7 +717,7 @@ replComponent
717717
preprocessComponent pkg_descr comp lbi clbi False verbosity suffixHandlers
718718
extras <- preprocessExtras verbosity comp lbi
719719
let libbi = libBuildInfo lib
720-
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map extraSourceFromPath extras}}
720+
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map (flip ExtraSourcePkg []) extras}}
721721
replLib replFlags pkg lbi lib' libClbi
722722
replComponent
723723
replFlags
@@ -734,23 +734,23 @@ replComponent
734734
case comp of
735735
CLib lib -> do
736736
let libbi = libBuildInfo lib
737-
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map extraSourceFromPath extras}}
737+
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map (flip ExtraSourcePkg []) extras}}
738738
replLib replFlags pkg_descr lbi lib' clbi
739739
CFLib flib ->
740740
replFLib replFlags pkg_descr lbi flib clbi
741741
CExe exe -> do
742742
let ebi = buildInfo exe
743-
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath extras}}
743+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map (flip ExtraSourcePkg []) extras}}
744744
replExe replFlags pkg_descr lbi exe' clbi
745745
CTest test@TestSuite{testInterface = TestSuiteExeV10{}} -> do
746746
let exe = testSuiteExeV10AsExe test
747747
let ebi = buildInfo exe
748-
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath extras}}
748+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map (flip ExtraSourcePkg []) extras}}
749749
replExe replFlags pkg_descr lbi exe' clbi
750750
CBench bm@Benchmark{benchmarkInterface = BenchmarkExeV10{}} -> do
751751
let exe = benchmarkExeV10asExe bm
752752
let ebi = buildInfo exe
753-
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath extras}}
753+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map (flip ExtraSourcePkg []) extras}}
754754
replExe replFlags pkg_descr lbi exe' clbi
755755
#if __GLASGOW_HASKELL__ < 811
756756
-- silence pattern-match warnings prior to GHC 9.0

0 commit comments

Comments
 (0)