Skip to content

Commit 1b4ddb0

Browse files
committed
Merge bitcoin/bitcoin#32356: cmake: Respect user-provided configuration-specific flags
edde963 cmake: Respect user-provided configuration-specific flags (Hennadii Stepanov) Pull request description: This PR addresses [this](bitcoin/bitcoin#31491 (comment)) comment: > I suppose that should only happen if the `-O3` isn't coming from an explicitly set `CMAKE_CXX_FLAGS_RELEASE`. With this PR: ``` $ cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS_RELEASE="-O3" <snip> C++ compiler flags .................... -O3 -std=c++20 -fPIC -fno-extended-identifiers -fdebug-prefix-map=/home/hebasto/dev/bitcoin/src=. -fmacro-prefix-map=/home/hebasto/dev/bitcoin/src=. -fstack-reuse=none -Wall -Wextra -Wformat -Wformat-security -Wvla -Wredundant-decls -Wdate-time -Wduplicated-branches -Wduplicated-cond -Wlogical-op -Woverloaded-virtual -Wsuggest-override -Wimplicit-fallthrough -Wunreachable-code -Wbidi-chars=any -Wundef -Wno-unused-parameter -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -Wstack-protector -fstack-protector-all -fcf-protection=full -fstack-clash-protection Linker flags .......................... -O3 -fstack-reuse=none -fstack-protector-all -fcf-protection=full -fstack-clash-protection -Wl,-z,relro -Wl,-z,now -Wl,-z,separate-code -fPIE -pie ``` and ``` $ cmake -B build -DCMAKE_BUILD_TYPE=Release <snip> C++ compiler flags .................... -O2 -std=c++20 -fPIC -fno-extended-identifiers -fdebug-prefix-map=/home/hebasto/dev/bitcoin/src=. -fmacro-prefix-map=/home/hebasto/dev/bitcoin/src=. -fstack-reuse=none -Wall -Wextra -Wformat -Wformat-security -Wvla -Wredundant-decls -Wdate-time -Wduplicated-branches -Wduplicated-cond -Wlogical-op -Woverloaded-virtual -Wsuggest-override -Wimplicit-fallthrough -Wunreachable-code -Wbidi-chars=any -Wundef -Wno-unused-parameter -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -Wstack-protector -fstack-protector-all -fcf-protection=full -fstack-clash-protection Linker flags .......................... -O2 -fstack-reuse=none -fstack-protector-all -fcf-protection=full -fstack-clash-protection -Wl,-z,relro -Wl,-z,now -Wl,-z,separate-code -fPIE -pie ``` When calling `cmake` repeatedly using the same build directory, each newly provided `CMAKE_CXX_FLAGS_RELEASE` value will be accommodated. In such a scenario, if the user wishes to revert to the build system defaults, they should unset the `CMAKE_CXX_FLAGS_RELEASE` variable by passing `-UCMAKE_CXX_FLAGS_RELEASE` to `cmake`. --- This PR does not aim to resolve _all_ issues mentioned in bitcoin/bitcoin#31491. ACKs for top commit: purpleKarrot: ACK edde963 janb84: ACK [edde963](bitcoin/bitcoin@edde963) ryanofsky: Code review ACK edde963 Tree-SHA512: 1fbc879bd02cf0be726ced490f65985e728f0686ccb3a32cd38787b56377aa666e1965448e5069515abc814df49a0083c8000bc3f6f322f5f395695638168fb6
2 parents 66c968b + edde963 commit 1b4ddb0

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ if(POLICY CMP0171)
1919
cmake_policy(SET CMP0171 NEW)
2020
endif()
2121

22+
# When adjusting CMake flag variables, we must not override those explicitly
23+
# set by the user. These are a subset of the CACHE_VARIABLES property.
24+
get_directory_property(precious_variables CACHE_VARIABLES)
25+
2226
#=============================
2327
# Project / Package metadata
2428
#=============================

cmake/module/ProcessConfigurations.cmake

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,13 @@ function(remove_cxx_flag_from_all_configs flag)
105105
endfunction()
106106

107107
function(replace_cxx_flag_in_config config old_flag new_flag)
108-
string(TOUPPER "${config}" config_uppercase)
109-
string(REGEX REPLACE "(^| )${old_flag}( |$)" "\\1${new_flag}\\2" new_flags "${CMAKE_CXX_FLAGS_${config_uppercase}}")
110-
set(CMAKE_CXX_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE)
111-
set(CMAKE_CXX_FLAGS_${config_uppercase} "${new_flags}"
112-
CACHE STRING
113-
"Flags used by the CXX compiler during ${config_uppercase} builds."
114-
FORCE
115-
)
108+
string(TOUPPER "CMAKE_CXX_FLAGS_${config}" var_name)
109+
if("${var_name}" IN_LIST precious_variables)
110+
return()
111+
endif()
112+
string(REGEX REPLACE "(^| )${old_flag}( |$)" "\\1${new_flag}\\2" ${var_name} "${${var_name}}")
113+
set(${var_name} "${${var_name}}" PARENT_SCOPE)
114+
set_property(CACHE ${var_name} PROPERTY VALUE "${${var_name}}")
116115
endfunction()
117116

118117
set_default_config(RelWithDebInfo)

0 commit comments

Comments
 (0)