Skip to content

Commit

Permalink
Add --enable-executable-static flag. Fixes haskell#391.
Browse files Browse the repository at this point in the history
Also update the docs for `--enable-executable-dynamic`
as they were slightly misleading.
  • Loading branch information
nh2 authored and 23Skidoo committed Feb 7, 2019
1 parent 14ebc93 commit c9f02b2
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 7 deletions.
8 changes: 8 additions & 0 deletions Cabal/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ configure (pkg_descr0, pbi) cfg = do
die' verbosity $ "--enable-tests/--enable-benchmarks are incompatible with" ++
" explicitly specifying a component to configure."

-- Some sanity checks related to dynamic/static linking.
when (fromFlag (configDynExe cfg) && fromFlag (configFullyStaticExe cfg)) $
die' verbosity $ "--enable-executable-dynamic and --enable-executable-static" ++
" are incompatible with each other."

-- allConstraints: The set of all 'Dependency's we have. Used ONLY
-- to 'configureFinalizedPackage'.
-- requiredDepsMap: A map from 'PackageName' to the specifically
Expand Down Expand Up @@ -684,6 +689,8 @@ configure (pkg_descr0, pbi) cfg = do
fromFlagOrDefault False $ configStaticLib cfg

withDynExe_ = fromFlag $ configDynExe cfg

