Skip to content

Commit 03809b3

Browse files
authored
Merge pull request #9443 from erikd/erikd/relocatable-flag-2
Use linker capability detection to improve linker use
2 parents a134b26 + 53fc3d3 commit 03809b3

File tree

5 files changed

+49
-22
lines changed

5 files changed

+49
-22
lines changed

Cabal/src/Distribution/Simple/Configure.hs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import Distribution.Simple.PackageIndex (InstalledPackageIndex)
8282
import qualified Distribution.Simple.PackageIndex as PackageIndex
8383
import Distribution.Simple.PreProcess
8484
import Distribution.Simple.Program
85+
import Distribution.Simple.Program.Db (lookupProgramByName)
8586
import Distribution.Simple.Setup.Common as Setup
8687
import Distribution.Simple.Setup.Config as Setup
8788
import Distribution.Simple.Utils
@@ -767,22 +768,16 @@ configure (pkg_descr0, pbi) cfg = do
767768
)
768769
return False
769770

770-
let compilerSupportsGhciLibs :: Bool
771-
compilerSupportsGhciLibs =
772-
case compilerId comp of
773-
CompilerId GHC version
774-
| version > mkVersion [9, 3] && windows ->
775-
False
776-
CompilerId GHC _ ->
777-
True
778-
CompilerId GHCJS _ ->
779-
True
780-
_ -> False
781-
where
782-
windows = case compPlatform of
783-
Platform _ Windows -> True
784-
Platform _ _ -> False
785-
771+
-- Basically yes/no/unknown.
772+
let linkerSupportsRelocations :: Maybe Bool
773+
linkerSupportsRelocations =
774+
case lookupProgramByName "ld" programDb'' of
775+
Nothing -> Nothing
776+
Just ld ->
777+
case Map.lookup "Supports relocatable output" $ programProperties ld of
778+
Just "YES" -> Just True
779+
Just "NO" -> Just False
780+
_other -> Nothing
786781
let ghciLibByDefault =
787782
case compilerId comp of
788783
CompilerId GHC _ ->
@@ -801,10 +796,12 @@ configure (pkg_descr0, pbi) cfg = do
801796

802797
withGHCiLib_ <-
803798
case fromFlagOrDefault ghciLibByDefault (configGHCiLib cfg) of
804-
True | not compilerSupportsGhciLibs -> do
799+
-- NOTE: If linkerSupportsRelocations is Nothing this may still fail if the
800+
-- linker does not support -r.
801+
True | not (fromMaybe True linkerSupportsRelocations) -> do
805802
warn verbosity $
806-
"--enable-library-for-ghci is no longer supported on Windows with"
807-
++ " GHC 9.4 and later; ignoring..."
803+
"--enable-library-for-ghci is not supported with the current"
804+
++ " linker; ignoring..."
808805
return False
809806
v -> return v
810807

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ configureToolchain _implInfo ghcProg ghcInfo =
114114
. addKnownProgram
115115
ldProgram
116116
{ programFindLocation = findProg ldProgramName extraLdPath
117-
, programPostConf = configureLd
117+
, programPostConf = \v cp ->
118+
-- Call any existing configuration first and then add any new configuration
119+
configureLd v =<< programPostConf ldProgram v cp
118120
}
119121
. addKnownProgram
120122
arProgram

Cabal/src/Distribution/Simple/Program/Builtin.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,19 @@ ldProgram =
370370
-- `lld` only accepts `-help`.
371371
`catchIO` (\_ -> return "")
372372
let k = "Supports relocatable output"
373-
v = if "--relocatable" `isInfixOf` ldHelpOutput then "YES" else "NO"
373+
-- Standard GNU `ld` uses `--relocatable` while `ld.gold` uses
374+
-- `-relocatable` (single `-`).
375+
v
376+
| "-relocatable" `isInfixOf` ldHelpOutput = "YES"
377+
-- ld64 on macOS has this lovely response for "--help"
378+
--
379+
-- ld64: For information on command line options please use 'man ld'.
380+
--
381+
-- it does however support -r, if you read the manpage
382+
-- (e.g. https://www.manpagez.com/man/1/ld64/)
383+
| "ld64:" `isPrefixOf` ldHelpOutput = "YES"
384+
| otherwise = "NO"
385+
374386
m = Map.insert k v (programProperties ldProg)
375387
return $ ldProg{programProperties = m}
376388
}

Cabal/src/Distribution/Simple/Program/Db.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module Distribution.Simple.Program.Db
4646
, userSpecifyArgss
4747
, userSpecifiedArgs
4848
, lookupProgram
49+
, lookupProgramByName
4950
, updateProgram
5051
, configuredPrograms
5152

@@ -299,7 +300,11 @@ userSpecifiedArgs prog =
299300

300301
-- | Try to find a configured program
301302
lookupProgram :: Program -> ProgramDb -> Maybe ConfiguredProgram
302-
lookupProgram prog = Map.lookup (programName prog) . configuredProgs
303+
lookupProgram = lookupProgramByName . programName
304+
305+
-- | Try to find a configured program
306+
lookupProgramByName :: String -> ProgramDb -> Maybe ConfiguredProgram
307+
lookupProgramByName name = Map.lookup name . configuredProgs
303308

304309
-- | Update a configured program in the database.
305310
updateProgram

changelog.d/pr-9443

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
synopsis: Use linker capability detection to improve linker use
2+
packages: Cabal
3+
prs: #9443
4+
5+
description: {
6+
7+
- Previously the GHC version number and platform were used as a proxy for whether
8+
the linker can generate relocatable objects.
9+
- Now, the ability of the linker to create relocatable objects is detected.
10+
11+
}

0 commit comments

Comments
 (0)