From: Matthias Maier <tamiko@kyomu.43-1.org>
Date: Thu, 11 Jul 2013 22:17:25 +0000 (+0000)
Subject: CMake: Add support for fully static linkage
X-Git-Tag: v8.0.0~143
X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6ddc0e16e656a16a466e4a73e592f53b67872611;p=dealii.git

CMake: Add support for fully static linkage

git-svn-id: https://svn.dealii.org/trunk@29978 0785d39b-7218-0410-832d-ea1e28bc413d
---

diff --git a/deal.II/cmake/checks/check_01_compiler_features.cmake b/deal.II/cmake/checks/check_01_compiler_features.cmake
index 17063b529e..736514b1d6 100644
--- a/deal.II/cmake/checks/check_01_compiler_features.cmake
+++ b/deal.II/cmake/checks/check_01_compiler_features.cmake
@@ -148,7 +148,7 @@ CHECK_CXX_SOURCE_COMPILES(
   "
   HAVE_GLIBC_STACKTRACE)
 
-IF(HAVE_GLIBC_STACKTRACE)
+IF(HAVE_GLIBC_STACKTRACE AND NOT DEAL_II_STATIC_EXECUTABLE)
   ENABLE_IF_SUPPORTED(CMAKE_SHARED_LINKER_FLAGS "-rdynamic")
 ENDIF()
 
diff --git a/deal.II/cmake/config/Config.cmake.in b/deal.II/cmake/config/Config.cmake.in
index c28eb39495..0d33e257ab 100644
--- a/deal.II/cmake/config/Config.cmake.in
+++ b/deal.II/cmake/config/Config.cmake.in
@@ -126,6 +126,13 @@ SET(DEAL_II_USER_DEFINITIONS_DEBUG "@DEAL_II_USER_DEFINITIONS_DEBUG@")
 SET(DEAL_II_USER_DEFINITIONS_RELEASE "@DEAL_II_USER_DEFINITIONS_RELEASE@")
 
 
+#
+# Build a static executable:
+#
+
+SET(DEAL_II_STATIC_EXECUTABLE "@DEAL_II_STATIC_EXECUTABLE@")
+
+
 #
 # Information about include directories and libraries
 #
diff --git a/deal.II/cmake/macros/macro_deal_ii_setup_target.cmake b/deal.II/cmake/macros/macro_deal_ii_setup_target.cmake
index ef17eb7e7f..3eaee85366 100644
--- a/deal.II/cmake/macros/macro_deal_ii_setup_target.cmake
+++ b/deal.II/cmake/macros/macro_deal_ii_setup_target.cmake
@@ -116,4 +116,14 @@ MACRO(DEAL_II_SETUP_TARGET _target)
   #
   TARGET_LINK_LIBRARIES(${_target} ${DEAL_II_TARGET})
 
+  #
+  # If DEAL_II_STATIC_EXECUTABLE is set, switch to final link type to
+  # static:
+  #
+  IF(DEAL_II_STATIC_EXECUTABLE)
+    SET_PROPERTY(TARGET ${_target} PROPERTY
+      LINK_SEARCH_END_STATIC TRUE
+      )
+  ENDIF()
+
 ENDMACRO()
diff --git a/deal.II/cmake/macros/macro_enable_if_supported.cmake b/deal.II/cmake/macros/macro_enable_if_supported.cmake
index 9c98c9155e..8c9f0e0af8 100644
--- a/deal.II/cmake/macros/macro_enable_if_supported.cmake
+++ b/deal.II/cmake/macros/macro_enable_if_supported.cmake
@@ -26,6 +26,7 @@ MACRO(ENABLE_IF_SUPPORTED _variable _flag)
     STRING(REGEX REPLACE "^-" "" _flag_name "${_flag_stripped}")
     STRING(REPLACE "," "" _flag_name "${_flag_name}")
     STRING(REPLACE "--" "__" _flag_name "${_flag_name}")
