CMake: Enable -Wl,--no-undefined on ELF platforms

This is mostly a development helper, to move all undefined symbols
in shared libraries to link time rather than load time.

At the same time, set --no-allow-shlib-undefined and -z,defs to
further enforce the rule.
This commit is contained in:
Andrew Kaster 2024-06-18 09:24:10 -06:00
parent a478bbfcb2
commit 72ee54b6f0
3 changed files with 17 additions and 0 deletions

View file

@ -15,6 +15,11 @@ set(BUILD_TESTING_SAVE ${BUILD_TESTING})
set(BUILD_TESTING OFF)
set(BUILD_EXAMPLES OFF)
# FIXME: https://github.com/apple/swift-collections/issues/440
set(BUILD_SHARED_LIBS_SAVE ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
FetchContent_MakeAvailable(SwiftCollections)
set(BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_SAVE})
set(BUILD_TESTING ${BUILD_TESTING_SAVE})

View file

@ -54,6 +54,13 @@ if (NOT WIN32)
endif()
endif()
if (NOT WIN32 AND NOT APPLE AND NOT ENABLE_FUZZERS)
# NOTE: Assume ELF
add_link_options(LINKER:-z,defs)
add_link_options(LINKER:--no-undefined)
add_link_options(LINKER:--no-allow-shlib-undefined)
endif()
if (ENABLE_LAGOM_COVERAGE_COLLECTION)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang$" AND NOT ENABLE_FUZZERS)
add_cxx_compile_options(-fprofile-instr-generate -fcoverage-mapping)

View file

@ -16,6 +16,11 @@ function(clang_plugin target_name)
set_property(GLOBAL APPEND PROPERTY CLANG_PLUGINS_COMPILE_OPTIONS_FOR_TESTS -fplugin=${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/lib${target_name}.so)
add_lagom_library_install_rules(${target_name})
if (NOT WIN32 AND NOT APPLE)
target_link_options(${target_name} PRIVATE LINKER:-z,undefs)
target_link_options(${target_name} PRIVATE LINKER:--allow-shlib-undefined)
endif()
endfunction()
function(depend_on_clang_plugin target_name plugin_name)