From de80cb31504d35aaa3e45857b8052981418ff749 Mon Sep 17 00:00:00 2001 From: Roger Leigh Date: Tue, 3 Jan 2017 18:47:06 +0000 Subject: [PATCH] cmake: Add ld-version-script option Add option to enable or disable the use of linker version scripts. Check support for version scripts, and enable if not explicitly disabled. --- CMakeLists.txt | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a954a6..078155d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,27 @@ if(MSVC) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) endif() +option(ld-version-script "Enable linker version script" ON) +# Check if LD supports linker scripts. +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/conftest.map" "VERS_1 { + global: sym; +}; + +VERS_2 { + global: sym; +} VERS_1; +") +set(CMAKE_REQUIRED_FLAGS_SAVE ${CMAKE_REQUIRED_FLAGS}) +set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} "-Wl,--version-script=\"${CMAKE_CURRENT_BINARY_DIR}/conftest.map\"") +check_c_source_compiles("int main(void){return 0;}" HAVE_LD_VERSION_SCRIPT) +set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_SAVE}) +file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/conftest.map") +if (ld-version-script AND HAVE_LD_VERSION_SCRIPT) + set(HAVE_LD_VERSION_SCRIPT TRUE) +else() + set(HAVE_LD_VERSION_SCRIPT FALSE) +endif() + if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR) # If we're doing an out of source build and the user has a zconf.h # in their source tree... @@ -202,7 +223,7 @@ endif() if(UNIX) # On unix-like platforms the library is almost always called libz set_target_properties(zlib zlibstatic PROPERTIES OUTPUT_NAME z) - if(NOT APPLE) + if(HAVE_LD_VERSION_SCRIPT) set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"") endif() elseif(BUILD_SHARED_LIBS AND WIN32)