From 1abae91b7700da0c2d23b96915b1f368ec2fb2b6 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Fri, 28 Jun 2024 18:17:50 +0100 Subject: [PATCH] cmake: Add `--no-undefined` linker option --- CMakeLists.txt | 5 +++++ cmake/TryAppendLinkerFlags.cmake | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 cmake/TryAppendLinkerFlags.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e3465a75b..9e2c5d55ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -269,6 +269,11 @@ if(SECP256K1_BUILD_CTIME_TESTS) unset(msan_enabled) endif() +if(NOT SECP256K1_USE_EXTERNAL_DEFAULT_CALLBACKS) + include(TryAppendLinkerFlags) + try_append_linker_flags(-Wl,--no-undefined) +endif() + include(CTest) # We do not use CTest's BUILD_TESTING because a single toggle for all tests is too coarse for our needs. mark_as_advanced(BUILD_TESTING) diff --git a/cmake/TryAppendLinkerFlags.cmake b/cmake/TryAppendLinkerFlags.cmake new file mode 100644 index 0000000000..457549001b --- /dev/null +++ b/cmake/TryAppendLinkerFlags.cmake @@ -0,0 +1,32 @@ +include_guard(GLOBAL) + +include(CheckCSourceCompiles) + +function(secp256k1_check_linker_flags_internal flags output) + string(MAKE_C_IDENTIFIER "${flags}" result) + string(TOUPPER "${result}" result) + set(result "LINKER_SUPPORTS_${result}") + set(CMAKE_REQUIRED_FLAGS "${flags}") + if(MSVC) + string(APPEND " /WX") + elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") + string(APPEND " -Wl,-fatal_warnings") + else() + string(APPEND " -Wl,--fatal-warnings") + endif() + + # This ensures running a linker. + set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE) + check_c_source_compiles("int main() { return 0; }" ${result}) + + set(${output} ${${result}} PARENT_SCOPE) +endfunction() + +# Append flags to the LINK_OPTIONS directory property if a linker accepts them. +macro(try_append_linker_flags) + secp256k1_check_linker_flags_internal("${ARGV}" result) + if(result) + add_link_options(${ARGV}) + endif() + unset(result) +endmacro()