Skip to content

Commit c9f02b2

Browse files
nh223Skidoo
authored andcommitted
Add --enable-executable-static flag. Fixes #391.
Also update the docs for `--enable-executable-dynamic` as they were slightly misleading.
1 parent 14ebc93 commit c9f02b2

14 files changed

Lines changed: 70 additions & 7 deletions

File tree

Cabal/Distribution/Simple/Configure.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@ configure (pkg_descr0, pbi) cfg = do
426426
die' verbosity $ "--enable-tests/--enable-benchmarks are incompatible with" ++
427427
" explicitly specifying a component to configure."
428428

429+
-- Some sanity checks related to dynamic/static linking.
430+
when (fromFlag (configDynExe cfg) && fromFlag (configFullyStaticExe cfg)) $
431+
die' verbosity $ "--enable-executable-dynamic and --enable-executable-static" ++
432+
" are incompatible with each other."
433+
429434
-- allConstraints: The set of all 'Dependency's we have. Used ONLY
430435
-- to 'configureFinalizedPackage'.
431436
-- requiredDepsMap: A map from 'PackageName' to the specifically
@@ -684,6 +689,8 @@ configure (pkg_descr0, pbi) cfg = do
684689
fromFlagOrDefault False $ configStaticLib cfg
685690

686691
withDynExe_ = fromFlag $ configDynExe cfg
692+
693+
withFullyStaticExe_ = fromFlag $ configFullyStaticExe cfg
687694
when (withDynExe_ && not withSharedLib_) $ warn verbosity $
688695
"Executables will use dynamic linking, but a shared library "
689696
++ "is not being built. Linking will fail if any executables "
@@ -723,6 +730,7 @@ configure (pkg_descr0, pbi) cfg = do
723730
withSharedLib = withSharedLib_,
724731
withStaticLib = withStaticLib_,
725732
withDynExe = withDynExe_,
733+
withFullyStaticExe = withFullyStaticExe_,
726734
withProfLib = False,
727735
withProfLibDetail = ProfDetailNone,
728736
withProfExe = False,

