Skip to content

Commit e522b12

Browse files
committed
Pass *-options and -pgmc to GHC
Refactor componentGhcOptions for pass *-options to all invoking GHC. During the refactoring process we needed to add componentGhcOptions to all Haskell sources. It was also worth add linkGhcOptions to linkLibrary same as componentGhcOptions to linkExecutable, linkFLib. Add test for PackageTests/FFI/ForeignOptsCapi to pass cc-options flags to *.h. Add -pgmc gcc to componentGhcOptions for versions newer then 9.4. Add more tests like PackageTests/ShowBuildInfo/Complex for -pgmc gcc and -optc-O2,-optcxx-O2,-opta-O2. Fixes #9801 #4435
1 parent 71b3c85 commit e522b12

25 files changed

Lines changed: 780 additions & 293 deletions

File tree

Cabal/src/Distribution/Simple/GHC.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ module Distribution.Simple.GHC
6060
, hcPkgInfo
6161
, registerPackage
6262
, Internal.componentGhcOptions
63-
, Internal.componentCcGhcOptions
6463
, getGhcAppDir
6564
, getLibDir
6665
, compilerBuildWay
@@ -825,7 +824,7 @@ libAbiHash verbosity _pkg_descr lbi lib clbi = do
825824
, ghcOptFPic = toFlag True
826825
, ghcOptHiSuffix = toFlag "dyn_hi"
827826
, ghcOptObjSuffix = toFlag "dyn_o"
828-
, ghcOptExtra = hcOptions GHC libBi ++ hcSharedOptions GHC libBi
827+
, ghcOptExtra = hcSharedOptions GHC libBi
829828
}
830829
profArgs =
831830
vanillaArgs
@@ -837,7 +836,7 @@ libAbiHash verbosity _pkg_descr lbi lib clbi = do
837836
(withProfLibDetail lbi)
838837
, ghcOptHiSuffix = toFlag "p_hi"
839838
, ghcOptObjSuffix = toFlag "p_o"
840-
, ghcOptExtra = hcOptions GHC libBi ++ hcProfOptions GHC libBi
839+
, ghcOptExtra = hcProfOptions GHC libBi
841840
}
842841
profDynArgs =
843842
vanillaArgs
@@ -851,7 +850,7 @@ libAbiHash verbosity _pkg_descr lbi lib clbi = do
851850
, ghcOptFPic = toFlag True
852851
, ghcOptHiSuffix = toFlag "p_dyn_hi"
853852
, ghcOptObjSuffix = toFlag "p_dyn_o"
854-
, ghcOptExtra = hcOptions GHC libBi ++ hcProfSharedOptions GHC libBi
853+
, ghcOptExtra = hcProfSharedOptions GHC libBi
855854
}
856855
ghcArgs =
857856
let (libWays, _, _) = buildWays lbi

Cabal/src/Distribution/Simple/GHC/Build/ExtraSources.hs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import Distribution.Utils.NubList
1717
import Distribution.Types.BuildInfo
1818
import Distribution.Types.Component
1919
import Distribution.Types.TargetInfo
20+
import Distribution.Types.Version
2021