+    STRING(REPLACE "++" "__" _flag_name "${_flag_name}")
     CHECK_CXX_COMPILER_FLAG(
       "${_flag_stripped}"
       DEAL_II_HAVE_FLAG_${_flag_name}
diff --git a/deal.II/cmake/setup_cached_variables.cmake b/deal.II/cmake/setup_cached_variables.cmake
index 4213865644..4e12f42414 100644
--- a/deal.II/cmake/setup_cached_variables.cmake
+++ b/deal.II/cmake/setup_cached_variables.cmake
@@ -30,12 +30,13 @@
 #
 # Options regarding compilation and linking:
 #
+#     CMAKE_BUILD_TYPE
 #     DEAL_II_ALLOW_PLATFORM_INTROSPECTION
 #     DEAL_II_SETUP_DEFAULT_COMPILER_FLAGS
-#     CMAKE_BUILD_TYPE
+#     DEAL_II_STATIC_EXECUTABLE
 #     BUILD_SHARED_LIBS
 #     CMAKE_INSTALL_RPATH_USE_LINK_PATH
-#     CMAKE_C_FLAGS
+#     CMAKE_C_FLAGS                     *)
 #     CMAKE_CXX_FLAGS                   *)
 #     CMAKE_SHARED_LINKER_FLAGS         *)
 #     DEAL_II_CXX_FLAGS_DEBUG
@@ -45,8 +46,8 @@
 #     DEAL_II_WITH_64BIT_INDICES
 #
 # *)  May also be set via environment variable (CFLAGS, CXXFLAGS, LDFLAGS)
-#     (nonempty cached variable has precedence will not be overwritten by
-#     environment)
+#     (a nonempty cached variable has precedence and will not be
+#     overwritten by environment)
 #
 
 
@@ -115,18 +116,6 @@ ENDIF()
 #                                                                         #
 ###########################################################################
 
-OPTION(DEAL_II_ALLOW_PLATFORM_INTROSPECTION
-  "Allow platform introspection for CPU command set, SSE and AVX"
-  ON
-  )
-MARK_AS_ADVANCED(DEAL_II_ALLOW_PLATFORM_INTROSPECTION)
-
-OPTION(DEAL_II_SETUP_DEFAULT_COMPILER_FLAGS
-  "Configure sensible default CFLAGS and CXXFLAGS depending on platform, compiler and build target."
-  ON
-  )
-MARK_AS_ADVANCED(DEAL_II_SETUP_DEFAULT_COMPILER_FLAGS)
-
 #
 # Setup CMAKE_BUILD_TYPE:
 #
@@ -148,11 +137,37 @@ IF( NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Release" AND
     )
 ENDIF()
 
-SET(BUILD_SHARED_LIBS "ON" CACHE BOOL
-  "Build a shared library"
+
+OPTION(DEAL_II_ALLOW_PLATFORM_INTROSPECTION
+  "Allow platform introspection for CPU command set, SSE and AVX"
+  ON
+  )
+MARK_AS_ADVANCED(DEAL_II_ALLOW_PLATFORM_INTROSPECTION)
+
+OPTION(DEAL_II_SETUP_DEFAULT_COMPILER_FLAGS
+  "Configure sensible default CFLAGS and CXXFLAGS depending on platform, compiler and build target."
+  ON
   )
+MARK_AS_ADVANCED(DEAL_II_SETUP_DEFAULT_COMPILER_FLAGS)
+
+OPTION(DEAL_II_STATIC_EXECUTABLE
+  "Provide a link interface that is suitable for static linkage of executables. Enabling this option forces BUILD_SHARED_LIBS=OFF"
+  OFF
+  )
+MARK_AS_ADVANCED(DEAL_II_STATIC_EXECUTABLE)
+
+IF(DEAL_II_STATIC_EXECUTABLE)
+  SET(BUILD_SHARED_LIBS "OFF" CACHE BOOL
+    "Build a shared library" FORCE
+    )
+ELSE()
+  SET(BUILD_SHARED_LIBS "ON" CACHE BOOL
+    "Build a shared library"
+    )
+ENDIF()
 MARK_AS_ADVANCED(BUILD_SHARED_LIBS)
 
+
 #
 # Set CMAKE_INSTALL_RPATH_USE_LINK_PATH to default to ON and promote to
 # cache so that the user can see the value.
@@ -162,6 +177,7 @@ SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH "ON" CACHE BOOL
   )
 MARK_AS_ADVANCED(CMAKE_INSTALL_RPATH_USE_LINK_PATH)
 
+
 #
 # Define the variable that defines whether we should use 32- or 64-bit
 # global DoF indices.
diff --git a/deal.II/cmake/setup_compiler_flags_gnu.cmake b/deal.II/cmake/setup_compiler_flags_gnu.cmake
index fdc33164cd..1f6135460e 100644
--- a/deal.II/cmake/setup_compiler_flags_gnu.cmake
+++ b/deal.II/cmake/setup_compiler_flags_gnu.cmake
@@ -95,6 +95,18 @@ IF(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
 ENDIF()
 
 
+IF(DEAL_II_STATIC_EXECUTABLE)
+  #
+  # To produce a static executable, we have to statically link libstdc++
+  # and gcc's support libraries and glibc:
+  #
+  # (Well... the name "CMAKE_SHARED_LINKER_FLAGS" is a bit misleading :-])
+  #
+  ENABLE_IF_SUPPORTED(CMAKE_SHARED_LINKER_FLAGS "-static")
+  ENABLE_IF_SUPPORTED(CMAKE_SHARED_LINKER_FLAGS "-pthread")
+ENDIF()
+
+
 #############################
 #                           #
 #    For Release target:    #
diff --git a/deal.II/doc/development/Config.sample b/deal.II/doc/development/Config.sample
index 393634f337..db9285adfe 100644
--- a/deal.II/doc/development/Config.sample
+++ b/deal.II/doc/development/Config.sample
@@ -107,6 +107,11 @@
 #   "The user supplied cache variable will be appended _at the end_ of the auto generated DEAL_II_SHARED_LINKER_FLAGS_RELEASE variable"
 #   )
 #
+# OPTION(DEAL_II_STATIC_EXECUTABLE "ON" CACHE BOOL
+#   "Provide a link interface that is suitable for static linkage of executables. Enabling this option forces BUILD_SHARED_LIBS=OFF"
+#   )
+#
+#
 # SET(BUILD_SHARED_LIBS "ON" CACHE BOOL
 #   "Build a shared library"
 #   )
diff --git a/deal.II/doc/development/cmake.html b/deal.II/doc/development/cmake.html
index 0b1c072f98..b169825b1d 100644
--- a/deal.II/doc/development/cmake.html
+++ b/deal.II/doc/development/cmake.html
@@ -570,6 +570,13 @@
           <code>BUILD_SHARED_LIBS</code>: If set (default),
           <acronym>deal.II</acronym> will be linked as a shared library
 
+	<li>
+          <code>DEAL_II_STATIC_EXECUTABLE</code> (defaults to off):
+          If set to true, <acronym>deal.II</acronym> will be configured in
+          a way to provide a link interface that is suitable for static
+          linkage of executables. Enabling this option forces
+          <code>BUILD_SHARED_LIBS=OFF</code>
+
 	<li>
 	  <code>CMAKE_INSTALL_RPATH_USE_LINK_PATH</code>: If set
 	  (default), the <acronym>deal.II</acronym> library will be