Skip to content

Commit dbcaf10

Browse files
committed
Add per-file options to extra source files
1 parent 4da153d commit dbcaf10

File tree

20 files changed

+225
-150
lines changed

20 files changed

+225
-150
lines changed

Cabal-syntax/Cabal-syntax.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ library
140140
Distribution.Types.Executable
141141
Distribution.Types.Executable.Lens
142142
Distribution.Types.ExecutableScope
143+
Distribution.Types.ExtraSource
143144
Distribution.Types.ExposedModule
144145
Distribution.Types.Flag
145146
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
@@ -174,6 +174,7 @@ 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)
177178
, c (List CommaVCat (Identity ModuleReexport) ModuleReexport)
178179
, c (List FSep (MQuoted Extension) Extension)
179180
, c (List FSep (MQuoted Language) Language)
@@ -224,6 +225,7 @@ foreignLibFieldGrammar
224225
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
225226
, c (List CommaVCat (Identity Dependency) Dependency)
226227
, c (List CommaVCat (Identity Mixin) Mixin)
228+
, c (List VCat (Identity ExtraSource) ExtraSource)
227229
, c (List FSep (Identity ForeignLibOption) ForeignLibOption)
228230
, c (List FSep (MQuoted Extension) Extension)
229231
, c (List FSep (MQuoted Language) Language)
@@ -263,6 +265,7 @@ executableFieldGrammar
263265
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
264266
, c (List CommaVCat (Identity Dependency) Dependency)
265267
, c (List CommaVCat (Identity Mixin) Mixin)
268+
, c (List VCat (Identity ExtraSource) ExtraSource)
266269
, c (List FSep (MQuoted Extension) Extension)
267270
, c (List FSep (MQuoted Language) Language)
268271
, c (List FSep Token String)
@@ -339,6 +342,7 @@ testSuiteFieldGrammar
339342
, c (List CommaFSep Token String)
340343
, c (List CommaVCat (Identity Dependency) Dependency)
341344
, c (List CommaVCat (Identity Mixin) Mixin)
345+
, c (List VCat (Identity ExtraSource) ExtraSource)
342346
, c (List FSep (MQuoted Extension) Extension)
343347
, c (List FSep (MQuoted Language) Language)
344348
, c (List FSep Token String)
@@ -483,6 +487,7 @@ benchmarkFieldGrammar
483487
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
484488
, c (List CommaVCat (Identity Dependency) Dependency)
485489
, c (List CommaVCat (Identity Mixin) Mixin)
490+
, c (List VCat (Identity ExtraSource) ExtraSource)
486491
, c (List FSep (MQuoted Extension) Extension)
487492
, c (List FSep (MQuoted Language) Language)
488493
, c (List FSep Token String)
@@ -585,6 +590,7 @@ buildInfoFieldGrammar
585590
, c (List CommaFSep (Identity PkgconfigDependency) PkgconfigDependency)
586591
, c (List CommaVCat (Identity Dependency) Dependency)
587592
, c (List CommaVCat (Identity Mixin) Mixin)
593+
, c (List VCat (Identity ExtraSource) ExtraSource)
588594
, c (List FSep (MQuoted Extension) Extension)
589595
, c (List FSep (MQuoted Language) Language)
590596
, c (List FSep Token String)
@@ -627,14 +633,14 @@ buildInfoFieldGrammar =
627633
<*> monoidalFieldAla "pkgconfig-depends" (alaList CommaFSep) L.pkgconfigDepends
628634
<*> monoidalFieldAla "frameworks" (alaList' FSep RelativePathNT) L.frameworks
629635
<*> monoidalFieldAla "extra-framework-dirs" (alaList' FSep SymbolicPathNT) L.extraFrameworkDirs
630-
<*> monoidalFieldAla "asm-sources" (alaList' VCat SymbolicPathNT) L.asmSources
636+
<*> monoidalFieldAla "asm-sources" formatExtraSources L.asmSources
631637
^^^ availableSince CabalSpecV3_0 []
632-
<*> monoidalFieldAla "cmm-sources" (alaList' VCat SymbolicPathNT) L.cmmSources
638+
<*> monoidalFieldAla "cmm-sources" formatExtraSources L.cmmSources
633639
^^^ availableSince CabalSpecV3_0 []
634-
<*> monoidalFieldAla "c-sources" (alaList' VCat SymbolicPathNT) L.cSources
635-
<*> monoidalFieldAla "cxx-sources" (alaList' VCat SymbolicPathNT) L.cxxSources
640+
<*> monoidalFieldAla "c-sources" formatExtraSources L.cSources
641+
<*> monoidalFieldAla "cxx-sources" formatExtraSources L.cxxSources
636642
^^^ availableSince CabalSpecV2_2 []
637-
<*> monoidalFieldAla "js-sources" (alaList' VCat SymbolicPathNT) L.jsSources
643+
<*> monoidalFieldAla "js-sources" formatExtraSources L.jsSources
638644
<*> hsSourceDirsGrammar
639645
<*> monoidalFieldAla "other-modules" formatOtherModules L.otherModules
640646
<*> monoidalFieldAla "virtual-modules" (alaList' VCat MQuoted) L.virtualModules
@@ -834,6 +840,9 @@ formatOtherExtensions = alaList' FSep MQuoted
834840
formatOtherModules :: [ModuleName] -> List VCat (MQuoted ModuleName) ModuleName
835841
formatOtherModules = alaList' VCat MQuoted
836842

843+
formatExtraSources :: [ExtraSource] -> List VCat (Identity ExtraSource) ExtraSource
844+
formatExtraSources = alaList' VCat Identity
845+
837846
-------------------------------------------------------------------------------
838847
-- newtypes
839848
-------------------------------------------------------------------------------

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Prelude ()
2121

2222
import Distribution.Types.Dependency
2323
import Distribution.Types.ExeDependency
24+
import Distribution.Types.ExtraSource
2425
import Distribution.Types.LegacyExeDependency
2526
import Distribution.Types.Mixin
2627
import Distribution.Types.PkgconfigDependency
@@ -70,14 +71,17 @@ data BuildInfo = BuildInfo
7071
, frameworks :: [RelativePath Framework File]
7172
-- ^ support frameworks for Mac OS X
7273
, extraFrameworkDirs :: [SymbolicPath Pkg (Dir Framework)]
73-
-- ^ extra locations to find frameworks.
74-
, asmSources :: [SymbolicPath Pkg File]
75-
-- ^ Assembly files.
76-
, cmmSources :: [SymbolicPath Pkg File]
77-
-- ^ C-- files.
78-
, cSources :: [SymbolicPath Pkg File]
79-
, cxxSources :: [SymbolicPath Pkg File]
80-
, jsSources :: [SymbolicPath Pkg File]
74+
-- ^ extra locations to find frameworks
75+
, asmSources :: [ExtraSource]
76+
-- ^ Assembly source files
77+
, cmmSources :: [ExtraSource]
78+
-- ^ C-- source files
79+
, cSources :: [ExtraSource]
80+
-- ^ C source files
81+
, cxxSources :: [ExtraSource]
82+
-- ^ C++ source files
83+
, jsSources :: [ExtraSource]
84+
-- ^ JavaScript source file
8185
, hsSourceDirs :: [SymbolicPath Pkg (Dir Source)]
8286
-- ^ where to look for the Haskell module hierarchy
8387
, -- NB: these are symbolic paths are not relative paths,

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Prelude ()
1313
import Distribution.Compiler (PerCompilerFlavor)
1414
import Distribution.ModuleName (ModuleName)
1515
import Distribution.Types.BuildInfo (BuildInfo)
16+
import Distribution.Types.ExtraSource (ExtraSource)
1617
import Distribution.Types.Dependency (Dependency)
1718
import Distribution.Types.ExeDependency (ExeDependency)
1819
import Distribution.Types.LegacyExeDependency (LegacyExeDependency)
@@ -79,23 +80,23 @@ class HasBuildInfo a where
7980
extraFrameworkDirs = buildInfo . extraFrameworkDirs
8081
{-# INLINE extraFrameworkDirs #-}
8182

82-
asmSources :: Lens' a [SymbolicPath Pkg File]
83+
asmSources :: Lens' a [ExtraSource]
8384
asmSources = buildInfo . asmSources
8485
{-# INLINE asmSources #-}
8586

86-
cmmSources :: Lens' a [SymbolicPath Pkg File]
87+
cmmSources :: Lens' a [ExtraSource]
8788
cmmSources = buildInfo . cmmSources
8889
{-# INLINE cmmSources #-}
8990

90-
cSources :: Lens' a [SymbolicPath Pkg File]
91+
cSources :: Lens' a [ExtraSource]
9192
cSources = buildInfo . cSources
9293
{-# INLINE cSources #-}
9394

94-
cxxSources :: Lens' a [SymbolicPath Pkg File]
95+
cxxSources :: Lens' a [ExtraSource]
9596
cxxSources = buildInfo . cxxSources
9697
{-# INLINE cxxSources #-}
9798

98-
jsSources :: Lens' a [SymbolicPath Pkg File]
99+
jsSources :: Lens' a [ExtraSource]
99100
jsSources = buildInfo . jsSources
100101
{-# INLINE jsSources #-}
101102

@@ -267,7 +268,7 @@ instance HasBuildInfo BuildInfo where
267268
cSources f s = fmap (\x -> s{T.cSources = x}) (f (T.cSources s))
268269
{-# INLINE cSources #-}
269270

270-
cxxSources f s = fmap (\x -> s{T.cSources = x}) (f (T.cxxSources s))
271+
cxxSources f s = fmap (\x -> s{T.cxxSources = x}) (f (T.cxxSources s))
271272
{-# INLINE cxxSources #-}
272273

273274
jsSources f s = fmap (\x -> s{T.jsSources = x}) (f (T.jsSources s))
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{-# LANGUAGE DeriveDataTypeable #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE DataKinds #-}
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.Utils.Path (SymbolicPath, FileOrDir(..), Pkg)
16+
17+
import qualified Distribution.Compat.CharParsing as P
18+
import qualified Text.PrettyPrint as PP
19+
import Distribution.FieldGrammar.Newtypes (SymbolicPathNT(..))
20+
21+
data ExtraSource = ExtraSource
22+
{ extraSourceFile :: SymbolicPath Pkg File
23+
, extraSourceOpts :: [String]
24+
}
25+
deriving (Generic, Show, Read, Eq, Ord, Typeable, Data)
26+
27+
instance Binary ExtraSource
28+
instance Structured ExtraSource
29+
instance NFData ExtraSource where rnf = genericRnf
30+
31+
instance Parsec ExtraSource where
32+
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))
36+
where
37+
p :: P.CharParsing p => p String
38+
p = some $ P.satisfy (\c -> not (isSpace c) && not (c == ')'))
39+
40+
parensLax :: (P.CharParsing m) => m a -> m a
41+
parensLax p = P.between (P.char '(' *> P.spaces) (P.char ')' *> P.spaces) p
42+
43+
instance Pretty ExtraSource where
44+
pretty (ExtraSource path opts) =
45+
pretty (SymbolicPathNT path) <<>> PP.parens (PP.hsep (map PP.text opts))
46+
47+
extraSourceFromPath :: SymbolicPath Pkg File -> ExtraSource
48+
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
@@ -432,11 +432,11 @@ checkBuildInfoPathsContent bi = do
432432
-- Paths well-formedness check for BuildInfo.
433433
checkBuildInfoPathsWellFormedness :: Monad m => BuildInfo -> CheckM m ()
434434
checkBuildInfoPathsWellFormedness bi = do
435-
mapM_ (checkPath False "asm-sources" PathKindFile . getSymbolicPath) (asmSources bi)
436-
mapM_ (checkPath False "cmm-sources" PathKindFile . getSymbolicPath) (cmmSources bi)
437-
mapM_ (checkPath False "c-sources" PathKindFile . getSymbolicPath) (cSources bi)
438-
mapM_ (checkPath False "cxx-sources" PathKindFile . getSymbolicPath) (cxxSources bi)
439-
mapM_ (checkPath False "js-sources" PathKindFile . getSymbolicPath) (jsSources bi)
435+
mapM_ (checkPath False "asm-sources" PathKindFile . getSymbolicPath . extraSourceFile) (asmSources bi)
436+
mapM_ (checkPath False "cmm-sources" PathKindFile . getSymbolicPath . extraSourceFile) (cmmSources bi)
437+
mapM_ (checkPath False "c-sources" PathKindFile . getSymbolicPath . extraSourceFile) (cSources bi)
438+
mapM_ (checkPath False "cxx-sources" PathKindFile . getSymbolicPath . extraSourceFile) (cxxSources bi)
439+
mapM_ (checkPath False "js-sources" PathKindFile . getSymbolicPath . extraSourceFile) (jsSources bi)
440440
mapM_
441441
(checkPath False "install-includes" PathKindFile . getSymbolicPath)
442442
(installIncludes bi)
@@ -502,8 +502,8 @@ checkBuildInfoFeatures bi sv = do
502502
(PackageBuildWarning CVExtensionsDeprecated)
503503

504504
-- asm-sources, cmm-sources and friends only w/ spec ≥ 1.10
505-
checkCVSources (map getSymbolicPath $ asmSources bi)
506-
checkCVSources (map getSymbolicPath $ cmmSources bi)
505+
checkCVSources (map (getSymbolicPath . extraSourceFile) $ asmSources bi)
506+
checkCVSources (map (getSymbolicPath . extraSourceFile) $ cmmSources bi)
507507
checkCVSources (extraBundledLibs bi)
508508
checkCVSources (extraLibFlavours bi)
509509

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 (extras ++ cSources bi)
646+
new = ordNub (map extraSourceFromPath 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 (extras ++ cxxSources bi)
653+
new = ordNub (map extraSourceFromPath 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 (extras ++ cmmSources bi)
660+
new = ordNub (map extraSourceFromPath 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 (extras ++ asmSources bi)
667+
new = ordNub (map extraSourceFromPath 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 (extras ++ jsSources bi)
674+
new = ordNub (map extraSourceFromPath 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 ++ extras}}
720+
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map extraSourceFromPath 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 ++ extras}}
737+
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map extraSourceFromPath 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 ++ extras}}
743+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath 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 ++ extras}}
748+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath 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 ++ extras}}
753+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath extras}}
754754
replExe replFlags pkg_descr lbi exe' clbi
755755
#if __GLASGOW_HASKELL__ < 811
756756
-- 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
@@ -498,11 +498,11 @@ pkgComponentInfo pkg =
498498
, cinfoSrcDirs = map getSymbolicPath $ hsSourceDirs bi
499499
, cinfoModules = componentModules c
500500
, cinfoHsFiles = map getSymbolicPath $ componentHsFiles c
501-
, cinfoAsmFiles = map getSymbolicPath $ asmSources bi
502-
, cinfoCmmFiles = map getSymbolicPath $ cmmSources bi
503-
, cinfoCFiles = map getSymbolicPath $ cSources bi
504-
, cinfoCxxFiles = map getSymbolicPath $ cxxSources bi
505-
, cinfoJsFiles = map getSymbolicPath $ jsSources bi
501+
, cinfoAsmFiles = map (getSymbolicPath . extraSourceFile) $ asmSources bi
502+
, cinfoCmmFiles = map (getSymbolicPath . extraSourceFile) $ cmmSources bi
503+
, cinfoCFiles = map (getSymbolicPath . extraSourceFile) $ cSources bi
504+
, cinfoCxxFiles = map (getSymbolicPath . extraSourceFile) $ cxxSources bi
505+
, cinfoJsFiles = map (getSymbolicPath . extraSourceFile) $ jsSources bi
506506
}
507507
| c <- pkgComponents pkg
508508
, let bi = componentBuildInfo c

0 commit comments

Comments
 (0)