2122
import Distribution.Simple.Build.Inputs
22-
import Distribution.Simple.GHC.Build.Modules
23+
import Distribution.Simple.BuildWay
2324
import Distribution.Simple.GHC.Build.Utils
2425
import Distribution.Simple.LocalBuildInfo
2526
import Distribution.Simple.Program.Types
@@ -77,7 +78,23 @@ buildCSources
7778
buildCSources mbMainFile =
7879
buildExtraSources
7980
"C Sources"
80-
Internal.componentCcGhcOptions
81+
( \verbosity lbi bi clbi odir filename ->
82+
(Internal.sourcesGhcOptions verbosity lbi bi clbi odir filename)
83+
{ -- C++ compiler options: GHC >= 8.10 requires -optcxx, older requires -optc
84+
-- we want to be able to support cxx-options and cc-options separately
85+
-- https://gitlab.haskell.org/ghc/ghc/-/issues/16477
86+
-- see example in cabal-testsuite/PackageTests/FFI/ForeignOptsC
87+
ghcOptCxxOptions =
88+
Internal.separateGhcOptions
89+
(mkVersion [8, 10])
90+
(compiler lbi)
91+
(Internal.defaultGhcOptCxxOptions lbi bi)
92+
, -- there are problems with linking with versions below 9.4,
93+
-- that's why we need this replacement for linkGhcOptions
94+
-- see example in cabal-testsuite/PackageTests/ShowBuildInfo/Complex
95+
ghcOptCcProgram = Internal.defaultGhcOptCcProgram lbi
96+
}
97+
)
8198
( \c -> do
8299
let cFiles = cSources (componentBuildInfo c)
83100
case c of
@@ -90,7 +107,23 @@ buildCSources mbMainFile =
90107
buildCxxSources mbMainFile =
91108
buildExtraSources
92109
"C++ Sources"
93-
Internal.componentCxxGhcOptions
110+
( \verbosity lbi bi clbi odir filename ->
111+
(Internal.sourcesGhcOptions verbosity lbi bi clbi odir filename)
112+
{ -- C++ compiler options: GHC >= 8.10 requires -optcxx, older requires -optc
113+
-- we want to be able to support cxx-options and cc-options separately
114+
-- https://gitlab.haskell.org/ghc/ghc/-/issues/16477
115+
-- see example in cabal-testsuite/PackageTests/FFI/ForeignOptsCxx
116+
ghcOptCcOptions =
117+
Internal.separateGhcOptions
118+
(mkVersion [8, 10])
119+
(compiler lbi)
120+
(Internal.defaultGhcOptCcOptions lbi bi)
121+
, -- there are problems with linking with versions below 9.4,
122+
-- that's why we need this replacement for linkGhcOptions
123+
-- see example in cabal-testsuite/PackageTests/ShowBuildInfo/Complex
124+
ghcOptCcProgram = Internal.defaultGhcOptCcProgram lbi
125+
}
126+
)
94127
( \c -> do
95128
let cxxFiles = cxxSources (componentBuildInfo c)
96129
case c of
@@ -105,7 +138,7 @@ buildJsSources _mbMainFile ghcProg buildTargetDir neededWays verbHandles = do
105138
let hasJsSupport = hostArch == JavaScript
106139
buildExtraSources
107140
"JS Sources"
108-
Internal.componentJsGhcOptions
141+
Internal.sourcesGhcOptions
109142
( \c ->
110143
if hasJsSupport
111144
then -- JS files are C-like with GHC's JS backend: they are
@@ -122,12 +155,12 @@ buildJsSources _mbMainFile ghcProg buildTargetDir neededWays verbHandles = do
122155
buildAsmSources _mbMainFile =
123156
buildExtraSources
124157
"Assembler Sources"
125-
Internal.componentAsmGhcOptions
158+
Internal.sourcesGhcOptions
126159
(asmSources . componentBuildInfo)
127160
buildCmmSources _mbMainFile =
128161
buildExtraSources
129162
"C-- Sources"
130-
Internal.componentCmmGhcOptions
163+
Internal.sourcesGhcOptions
131164
(cmmSources . componentBuildInfo)
132165

133166
-- | Create 'PreBuildComponentRules' for a given type of extra build sources
@@ -145,9 +178,7 @@ buildExtraSources
145178
-> GhcOptions
146179
)
147180
-- ^ Function to determine the @'GhcOptions'@ for the
148-
-- invocation of GHC when compiling these extra sources (e.g.
149-
-- @'Internal.componentCxxGhcOptions'@,
150-
-- @'Internal.componentCmmGhcOptions'@)
181+
-- invocation of GHC when compiling these extra sources
151182
-> (Component -> [SymbolicPath Pkg File])
152183
-- ^ View the extra sources of a component, typically from
153184
-- the build info (e.g. @'asmSources'@, @'cSources'@).

Cabal/src/Distribution/Simple/GHC/Build/Link.hs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,9 @@ linkOrLoadComponent
122122
linkerOpts rpaths =
123123
mempty
124124
{ ghcOptLinkOptions =
125-
PD.ldOptions bi
126-
++ [ "-static"
127-
| withFullyStaticExe lbi
128-
]
125+
[ "-static"
126+
| withFullyStaticExe lbi
127+
]
129128
-- Pass extra `ld-options` given
130129
-- through to GHC's linker.
131130
++ maybe
@@ -356,36 +355,11 @@ linkLibrary buildTargetDir cleanedExtraLibDirs pkg_descr verbosity runGhcProg li
356355
-- Right now, instead, we pass the path to each object file.
357356
ghcBaseLinkArgs :: GhcOptions
358357
ghcBaseLinkArgs =
359-
mempty
360-
{ -- TODO: This basically duplicates componentGhcOptions.
361-
-- I think we want to do the same as we do for executables: re-use the
362-
-- base options, and link by module names, not object paths.
363-
ghcOptExtra = hcStaticOptions GHC libBi
364-
, ghcOptHideAllPackages = toFlag True
365-
, ghcOptNoAutoLinkPackages = toFlag True
366-
, ghcOptPackageDBs = withPackageDB lbi
367-
, ghcOptThisUnitId = case clbi of
368-
LibComponentLocalBuildInfo{componentCompatPackageKey = pk} ->
369-
toFlag pk
370-
_ -> mempty
371-
, ghcOptThisComponentId = case clbi of
372-
LibComponentLocalBuildInfo
373-
{ componentInstantiatedWith = insts
374-
} ->
375-
if null insts
376-
then mempty
377-
else toFlag (componentComponentId clbi)
378-
_ -> mempty
379-
, ghcOptInstantiatedWith = case clbi of
380-
LibComponentLocalBuildInfo
381-
{ componentInstantiatedWith = insts
382-
} ->
383-
insts
384-
_ -> []
385-
, ghcOptPackages =
386-
toNubListR $
387-
Internal.mkGhcOptPackages mempty clbi
388-
}
358+
Internal.linkGhcOptions (verbosityLevel verbosity) lbi libBi clbi
359+
<> mempty
360+
{ ghcOptExtra = hcStaticOptions GHC libBi
361+
, ghcOptNoAutoLinkPackages = toFlag True
362+
}
389363

390364
-- After the relocation lib is created we invoke ghc -shared
391365
-- with the dependencies spelled out as -package arguments

0 commit comments

Comments
 (0)