Skip to content

Commit 1abae91

Browse files
committed
cmake: Add --no-undefined linker option
1 parent 4af241b commit 1abae91

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ if(SECP256K1_BUILD_CTIME_TESTS)
269269
unset(msan_enabled)
270270
endif()
271271

272+
if(NOT SECP256K1_USE_EXTERNAL_DEFAULT_CALLBACKS)
273+
include(TryAppendLinkerFlags)
274+
try_append_linker_flags(-Wl,--no-undefined)
275+
endif()
276+
272277
include(CTest)
273278
# We do not use CTest's BUILD_TESTING because a single toggle for all tests is too coarse for our needs.
274279
mark_as_advanced(BUILD_TESTING)

cmake/TryAppendLinkerFlags.cmake

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
include_guard(GLOBAL)
2+
3+
include(CheckCSourceCompiles)
4+
5+
function(secp256k1_check_linker_flags_internal flags output)
6+
string(MAKE_C_IDENTIFIER "${flags}" result)
7+
string(TOUPPER "${result}" result)
8+
set(result "LINKER_SUPPORTS_${result}")
9+
set(CMAKE_REQUIRED_FLAGS "${flags}")
10+
if(MSVC)
11+
string(APPEND " /WX")
12+
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
13+
string(APPEND " -Wl,-fatal_warnings")
14+
else()
15+
string(APPEND " -Wl,--fatal-warnings")
16+
endif()
17+
18+
# This ensures running a linker.
19+
set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE)
20+
check_c_source_compiles("int main() { return 0; }" ${result})
21+
22+
set(${output} ${${result}} PARENT_SCOPE)
23+
endfunction()
24+
25+
# Append flags to the LINK_OPTIONS directory property if a linker accepts them.
26+
macro(try_append_linker_flags)
27+
secp256k1_check_linker_flags_internal("${ARGV}" result)
28+
if(result)
29+
add_link_options(${ARGV})
30+
endif()
31+
unset(result)
32+
endmacro()

0 commit comments

Comments
 (0)