From 52ac17757eed5056d03a6861bcc24ee864c17385 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Mon, 10 Mar 2025 16:55:48 +0000 Subject: [PATCH] cmake: Add `NO_CACHE_IF_FAILED` option for checking linker flags Use it for checking `-fsanitize`. This change improves the user experience when the configuration step fails due to a missing library. Now, there is no need to manually clean the CMake cache after installing the required library. --- CMakeLists.txt | 1 + cmake/module/TryAppendLinkerFlag.cmake | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a51c3e4fcd..28190bd1e3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -376,6 +376,7 @@ if(SANITIZERS) int main() { return 0; } " RESULT_VAR linker_supports_sanitizers + NO_CACHE_IF_FAILED ) if(NOT linker_supports_sanitizers) message(FATAL_ERROR "Linker did not accept requested flags, you are missing required libraries.") diff --git a/cmake/module/TryAppendLinkerFlag.cmake b/cmake/module/TryAppendLinkerFlag.cmake index be41a2e1cc2..fe7c2bce51e 100644 --- a/cmake/module/TryAppendLinkerFlag.cmake +++ b/cmake/module/TryAppendLinkerFlag.cmake @@ -20,7 +20,7 @@ In configuration output, this function prints a string by the following pattern: function(try_append_linker_flag flag) cmake_parse_arguments(PARSE_ARGV 1 TALF # prefix - "" # options + "NO_CACHE_IF_FAILED" # options "TARGET;VAR;SOURCE;RESULT_VAR" # one_value_keywords "IF_CHECK_PASSED" # multi_value_keywords ) @@ -68,6 +68,10 @@ function(try_append_linker_flag flag) if(DEFINED TALF_RESULT_VAR) set(${TALF_RESULT_VAR} "${${result}}" PARENT_SCOPE) endif() + + if(NOT ${result} AND TALF_NO_CACHE_IF_FAILED) + unset(${result} CACHE) + endif() endfunction() if(MSVC)