Skip to content

Commit 59592b6

Browse files
committed
Add per-file options to extra source files
1 parent 260ecdc commit 59592b6

File tree

20 files changed

+407
-366
lines changed

20 files changed

+407
-366
lines changed

Cabal-syntax/Cabal-syntax.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ library
137137
Distribution.Types.Executable
138138
Distribution.Types.Executable.Lens
139139
Distribution.Types.ExecutableScope
140+
Distribution.Types.ExtraSource
140141
Distribution.Types.ExposedModule
141142
Distribution.Types.Flag
142143
Distribution.Types.ForeignLib

Cabal-syntax/src/Distribution/PackageDescription.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ module Distribution.PackageDescription
4848
, module Distribution.Types.HookedBuildInfo
4949
, module Distribution.Types.SetupBuildInfo
5050

51+
-- * Extra sources
52+
, module Distribution.Types.ExtraSource
53+
5154
-- * Flags
5255
, module Distribution.Types.Flag
5356

@@ -95,6 +98,7 @@ import Distribution.Types.ComponentName
9598
import Distribution.Types.CondTree
9699
import Distribution.Types.Condition
97100
import Distribution.Types.ConfVar
101+
import Distribution.Types.ExtraSource
98102
import Distribution.Types.Dependency
99103
import Distribution.Types.ExeDependency
100104
import Distribution.Types.Executable

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ libraryFieldGrammar
175175
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
176176
, c (List CommaVCat (Identity Dependency) Dependency)
177177
, c (List CommaVCat (Identity Mixin) Mixin)
178+
, c (List VCat (Identity ExtraSource) ExtraSource)
178179
, c (List CommaVCat (Identity ModuleReexport) ModuleReexport)
179180
, c (List FSep (MQuoted Extension) Extension)
180181
, c (List FSep (MQuoted Language) Language)
@@ -225,6 +226,7 @@ foreignLibFieldGrammar
225226
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
226227
, c (List CommaVCat (Identity Dependency) Dependency)
227228
, c (List CommaVCat (Identity Mixin) Mixin)
229+
, c (List VCat (Identity ExtraSource) ExtraSource)
228230
, c (List FSep (Identity ForeignLibOption) ForeignLibOption)
229231
, c (List FSep (MQuoted Extension) Extension)
230232
, c (List FSep (MQuoted Language) Language)
@@ -264,6 +266,7 @@ executableFieldGrammar
264266
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
265267
, c (List CommaVCat (Identity Dependency) Dependency)
266268
, c (List CommaVCat (Identity Mixin) Mixin)
269+
, c (List VCat (Identity ExtraSource) ExtraSource)
267270
, c (List FSep (MQuoted Extension) Extension)
268271
, c (List FSep (MQuoted Language) Language)
269272
, c (List FSep FilePathNT String)
@@ -336,6 +339,7 @@ testSuiteFieldGrammar
336339
, c (List CommaFSep Token String)
337340
, c (List CommaVCat (Identity Dependency) Dependency)
338341
, c (List CommaVCat (Identity Mixin) Mixin)
342+
, c (List VCat (Identity ExtraSource) ExtraSource)
339343
, c (List FSep (MQuoted Extension) Extension)
340344
, c (List FSep (MQuoted Language) Language)
341345
, c (List FSep FilePathNT String)
@@ -479,6 +483,7 @@ benchmarkFieldGrammar
479483
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
480484
, c (List CommaVCat (Identity Dependency) Dependency)
481485
, c (List CommaVCat (Identity Mixin) Mixin)
486+
, c (List VCat (Identity ExtraSource) ExtraSource)
482487
, c (List FSep (MQuoted Extension) Extension)
483488
, c (List FSep (MQuoted Language) Language)
484489
, c (List FSep FilePathNT String)
@@ -577,6 +582,7 @@ buildInfoFieldGrammar
577582
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
578583
, c (List CommaVCat (Identity Dependency) Dependency)
579584
, c (List CommaVCat (Identity Mixin) Mixin)
585+
, c (List VCat (Identity ExtraSource) ExtraSource)
580586
, c (List FSep (MQuoted Extension) Extension)
581587
, c (List FSep (MQuoted Language) Language)
582588
, c (List FSep FilePathNT String)
@@ -619,14 +625,14 @@ buildInfoFieldGrammar =
619625
<*> monoidalFieldAla "pkgconfig-depends" (alaList CommaFSep) L.pkgconfigDepends
620626
<*> monoidalFieldAla "frameworks" (alaList' FSep Token) L.frameworks
621627
<*> monoidalFieldAla "extra-framework-dirs" (alaList' FSep FilePathNT) L.extraFrameworkDirs
622-
<*> monoidalFieldAla "asm-sources" (alaList' VCat FilePathNT) L.asmSources
628+
<*> monoidalFieldAla "asm-sources" formatExtraSources L.asmSources
623629
^^^ availableSince CabalSpecV3_0 []
624-
<*> monoidalFieldAla "cmm-sources" (alaList' VCat FilePathNT) L.cmmSources
630+
<*> monoidalFieldAla "cmm-sources" formatExtraSources L.cmmSources
625631
^^^ availableSince CabalSpecV3_0 []
626-
<*> monoidalFieldAla "c-sources" (alaList' VCat FilePathNT) L.cSources
627-
<*> monoidalFieldAla "cxx-sources" (alaList' VCat FilePathNT) L.cxxSources
632+
<*> monoidalFieldAla "c-sources" formatExtraSources L.cSources
633+
<*> monoidalFieldAla "cxx-sources" formatExtraSources L.cxxSources
628634
^^^ availableSince CabalSpecV2_2 []
629-
<*> monoidalFieldAla "js-sources" (alaList' VCat FilePathNT) L.jsSources
635+
<*> monoidalFieldAla "js-sources" formatExtraSources L.jsSources
630636
<*> hsSourceDirsGrammar
631637
<*> monoidalFieldAla "other-modules" formatOtherModules L.otherModules
632638
<*> monoidalFieldAla "virtual-modules" (alaList' VCat MQuoted) L.virtualModules
@@ -812,6 +818,9 @@ formatOtherExtensions = alaList' FSep MQuoted
812818
formatOtherModules :: [ModuleName] -> List VCat (MQuoted ModuleName) ModuleName
813819
formatOtherModules = alaList' VCat MQuoted
814820

821+
formatExtraSources :: [ExtraSource] -> List VCat (Identity ExtraSource) ExtraSource
822+
formatExtraSources = alaList' VCat Identity
823+
815824
-------------------------------------------------------------------------------
816825
-- newtypes
817826
-------------------------------------------------------------------------------

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Prelude ()
1919

2020
import Distribution.Types.Dependency
2121
import Distribution.Types.ExeDependency
22+
import Distribution.Types.ExtraSource
2223
import Distribution.Types.LegacyExeDependency
2324
import Distribution.Types.Mixin
2425
import Distribution.Types.PkgconfigDependency
@@ -69,13 +70,15 @@ data BuildInfo = BuildInfo
6970
-- ^ support frameworks for Mac OS X
7071
, extraFrameworkDirs :: [String]
7172
-- ^ extra locations to find frameworks.
72-
, asmSources :: [FilePath]
73-
-- ^ Assembly files.
74-
, cmmSources :: [FilePath]
75-
-- ^ C-- files.
76-
, cSources :: [FilePath]
77-
, cxxSources :: [FilePath]
78-
, jsSources :: [FilePath]
73+
, asmSources :: [ExtraSource]
74+
-- ^ Assembly source files
75+
, cmmSources :: [ExtraSource]
76+
-- ^ C-- source files
77+
, cSources :: [ExtraSource]
78+
-- ^ C source files
79+
, cxxSources :: [ExtraSource]
80+
-- ^ C++ source files
81+
, jsSources :: [ExtraSource]
7982
, hsSourceDirs :: [SymbolicPath PackageDir SourceDir]
8083
-- ^ where to look for the Haskell module hierarchy
8184
, otherModules :: [ModuleName]

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Prelude ()
1111
import Distribution.Compiler (PerCompilerFlavor)
1212
import Distribution.ModuleName (ModuleName)
1313
import Distribution.Types.BuildInfo (BuildInfo)
14+
import Distribution.Types.ExtraSource (ExtraSource)
1415
import Distribution.Types.Dependency (Dependency)
1516
import Distribution.Types.ExeDependency (ExeDependency)
1617
import Distribution.Types.LegacyExeDependency (LegacyExeDependency)
@@ -77,23 +78,23 @@ class HasBuildInfo a where
7778
extraFrameworkDirs = buildInfo . extraFrameworkDirs
7879
{-# INLINE extraFrameworkDirs #-}
7980

80-
asmSources :: Lens' a [FilePath]
81+
asmSources :: Lens' a [ExtraSource]
8182
asmSources = buildInfo . asmSources
8283
{-# INLINE asmSources #-}
8384

84-
cmmSources :: Lens' a [FilePath]
85+
cmmSources :: Lens' a [ExtraSource]
8586
cmmSources = buildInfo . cmmSources
8687
{-# INLINE cmmSources #-}
8788

88-
cSources :: Lens' a [FilePath]
89+
cSources :: Lens' a [ExtraSource]
8990
cSources = buildInfo . cSources
9091
{-# INLINE cSources #-}
9192

92-
cxxSources :: Lens' a [FilePath]
93+
cxxSources :: Lens' a [ExtraSource]
9394
cxxSources = buildInfo . cxxSources
9495
{-# INLINE cxxSources #-}
9596

96-
jsSources :: Lens' a [FilePath]
97+
jsSources :: Lens' a [ExtraSource]
9798
jsSources = buildInfo . jsSources
9899
{-# INLINE jsSources #-}
99100

@@ -261,7 +262,7 @@ instance HasBuildInfo BuildInfo where
261262
cSources f s = fmap (\x -> s{T.cSources = x}) (f (T.cSources s))
262263
{-# INLINE cSources #-}
263264

264-
cxxSources f s = fmap (\x -> s{T.cSources = x}) (f (T.cxxSources s))
265+
cxxSources f s = fmap (\x -> s{T.cxxSources = x}) (f (T.cxxSources s))
265266
{-# INLINE cxxSources #-}
266267

267268
jsSources f s = fmap (\x -> s{T.jsSources = x}) (f (T.jsSources s))
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE DeriveDataTypeable #-}
3+
{-# LANGUAGE DeriveGeneric #-}
4+
5+
module Distribution.Types.ExtraSource
6+
( ExtraSource (..)
7+
, extraSourceFromPath
8+
) where
9+
10+
import Distribution.Compat.Prelude
11+
import Prelude ()
12+
13+
import Distribution.Parsec
14+
import Distribution.Pretty
15+
import Distribution.FieldGrammar.Newtypes (FilePathNT(..))
16+
17+
import qualified Distribution.Compat.CharParsing as P
18+
import qualified Text.PrettyPrint as PP
19+
20+
data ExtraSource = ExtraSource
21+
{ extraSourceFile :: FilePath
22+
, extraSourceOpts :: [String]
23+
}
24+
deriving (Generic, Show, Read, Eq, Ord, Typeable, Data)
25+
26+
instance Binary ExtraSource
27+
instance Structured ExtraSource
28+
instance NFData ExtraSource where rnf = genericRnf
29+
30+
instance Parsec ExtraSource where
31+
parsec = do
32+
FilePathNT path <- parsec <* P.spaces
33+
opts <- P.optional (parensLax (P.sepBy p P.spaces))
34+
return (ExtraSource path (fromMaybe mempty opts))
35+
where
36+
p :: P.CharParsing p => p String
37+
p = some $ P.satisfy (\c -> not (isSpace c) && not (c == ')'))
38+
39+
parensLax :: P.CharParsing m => m a -> m a
40+
parensLax p = P.between (P.char '(' *> P.spaces) (P.char ')' *> P.spaces) p
41+
42+
instance Pretty ExtraSource where
43+
pretty (ExtraSource path opts) =
44+
pretty (FilePathNT path) <<>> PP.parens (PP.hsep (map PP.text opts))
45+
46+
extraSourceFromPath :: FilePath -> ExtraSource
47+
extraSourceFromPath fp = ExtraSource fp mempty

Cabal/src/Distribution/PackageDescription/Check/Target.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,11 @@ checkBuildInfoPathsContent bi = do
431431
-- Paths well-formedness check for BuildInfo.
432432
checkBuildInfoPathsWellFormedness :: Monad m => BuildInfo -> CheckM m ()
433433
checkBuildInfoPathsWellFormedness bi = do
434-
mapM_ (checkPath False "asm-sources" PathKindFile) (asmSources bi)
435-
mapM_ (checkPath False "cmm-sources" PathKindFile) (cmmSources bi)
436-
mapM_ (checkPath False "c-sources" PathKindFile) (cSources bi)
437-
mapM_ (checkPath False "cxx-sources" PathKindFile) (cxxSources bi)
438-
mapM_ (checkPath False "js-sources" PathKindFile) (jsSources bi)
434+
mapM_ (checkPath False "asm-sources" PathKindFile . extraSourceFile) (asmSources bi)
435+
mapM_ (checkPath False "cmm-sources" PathKindFile . extraSourceFile) (cmmSources bi)
436+
mapM_ (checkPath False "c-sources" PathKindFile . extraSourceFile) (cSources bi)
437+
mapM_ (checkPath False "cxx-sources" PathKindFile . extraSourceFile) (cxxSources bi)
438+
mapM_ (checkPath False "js-sources" PathKindFile . extraSourceFile) (jsSources bi)
439439
mapM_
440440
(checkPath False "install-includes" PathKindFile)
441441
(installIncludes bi)
@@ -501,8 +501,8 @@ checkBuildInfoFeatures bi sv = do
501501
(PackageBuildWarning CVExtensionsDeprecated)
502502

503503
-- asm-sources, cmm-sources and friends only w/ spec ≥ 1.10
504-
checkCVSources (asmSources bi)
505-
checkCVSources (cmmSources bi)
504+
checkCVSources (map extraSourceFile $ asmSources bi)
505+
checkCVSources (map extraSourceFile $ cmmSources bi)
506506
checkCVSources (extraBundledLibs bi)
507507
checkCVSources (extraLibFlavours bi)
508508

Cabal/src/Distribution/Simple/Build.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -547,35 +547,35 @@ generateCode codeGens nm pdesc bi lbi clbi verbosity = do
547547
addExtraCSources :: BuildInfo -> [FilePath] -> BuildInfo
548548
addExtraCSources bi extras = bi{cSources = new}
549549
where
550-
new = ordNub (extras ++ cSources bi)
550+
new = ordNub (map extraSourceFromPath extras ++ cSources bi)
551551

552552
-- | Add extra C++ sources generated by preprocessing to build
553553
-- information.
554554
addExtraCxxSources :: BuildInfo -> [FilePath] -> BuildInfo
555555
addExtraCxxSources bi extras = bi{cxxSources = new}
556556
where
557-
new = ordNub (extras ++ cxxSources bi)
557+
new = ordNub (map extraSourceFromPath extras ++ cxxSources bi)
558558

559559
-- | Add extra C-- sources generated by preprocessing to build
560560
-- information.
561561
addExtraCmmSources :: BuildInfo -> [FilePath] -> BuildInfo
562562
addExtraCmmSources bi extras = bi{cmmSources = new}
563563
where
564-
new = ordNub (extras ++ cmmSources bi)
564+
new = ordNub (map extraSourceFromPath extras ++ cmmSources bi)
565565

566566
-- | Add extra ASM sources generated by preprocessing to build
567567
-- information.
568568
addExtraAsmSources :: BuildInfo -> [FilePath] -> BuildInfo
569569
addExtraAsmSources bi extras = bi{asmSources = new}
570570
where
571-
new = ordNub (extras ++ asmSources bi)
571+
new = ordNub (map extraSourceFromPath extras ++ asmSources bi)
572572

573573
-- | Add extra JS sources generated by preprocessing to build
574574
-- information.
575575
addExtraJsSources :: BuildInfo -> [FilePath] -> BuildInfo
576576
addExtraJsSources bi extras = bi{jsSources = new}
577577
where
578-
new = ordNub (extras ++ jsSources bi)
578+
new = ordNub (map extraSourceFromPath extras ++ jsSources bi)
579579

580580
-- | Add extra HS modules generated by preprocessing to build
581581
-- information.
@@ -621,7 +621,7 @@ replComponent
621621
preprocessComponent pkg_descr comp lbi clbi False verbosity suffixes
622622
extras <- preprocessExtras verbosity comp lbi
623623
let libbi = libBuildInfo lib
624-
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ extras}}
624+
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map extraSourceFromPath extras}}
625625
replLib replFlags pkg lbi lib' libClbi
626626
replComponent
627627
replFlags
@@ -638,23 +638,23 @@ replComponent
638638
case comp of
639639
CLib lib -> do
640640
let libbi = libBuildInfo lib
641-
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ extras}}
641+
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map extraSourceFromPath extras}}
642642
replLib replFlags pkg_descr lbi lib' clbi
643643
CFLib flib ->
644644
replFLib replFlags pkg_descr lbi flib clbi
645645
CExe exe -> do
646646
let ebi = buildInfo exe
647-
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ extras}}
647+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath extras}}
648648
replExe replFlags pkg_descr lbi exe' clbi
649649
CTest test@TestSuite{testInterface = TestSuiteExeV10{}} -> do
650650
let exe = testSuiteExeV10AsExe test
651651
let ebi = buildInfo exe
652-
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ extras}}
652+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath extras}}
653653
replExe replFlags pkg_descr lbi exe' clbi
654654
CBench bm@Benchmark{benchmarkInterface = BenchmarkExeV10{}} -> do
655655
let exe = benchmarkExeV10asExe bm
656656
let ebi = buildInfo exe
657-
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ extras}}
657+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath extras}}
658658
replExe replFlags pkg_descr lbi exe' clbi
659659
#if __GLASGOW_HASKELL__ < 811
660660
-- silence pattern-match warnings prior to GHC 9.0

Cabal/src/Distribution/Simple/BuildTarget.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,11 @@ pkgComponentInfo pkg =
497497
, cinfoSrcDirs = map getSymbolicPath $ hsSourceDirs bi
498498
, cinfoModules = componentModules c
499499
, cinfoHsFiles = componentHsFiles c
500-
, cinfoAsmFiles = asmSources bi
501-
, cinfoCmmFiles = cmmSources bi
502-
, cinfoCFiles = cSources bi
503-
, cinfoCxxFiles = cxxSources bi
504-
, cinfoJsFiles = jsSources bi
500+
, cinfoAsmFiles = map extraSourceFile $ asmSources bi
501+
, cinfoCmmFiles = map extraSourceFile $ cmmSources bi
502+
, cinfoCFiles = map extraSourceFile $ cSources bi
503+
, cinfoCxxFiles = map extraSourceFile $ cxxSources bi
504+
, cinfoJsFiles = map extraSourceFile $ jsSources bi
505505
}
506506
| c <- pkgComponents pkg
507507
, let bi = componentBuildInfo c

0 commit comments

Comments
 (0)