Skip to content

Commit fc00402

Browse files
angermangeekosaur
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. Co-authored-by: brandon s allbery kf8nh <allbery.b@gmail.com>
1 parent 55aba14 commit fc00402

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

Cabal/src/Distribution/Simple/GHC.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,10 @@ configure verbosity hcPath hcPkgPath conf0 = do
261261
, compilerProperties = ghcInfoMap
262262
}
263263
compPlatform = Internal.targetPlatform ghcInfo
264-
-- configure gcc and ld
265-
progdb4 = Internal.configureToolchain implInfo ghcProg ghcInfoMap progdb3
264+
-- configure gcc and ld
265+
-- similarly to how we need ghc above, we need to know the c compiler
266+
-- generally named `gcc` in cabal, to configure other programs, e.g. ld.
267+
progdb4 <- Internal.configureToolchain verbosity implInfo ghcProg ghcInfoMap progdb3
266268
return (comp, compPlatform, progdb4)
267269

268270
-- | Given something like /usr/local/bin/ghc-6.6.1(.exe) we try and find

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

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -100,37 +100,48 @@ targetPlatform ghcInfo = platformFromTriple =<< lookup "Target platform" ghcInfo
100100

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

233-
configureLd :: Verbosity -> ConfiguredProgram -> IO ConfiguredProgram
234-
configureLd v ldProg = do
235-
ldProg' <- configureLd' v ldProg
244+
configureLd :: ConfiguredProgram -> Verbosity -> ConfiguredProgram -> IO ConfiguredProgram
245+
configureLd gccProg v ldProg = do
246+
ldProg' <- configureLd' gccProg v ldProg
236247
return
237248
ldProg'
238249
{ programDefaultArgs = programDefaultArgs ldProg' ++ ldLinkerFlags
239250
}
240251

241252
-- we need to find out if ld supports the -x flag
242-
configureLd' :: Verbosity -> ConfiguredProgram -> IO ConfiguredProgram
243-
configureLd' verbosity ldProg = do
253+
configureLd' :: ConfiguredProgram -> Verbosity -> ConfiguredProgram -> IO ConfiguredProgram
254+
configureLd' gccProg verbosity ldProg = do
244255
ldx <- withTempFile ".c" $ \testcfile testchnd ->
245256
withTempFile ".o" $ \testofile testohnd -> do
246257
hPutStrLn testchnd "int foo() { return 0; }"
247258
hClose testchnd
248259
hClose testohnd
249260
runProgram
250261
verbosity
251-
ghcProg
252-
[ "-hide-all-packages"
253-
, "-c"
262+
gccProg
263+
[ "-c"
254264
, testcfile
255265
, "-o"
256266
, testofile

0 commit comments

Comments
 (0)