Skip to content

Commit

Permalink
cmake: Add --no-undefined linker option
Browse files Browse the repository at this point in the history
  • Loading branch information
hebasto committed Jun 28, 2024
1 parent 4af241b commit 1abae91
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions cmake/TryAppendLinkerFlags.cmake
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 1abae91

Please sign in to comment.