withFullyStaticExe_ = fromFlag $ configFullyStaticExe cfg
when (withDynExe_ && not withSharedLib_) $ warn verbosity $
"Executables will use dynamic linking, but a shared library "
++ "is not being built. Linking will fail if any executables "
Expand Down Expand Up @@ -723,6 +730,7 @@ configure (pkg_descr0, pbi) cfg = do
withSharedLib = withSharedLib_,
withStaticLib = withStaticLib_,
withDynExe = withDynExe_,
withFullyStaticExe = withFullyStaticExe_,
withProfLib = False,
withProfLibDetail = ProfDetailNone,
withProfExe = False,
Expand Down
6 changes: 4 additions & 2 deletions Cabal/Distribution/Simple/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ buildOrReplLib mReplFlags verbosity numJobs pkg_descr lbi lib clbi = do
ghcOptHPCDir = hpcdir Hpc.Dyn
}
linkerOpts = mempty {
ghcOptLinkOptions = PD.ldOptions libBi,
ghcOptLinkOptions = PD.ldOptions libBi
++ [ "-static" | withFullyStaticExe lbi ],
ghcOptLinkLibs = extraLibs libBi,
ghcOptLinkLibPath = toNubListR $ extraLibDirs libBi,
ghcOptLinkFrameworks = toNubListR $ PD.frameworks libBi,
Expand Down Expand Up @@ -1254,7 +1255,8 @@ gbuild verbosity numJobs pkg_descr lbi bm clbi = do
ghcOptHPCDir = hpcdir Hpc.Dyn
}
linkerOpts = mempty {
ghcOptLinkOptions = PD.ldOptions bnfo,
ghcOptLinkOptions = PD.ldOptions bnfo
++ [ "-static" | withFullyStaticExe lbi ],
ghcOptLinkLibs = extraLibs bnfo,
ghcOptLinkLibPath = toNubListR $ extraLibDirs bnfo,
ghcOptLinkFrameworks = toNubListR $
Expand Down
9 changes: 9 additions & 0 deletions Cabal/Distribution/Simple/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ data ConfigFlags = ConfigFlags {
configStaticLib :: Flag Bool, -- ^Build static library
configDynExe :: Flag Bool, -- ^Enable dynamic linking of the
-- executables.
configFullyStaticExe :: Flag Bool, -- ^Enable fully static linking of the
-- executables.
configProfExe :: Flag Bool, -- ^Enable profiling in the
-- executables.
configProf :: Flag Bool, -- ^Enable profiling in the library
Expand Down Expand Up @@ -300,6 +302,7 @@ instance Eq ConfigFlags where
&& equal configSharedLib
&& equal configStaticLib
&& equal configDynExe
&& equal configFullyStaticExe
&& equal configProfExe
&& equal configProf
&& equal configProfDetail
Expand Down Expand Up @@ -354,6 +357,7 @@ defaultConfigFlags progDb = emptyConfigFlags {
configSharedLib = NoFlag,
configStaticLib = NoFlag,
configDynExe = Flag False,
configFullyStaticExe = Flag False,
configProfExe = NoFlag,
configProf = NoFlag,
configProfDetail = NoFlag,
Expand Down Expand Up @@ -492,6 +496,11 @@ configureOptions showOrParseArgs =
configDynExe (\v flags -> flags { configDynExe = v })
(boolOpt [] [])

,option "" ["executable-static"]
"Executable fully static linking"
configFullyStaticExe (\v flags -> flags { configFullyStaticExe = v })
(boolOpt [] [])

,option "" ["profiling"]
"Executable and library profiling"
configProf (\v flags -> flags { configProf = v })
Expand Down
1 change: 1 addition & 0 deletions Cabal/Distribution/Types/LocalBuildInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ data LocalBuildInfo = LocalBuildInfo {
withSharedLib :: Bool, -- ^Whether to build shared versions of libs.
withStaticLib :: Bool, -- ^Whether to build static versions of libs (with all other libs rolled in)
withDynExe :: Bool, -- ^Whether to link executables dynamically
withFullyStaticExe :: Bool, -- ^Whether to link executables fully statically
withProfExe :: Bool, -- ^Whether to build executables for profiling.
withProfLibDetail :: ProfDetailLevel, -- ^Level of automatic profile detail.
withProfExeDetail :: ProfDetailLevel, -- ^Level of automatic profile detail.
Expand Down
20 changes: 17 additions & 3 deletions Cabal/doc/installing-packages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1160,13 +1160,27 @@ Miscellaneous options

.. option:: --enable-executable-dynamic

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

.. option:: --disable-executable-dynamic

(default) Link executables statically.
(default) Link dependent Haskell libraries into executables statically.
Non-Haskell (C) libraries are still linked dynamically, including libc,
so the result is still not a fully static executable
unless :option:`--enable-executable-dynamic` is given.

.. option:: --enable-executable-static

Build fully static executables.
This link all dependent libraries into executables statically,
including libc.

.. option:: --disable-executable-static

(default) Do not build fully static executables.

.. option:: --configure-option=str

Expand Down
13 changes: 13 additions & 0 deletions Cabal/doc/nix-local-build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,19 @@ Static linking options
This uses GHCs ``-staticlib`` flag, which is available for iOS and with
GHC 8.4 and later for other platforms as well.

.. cfg-field:: executable-static: boolean
--enable-executable-static
--disable-executable-static
:synopsis: Build fully static executables.


:default: False

Build fully static executables.
This link all dependent libraries into executables statically,
including libc.
This passes ``-static`` and ``-optl=-static`` to GHC.

Foreign function interface options
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
1 change: 1 addition & 0 deletions cabal-install/Distribution/Client/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ instance Semigroup SavedConfig where
configSharedLib = combine configSharedLib,
configStaticLib = combine configStaticLib,
configDynExe = combine configDynExe,
configFullyStaticExe = combine configFullyStaticExe,
configProfExe = combine configProfExe,
configProfDetail = combine configProfDetail,
configProfLibDetail = combine configProfLibDetail,
Expand Down
2 changes: 2 additions & 0 deletions cabal-install/Distribution/Client/PackageHash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ data PackageHashConfigInputs = PackageHashConfigInputs {
pkgHashVanillaLib :: Bool,
pkgHashSharedLib :: Bool,
pkgHashDynExe :: Bool,
pkgHashFullyStaticExe :: Bool,
pkgHashGHCiLib :: Bool,
pkgHashProfLib :: Bool,
pkgHashProfExe :: Bool,
Expand Down Expand Up @@ -287,6 +288,7 @@ renderPackageHashInputs PackageHashInputs{
, opt "vanilla-lib" True display pkgHashVanillaLib
, opt "shared-lib" False display pkgHashSharedLib
, opt "dynamic-exe" False display pkgHashDynExe
, opt "fully-static-exe" False display pkgHashFullyStaticExe
, opt "ghci-lib" False display pkgHashGHCiLib
, opt "prof-lib" False display pkgHashProfLib
, opt "prof-exe" False display pkgHashProfExe
Expand Down
3 changes: 3 additions & 0 deletions cabal-install/Distribution/Client/ProjectConfig/Legacy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ convertLegacyPerPackageFlags configFlags installFlags haddockFlags testFlags =
configSharedLib = packageConfigSharedLib,
configStaticLib = packageConfigStaticLib,
configDynExe = packageConfigDynExe,
configFullyStaticExe = packageConfigFullyStaticExe,
configProfExe = packageConfigProfExe,
configProf = packageConfigProf,
configProfDetail = packageConfigProfDetail,
Expand Down Expand Up @@ -632,6 +633,7 @@ convertToLegacyAllPackageConfig
configSharedLib = mempty,
configStaticLib = mempty,
configDynExe = mempty,
configFullyStaticExe = mempty,
configProfExe = mempty,
configProf = mempty,
configProfDetail = mempty,
Expand Down Expand Up @@ -701,6 +703,7 @@ convertToLegacyPerPackageConfig PackageConfig {..} =
configSharedLib = packageConfigSharedLib,
configStaticLib = packageConfigStaticLib,
configDynExe = packageConfigDynExe,
configFullyStaticExe = packageConfigFullyStaticExe,
configProfExe = packageConfigProfExe,
configProf = packageConfigProf,
configProfDetail = packageConfigProfDetail,
Expand Down
1 change: 1 addition & 0 deletions cabal-install/Distribution/Client/ProjectConfig/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ data PackageConfig
packageConfigSharedLib :: Flag Bool,
packageConfigStaticLib :: Flag Bool,
packageConfigDynExe :: Flag Bool,
packageConfigFullyStaticExe :: Flag Bool,
packageConfigProf :: Flag Bool, --TODO: [code cleanup] sort out
packageConfigProfLib :: Flag Bool, -- this duplication
packageConfigProfExe :: Flag Bool, -- and consistency
Expand Down
3 changes: 3 additions & 0 deletions cabal-install/Distribution/Client/ProjectPlanning.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1819,6 +1819,7 @@ elaborateInstallPlan verbosity platform compiler compilerprogdb pkgConfigDB
elabSharedLib = pkgid `Set.member` pkgsUseSharedLibrary
elabStaticLib = perPkgOptionFlag pkgid False packageConfigStaticLib
elabDynExe = perPkgOptionFlag pkgid False packageConfigDynExe
elabFullyStaticExe = perPkgOptionFlag pkgid False packageConfigFullyStaticExe
elabGHCiLib = perPkgOptionFlag pkgid False packageConfigGHCiLib --TODO: [required feature] needs to default to enabled on windows still

elabProfExe = perPkgOptionFlag pkgid False packageConfigProf
Expand Down Expand Up @@ -3272,6 +3273,7 @@ setupHsConfigureFlags (ReadyPackage elab@ElaboratedConfiguredPackage{..})
configStaticLib = toFlag elabStaticLib

configDynExe = toFlag elabDynExe
configFullyStaticExe = toFlag elabFullyStaticExe
configGHCiLib = toFlag elabGHCiLib
configProfExe = mempty
configProfLib = toFlag elabProfLib
Expand Down Expand Up @@ -3625,6 +3627,7 @@ packageHashConfigInputs shared@ElaboratedSharedConfig{..} pkg =
pkgHashVanillaLib = elabVanillaLib,
pkgHashSharedLib = elabSharedLib,
pkgHashDynExe = elabDynExe,
pkgHashFullyStaticExe = elabFullyStaticExe,
pkgHashGHCiLib = elabGHCiLib,
pkgHashProfLib = elabProfLib,
pkgHashProfExe = elabProfExe,
Expand Down
1 change: 1 addition & 0 deletions cabal-install/Distribution/Client/ProjectPlanning/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ data ElaboratedConfiguredPackage
elabSharedLib :: Bool,
elabStaticLib :: Bool,
elabDynExe :: Bool,
elabFullyStaticExe :: Bool,
elabGHCiLib :: Bool,
elabProfLib :: Bool,
elabProfExe :: Bool,
Expand Down
2 changes: 2 additions & 0 deletions cabal-install/Distribution/Client/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,15 @@ filterConfigureFlags flags cabalLibVersion
-- Cabal < 1.23 doesn't know about '--profiling-detail'.
-- Cabal < 1.23 has a hacked up version of 'enable-profiling'
-- which we shouldn't use.
-- Cabal < 1.23 doesn't know about '--enable/disable-executable-static'.
(tryLibProfiling, tryExeProfiling) = computeEffectiveProfiling flags
flags_1_23_0 = flags_1_25_0 { configProfDetail = NoFlag
, configProfLibDetail = NoFlag
, configIPID = NoFlag
, configProf = NoFlag
, configProfExe = Flag tryExeProfiling
, configProfLib = Flag tryLibProfiling
, configFullyStaticExe = NoFlag
}

-- Cabal < 1.22 doesn't know about '--disable-debug-info'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ instance Arbitrary PackageConfig where
<*> arbitrary
<*> arbitrary <*> arbitrary <*> arbitrary
<*> arbitrary <*> arbitrary
<*> arbitrary
<*> arbitrary <*> arbitrary
<*> arbitrary <*> arbitrary
<*> shortListOf 5 arbitraryShortToken
Expand Down Expand Up @@ -590,6 +591,7 @@ instance Arbitrary PackageConfig where
, packageConfigSharedLib = x05
, packageConfigStaticLib = x42
, packageConfigDynExe = x06
, packageConfigFullyStaticExe = x44
, packageConfigProf = x07
, packageConfigProfLib = x08
, packageConfigProfExe = x09
Expand Down Expand Up @@ -642,6 +644,7 @@ instance Arbitrary PackageConfig where
, packageConfigSharedLib = x05'
, packageConfigStaticLib = x42'
, packageConfigDynExe = x06'
, packageConfigFullyStaticExe = x44'
, packageConfigProf = x07'
, packageConfigProfLib = x08'
, packageConfigProfExe = x09'
Expand Down Expand Up @@ -687,7 +690,7 @@ instance Arbitrary PackageConfig where
, packageConfigTestFailWhenNoTestSuites = x48'
, packageConfigTestTestOptions = x49' }
| (((x00', x01', x02', x03', x04'),
(x05', x42', x06', x07', x08', x09'),
(x05', x42', x06', x44', x07', x08', x09'),
(x10', x11', x12', x13', x14'),
(x15', x16', x17', x18', x19')),
((x20', x20_1', x21', x22', x23', x24'),
Expand All @@ -698,7 +701,7 @@ instance Arbitrary PackageConfig where
(x44', x45', x46', x47', x48', x49')))
<- shrink
(((preShrink_Paths x00, preShrink_Args x01, x02, x03, x04),
(x05, x42, x06, x07, x08, x09),
(x05, x42, x06, x44, x07, x08, x09),
(x10, x11, map NonEmpty x12, x13, x14),
(x15, map NonEmpty x16,
map NonEmpty x17,
Expand Down

0 comments on commit c9f02b2

Please sign in to comment.