From 5abfb571843dba949e010f3b2840d8882d0f3d73 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 3 Mar 2011 17:08:31 -0500 Subject: [PATCH 1/2] 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. --- Source/cmComputeLinkInformation.cxx | 6 +++--- Source/cmTarget.cxx | 31 +++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Source/cmComputeLinkInformation.cxx b/Source/cmComputeLinkInformation.cxx index d53200c87..e3c33a2ff 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 75ff7128c..0ca46c448 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, From 077954d4cbd0d2f8b8ae96964bfe5c9747934fe8 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 3 Mar 2011 18:02:53 -0500 Subject: [PATCH 2/2] 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. --- Tests/CMakeLists.txt | 16 ++++++++++++++++ Tests/LinkStatic/CMakeLists.txt | 27 +++++++++++++++++++++++++++ Tests/LinkStatic/LinkStatic.c | 5 +++++ 3 files changed, 48 insertions(+) create mode 100644 Tests/LinkStatic/CMakeLists.txt create mode 100644 Tests/LinkStatic/LinkStatic.c diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index f41805874..d025b1ea8 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 000000000..2062c4345 --- /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 000000000..360097744 --- /dev/null +++ b/Tests/LinkStatic/LinkStatic.c @@ -0,0 +1,5 @@ +#include +int main(void) +{ + return (int)sin(0); +}