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
This commit is contained in:
Brad King 2011-03-08 14:22:20 -05:00 committed by CMake Topic Stage
commit 27b41deaed
5 changed files with 74 additions and 11 deletions

View File

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

View File

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

View File

@ -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 ( linkorder2 PROPERTIES DEPENDS linkorder1)
SET_TESTS_PROPERTIES ( SimpleInstall-Stage2 PROPERTIES DEPENDS SimpleInstall) 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) IF(NOT CMAKE_TEST_DIFFERENT_GENERATOR)
ADD_TEST(kwsys ${CMAKE_CTEST_COMMAND} ADD_TEST(kwsys ${CMAKE_CTEST_COMMAND}
--build-and-test --build-and-test

View File

@ -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

View File

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