From 3b8cf47307c65f6adfe20e13e85e074a8e5cc044 Mon Sep 17 00:00:00 2001 From: kinichiro Date: Tue, 17 Jan 2017 21:27:41 +0900 Subject: [PATCH] Add export symbol support to CMake --- CMakeLists.txt | 3 +++ Makefile.am | 2 +- cmake_export_symbol.cmake | 41 +++++++++++++++++++++++++++++++++++++++ ssl/CMakeLists.txt | 3 +-- tls/CMakeLists.txt | 3 +-- 5 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 cmake_export_symbol.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 9cf14de..558a6f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,9 @@ include(CheckLibraryExists) include(CheckIncludeFiles) include(CheckTypeSize) +set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}" ${CMAKE_MODULE_PATH}) +include(cmake_export_symbol) + project (LibreSSL C) enable_testing() diff --git a/Makefile.am b/Makefile.am index 48da18b..b4b9dfc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5,7 +5,7 @@ pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libcrypto.pc libssl.pc libtls.pc openssl.pc EXTRA_DIST = README.md README.windows VERSION config scripts -EXTRA_DIST += CMakeLists.txt cmake_uninstall.cmake.in +EXTRA_DIST += CMakeLists.txt cmake_export_symbol.cmake cmake_uninstall.cmake.in .PHONY: install_sw install_sw: install diff --git a/cmake_export_symbol.cmake b/cmake_export_symbol.cmake new file mode 100644 index 0000000..7dde938 --- /dev/null +++ b/cmake_export_symbol.cmake @@ -0,0 +1,41 @@ +macro(export_symbol TARGET FILENAME) + + set(FLAG "") + + if(WIN32) + string(REPLACE ".sym" ".def" DEF_FILENAME ${FILENAME}) + target_sources(${TARGET} PRIVATE ${DEF_FILENAME}) + + elseif(APPLE) + set(FLAG "-exported_symbols_list ${FILENAME}") + set_target_properties(${TARGET} PROPERTIES LINK_FLAGS ${FLAG}) + + elseif(CMAKE_SYSTEM_NAME MATCHES "HP-UX") + file(READ ${FILENAME} SYMBOLS) + string(REGEX REPLACE "\n$" "" SYMBOLS ${SYMBOLS}) + string(REPLACE "\n" "\n+e " SYMBOLS ${SYMBOLS}) + string(REPLACE ".sym" ".opt" OPT_FILENAME ${FILENAME}) + file(WRITE ${OPT_FILENAME} "+e ${SYMBOLS}") + set(FLAG "-Wl,-c,${OPT_FILENAME}") + set_target_properties(${TARGET} PROPERTIES LINK_FLAGS ${FLAG}) + + elseif(CMAKE_SYSTEM_NAME MATCHES "SunOS") + file(READ ${FILENAME} SYMBOLS) + string(REPLACE "\n" ";\n" SYMBOLS ${SYMBOLS}) + string(REPLACE ".sym" ".ver" VER_FILENAME ${FILENAME}) + file(WRITE ${VER_FILENAME} + "{\nglobal:\n${SYMBOLS}\nlocal:\n*;\n};\n") + set(FLAG "-Wl,-M${VER_FILENAME}") + set_target_properties(${TARGET} PROPERTIES LINK_FLAGS ${FLAG}) + + elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang") + file(READ ${FILENAME} SYMBOLS) + string(REPLACE "\n" ";\n" SYMBOLS ${SYMBOLS}) + string(REPLACE ".sym" ".ver" VER_FILENAME ${FILENAME}) + file(WRITE ${VER_FILENAME} + "{\nglobal:\n${SYMBOLS}\nlocal:\n*;\n};\n") + set(FLAG "-Wl,--version-script,\"${VER_FILENAME}\"") + set_target_properties(${TARGET} PROPERTIES LINK_FLAGS ${FLAG}) + endif() + +endmacro() diff --git a/ssl/CMakeLists.txt b/ssl/CMakeLists.txt index d511c8e..67115e4 100644 --- a/ssl/CMakeLists.txt +++ b/ssl/CMakeLists.txt @@ -52,9 +52,8 @@ add_library(ssl-objects OBJECT ${SSL_SRC}) if (BUILD_SHARED) add_library(ssl STATIC $) add_library(ssl-shared SHARED $) + export_symbol(ssl-shared ${CMAKE_CURRENT_SOURCE_DIR}/ssl.sym) if (WIN32) - target_sources(ssl-shared PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/ssl.def) target_link_libraries(ssl-shared crypto-shared Ws2_32.lib) set(SSL_POSTFIX -${SSL_MAJOR_VERSION}) endif() diff --git a/tls/CMakeLists.txt b/tls/CMakeLists.txt index 1f78038..dcefad7 100644 --- a/tls/CMakeLists.txt +++ b/tls/CMakeLists.txt @@ -33,9 +33,8 @@ add_library(tls-objects OBJECT ${TLS_SRC}) if (BUILD_SHARED) add_library(tls STATIC $) add_library(tls-shared SHARED $) + export_symbol(tls-shared ${CMAKE_CURRENT_SOURCE_DIR}/tls.sym) if (WIN32) - target_sources(tls-shared PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/tls.def) target_link_libraries(tls-shared ssl-shared crypto-shared Ws2_32.lib) set(TLS_POSTFIX -${TLS_MAJOR_VERSION}) endif()