Skip to content

Commit 4360a1e

Browse files
committed
feat: support generated cmm-sources
1 parent 43e84d8 commit 4360a1e

File tree

12 files changed

+164
-92
lines changed

12 files changed

+164
-92
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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,27 @@ class HasBuildInfo a where
8080
extraFrameworkDirs = buildInfo . extraFrameworkDirs
8181
{-# INLINE extraFrameworkDirs #-}
8282

83-
asmSources :: Lens' a [ExtraSource]
83+
asmSources :: Lens' a [ExtraSource Pkg]
8484
asmSources = buildInfo . asmSources
8585
{-# INLINE asmSources #-}
8686

87-
cmmSources :: Lens' a [ExtraSource]
87+
autogenCmmSources :: Lens' a [ExtraSource Build]
88+
autogenCmmSources = buildInfo . autogenCmmSources
89+
{-# INLINE autogenCmmSources #-}
90+
91+
cmmSources :: Lens' a [ExtraSource Pkg]
8892
cmmSources = buildInfo . cmmSources
8993
{-# INLINE cmmSources #-}
9094

91-
cSources :: Lens' a [ExtraSource]
95+
cSources :: Lens' a [ExtraSource Pkg]
9296
cSources = buildInfo . cSources
9397
{-# INLINE cSources #-}
9498

95-
cxxSources :: Lens' a [ExtraSource]
99+
cxxSources :: Lens' a [ExtraSource Pkg]
96100
cxxSources = buildInfo . cxxSources
97101
{-# INLINE cxxSources #-}
98102

99-
jsSources :: Lens' a [ExtraSource]
103+
jsSources :: Lens' a [ExtraSource Pkg]
100104
jsSources = buildInfo . jsSources
101105
{-# INLINE jsSources #-}
102106

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-tests/tests/NoThunks.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Distribution.ModuleName (ModuleName)
2525
import Distribution.PackageDescription.Parsec (parseGenericPackageDescription)
2626
import Distribution.SPDX (License, LicenseExceptionId, LicenseExpression, LicenseId, LicenseRef, SimpleLicenseExpression)
2727
import Distribution.System (Arch, OS)
28-
import Distribution.Utils.Path (SymbolicPathX)
28+
import Distribution.Utils.Path (SymbolicPathX, Pkg, Build)
2929
import Distribution.Utils.ShortText (ShortText)
3030
import Distribution.Version (Version, VersionRange)
3131
import Language.Haskell.Extension (Extension, KnownExtension, Language)
@@ -73,7 +73,8 @@ instance NoThunks ConfVar
7373
instance NoThunks Dependency
7474
instance NoThunks Executable
7575
instance NoThunks ExecutableScope
76-
instance NoThunks ExtraSource
76+
instance NoThunks (ExtraSource Build)
77+
instance NoThunks (ExtraSource Pkg)
7778
instance NoThunks FlagName
7879
instance NoThunks ForeignLib
7980
instance NoThunks ForeignLibOption

Cabal-tree-diff/src/Data/TreeDiff/Instances/Cabal.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import Distribution.Types.DumpBuildInfo (DumpBuildInfo)
2929
import Distribution.Types.PackageVersionConstraint
3030
import Distribution.Types.UnitId (DefUnitId, UnitId)
3131
import Distribution.Utils.NubList (NubList)
32-
import Distribution.Utils.Path (SymbolicPathX)
32+
import Distribution.Utils.Path (SymbolicPathX, Build, Pkg)
3333
import Distribution.Utils.ShortText (ShortText, fromShortText)
3434
import Distribution.Verbosity
3535
import Distribution.Verbosity.Internal
@@ -78,7 +78,8 @@ instance ToExpr ExeDependency
7878
instance ToExpr Executable
7979
instance ToExpr ExecutableScope
8080
instance ToExpr ExposedModule
81-
instance ToExpr ExtraSource
81+
instance ToExpr (ExtraSource Build)
82+
instance ToExpr (ExtraSource Pkg)
8283
instance ToExpr FlagAssignment
8384
instance ToExpr FlagName
8485
instance ToExpr ForeignLib

Cabal/src/Distribution/Simple/Build.hs

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

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

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

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

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

675675
-- | Add extra HS modules generated by preprocessing to build
676676
-- information.
@@ -716,7 +716,7 @@ replComponent
716716
preprocessComponent pkg_descr comp lbi clbi False verbosity suffixHandlers
717717
extras <- preprocessExtras verbosity comp lbi
718718
let libbi = libBuildInfo lib
719-
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map extraSourceFromPath extras}}
719+
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map (flip ExtraSourcePkg []) extras}}
720720
replLib replFlags pkg lbi lib' libClbi
721721
replComponent
722722
replFlags
@@ -733,23 +733,23 @@ replComponent
733733
case comp of
734734
CLib lib -> do
735735
let libbi = libBuildInfo lib
736-
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map extraSourceFromPath extras}}
736+
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map (flip ExtraSourcePkg []) extras}}
737737
replLib replFlags pkg_descr lbi lib' clbi
738738
CFLib flib ->
739739
replFLib replFlags pkg_descr lbi flib clbi
740740
CExe exe -> do
741741
let ebi = buildInfo exe
742-
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath extras}}
742+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map (flip ExtraSourcePkg []) extras}}
743743
replExe replFlags pkg_descr lbi exe' clbi
744744
CTest test@TestSuite{testInterface = TestSuiteExeV10{}} -> do
745745
let exe = testSuiteExeV10AsExe test
746746
let ebi = buildInfo exe
747-
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath extras}}
747+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map (flip ExtraSourcePkg []) extras}}
748748
replExe replFlags pkg_descr lbi exe' clbi
749749
CBench bm@Benchmark{benchmarkInterface = BenchmarkExeV10{}} -> do
750750
let exe = benchmarkExeV10asExe bm
751751
let ebi = buildInfo exe
752-
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath extras}}
752+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map (flip ExtraSourcePkg []) extras}}
753753
replExe replFlags pkg_descr lbi exe' clbi
754754
#if __GLASGOW_HASKELL__ < 811
755755
-- silence pattern-match warnings prior to GHC 9.0

0 commit comments

Comments
 (0)