From 2f68531c53f71d7343f3a6e06f3389136c80dd9b Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Sat, 26 Apr 2025 19:19:49 +0100 Subject: [PATCH] cmake: Respect user-provided configuration-specific flags --- CMakeLists.txt | 4 ++++ cmake/module/ProcessConfigurations.cmake | 14 +++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b272658177f..bc6f84eb97e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,10 @@ if(POLICY CMP0171) cmake_policy(SET CMP0171 NEW) endif() +# When adjusting CMake flag variables, we must not override those explicitly +# set by the user. These are a subset of the CACHE_VARIABLES property. +get_directory_property(precious_variables CACHE_VARIABLES) + #============================= # Project / Package metadata #============================= diff --git a/cmake/module/ProcessConfigurations.cmake b/cmake/module/ProcessConfigurations.cmake index 9a510a00a55..ce24e3b79b9 100644 --- a/cmake/module/ProcessConfigurations.cmake +++ b/cmake/module/ProcessConfigurations.cmake @@ -106,13 +106,13 @@ endfunction() function(replace_cxx_flag_in_config config old_flag new_flag) string(TOUPPER "${config}" config_uppercase) - string(REGEX REPLACE "(^| )${old_flag}( |$)" "\\1${new_flag}\\2" new_flags "${CMAKE_CXX_FLAGS_${config_uppercase}}") - set(CMAKE_CXX_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE) - set(CMAKE_CXX_FLAGS_${config_uppercase} "${new_flags}" - CACHE STRING - "Flags used by the CXX compiler during ${config_uppercase} builds." - FORCE - ) + set(var_name CMAKE_CXX_FLAGS_${config_uppercase}) + if("${var_name}" IN_LIST precious_variables) + return() + endif() + string(REGEX REPLACE "(^| )${old_flag}( |$)" "\\1${new_flag}\\2" ${var_name} "${${var_name}}") + set(${var_name} "${${var_name}}" PARENT_SCOPE) + set_property(CACHE ${var_name} PROPERTY VALUE "${${var_name}}") endfunction() set_default_config(RelWithDebInfo)