Skip to content

Commit 578f778

Browse files
angermanandreabedini
authored andcommitted
lib:Cabal - do not use GHC to configure LD.
Cabal uses a peculiar c program to check if LD supports and should use -x. To do this, it shells out to GHC to compiler the C file. This however requires that GHC will not bail out, yet cabal does not pass --package-db flags to this GHC invocation, and as such we can run into situations where GHC bails out, especially during GHC bootstrap phases where not all boot packages are available. We however do not need GHC to compiler a c program, and can rely on the C compiler. Fundamentally cabal does not allow modelling program dependencies in the program db, as such we must configure gcc first before using it. We make a small change to lib:Cabal (specifically the GHC module, and it's Internal companion) to allow it to configure gcc first, before trying to configure ld, and thus having gcc in scope while configuring ld. This removes the need for the awkward ghc invocation to compiler the test program.
1 parent c17d3fe commit 578f778

File tree

2 files changed

+44
-43
lines changed

2 files changed

+44
-43
lines changed

Cabal/src/Distribution/Simple/GHC.hs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,8 @@ compilerProgramDb verbosity comp progdb1 hcPkgPath = do
312312
addKnownProgram hpcProgram' $
313313
addKnownProgram runghcProgram' progdb2
314314

315-
-- configure gcc, ld, ar etc... based on the paths stored
316-
-- in the GHC settings file
317-
progdb4 =
318-
Internal.configureToolchain
319-
(ghcVersionImplInfo ghcVersion)
320-
ghcProg
321-
(compilerProperties comp)
322-
progdb3
323-
return progdb4
315+
-- configure gcc, ld, ar etc... based on the paths stored in the GHC settings file
316+
Internal.configureToolchain verbosity (ghcVersionImplInfo ghcVersion) ghcProg (compilerProperties comp) progdb3
324317

325318
-- | Given something like /usr/local/bin/ghc-6.6.1(.exe) we try and find
326319
-- the corresponding tool; e.g. if the tool is ghc-pkg, we try looking

Cabal/src/Distribution/Simple/GHC/Internal.hs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -104,37 +104,46 @@ targetPlatform ghcInfo = platformFromTriple =<< lookup "Target platform" ghcInfo
104104

105105
-- | Adjust the way we find and configure gcc and ld
106106
configureToolchain
107-
:: GhcImplInfo
107+
:: Verbosity
108+
-> GhcImplInfo
108109
-> ConfiguredProgram
109110
-> Map String String
110111
-> ProgramDb
111-
-> ProgramDb
112-
configureToolchain _implInfo ghcProg ghcInfo =
113-
addKnownProgram
114-
gccProgram
115-
{ programFindLocation = findProg gccProgramName extraGccPath
116-
, programPostConf = configureGcc
117-
}
118-
. addKnownProgram
119-
gppProgram
112+
-> IO ProgramDb
113+
configureToolchain verbosity _implInfo ghcProg ghcInfo db = do
114+
-- this is a bit of a hack. We have a dependency of ld on gcc.
115+
-- ld needs to compiler a c program, to check an ld feature.
116+
-- we _could_ use ghc as a c frontend, but we do not pass all
117+
-- db stack appropriately, and thus we can run into situations
118+
-- where GHC will fail if it's stricter in it's wired-in-unit
119+
-- selction and has the wrong db stack. However we don't need
120+
-- ghc to compile a _test_ c program. So we configure `gcc`
121+
-- first and then use `gcc` (the generic c compiler in cabal
122+
-- terminology) to compile the test program.
123+
let db' = flip addKnownProgram db $
124+
gccProgram
125+
{ programFindLocation = findProg gccProgramName extraGccPath
126+
, programPostConf = configureGcc
127+
}
128+
(gccProg, db'') <- requireProgram verbosity gccProgram db'
129+
return $ flip addKnownPrograms db'' $
130+
[ gppProgram
120131
{ programFindLocation = findProg gppProgramName extraGppPath
121132
, programPostConf = configureGpp
122133
}
123-
. addKnownProgram
124-
ldProgram
125-
{ programFindLocation = findProg ldProgramName extraLdPath
126-
, programPostConf = \v cp ->
127-
-- Call any existing configuration first and then add any new configuration
128-
configureLd v =<< programPostConf ldProgram v cp
129-
}
130-
. addKnownProgram
131-
arProgram
132-
{ programFindLocation = findProg arProgramName extraArPath
133-
}
134-
. addKnownProgram
135-
stripProgram
136-
{ programFindLocation = findProg stripProgramName extraStripPath
137-
}
134+
, ldProgram
135+
{ programFindLocation = findProg ldProgramName extraLdPath
136+
, programPostConf = \v cp ->
137+
-- Call any existing configuration first and then add any new configuration
138+
configureLd gccProg v =<< programPostConf ldProgram v cp
139+
}
140+
, arProgram
141+
{ programFindLocation = findProg arProgramName extraArPath
142+
}
143+
, stripProgram
144+
{ programFindLocation = findProg stripProgramName extraStripPath
145+
}
146+
]
138147
where
139148
compilerDir, base_dir, mingwBinDir :: FilePath
140149
compilerDir = takeDirectory (programPath ghcProg)
@@ -234,27 +243,26 @@ configureToolchain _implInfo ghcProg ghcInfo =
234243
++ cxxFlags
235244
}
236245

237-
configureLd :: Verbosity -> ConfiguredProgram -> IO ConfiguredProgram
238-
configureLd v ldProg = do
239-
ldProg' <- configureLd' v ldProg
246+
configureLd :: ConfiguredProgram -> Verbosity -> ConfiguredProgram -> IO ConfiguredProgram
247+
configureLd gccProg v ldProg = do
248+
ldProg' <- configureLd' gccProg v ldProg
240249
return
241250
ldProg'
242251
{ programDefaultArgs = programDefaultArgs ldProg' ++ ldLinkerFlags
243252
}
244253

245254
-- we need to find out if ld supports the -x flag
246-
configureLd' :: Verbosity -> ConfiguredProgram -> IO ConfiguredProgram
247-
configureLd' verbosity ldProg = do
255+
configureLd' :: ConfiguredProgram -> Verbosity -> ConfiguredProgram -> IO ConfiguredProgram
256+
configureLd' gccProg v ldProg = do
248257
ldx <- withTempFile ".c" $ \testcfile testchnd ->
249258
withTempFile ".o" $ \testofile testohnd -> do
250259
hPutStrLn testchnd "int foo() { return 0; }"
251260
hClose testchnd
252261
hClose testohnd
253262
runProgram
254-
verbosity
255-
ghcProg
256-
[ "-hide-all-packages"
257-
, "-c"
263+
v
264+
gccProg
265+
[ "-c"
258266
, testcfile
259267
, "-o"
260268
, testofile

0 commit comments

Comments
 (0)