Skip to content

Commit 8a44cde

Browse files
committed
Fix statically linking executables wrt haskell#11224
By supporting GHCs '-fully-static'
1 parent bc52b09 commit 8a44cde

File tree

1 file changed

+23
-15
lines changed
  • Cabal/src/Distribution/Simple/GHC/Build

1 file changed

+23
-15
lines changed

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ linkOrLoadComponent
101101
isIndef = componentIsIndefinite clbi
102102
mbWorkDir = mbWorkDirLBI lbi
103103
tempFileOptions = commonSetupTempFileOptions $ buildingWhatCommonFlags what
104+
comp = compiler lbi
105+
ghcVersion = compilerVersion comp
104106

105107
-- See Note [Symbolic paths] in Distribution.Utils.Path
106108
i = interpretSymbolicPathLBI lbi
@@ -110,6 +112,10 @@ linkOrLoadComponent
110112
cleanedExtraLibDirsStatic <- liftIO $ filterM (doesDirectoryExist . i) (extraLibDirsStatic bi)
111113

112114
let
115+
-- TODO
116+
-- whether GHC supports the -fully-static switch
117+
supportsGhcFullyStatic = ghcVersion >= mkVersion [9, 14]
118+
113119
extraSourcesObjs :: [RelativePath Artifacts File]
114120
extraSourcesObjs =
115121
[ makeRelativePathEx $ getSymbolicPath src `replaceExtension` objExtension
@@ -120,17 +126,19 @@ linkOrLoadComponent
120126
-- for foreign libs in the three cases where we use `withFullyStaticExe` below?
121127
linkerOpts rpaths =
122128
mempty
123-
{ ghcOptLinkOptions =
129+
{ ghcOptExtraDefault =
130+
if supportsGhcFullyStatic
131+
then [ "-fully-static" | withFullyStaticExe lbi]
132+
else []
133+
, ghcOptLinkOptions =
124134
PD.ldOptions bi
125-
++ [ "-static"
126-
| withFullyStaticExe lbi
127-
]
128-
-- Pass extra `ld-options` given
129-
-- through to GHC's linker.
130-
++ maybe
131-
[]
132-
programOverrideArgs
133-
(lookupProgram ldProgram (withPrograms lbi))
135+
++ (if supportsGhcFullyStatic then [] else [ "-static" | withFullyStaticExe lbi])
136+
-- Pass extra `ld-options` given
137+
-- through to GHC's linker.
138+
++ maybe
139+
[]
140+
programOverrideArgs
141+
(lookupProgram ldProgram (withPrograms lbi))
134142
, ghcOptLinkLibs =
135143
if withFullyStaticExe lbi
136144
then extraLibsStatic bi
@@ -211,7 +219,6 @@ linkOrLoadComponent
211219
platform
212220
mbWorkDir
213221
platform = hostPlatform lbi
214-
comp = compiler lbi
215222
get_rpaths ways =
216223
if DynWay `Set.member` ways then getRPaths pbci else return (toNubListR [])
217224
in
@@ -226,7 +233,7 @@ linkOrLoadComponent
226233
CLib lib -> do
227234
let libWays = wantedLibWays isIndef
228235
rpaths <- get_rpaths (Set.fromList libWays)
229-
linkLibrary buildTargetDir cleanedExtraLibDirs pkg_descr verbosity runGhcProg lib lbi clbi extraSources rpaths libWays
236+
linkLibrary buildTargetDir cleanedExtraLibDirs cleanedExtraLibDirsStatic pkg_descr verbosity runGhcProg lib lbi clbi extraSources rpaths libWays
230237
CFLib flib -> do
231238
let flib_way = wantedFLibWay (withDynFLib flib)
232239
rpaths <- get_rpaths (Set.singleton flib_way)
@@ -241,6 +248,8 @@ linkLibrary
241248
-- ^ The library target build directory
242249
-> [SymbolicPath Pkg (Dir Lib)]
243250
-- ^ The list of extra lib dirs that exist (aka "cleaned")
251+
-> [SymbolicPath Pkg (Dir Lib)]
252+
-- ^ The list of extra static lib dirs that exist (aka "cleaned")
244253
-> PackageDescription
245254
-- ^ The package description containing this library
246255
-> Verbosity
@@ -256,7 +265,7 @@ linkLibrary
256265
-> [BuildWay]
257266
-- ^ Wanted build ways and corresponding build options
258267
-> IO ()
259-
linkLibrary buildTargetDir cleanedExtraLibDirs pkg_descr verbosity runGhcProg lib lbi clbi extraSources rpaths wantedWays = do
268+
linkLibrary buildTargetDir cleanedExtraLibDirs cleanedExtraLibDirsStatic pkg_descr verbosity runGhcProg lib lbi clbi extraSources rpaths wantedWays = do
260269
let
261270
common = configCommonFlags $ configFlags lbi
262271
mbWorkDir = flagToMaybe $ setupWorkingDir common
@@ -432,8 +441,7 @@ linkLibrary buildTargetDir cleanedExtraLibDirs pkg_descr verbosity runGhcProg li
432441
, ghcOptInputFiles = toNubListR $ map coerceSymbolicPath staticObjectFiles
433442
, ghcOptOutputFile = toFlag staticLibFilePath
434443
, ghcOptLinkLibs = extraLibs libBi
435-
, -- TODO: Shouldn't this use cleanedExtraLibDirsStatic instead?
436-
ghcOptLinkLibPath = toNubListR $ cleanedExtraLibDirs
444+
, ghcOptLinkLibPath = toNubListR $ cleanedExtraLibDirsStatic
437445
}
438446

439447
staticObjectFiles <- getObjFiles StaticWay

0 commit comments

Comments
 (0)