Browse Source

Merge topic 'link-static'

077954d Test static linking with LINK_SEARCH_START_STATIC
5abfb57 Add target property LINK_SEARCH_START_STATIC to aid static linking
Brad King 14 years ago
parent
commit
27b41deaed

+ 3 - 3
Source/cmComputeLinkInformation.cxx

@@ -817,9 +817,9 @@ void cmComputeLinkInformation::ComputeLinkTypeInfo()
     this->SharedLinkTypeFlag = shared_link_type_flag;
     }
 
-  // TODO: Lookup the starting link type from the target (is it being
-  // linked statically?).
-  this->StartLinkType = LinkShared;
+  // Lookup the starting link type from the target (linked statically?).
+  const char* lss = this->Target->GetProperty("LINK_SEARCH_START_STATIC");
+  this->StartLinkType = cmSystemTools::IsOn(lss)? LinkStatic : LinkShared;
   this->CurrentLinkType = this->StartLinkType;
 }
 

+ 23 - 8
Source/cmTarget.cxx

@@ -487,17 +487,32 @@ void cmTarget::DefineProperties(cmake *cm)
      "Per-configuration linker flags for a target.",
      "This is the configuration-specific version of LINK_FLAGS.");
 
+#define CM_LINK_SEARCH_SUMMARY \
+  "Some linkers support switches such as -Bstatic and -Bdynamic " \
+  "to determine whether to use static or shared libraries for -lXXX " \
+  "options.  CMake uses these options to set the link type for " \
+  "libraries whose full paths are not known or (in some cases) are in " \
+  "implicit link directories for the platform.  "
+
+  cm->DefineProperty
+    ("LINK_SEARCH_START_STATIC", cmProperty::TARGET,
+     "Assume the linker looks for static libraries by default.",
+     CM_LINK_SEARCH_SUMMARY
+     "By default the linker search type is assumed to be -Bdynamic at "
+     "the beginning of the library list.  This property switches the "
+     "assumption to -Bstatic.  It is intended for use when linking an "
+     "executable statically (e.g. with the GNU -static option).  "
+     "See also LINK_SEARCH_END_STATIC.");
+
   cm->DefineProperty
     ("LINK_SEARCH_END_STATIC", cmProperty::TARGET,
      "End a link line such that static system libraries are used.",
-     "Some linkers support switches such as -Bstatic and -Bdynamic "
-     "to determine whether to use static or shared libraries for -lXXX "
-     "options.  CMake uses these options to set the link type for "
-     "libraries whose full paths are not known or (in some cases) are in "
-     "implicit link directories for the platform.  By default the "
-     "linker search type is left at -Bdynamic by the end of the library "
-     "list.  This property switches the final linker search type to "
-     "-Bstatic.");
+     CM_LINK_SEARCH_SUMMARY
+     "By default CMake adds an option at the end of the library list (if "
+     "necessary) to set the linker search type back to its starting type.  "
+     "This property switches the final linker search type to -Bstatic "
+     "regardless of how it started.  "
+     "See also LINK_SEARCH_START_STATIC.");
 
   cm->DefineProperty
     ("LINKER_LANGUAGE", cmProperty::TARGET,

+ 16 - 0
Tests/CMakeLists.txt

@@ -999,6 +999,22 @@ ${CMake_BINARY_DIR}/bin/cmake -DVERSION=master -P ${CMake_SOURCE_DIR}/Utilities/
   SET_TESTS_PROPERTIES ( linkorder2 PROPERTIES DEPENDS linkorder1)
   SET_TESTS_PROPERTIES ( SimpleInstall-Stage2 PROPERTIES DEPENDS SimpleInstall)
 
+  # Test static linking on toolchains known to support it.
+  IF("${CMAKE_C_COMPILER_ID}" MATCHES "^(GNU)$"
+      AND NOT APPLE AND NOT WIN32 AND NOT CYGWIN
+      AND EXISTS "/usr/lib/libm.a")
+    ADD_TEST(LinkStatic  ${CMAKE_CTEST_COMMAND}
+      --build-and-test
+      "${CMake_SOURCE_DIR}/Tests/LinkStatic"
+      "${CMake_BINARY_DIR}/Tests/LinkStatic"
+      --build-generator ${CMAKE_TEST_GENERATOR}
+      --build-makeprogram ${CMAKE_TEST_MAKEPROGRAM}
+      --build-project LinkStatic
+      --build-options -DMATH_LIBRARY:FILEPATH=/usr/lib/libm.a
+      --test-command LinkStatic
+      )
+  ENDIF()
+
   IF(NOT CMAKE_TEST_DIFFERENT_GENERATOR)
     ADD_TEST(kwsys ${CMAKE_CTEST_COMMAND}
       --build-and-test

+ 27 - 0
Tests/LinkStatic/CMakeLists.txt

@@ -0,0 +1,27 @@
+cmake_minimum_required(VERSION 2.8.4.20110303 FATAL_ERROR)
+project(LinkStatic C)
+
+if(NOT "${CMAKE_C_COMPILER_ID}" MATCHES "^(GNU)$")
+  message(FATAL_ERROR "This test works only with the GNU compiler!")
+endif()
+
+find_library(MATH_LIBRARY NAMES libm.a)
+if(MATH_LIBRARY)
+  get_filename_component(MATH_LIB_DIR ${MATH_LIBRARY} PATH)
+  link_directories(${MATH_LIB_DIR})
+  # Name the library both with a full path and as "-lm" to
+  # activate the link type switching code for both cases.
+  # If the second one links shared then the link will fail.
+  set(MATH_LIBRARIES ${MATH_LIBRARY} -lm)
+else()
+  message(FATAL_ERROR "Cannot find libm.a needed for this test")
+endif()
+
+add_executable(LinkStatic LinkStatic.c)
+target_link_libraries(LinkStatic ${MATH_LIBRARIES})
+
+# Enable static linking.
+set(LinkStatic_FLAG "-static" CACHE STRING "Flag to link statically")
+set_property(TARGET LinkStatic PROPERTY LINK_FLAGS "${LinkStatic_FLAG}")
+set_property(TARGET LinkStatic PROPERTY LINK_SEARCH_START_STATIC 1)
+#set_property(TARGET LinkStatic PROPERTY LINK_SEARCH_END_STATIC 1) # insufficient

+ 5 - 0
Tests/LinkStatic/LinkStatic.c

@@ -0,0 +1,5 @@
+#include <math.h>
+int main(void)
+{
+  return (int)sin(0);
+}