Skip to content

Commit 28e2a71

Browse files
committed
feat: add per-file options to extra source files
1 parent 44086db commit 28e2a71

File tree

23 files changed

+237
-154
lines changed

23 files changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE FlexibleInstances #-}
23
#if !(__GLASGOW_HASKELL__ >= 806 && defined(MIN_VERSION_nothunks))
34
module Main (main) where
45
main :: IO ()
@@ -24,7 +25,7 @@ import Distribution.ModuleName (ModuleName)
2425
import Distribution.PackageDescription.Parsec (parseGenericPackageDescription)
2526
import Distribution.SPDX (License, LicenseExceptionId, LicenseExpression, LicenseId, LicenseRef, SimpleLicenseExpression)
2627
import Distribution.System (Arch, OS)
27-
import Distribution.Utils.Path (SymbolicPathX)
28+
import Distribution.Utils.Path (SymbolicPathX, Pkg, Build)
2829
import Distribution.Utils.ShortText (ShortText)
2930
import Distribution.Version (Version, VersionRange)
3031
import Language.Haskell.Extension (Extension, KnownExtension, Language)
@@ -72,6 +73,8 @@ instance NoThunks ConfVar
7273
instance NoThunks Dependency
7374
instance NoThunks Executable
7475
instance NoThunks ExecutableScope
76+
instance NoThunks (ExtraSource Pkg)
77+
instance NoThunks (ExtraSource Build)
7578
instance NoThunks FlagName
7679
instance NoThunks ForeignLib
7780
instance NoThunks ForeignLibOption

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# OPTIONS_GHC -freduction-depth=0 #-}
22
{-# OPTIONS_GHC -Wno-orphans #-}
3+
{-# LANGUAGE FlexibleInstances #-}
34
module Data.TreeDiff.Instances.Cabal () where
45

56
import Data.TreeDiff
@@ -28,7 +29,7 @@ import Distribution.Types.DumpBuildInfo (DumpBuildInfo)
2829
import Distribution.Types.PackageVersionConstraint
2930
import Distribution.Types.UnitId (DefUnitId, UnitId)
3031
import Distribution.Utils.NubList (NubList)
31-
import Distribution.Utils.Path (SymbolicPathX)
32+
import Distribution.Utils.Path (SymbolicPathX, Pkg, Build)
3233
import Distribution.Utils.ShortText (ShortText, fromShortText)
3334
import Distribution.Verbosity
3435
import Distribution.Verbosity.Internal
@@ -77,6 +78,8 @@ instance ToExpr ExeDependency
7778
instance ToExpr Executable
7879
instance ToExpr ExecutableScope
7980
instance ToExpr ExposedModule
81+
instance ToExpr (ExtraSource Pkg)
82+
instance ToExpr (ExtraSource Build)
8083
instance ToExpr FlagAssignment
8184
instance ToExpr FlagName
8285
instance ToExpr ForeignLib

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,11 @@ checkBuildInfoPathsContent bi = do
445445
-- Paths well-formedness check for BuildInfo.
446446
checkBuildInfoPathsWellFormedness :: Monad m => BuildInfo -> CheckM m ()
447447
checkBuildInfoPathsWellFormedness bi = do
448-
mapM_ (checkPath False "asm-sources" PathKindFile . getSymbolicPath) (asmSources bi)
449-
mapM_ (checkPath False "cmm-sources" PathKindFile . getSymbolicPath) (cmmSources bi)
450-
mapM_ (checkPath False "c-sources" PathKindFile . getSymbolicPath) (cSources bi)
451-
mapM_ (checkPath False "cxx-sources" PathKindFile . getSymbolicPath) (cxxSources bi)
452-
mapM_ (checkPath False "js-sources" PathKindFile . getSymbolicPath) (jsSources bi)
448+
mapM_ (checkPath False "asm-sources" PathKindFile . getSymbolicPath . extraSourceFile) (asmSources bi)
449+
mapM_ (checkPath False "cmm-sources" PathKindFile . getSymbolicPath . extraSourceFile) (cmmSources bi)
450+
mapM_ (checkPath False "c-sources" PathKindFile . getSymbolicPath . extraSourceFile) (cSources bi)
451+
mapM_ (checkPath False "cxx-sources" PathKindFile . getSymbolicPath . extraSourceFile) (cxxSources bi)
452+
mapM_ (checkPath False "js-sources" PathKindFile . getSymbolicPath . extraSourceFile) (jsSources bi)
453453
mapM_
454454
(checkPath False "install-includes" PathKindFile . getSymbolicPath)
455455
(installIncludes bi)
@@ -515,8 +515,8 @@ checkBuildInfoFeatures bi sv = do
515515
(PackageBuildWarning CVExtensionsDeprecated)
516516

517517
-- asm-sources, cmm-sources and friends only w/ spec ≥ 1.10
518-
checkCVSources (map getSymbolicPath $ asmSources bi)
519-
checkCVSources (map getSymbolicPath $ cmmSources bi)
518+
checkCVSources (map (getSymbolicPath . extraSourceFile) $ asmSources bi)
519+
checkCVSources (map (getSymbolicPath . extraSourceFile) $ cmmSources bi)
520520
checkCVSources (extraBundledLibs bi)
521521
checkCVSources (extraLibFlavours bi)
522522

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 (extras ++ cSources bi)
645+
new = ordNub (map extraSourceFromPath 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 (extras ++ cxxSources bi)
652+
new = ordNub (map extraSourceFromPath 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 (extras ++ cmmSources bi)
659+
new = ordNub (map extraSourceFromPath 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 (extras ++ asmSources bi)
666+
new = ordNub (map extraSourceFromPath 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 (extras ++ jsSources bi)
673+
new = ordNub (map extraSourceFromPath 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 ++ extras}}
719+
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map extraSourceFromPath 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 ++ extras}}
736+
lib' = lib{libBuildInfo = libbi{cSources = cSources libbi ++ map extraSourceFromPath 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 ++ extras}}
742+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath 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 ++ extras}}
747+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath 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 ++ extras}}
752+
exe' = exe{buildInfo = ebi{cSources = cSources ebi ++ map extraSourceFromPath 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)