From 2976fef4fb0e69be40d2347d2be10c9341954786 Mon Sep 17 00:00:00 2001 From: Thomas Tuegel <ttuegel@gmail.com> Date: Wed, 17 Dec 2014 14:40:26 -0600 Subject: [PATCH] Build shared library when linking executables dynamically Dynamically linking executables will fail without a shared library unless the executables do not depend on the library or there is no library. If there is no library, building shared libraries comes at no cost. If there is a library, the executables usually depend on it, so it makes sense to --enable-shared. If the user passes --disable-shared, it will still be honored, but a warning will be produced. --- Cabal/Distribution/Simple/Configure.hs | 25 +++++++++++++++---- Cabal/changelog | 1 + Cabal/doc/installing-packages.markdown | 8 ++++++ .../PackageTests/TestSuiteExeV10/Check.hs | 7 +++--- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/Cabal/Distribution/Simple/Configure.hs b/Cabal/Distribution/Simple/Configure.hs index 3604f186a8..5af7a39774 100644 --- a/Cabal/Distribution/Simple/Configure.hs +++ b/Cabal/Distribution/Simple/Configure.hs @@ -601,8 +601,12 @@ configure (pkg_descr0, pbi) cfg not (GHCJS.isDynamic comp) _ -> False - let sharedLibsByDefault = - case compilerId comp of + let sharedLibsByDefault + | fromFlag (configDynExe cfg) = + -- build a shared library if dynamically-linked + -- executables are requested + True + | otherwise = case compilerId comp of CompilerId GHC _ -> -- if ghc is dynamic, then ghci needs a shared -- library, so we build one by default. @@ -610,6 +614,18 @@ configure (pkg_descr0, pbi) cfg CompilerId GHCJS _ -> GHCJS.isDynamic comp _ -> False + withSharedLib_ = + -- build shared libraries if required by GHC or by the + -- executable linking mode, but allow the user to force + -- building only static library archives with + -- --disable-shared. + fromFlagOrDefault sharedLibsByDefault $ configSharedLib cfg + withDynExe_ = fromFlag $ configDynExe 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 " + ++ "depend on the library." reloc <- if not (fromFlag $ configRelocatable cfg) @@ -634,9 +650,8 @@ configure (pkg_descr0, pbi) cfg withPrograms = programsConfig''', withVanillaLib = fromFlag $ configVanillaLib cfg, withProfLib = fromFlag $ configProfLib cfg, - withSharedLib = fromFlagOrDefault sharedLibsByDefault $ - configSharedLib cfg, - withDynExe = fromFlag $ configDynExe cfg, + withSharedLib = withSharedLib_, + withDynExe = withDynExe_, withProfExe = fromFlag $ configProfExe cfg, withOptimization = fromFlag $ configOptimization cfg, withGHCiLib = fromFlagOrDefault ghciLibByDefault $ diff --git a/Cabal/changelog b/Cabal/changelog index c29e98e801..e84468f23f 100644 --- a/Cabal/changelog +++ b/Cabal/changelog @@ -23,6 +23,7 @@ * Drop support for Haddock < 2.0 (#1808, #1718). * Make 'cabal test'/'cabal bench' build only what's needed for running tests/benchmarks (#1821). + * Build shared libraries by default when linking executables dynamically. 1.20.0.1 Johan Tibell <johan.tibell@gmail.com> May 2014 * Fix streaming test output. diff --git a/Cabal/doc/installing-packages.markdown b/Cabal/doc/installing-packages.markdown index b90df2152d..299f3677eb 100644 --- a/Cabal/doc/installing-packages.markdown +++ b/Cabal/doc/installing-packages.markdown @@ -692,6 +692,14 @@ be controlled with the following command line options. `--disable-shared` : (default) Do not build shared library. +`--enable-executable-dynamic` +: Link executables dynamically. The executable's library dependencies should + be built as shared objects. This implies `--enable-shared` unless + `--disable-shared` is explicitly specified. + +`--disable-executable-dynamic` +: (default) Link executables statically. + `--configure-option=`_str_ : An extra option to an external `configure` script, if one is used (see the section on [system-dependent diff --git a/Cabal/tests/PackageTests/TestSuiteExeV10/Check.hs b/Cabal/tests/PackageTests/TestSuiteExeV10/Check.hs index e140ff9583..2b25cab191 100644 --- a/Cabal/tests/PackageTests/TestSuiteExeV10/Check.hs +++ b/Cabal/tests/PackageTests/TestSuiteExeV10/Check.hs @@ -1,5 +1,6 @@ module PackageTests.TestSuiteExeV10.Check (checks) where +import Data.Maybe (catMaybes) import System.Directory ( doesFileExist ) import System.FilePath import qualified Test.Framework as TF @@ -33,9 +34,9 @@ hpcTestMatrix ghcPath = do , if shared then "Shared" else "" ] enable cond flag - | cond = "--enable-" ++ flag - | otherwise = "--disable-" ++ flag - opts = + | cond = Just $ "--enable-" ++ flag + | otherwise = Nothing + opts = catMaybes [ enable libProf "library-profiling" , enable exeProf "executable-profiling" , enable exeDyn "executable-dynamic" -- GitLab