Skip to content

Commit b070fc9

Browse files
Merge pull request #4810 from recursion-ninja/master
Added `cxx-options` and `cxx-sources` build info fields for separate compilation of C and C++ source files.
2 parents 49f804f + 818aec5 commit b070fc9

File tree

12 files changed

+150
-10
lines changed

12 files changed

+150
-10
lines changed

Cabal/Distribution/PackageDescription/Check.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,7 @@ checkPaths pkg =
10301030
++ [ (path, "license-file") | path <- licenseFiles pkg ]
10311031
++ concat
10321032
[ [ (path, "c-sources") | path <- cSources bi ]
1033+
++ [ (path, "cxx-sources") | path <- cxxSources bi ]
10331034
++ [ (path, "js-sources") | path <- jsSources bi ]
10341035
++ [ (path, "install-includes") | path <- installIncludes bi ]
10351036
++ [ (path, "hs-source-dirs") | path <- hsSourceDirs bi ]

Cabal/Distribution/PackageDescription/FieldGrammar.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,13 @@ buildInfoFieldGrammar = BuildInfo
361361
^^^ availableSince [2,0]
362362
<*> monoidalFieldAla "cpp-options" (alaList' NoCommaFSep Token') L.cppOptions
363363
<*> monoidalFieldAla "cc-options" (alaList' NoCommaFSep Token') L.ccOptions
364+
<*> monoidalFieldAla "cxx-options" (alaList' NoCommaFSep Token') L.cxxOptions
364365
<*> monoidalFieldAla "ld-options" (alaList' NoCommaFSep Token') L.ldOptions
365366
<*> monoidalFieldAla "pkgconfig-depends" (alaList CommaFSep) L.pkgconfigDepends
366367
<*> monoidalFieldAla "frameworks" (alaList' FSep Token) L.frameworks
367368
<*> monoidalFieldAla "extra-framework-dirs" (alaList' FSep FilePathNT) L.extraFrameworkDirs
368369
<*> monoidalFieldAla "c-sources" (alaList' VCat FilePathNT) L.cSources
370+
<*> monoidalFieldAla "cxx-sources" (alaList' VCat FilePathNT) L.cxxSources
369371
<*> monoidalFieldAla "js-sources" (alaList' VCat FilePathNT) L.jsSources
370372
<*> hsSourceDirsGrammar
371373
<*> monoidalFieldAla "other-modules" (alaList' VCat MQuoted) L.otherModules

Cabal/Distribution/PackageDescription/Parse.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ binfoFieldDescrs =
441441
, spaceListField "cc-options"
442442
showToken parseTokenQ'
443443
ccOptions (\val binfo -> binfo{ccOptions=val})
444+
, spaceListField "cxx-options"
445+
showToken parseTokenQ'
446+
cxxOptions (\val binfo -> binfo{cxxOptions=val})
444447
, spaceListField "ld-options"
445448
showToken parseTokenQ'
446449
ldOptions (\val binfo -> binfo{ldOptions=val})
@@ -456,6 +459,9 @@ binfoFieldDescrs =
456459
, listFieldWithSep vcat "c-sources"
457460
showFilePath parseFilePathQ
458461
cSources (\paths binfo -> binfo{cSources=paths})
462+
, listFieldWithSep vcat "cxx-sources"
463+
showFilePath parseFilePathQ
464+
cxxSources (\paths binfo -> binfo{cxxSources=paths})
459465
, listFieldWithSep vcat "js-sources"
460466
showFilePath parseFilePathQ
461467
jsSources (\paths binfo -> binfo{jsSources=paths})

Cabal/Distribution/Simple/Build.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ buildComponent verbosity numJobs pkg_descr lbi suffixes
201201
setupMessage' verbosity "Building" (packageId pkg_descr)
202202
(componentLocalName clbi) (maybeComponentInstantiatedWith clbi)
203203
let libbi = libBuildInfo lib
204-
lib' = lib { libBuildInfo = addExtraCSources libbi extras }
204+
lib' = lib { libBuildInfo = addExtraCxxSources (addExtraCSources libbi extras) extras }
205205
buildLib verbosity numJobs pkg_descr lbi lib' clbi
206206

207207
let oneComponentRequested (OneComponentRequestedSpec _) = True
@@ -329,6 +329,15 @@ addExtraCSources bi extras = bi { cSources = new }
329329
exs = Set.fromList extras
330330

331331

332+
-- | Add extra C++ sources generated by preprocessing to build
333+
-- information.
334+
addExtraCxxSources :: BuildInfo -> [FilePath] -> BuildInfo
335+
addExtraCxxSources bi extras = bi { cxxSources = new }
336+
where new = Set.toList $ old `Set.union` exs
337+
old = Set.fromList $ cxxSources bi
338+
exs = Set.fromList extras
339+
340+
332341
replComponent :: Verbosity
333342
-> PackageDescription
334343
-> LocalBuildInfo

Cabal/Distribution/Simple/GHC.hs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,8 @@ buildOrReplLib forRepl verbosity numJobs pkg_descr lbi lib clbi = do
559559
createDirectoryIfMissingVerbose verbosity True libTargetDir
560560
-- TODO: do we need to put hs-boot files into place for mutually recursive
561561
-- modules?
562-
let cObjs = map (`replaceExtension` objExtension) (cSources libBi)
562+
let cLikeFiles = fromNubListR $ toNubListR (cSources libBi) <> toNubListR (cxxSources libBi)
563+
cObjs = map (`replaceExtension` objExtension) cLikeFiles
563564
baseOpts = componentGhcOptions verbosity lbi libBi clbi libTargetDir
564565
vanillaOpts = baseOpts `mappend` mempty {
565566
ghcOptMode = toFlag GhcModeMake,
@@ -644,6 +645,39 @@ buildOrReplLib forRepl verbosity numJobs pkg_descr lbi lib clbi = do
644645
else do vanilla; shared
645646
whenProfLib (runGhcProg profOpts)
646647

648+
-- build any C++ sources seperately
649+
unless (not has_code || null (cxxSources libBi)) $ do
650+
info verbosity "Building C++ Sources..."
651+
sequence_
652+
[ do let baseCxxOpts = Internal.componentCxxGhcOptions verbosity implInfo
653+
lbi libBi clbi libTargetDir filename
654+
vanillaCxxOpts = if isGhcDynamic
655+
then baseCxxOpts { ghcOptFPic = toFlag True }
656+
else baseCxxOpts
657+
profCxxOpts = vanillaCxxOpts `mappend` mempty {
658+
ghcOptProfilingMode = toFlag True,
659+
ghcOptObjSuffix = toFlag "p_o"
660+
}
661+
sharedCxxOpts = vanillaCxxOpts `mappend` mempty {
662+
ghcOptFPic = toFlag True,
663+
ghcOptDynLinkMode = toFlag GhcDynamicOnly,
664+
ghcOptObjSuffix = toFlag "dyn_o"
665+
}
666+
odir = fromFlag (ghcOptObjDir vanillaCxxOpts)
667+
createDirectoryIfMissingVerbose verbosity True odir
668+
let runGhcProgIfNeeded cxxOpts = do
669+
needsRecomp <- checkNeedsRecompilation filename cxxOpts
670+
when needsRecomp $ runGhcProg cxxOpts
671+
runGhcProgIfNeeded vanillaCxxOpts
672+
unless forRepl $
673+
whenSharedLib forceSharedLib (runGhcProgIfNeeded sharedCxxOpts)
674+
unless forRepl $ whenProfLib (runGhcProgIfNeeded profCxxOpts)
675+
| filename <- cxxSources libBi]
676+
677+
when has_code . ifReplLib $ do
678+
when (null (allLibModules lib clbi)) $ warn verbosity "No exposed modules"
679+
ifReplLib (runGhcProg replOpts)
680+
647681
-- build any C sources
648682
unless (not has_code || null (cSources libBi)) $ do
649683
info verbosity "Building C Sources..."
@@ -679,17 +713,13 @@ buildOrReplLib forRepl verbosity numJobs pkg_descr lbi lib clbi = do
679713
-- with ghci, but .c files can depend on .h files generated by ghc by ffi
680714
-- exports.
681715

682-
when has_code . ifReplLib $ do
683-
when (null (allLibModules lib clbi)) $ warn verbosity "No exposed modules"
684-
ifReplLib (runGhcProg replOpts)
685-
686716
-- link:
687717
when has_code . unless forRepl $ do
688718
info verbosity "Linking..."
689719
let cProfObjs = map (`replaceExtension` ("p_" ++ objExtension))
690-
(cSources libBi)
720+
(cSources libBi ++ cxxSources libBi)
691721
cSharedObjs = map (`replaceExtension` ("dyn_" ++ objExtension))
692-
(cSources libBi)
722+
(cSources libBi ++ cxxSources libBi)
693723
compiler_id = compilerId (compiler lbi)
694724
vanillaLibFilePath = libTargetDir </> mkLibName uid
695725
profileLibFilePath = libTargetDir </> mkProfLibName uid

Cabal/Distribution/Simple/GHC/Internal.hs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Distribution.Simple.GHC.Internal (
1919
targetPlatform,
2020
getGhcInfo,
2121
componentCcGhcOptions,
22+
componentCxxGhcOptions,
2223
componentGhcOptions,
2324
mkGHCiLibName,
2425
filterGhciFlags,
@@ -290,6 +291,40 @@ componentCcGhcOptions verbosity _implInfo lbi bi clbi odir filename =
290291
ghcOptObjDir = toFlag odir
291292
}
292293

294+
295+
componentCxxGhcOptions :: Verbosity -> GhcImplInfo -> LocalBuildInfo
296+
-> BuildInfo -> ComponentLocalBuildInfo
297+
-> FilePath -> FilePath
298+
-> GhcOptions
299+
componentCxxGhcOptions verbosity _implInfo lbi bi cxxlbi odir filename =
300+
mempty {
301+
-- Respect -v0, but don't crank up verbosity on GHC if
302+
-- Cabal verbosity is requested. For that, use --ghc-option=-v instead!
303+
ghcOptVerbosity = toFlag (min verbosity normal),
304+
ghcOptMode = toFlag GhcModeCompile,
305+
ghcOptInputFiles = toNubListR [filename],
306+
307+
ghcOptCppIncludePath = toNubListR $ [autogenComponentModulesDir lbi cxxlbi
308+
,autogenPackageModulesDir lbi
309+
,odir]
310+
++ PD.includeDirs bi,
311+
ghcOptHideAllPackages= toFlag True,
312+
ghcOptPackageDBs = withPackageDB lbi,
313+
ghcOptPackages = toNubListR $ mkGhcOptPackages cxxlbi,
314+
ghcOptCxxOptions = toNubListR $
315+
(case withOptimization lbi of
316+
NoOptimisation -> []
317+
_ -> ["-O2"]) ++
318+
(case withDebugInfo lbi of
319+
NoDebugInfo -> []
320+
MinimalDebugInfo -> ["-g1"]
321+
NormalDebugInfo -> ["-g"]
322+
MaximalDebugInfo -> ["-g3"]) ++
323+
PD.cxxOptions bi,
324+
ghcOptObjDir = toFlag odir
325+
}
326+
327+
293328
componentGhcOptions :: Verbosity -> GhcImplInfo -> LocalBuildInfo
294329
-> BuildInfo -> ComponentLocalBuildInfo -> FilePath
295330
-> GhcOptions

Cabal/Distribution/Simple/Program/GHC.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ data GhcOptions = GhcOptions {
154154
-- | Options to pass through to the C compiler; the @ghc -optc@ flag.
155155
ghcOptCcOptions :: NubListR String,
156156

157+
-- | Options to pass through to the C++ compiler.
158+
ghcOptCxxOptions :: NubListR String,
159+
157160
-- | Options to pass through to CPP; the @ghc -optP@ flag.
158161
ghcOptCppOptions :: NubListR String,
159162

@@ -392,13 +395,16 @@ renderGhcOptions comp _platform@(Platform _arch os) opts
392395
, [ "-i" ++ dir | dir <- flags ghcOptSourcePath ]
393396

394397
--------------------
395-
-- C and CPP stuff
398+
399+
--------------------
400+
-- CPP, C, and C++ stuff
396401

397402
, [ "-I" ++ dir | dir <- flags ghcOptCppIncludePath ]
398403
, [ "-optP" ++ opt | opt <- flags ghcOptCppOptions ]
399404
, concat [ [ "-optP-include", "-optP" ++ inc]
400405
| inc <- flags ghcOptCppIncludes ]
401406
, [ "-optc" ++ opt | opt <- flags ghcOptCcOptions ]
407+
, [ "-optc" ++ opt | opt <- flags ghcOptCxxOptions ]
402408

403409
-----------------
404410
-- Linker stuff

Cabal/Distribution/Types/BuildInfo.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ data BuildInfo = BuildInfo {
5252
buildToolDepends :: [ExeDependency],
5353
cppOptions :: [String], -- ^ options for pre-processing Haskell code
5454
ccOptions :: [String], -- ^ options for C compiler
55+
cxxOptions :: [String], -- ^ options for C++ compiler
5556
ldOptions :: [String], -- ^ options for linker
5657
pkgconfigDepends :: [PkgconfigDependency], -- ^ pkg-config packages that are used
5758
frameworks :: [String], -- ^support frameworks for Mac OS X
5859
extraFrameworkDirs:: [String], -- ^ extra locations to find frameworks.
5960
cSources :: [FilePath],
61+
cxxSources :: [FilePath],
6062
jsSources :: [FilePath],
6163
hsSourceDirs :: [FilePath], -- ^ where to look for the Haskell module hierarchy
6264
otherModules :: [ModuleName], -- ^ non-exposed or non-main modules
@@ -95,11 +97,13 @@ instance Monoid BuildInfo where
9597
buildToolDepends = [],
9698
cppOptions = [],
9799
ccOptions = [],
100+
cxxOptions = [],
98101
ldOptions = [],
99102
pkgconfigDepends = [],
100103
frameworks = [],
101104
extraFrameworkDirs = [],
102105
cSources = [],
106+
cxxSources = [],
103107
jsSources = [],
104108
hsSourceDirs = [],
105109
otherModules = [],
@@ -132,11 +136,13 @@ instance Semigroup BuildInfo where
132136
buildToolDepends = combine buildToolDepends,
133137
cppOptions = combine cppOptions,
134138
ccOptions = combine ccOptions,
139+
cxxOptions = combine cxxOptions,
135140
ldOptions = combine ldOptions,
136141
pkgconfigDepends = combine pkgconfigDepends,
137142
frameworks = combineNub frameworks,
138143
extraFrameworkDirs = combineNub extraFrameworkDirs,
139144
cSources = combineNub cSources,
145+
cxxSources = combineNub cxxSources,
140146
jsSources = combineNub jsSources,
141147
hsSourceDirs = combineNub hsSourceDirs,
142148
otherModules = combineNub otherModules,

Cabal/Distribution/Types/BuildInfo/Lens.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class HasBuildInfo a where
4343
ccOptions = buildInfo . ccOptions
4444
{-# INLINE ccOptions #-}
4545

46+
cxxOptions :: Lens' a [String]
47+
cxxOptions = buildInfo . cxxOptions
48+
{-# INLINE cxxOptions #-}
49+
4650
ldOptions :: Lens' a [String]
4751
ldOptions = buildInfo . ldOptions
4852
{-# INLINE ldOptions #-}
@@ -63,6 +67,10 @@ class HasBuildInfo a where
6367
cSources = buildInfo . cSources
6468
{-# INLINE cSources #-}
6569

70+
cxxSources :: Lens' a [FilePath]
71+
cxxSources = buildInfo . cxxSources
72+
{-# INLINE cxxSources #-}
73+
6674
jsSources :: Lens' a [FilePath]
6775
jsSources = buildInfo . jsSources
6876
{-# INLINE jsSources #-}
@@ -171,6 +179,9 @@ instance HasBuildInfo BuildInfo where
171179
ccOptions f s = fmap (\x -> s { T.ccOptions = x }) (f (T.ccOptions s))
172180
{-# INLINE ccOptions #-}
173181

182+
cxxOptions f s = fmap (\x -> s { T.cxxOptions = x }) (f (T.cxxOptions s))
183+
{-# INLINE cxxOptions #-}
184+
174185
ldOptions f s = fmap (\x -> s { T.ldOptions = x }) (f (T.ldOptions s))
175186
{-# INLINE ldOptions #-}
176187

@@ -186,6 +197,9 @@ instance HasBuildInfo BuildInfo where
186197
cSources f s = fmap (\x -> s { T.cSources = x }) (f (T.cSources s))
187198
{-# INLINE cSources #-}
188199

200+
cxxSources f s = fmap (\x -> s { T.cSources = x }) (f (T.cxxSources s))
201+
{-# INLINE cxxSources #-}
202+
189203
jsSources f s = fmap (\x -> s { T.jsSources = x }) (f (T.jsSources s))
190204
{-# INLINE jsSources #-}
191205

Cabal/changelog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
-*-change-log-*-
22

33
2.2.0.0 (current development version)
4+
* Added cxx-options and cxx-sources build info fields for seperate
5+
compilation of C++ source files (#3700)
46
* Remove unused '--allow-newer'/'--allow-older' support (#4527)
57
* Change `rawSystemStdInOut` to use proper type to represent
68
binary and textual data; new 'Distribution.Utils.IOData' module;

0 commit comments

Comments
 (0)