Cabal/Distribution/Simple/GHC.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ buildOrReplLib mReplFlags verbosity numJobs pkg_descr lbi lib clbi = do
572572
ghcOptHPCDir = hpcdir Hpc.Dyn
573573
}
574574
linkerOpts = mempty {
575-
ghcOptLinkOptions = PD.ldOptions libBi,
575+
ghcOptLinkOptions = PD.ldOptions libBi
576+
++ [ "-static" | withFullyStaticExe lbi ],
576577
ghcOptLinkLibs = extraLibs libBi,
577578
ghcOptLinkLibPath = toNubListR $ extraLibDirs libBi,
578579
ghcOptLinkFrameworks = toNubListR $ PD.frameworks libBi,
@@ -1254,7 +1255,8 @@ gbuild verbosity numJobs pkg_descr lbi bm clbi = do
12541255
ghcOptHPCDir = hpcdir Hpc.Dyn
12551256
}
12561257
linkerOpts = mempty {
1257-
ghcOptLinkOptions = PD.ldOptions bnfo,
1258+
ghcOptLinkOptions = PD.ldOptions bnfo
1259+
++ [ "-static" | withFullyStaticExe lbi ],
12581260
ghcOptLinkLibs = extraLibs bnfo,
12591261
ghcOptLinkLibPath = toNubListR $ extraLibDirs bnfo,
12601262
ghcOptLinkFrameworks = toNubListR $

Cabal/Distribution/Simple/Setup.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ data ConfigFlags = ConfigFlags {
218218
configStaticLib :: Flag Bool, -- ^Build static library
219219
configDynExe :: Flag Bool, -- ^Enable dynamic linking of the
220220
-- executables.
221+
configFullyStaticExe :: Flag Bool, -- ^Enable fully static linking of the
222+
-- executables.
221223
configProfExe :: Flag Bool, -- ^Enable profiling in the
222224
-- executables.
223225
configProf :: Flag Bool, -- ^Enable profiling in the library
@@ -300,6 +302,7 @@ instance Eq ConfigFlags where
300302
&& equal configSharedLib
301303
&& equal configStaticLib
302304
&& equal configDynExe
305+
&& equal configFullyStaticExe
303306
&& equal configProfExe
304307
&& equal configProf
305308
&& equal configProfDetail
@@ -354,6 +357,7 @@ defaultConfigFlags progDb = emptyConfigFlags {
354357
configSharedLib = NoFlag,
355358
configStaticLib = NoFlag,
356359
configDynExe = Flag False,
360+
configFullyStaticExe = Flag False,
357361
configProfExe = NoFlag,
358362
configProf = NoFlag,
359363
configProfDetail = NoFlag,
@@ -492,6 +496,11 @@ configureOptions showOrParseArgs =
492496
configDynExe (\v flags -> flags { configDynExe = v })
493497
(boolOpt [] [])
494498

499+
,option "" ["executable-static"]
500+
"Executable fully static linking"
501+
configFullyStaticExe (\v flags -> flags { configFullyStaticExe = v })
502+
(boolOpt [] [])
503+
495504
,option "" ["profiling"]
496505
"Executable and library profiling"
497506
configProf (\v flags -> flags { configProf = v })

Cabal/Distribution/Types/LocalBuildInfo.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ data LocalBuildInfo = LocalBuildInfo {
146146
withSharedLib :: Bool, -- ^Whether to build shared versions of libs.
147147
withStaticLib :: Bool, -- ^Whether to build static versions of libs (with all other libs rolled in)
148148
withDynExe :: Bool, -- ^Whether to link executables dynamically
149+
withFullyStaticExe :: Bool, -- ^Whether to link executables fully statically
149150
withProfExe :: Bool, -- ^Whether to build executables for profiling.
150151
withProfLibDetail :: ProfDetailLevel, -- ^Level of automatic profile detail.
151152
withProfExeDetail :: ProfDetailLevel, -- ^Level of automatic profile detail.

Cabal/doc/installing-packages.rst

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,13 +1160,27 @@ Miscellaneous options
11601160

11611161
.. option:: --enable-executable-dynamic
11621162

1163-
Link executables dynamically. The executable's library dependencies
1164-
should be built as shared objects. This implies :option:`--enable-shared`
1163+
Link dependent Haskell libraries into executables dynamically.
1164+
The executable's library dependencies must have been
1165+
built as shared objects. This implies :option:`--enable-shared`
11651166
unless :option:`--disable-shared` is explicitly specified.
11661167

11671168
.. option:: --disable-executable-dynamic
11681169

1169-
(default) Link executables statically.
1170+
(default) Link dependent Haskell libraries into executables statically.
1171+
Non-Haskell (C) libraries are still linked dynamically, including libc,
1172+
so the result is still not a fully static executable
1173+
unless :option:`--enable-executable-dynamic` is given.
1174+
1175+
.. option:: --enable-executable-static
1176+
1177+
Build fully static executables.
1178+
This link all dependent libraries into executables statically,
1179+
including libc.
1180+
1181+
.. option:: --disable-executable-static
1182+
1183+
(default) Do not build fully static executables.
11701184

11711185
.. option:: --configure-option=str
11721186

Cabal/doc/nix-local-build.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,19 @@ Static linking options
15441544
This uses GHCs ``-staticlib`` flag, which is available for iOS and with
15451545
GHC 8.4 and later for other platforms as well.
15461546

1547+
.. cfg-field:: executable-static: boolean
1548+
--enable-executable-static
1549+
--disable-executable-static
1550+
:synopsis: Build fully static executables.
1551+
1552+
1553+
:default: False
1554+
1555+
Build fully static executables.
1556+
This link all dependent libraries into executables statically,
1557+
including libc.
1558+
This passes ``-static`` and ``-optl=-static`` to GHC.
1559+
15471560
Foreign function interface options
15481561
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15491562

cabal-install/Distribution/Client/Config.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ instance Semigroup SavedConfig where
349349
configSharedLib = combine configSharedLib,
350350
configStaticLib = combine configStaticLib,
351351
configDynExe = combine configDynExe,
352+
configFullyStaticExe = combine configFullyStaticExe,
352353
configProfExe = combine configProfExe,
353354
configProfDetail = combine configProfDetail,
354355
configProfLibDetail = combine configProfLibDetail,

cabal-install/Distribution/Client/PackageHash.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ data PackageHashConfigInputs = PackageHashConfigInputs {
194194
pkgHashVanillaLib :: Bool,
195195
pkgHashSharedLib :: Bool,
196196
pkgHashDynExe :: Bool,
197+
pkgHashFullyStaticExe :: Bool,
197198
pkgHashGHCiLib :: Bool,
198199
pkgHashProfLib :: Bool,
199200
pkgHashProfExe :: Bool,
@@ -287,6 +288,7 @@ renderPackageHashInputs PackageHashInputs{
287288
, opt "vanilla-lib" True display pkgHashVanillaLib
288289
, opt "shared-lib" False display pkgHashSharedLib
289290
, opt "dynamic-exe" False display pkgHashDynExe
291+
, opt "fully-static-exe" False display pkgHashFullyStaticExe
290292
, opt "ghci-lib" False display pkgHashGHCiLib
291293
, opt "prof-lib" False display pkgHashProfLib
292294
, opt "prof-exe" False display pkgHashProfExe

cabal-install/Distribution/Client/ProjectConfig/Legacy.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ convertLegacyPerPackageFlags configFlags installFlags haddockFlags testFlags =
380380
configSharedLib = packageConfigSharedLib,
381381
configStaticLib = packageConfigStaticLib,
382382
configDynExe = packageConfigDynExe,
383+
configFullyStaticExe = packageConfigFullyStaticExe,
383384
configProfExe = packageConfigProfExe,
384385
configProf = packageConfigProf,
385386
configProfDetail = packageConfigProfDetail,
@@ -632,6 +633,7 @@ convertToLegacyAllPackageConfig
632633
configSharedLib = mempty,
633634
configStaticLib = mempty,
634635
configDynExe = mempty,
636+
configFullyStaticExe = mempty,
635637
configProfExe = mempty,
636638
configProf = mempty,
637639
configProfDetail = mempty,
@@ -701,6 +703,7 @@ convertToLegacyPerPackageConfig PackageConfig {..} =
701703
configSharedLib = packageConfigSharedLib,
702704
configStaticLib = packageConfigStaticLib,
703705
configDynExe = packageConfigDynExe,
706+
configFullyStaticExe = packageConfigFullyStaticExe,
704707
configProfExe = packageConfigProfExe,
705708
configProf = packageConfigProf,
706709
configProfDetail = packageConfigProfDetail,

cabal-install/Distribution/Client/ProjectConfig/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ data PackageConfig
242242
packageConfigSharedLib :: Flag Bool,
243243
packageConfigStaticLib :: Flag Bool,
244244
packageConfigDynExe :: Flag Bool,
245+
packageConfigFullyStaticExe :: Flag Bool,
245246
packageConfigProf :: Flag Bool, --TODO: [code cleanup] sort out
246247
packageConfigProfLib :: Flag Bool, -- this duplication
247248
packageConfigProfExe :: Flag Bool, -- and consistency

0 commit comments

Comments
 (0)