Facilitate linking of static binaries
authorRoland Schulz <roland@utk.edu>
Tue, 17 Mar 2015 04:03:40 +0000 (21:03 -0700)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Wed, 13 May 2015 08:45:38 +0000 (10:45 +0200)
Minimal solution. The user has to manually set both
-DBUILD_SHARED_EXE=no and CFLAGS=CXXFLAGS=-static, perhaps manage
their own toolchain, and certainly make static libraries available for
all dependencies. Also does not auto-detect if compiler defaults to
static (Cray). Works better than LINK_SEARCH_END_STATIC because
otherwise dynamic flags can be added to the middle if some libraries
in default search path exist as both dyanmic and shared.

Fixes #911
Related to #1641

Change-Id: If7b8192b44c33c861f126e3422df04388d2f2be5

CMakeLists.txt
cmake/gmxManageSharedLibraries.cmake
docs/install-guide/install-guide.md

index 4a2650bb7d30b93a4d78c55870ca0880d01f54a6..fd24ddffea0729da38d35daf1f49919c120af902 100644 (file)
@@ -328,6 +328,11 @@ option(GMX_DEVELOPER_BUILD
     OFF)
 mark_as_advanced(GMX_DEVELOPER_BUILD)
 
+option(GMX_BUILD_SHARED_EXE
+    "Build exectuables as shared binaries. If not set, this disables rpath and dynamic linker flags in an attempt to build a static binary, but this may require setting up the toolchain properly and making appropriate libraries available."
+    ON)
+mark_as_advanced(GMX_BUILD_SHARED_EXE)
+
 ######################################################################
 # Detect OpenMP support
 ######################################################################
@@ -869,7 +874,13 @@ include(gmxManageSuffixes)
 # CMake supports RPATH on OS X only from 2.8.12 upwards.
 # CMAKE_SYSTEM_VERSION > 8.0 matches OS X 10.5 and above, where RPATH support
 # was added.
-if((NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") OR
+
+if(NOT GMX_BUILD_SHARED_EXE)
+    # No rpath
+    set(CMAKE_SKIP_RPATH TRUE)
+    set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic
+    set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
+elseif((NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") OR
    ((CMAKE_SYSTEM_VERSION VERSION_GREATER 8.0) AND (NOT CMAKE_VERSION VERSION_LESS 2.8.12)))
     # The build folder always has bin/ and lib/; if we are also going to
     # install to lib/, then the installation RPATH works also in the build
index 0cfab0ce68c2c876b9b810c9961a6f0fbba7e781..b667ba7c9e78fb2b4feb09348a91dca17194a24c 100644 (file)
@@ -1,7 +1,7 @@
 #
 # This file is part of the GROMACS molecular simulation package.
 #
-# Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
+# Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by
 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
 # and including many others, as listed in the AUTHORS file in the
 # top-level source directory and at http://www.gromacs.org.
@@ -63,6 +63,10 @@ set(GMX_PREFER_STATIC_LIBS_DEFAULT OFF)
 if (WIN32 AND NOT CYGWIN AND NOT BUILD_SHARED_LIBS)
     set(GMX_PREFER_STATIC_LIBS_DEFAULT ON)
 endif()
+if (NOT GMX_BUILD_SHARED_EXE)
+    set(GMX_PREFER_STATIC_LIBS_DEFAULT ON)
+    set(SHARED_LIBS_DEFAULT OFF)
+endif()
 
 # Declare the user-visible options
 option(BUILD_SHARED_LIBS "Enable shared libraries (can be problematic e.g. with MPI, or on some HPC systems)" ${SHARED_LIBS_DEFAULT})
index 67185db2cf2768ae17549ea0490c8c4c2535377c..ea13901eff588e36e5263410d07dc61999f9d5c7 100644 (file)
@@ -548,6 +548,13 @@ still be dynamically linked against system libraries on platforms
 where that is the default. To use static system libraries, additional
 compiler/linker flags are necessary, e.g. `-static-libgcc
 -static-libstdc++`.
+* To attempt to link a fully static binary set
+`-DGMX_BUILD_SHARED_EXE=OFF`. This will prevent CMake from explicitly
+setting any dynamic linking flags. This option also sets
+`-DBUILD_SHARED_LIBS=OFF` and `-DGMX_PREFER_STATIC_LIBS=ON` by
+default, but the above caveats apply. For compilers which don't
+default to static linking, the required flags have to be specified. On
+Linux, this is usually `CFLAGS=-static CXXFLAGS=-static`.
 
 ### Portability aspects ###
 
@@ -874,13 +881,10 @@ the right tools get used.
 ## Building on Cray ##
 
 GROMACS builds mostly out of the box on modern Cray machines, but
-* you may need to specify the use of static or dynamic libraries
-  (depending on the machine) with `-DBUILD_SHARED_LIBS=off`,
+* you may need to specify the use of static binaries
+  with `-DGMX_BUILD_SHARED_EXE=off`,
 * you may need to set the F77 environmental variable to `ftn` when
   compiling FFTW,
-* you may need to use `-DCMAKE_SKIP_RPATH=YES`, and
-* you may need to modify the CMakeLists.txt files to specify the
-  `BUILD_SEARCH_END_STATIC` target property.
 
 ## Building on BlueGene ##