Skip to content

Commit

Permalink
Merge pull request haskell#9443 from erikd/erikd/relocatable-flag-2
Browse files Browse the repository at this point in the history
Use linker capability detection to improve linker use
  • Loading branch information
mergify[bot] authored Nov 17, 2023
2 parents a134b26 + 53fc3d3 commit 03809b3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 22 deletions.
35 changes: 16 additions & 19 deletions Cabal/src/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import Distribution.Simple.PackageIndex (InstalledPackageIndex)
import qualified Distribution.Simple.PackageIndex as PackageIndex
import Distribution.Simple.PreProcess
import Distribution.Simple.Program
import Distribution.Simple.Program.Db (lookupProgramByName)
import Distribution.Simple.Setup.Common as Setup
import Distribution.Simple.Setup.Config as Setup
import Distribution.Simple.Utils
Expand Down Expand Up @@ -767,22 +768,16 @@ configure (pkg_descr0, pbi) cfg = do
)
return False

let compilerSupportsGhciLibs :: Bool
compilerSupportsGhciLibs =
case compilerId comp of
CompilerId GHC version
| version > mkVersion [9, 3] && windows ->
False
CompilerId GHC _ ->
True
CompilerId GHCJS _ ->
True
_ -> False
where
windows = case compPlatform of
Platform _ Windows -> True
Platform _ _ -> False

-- Basically yes/no/unknown.
let linkerSupportsRelocations :: Maybe Bool
linkerSupportsRelocations =
case lookupProgramByName "ld" programDb'' of
Nothing -> Nothing
Just ld ->
case Map.lookup "Supports relocatable output" $ programProperties ld of
Just "YES" -> Just True
Just "NO" -> Just False
_other -> Nothing
let ghciLibByDefault =
case compilerId comp of
CompilerId GHC _ ->
Expand All @@ -801,10 +796,12 @@ configure (pkg_descr0, pbi) cfg = do

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

Expand Down
4 changes: 3 additions & 1 deletion Cabal/src/Distribution/Simple/GHC/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ configureToolchain _implInfo ghcProg ghcInfo =
. addKnownProgram
ldProgram
{ programFindLocation = findProg ldProgramName extraLdPath
, programPostConf = configureLd
, programPostConf = \v cp ->
-- Call any existing configuration first and then add any new configuration
configureLd v =<< programPostConf ldProgram v cp
}
. addKnownProgram
arProgram
Expand Down
14 changes: 13 additions & 1 deletion Cabal/src/Distribution/Simple/Program/Builtin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,19 @@ ldProgram =
-- `lld` only accepts `-help`.
`catchIO` (\_ -> return "")
let k = "Supports relocatable output"
v = if "--relocatable" `isInfixOf` ldHelpOutput then "YES" else "NO"
-- Standard GNU `ld` uses `--relocatable` while `ld.gold` uses
-- `-relocatable` (single `-`).
v
| "-relocatable" `isInfixOf` ldHelpOutput = "YES"
-- ld64 on macOS has this lovely response for "--help"
--
-- ld64: For information on command line options please use 'man ld'.
--
-- it does however support -r, if you read the manpage
-- (e.g. https://www.manpagez.com/man/1/ld64/)
| "ld64:" `isPrefixOf` ldHelpOutput = "YES"
| otherwise = "NO"

m = Map.insert k v (programProperties ldProg)
return $ ldProg{programProperties = m}
}
Expand Down
7 changes: 6 additions & 1 deletion Cabal/src/Distribution/Simple/Program/Db.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module Distribution.Simple.Program.Db
, userSpecifyArgss
, userSpecifiedArgs
, lookupProgram
, lookupProgramByName
, updateProgram
, configuredPrograms

Expand Down Expand Up @@ -299,7 +300,11 @@ userSpecifiedArgs prog =

-- | Try to find a configured program
lookupProgram :: Program -> ProgramDb -> Maybe ConfiguredProgram
lookupProgram prog = Map.lookup (programName prog) . configuredProgs
lookupProgram = lookupProgramByName . programName

-- | Try to find a configured program
lookupProgramByName :: String -> ProgramDb -> Maybe ConfiguredProgram
lookupProgramByName name = Map.lookup name . configuredProgs

-- | Update a configured program in the database.
updateProgram
Expand Down
11 changes: 11 additions & 0 deletions changelog.d/pr-9443
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
synopsis: Use linker capability detection to improve linker use
packages: Cabal
prs: #9443

description: {

- Previously the GHC version number and platform were used as a proxy for whether
the linker can generate relocatable objects.
- Now, the ability of the linker to create relocatable objects is detected.

}

0 comments on commit 03809b3

Please sign in to comment.