Skip to content

Commit

Permalink
fixup! cmake: Add TryAppendLinkerFlag module
Browse files Browse the repository at this point in the history
  • Loading branch information
hebasto committed Oct 26, 2023
1 parent 40973d6 commit e3a378b
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions cmake/module/TryAppendLinkerFlag.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include(CheckCXXSourceCompiles)
#[=[
Usage example:

try_append_linker_flag(core "-Wl,--major-subsystem-version,6")
try_append_linker_flag("-Wl,--major-subsystem-version,6" TARGET core)


In configuration output, this function prints a string by the following pattern:
Expand All @@ -17,61 +17,69 @@ In configuration output, this function prints a string by the following pattern:
-- Performing Test LINKER_SUPPORTS_[flag] - Success

]=]
function(try_append_linker_flag target flag)
cmake_parse_arguments(PARSE_ARGV 2
function(try_append_linker_flag flag)
cmake_parse_arguments(PARSE_ARGV 1
TALF # prefix
"" # options
"SOURCE;RESULT_VAR" # one_value_keywords
"TARGET;VAR;SOURCE;RESULT_VAR" # one_value_keywords
"IF_CHECK_PASSED;IF_CHECK_FAILED" # multi_value_keywords
)

string(MAKE_C_IDENTIFIER "${flag}" result)
string(TOUPPER "${result}" result)
set(result "LINKER_SUPPORTS_${result}")

# Every subsequent check_cxx_source_compiles((<code> <resultVar>) run will re-use
# the cached result rather than performing the check again, even if the <code> changes.
# Removing the cached result in order to force the check to be re-evaluated.
unset(${result} CACHE)

if(NOT DEFINED TALF_SOURCE)
set(TALF_SOURCE "int main() { return 0; }")
string(PREPEND result LINKER_SUPPORTS_)

set(source "int main() { return 0; }")
if(DEFINED TALF_SOURCE AND NOT TALF_SOURCE STREQUAL source)
set(source "${TALF_SOURCE}")
string(SHA256 source_hash "${source}")
string(SUBSTRING ${source_hash} 0 4 source_hash_head)
string(APPEND result _${source_hash_head})
endif()

# This forces running a linker.
set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE)

get_target_property(working_linker_werror_flag internal_werror INTERFACE_LINK_OPTIONS)
if(NOT working_linker_werror_flag)
set(working_linker_werror_flag)
endif()

set(CMAKE_REQUIRED_LINK_OPTIONS ${flag} ${working_linker_werror_flag})

check_cxx_source_compiles("${TALF_SOURCE}" ${result})
check_cxx_source_compiles("${source}" ${result})

if(${result})
if(DEFINED TALF_IF_CHECK_PASSED)
target_link_options(${target} INTERFACE ${TALF_IF_CHECK_PASSED})
if(DEFINED TALF_TARGET)
target_link_options(${TALF_TARGET} INTERFACE ${TALF_IF_CHECK_PASSED})
endif()
if(DEFINED TALF_VAR)
string(STRIP "${${TALF_VAR}} ${TALF_IF_CHECK_PASSED}" ${TALF_VAR})
endif()
else()
target_link_options(${target} INTERFACE ${flag})
if(DEFINED TALF_TARGET)
target_link_options(${TALF_TARGET} INTERFACE ${flag})
endif()
if(DEFINED TALF_VAR)
string(STRIP "${${TALF_VAR}} ${flag}" ${TALF_VAR})
endif()
endif()
elseif(DEFINED TALF_IF_CHECK_FAILED)
target_link_options(${target} INTERFACE ${TALF_IF_CHECK_FAILED})
if(DEFINED TALF_TARGET)
target_link_options(${TALF_TARGET} INTERFACE ${TACXXF_IF_CHECK_FAILED})
endif()
if(DEFINED TALF_VAR)
string(STRIP "${${TALF_VAR}} ${TACXXF_IF_CHECK_FAILED}" ${TALF_VAR})
endif()
endif()

if(DEFINED TALF_VAR)
set(${TALF_VAR} "${${TALF_VAR}}" PARENT_SCOPE)
endif()

set(${TALF_RESULT_VAR} "${${result}}" PARENT_SCOPE)
if(DEFINED TALF_RESULT_VAR)
set(${TALF_RESULT_VAR} "${${result}}" PARENT_SCOPE)
endif()
endfunction()

if(NOT TARGET internal_werror)
add_library(internal_werror INTERFACE)
endif()
if(MSVC)
set(warning_as_error_flag /WX)
try_append_linker_flag("/WX" VAR working_linker_werror_flag)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(warning_as_error_flag -Wl,-fatal_warnings)
try_append_linker_flag("-Wl,-fatal_warnings" VAR working_linker_werror_flag)
else()
set(warning_as_error_flag -Wl,--fatal-warnings)
try_append_linker_flag("-Wl,--fatal-warnings" VAR working_linker_werror_flag)
endif()
try_append_linker_flag(internal_werror "${warning_as_error_flag}")
unset(warning_as_error_flag)

0 comments on commit e3a378b

Please sign in to comment.