Skip to content

Commit

Permalink
cmake: Use linker version scripts
Browse files Browse the repository at this point in the history
Autotools build system already uses linker version scripts since
commit 13d165d.

Fixes: #6113
  • Loading branch information
Arfrever committed Feb 25, 2022
1 parent 6a77c9b commit b3fd1ca
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cmake/CMakeLists.txt
@@ -1,5 +1,5 @@
# Minimum CMake required
cmake_minimum_required(VERSION 3.1.3)
cmake_minimum_required(VERSION 3.18)

if(protobuf_VERBOSE)
message(STATUS "Protocol Buffers Configuring...")
Expand Down Expand Up @@ -127,6 +127,17 @@ if (protobuf_DISABLE_RTTI)
add_definitions(-DGOOGLE_PROTOBUF_NO_RTTI=1)
endif()

include(CheckLinkerFlag)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map
"{
global:
main;
local:
*;
};")
check_linker_flag(CXX -Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map protobuf_HAVE_LD_VERSION_SCRIPT)
file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map)

find_package(Threads REQUIRED)

set(_protobuf_FIND_ZLIB)
Expand Down
5 changes: 5 additions & 0 deletions cmake/libprotobuf-lite.cmake
Expand Up @@ -93,6 +93,11 @@ endif()

add_library(libprotobuf-lite ${protobuf_SHARED_OR_STATIC}
${libprotobuf_lite_files} ${libprotobuf_lite_includes} ${libprotobuf_lite_rc_files})
if(protobuf_HAVE_LD_VERSION_SCRIPT)
target_link_options(libprotobuf-lite PRIVATE -Wl,--version-script=${protobuf_source_dir}/src/libprotobuf-lite.map)
set_target_properties(libprotobuf-lite PROPERTIES
LINK_DEPENDS ${protobuf_source_dir}/src/libprotobuf-lite.map)
endif()
target_link_libraries(libprotobuf-lite ${CMAKE_THREAD_LIBS_INIT})
if(protobuf_LINK_LIBATOMIC)
target_link_libraries(libprotobuf-lite atomic)
Expand Down
5 changes: 5 additions & 0 deletions cmake/libprotobuf.cmake
Expand Up @@ -107,6 +107,11 @@ endif()

add_library(libprotobuf ${protobuf_SHARED_OR_STATIC}
${libprotobuf_lite_files} ${libprotobuf_files} ${libprotobuf_includes} ${libprotobuf_rc_files})
if(protobuf_HAVE_LD_VERSION_SCRIPT)
target_link_options(libprotobuf PRIVATE -Wl,--version-script=${protobuf_source_dir}/src/libprotobuf.map)
set_target_properties(libprotobuf PROPERTIES
LINK_DEPENDS ${protobuf_source_dir}/src/libprotobuf.map)
endif()
target_link_libraries(libprotobuf ${CMAKE_THREAD_LIBS_INIT})
if(protobuf_WITH_ZLIB)
target_link_libraries(libprotobuf ${ZLIB_LIBRARIES})
Expand Down
5 changes: 5 additions & 0 deletions cmake/libprotoc.cmake
Expand Up @@ -131,6 +131,11 @@ endif()

add_library(libprotoc ${protobuf_SHARED_OR_STATIC}
${libprotoc_files} ${libprotoc_headers} ${libprotoc_rc_files})
if(protobuf_HAVE_LD_VERSION_SCRIPT)
target_link_options(libprotoc PRIVATE -Wl,--version-script=${protobuf_source_dir}/src/libprotoc.map)
set_target_properties(libprotoc PROPERTIES
LINK_DEPENDS ${protobuf_source_dir}/src/libprotoc.map)
endif()
target_link_libraries(libprotoc libprotobuf)
if(MSVC AND protobuf_BUILD_SHARED_LIBS)
target_compile_definitions(libprotoc
Expand Down

0 comments on commit b3fd1ca

Please sign in to comment.