cmake: use check_linker_flag for checking linker flags

check_c_compiler_flag does not actually pass the flag to the linker, and
so the test file successfully compiles for any linker flag. This
caused the -Wl,z,relro flag to be passed to the compiler even if it was
not supported.

check_c_compiler_flag was introduced in cmake 3.18, so this commit also
bumps the minimum required cmake version to 3.18 (released in 2020).
This commit is contained in:
Robin Voetter
2023-08-12 14:04:54 +02:00
parent be47f8c560
commit 1495790f47
2 changed files with 5 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.18)
#///////////////////////////////////////////////////////////////////#
# PROJECT #

View File

@@ -1,3 +1,5 @@
include(CheckLinkerFlag)
function(add_compiler_option_to_target_type TARGET BUILDTYPE VISIBILITY OPTIONS)
include(CheckCCompilerFlag)
list(APPEND OPTIONS ${ARGN})
@@ -29,7 +31,7 @@ function(add_linker_option_to_target_type TARGET BUILDTYPE VISIBILITY OPTIONS)
list(APPEND OPTIONS ${ARGN})
foreach(LINK_OPTION IN LISTS OPTIONS)
string(REPLACE "," "_" LINK_OPTION_NAME "${LINK_OPTION}")
check_c_compiler_flag(${LINK_OPTION} "linker_has${LINK_OPTION_NAME}")
check_linker_flag(C "${LINK_OPTION}" "linker_has${LINK_OPTION_NAME}")
if (${linker_has${LINK_OPTION_NAME}})
target_link_libraries(${TARGET} ${VISIBILITY}
$<$<CONFIG:${BUILDTYPE}>:${LINK_OPTION}>)
@@ -42,7 +44,7 @@ function(add_linker_option_to_all_but_target_type TARGET BUILDTYPE VISIBILITY OP
list(APPEND OPTIONS ${ARGN})
foreach(LINK_OPTION IN LISTS OPTIONS)
string(REPLACE "," "_" LINK_OPTION_NAME "${LINK_OPTION}")
check_c_compiler_flag(${LINK_OPTION} "linker_has${LINK_OPTION_NAME}")
check_linker_flag(C "${LINK_OPTION}" "linker_has${LINK_OPTION_NAME}")
if (${linker_has${LINK_OPTION_NAME}})
target_link_libraries(${TARGET} ${VISIBILITY}
$<$<NOT:$<CONFIG:${BUILDTYPE}>>:${LINK_OPTION}>)