From: Roland Schulz Date: Tue, 17 Mar 2015 04:03:40 +0000 (-0700) Subject: Facilitate linking of static binaries X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?a=commitdiff_plain;h=88fda4755eeaa360bde13b71ba094ad685720aca;p=alexxy%2Fgromacs.git Facilitate linking of static binaries 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 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a2650bb7d..fd24ddffea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/cmake/gmxManageSharedLibraries.cmake b/cmake/gmxManageSharedLibraries.cmake index 0cfab0ce68..b667ba7c9e 100644 --- a/cmake/gmxManageSharedLibraries.cmake +++ b/cmake/gmxManageSharedLibraries.cmake @@ -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}) diff --git a/docs/install-guide/install-guide.md b/docs/install-guide/install-guide.md index 67185db2cf..ea13901eff 100644 --- a/docs/install-guide/install-guide.md +++ b/docs/install-guide/install-guide.md @@ -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 ##