Skip to content

Commit

Permalink
Add --enable-executable-static flag. Fixes #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 committed Jul 20, 2018
1 parent bf8b002 commit 28177e0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Cabal/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,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 @@ -678,6 +683,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 @@ -717,6 +724,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 @@ -609,7 +609,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 @@ -1290,7 +1291,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 @@ -219,6 +219,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 @@ -301,6 +303,7 @@ instance Eq ConfigFlags where
&& equal configSharedLib
&& equal configStaticLib
&& equal configDynExe
&& equal configFullyStaticExe
&& equal configProfExe
&& equal configProf
&& equal configProfDetail
Expand Down Expand Up @@ -355,6 +358,7 @@ defaultConfigFlags progDb = emptyConfigFlags {
configSharedLib = NoFlag,
configStaticLib = NoFlag,
configDynExe = Flag False,
configFullyStaticExe = Flag False,
configProfExe = NoFlag,
configProf = NoFlag,
configProfDetail = NoFlag,
Expand Down Expand Up @@ -493,6 +497,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 @@ -1156,13 +1156,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 @@ -1416,6 +1416,19 @@ Static linking options
This uses GHCs ``-staticlib`` flag, which is avaiable 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 @@ -296,6 +296,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

0 comments on commit 28177e0

Please sign in to comment.