summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2011-03-03 23:02:53 (GMT)
committerBrad King <brad.king@kitware.com>2011-03-04 13:37:57 (GMT)
commit077954d4cbd0d2f8b8ae96964bfe5c9747934fe8 (patch)
tree678c19be3cdce2b6f34013a62dbfe5376779b31a
parent5abfb571843dba949e010f3b2840d8882d0f3d73 (diff)
downloadCMake-077954d4cbd0d2f8b8ae96964bfe5c9747934fe8.zip
CMake-077954d4cbd0d2f8b8ae96964bfe5c9747934fe8.tar.gz
CMake-077954d4cbd0d2f8b8ae96964bfe5c9747934fe8.tar.bz2
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.
-rw-r--r--Tests/CMakeLists.txt16
-rw-r--r--Tests/LinkStatic/CMakeLists.txt27
-rw-r--r--Tests/LinkStatic/LinkStatic.c5
3 files changed, 48 insertions, 0 deletions
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);
+}