[Cmake-commits] CMake branch, next, updated. v2.8.4-1088-g5bdb411

Brad King brad.king at kitware.com
Fri Mar 4 08:41:58 EST 2011


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "CMake".

The branch, next has been updated
       via  5bdb41118dc7b529c1180d5253081958b2ff5429 (commit)
       via  077954d4cbd0d2f8b8ae96964bfe5c9747934fe8 (commit)
       via  5abfb571843dba949e010f3b2840d8882d0f3d73 (commit)
      from  4d54531341664fdc8e07d8892aa90ebcfe0f7a44 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=5bdb41118dc7b529c1180d5253081958b2ff5429
commit 5bdb41118dc7b529c1180d5253081958b2ff5429
Merge: 4d54531 077954d
Author:     Brad King <brad.king at kitware.com>
AuthorDate: Fri Mar 4 08:41:54 2011 -0500
Commit:     CMake Topic Stage <kwrobot at kitware.com>
CommitDate: Fri Mar 4 08:41:54 2011 -0500

    Merge topic 'link-static' into next
    
    077954d Test static linking with LINK_SEARCH_START_STATIC
    5abfb57 Add target property LINK_SEARCH_START_STATIC to aid static linking


http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=077954d4cbd0d2f8b8ae96964bfe5c9747934fe8
commit 077954d4cbd0d2f8b8ae96964bfe5c9747934fe8
Author:     Brad King <brad.king at kitware.com>
AuthorDate: Thu Mar 3 18:02:53 2011 -0500
Commit:     Brad King <brad.king at kitware.com>
CommitDate: Fri Mar 4 08:37:57 2011 -0500

    Test static linking with LINK_SEARCH_START_STATIC
    
    Add "LinkStatic" test that links a static executable against "libm.a".
    Pass both "/usr/lib/libm.a" and "-lm" to target_link_libraries to
    trigger the link type logic for both cases.  If CMake incorrectly
    switches the link type to shared for "-lm" then the link will fail.

diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index f418058..d025b1e 100644
--- a/Tests/CMakeLists.txt
+++ b/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
diff --git a/Tests/LinkStatic/CMakeLists.txt b/Tests/LinkStatic/CMakeLists.txt
new file mode 100644
index 0000000..2062c43
--- /dev/null
+++ b/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
diff --git a/Tests/LinkStatic/LinkStatic.c b/Tests/LinkStatic/LinkStatic.c
new file mode 100644
index 0000000..3600977
--- /dev/null
+++ b/Tests/LinkStatic/LinkStatic.c
@@ -0,0 +1,5 @@
+#include <math.h>
+int main(void)
+{
+  return (int)sin(0);
+}

http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=5abfb571843dba949e010f3b2840d8882d0f3d73
commit 5abfb571843dba949e010f3b2840d8882d0f3d73
Author:     Brad King <brad.king at kitware.com>
AuthorDate: Thu Mar 3 17:08:31 2011 -0500
Commit:     Brad King <brad.king at kitware.com>
CommitDate: Thu Mar 3 17:12:32 2011 -0500

    Add target property LINK_SEARCH_START_STATIC to aid static linking
    
    Commit afd7d4ca (Add target property LINK_SEARCH_END_STATIC, 2008-01-31)
    defined a property to ensure that static runtime libraries get selected.
    Add a property to specify that all libraries whose type is unknown, such
    as "-lm", should be assumed static.  Furthermore it assumes that an
    option such as "-static" is also used so that no initial -Bstatic is
    needed.

diff --git a/Source/cmComputeLinkInformation.cxx b/Source/cmComputeLinkInformation.cxx
index d53200c..e3c33a2 100644
--- a/Source/cmComputeLinkInformation.cxx
+++ b/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;
 }
 
diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx
index 75ff712..0ca46c4 100644
--- a/Source/cmTarget.cxx
+++ b/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,

-----------------------------------------------------------------------

Summary of changes:
 Source/cmComputeLinkInformation.cxx |    6 +++---
 Source/cmTarget.cxx                 |   31 +++++++++++++++++++++++--------
 Tests/CMakeLists.txt                |   16 ++++++++++++++++
 Tests/LinkStatic/CMakeLists.txt     |   27 +++++++++++++++++++++++++++
 Tests/LinkStatic/LinkStatic.c       |    5 +++++
 5 files changed, 74 insertions(+), 11 deletions(-)
 create mode 100644 Tests/LinkStatic/CMakeLists.txt
 create mode 100644 Tests/LinkStatic/LinkStatic.c


hooks/post-receive
-- 
CMake


More information about the Cmake-commits mailing list