Skip to content

Commit dba171f

Browse files
committed
cmake: Redefine configuration flags
1 parent fd9d1d2 commit dba171f

File tree

2 files changed

+175
-24
lines changed

2 files changed

+175
-24
lines changed

CMakeLists.txt

+48-24
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
8080

8181
set(configure_warnings)
8282

83+
include(TryAppendCXXFlags)
8384
include(TryAppendLinkerFlag)
8485

8586
if(WIN32)
@@ -158,6 +159,48 @@ include(cmake/leveldb.cmake)
158159
include(cmake/minisketch.cmake)
159160
include(cmake/secp256k1.cmake)
160161

162+
include(ProcessConfigurations)
163+
set_default_config(RelWithDebInfo)
164+
165+
# Redefine configuration flags.
166+
add_compile_definitions($<$<CONFIG:Debug>:DEBUG>)
167+
add_compile_definitions($<$<CONFIG:Debug>:DEBUG_LOCKORDER>)
168+
add_compile_definitions($<$<CONFIG:Debug>:DEBUG_LOCKCONTENTION>)
169+
add_compile_definitions($<$<CONFIG:Debug>:RPC_DOC_CHECK>)
170+
add_compile_definitions($<$<CONFIG:Debug>:ABORT_ON_FAILED_ASSUME>)
171+
# We leave assertions on.
172+
if(MSVC)
173+
remove_c_flag_from_all_configs(/DNDEBUG)
174+
remove_cxx_flag_from_all_configs(/DNDEBUG)
175+
else()
176+
remove_c_flag_from_all_configs(-DNDEBUG)
177+
remove_cxx_flag_from_all_configs(-DNDEBUG)
178+
179+
# Prefer -O2 optimization level. (-O3 is CMake's default for Release for many compilers.)
180+
replace_c_flag_in_config(Release -O3 -O2)
181+
replace_cxx_flag_in_config(Release -O3 -O2)
182+
183+
set(debug_c_flags "")
184+
set(debug_cxx_flags "")
185+
try_append_cxx_flags(debug_cxx_flags "-O0" RESULT_VAR compiler_supports_O0)
186+
if(compiler_supports_O0)
187+
string(STRIP "${debug_c_flags} -O0" debug_c_flags)
188+
endif()
189+
try_append_cxx_flags(debug_cxx_flags "-g3" RESULT_VAR compiler_supports_g3)
190+
if(compiler_supports_g3)
191+
string(STRIP "${debug_c_flags} -g3" debug_c_flags)
192+
else()
193+
try_append_cxx_flags(debug_cxx_flags "-g")
194+
string(STRIP "${debug_c_flags} -g" debug_c_flags)
195+
endif()
196+
try_append_cxx_flags(debug_cxx_flags "-ftrapv")
197+
if(MINGW)
198+
try_append_cxx_flags(debug_cxx_flags "-Wa,-mbig-obj")
199+
endif()
200+
set(CMAKE_C_FLAGS_DEBUG "${debug_c_flags}")
201+
set(CMAKE_CXX_FLAGS_DEBUG "${debug_cxx_flags}")
202+
endif()
203+
161204
include(cmake/optional.cmake)
162205

163206
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.14)
@@ -181,6 +224,9 @@ add_subdirectory(test)
181224

182225
include(cmake/tests.cmake)
183226

227+
get_directory_property(definitions COMPILE_DEFINITIONS)
228+
separate_configs(definitions)
229+
184230
message("\n")
185231
message("Configure summary")
186232
message("=================")
@@ -208,9 +254,7 @@ else()
208254
set(cross_status "FALSE")
209255
endif()
210256
message("Cross compiling ....................... ${cross_status}")
211-
get_directory_property(definitions COMPILE_DEFINITIONS)
212-
string(REPLACE ";" " " definitions "${definitions}")
213-
message("Preprocessor defined macros ........... ${definitions}")
257+
message("Preprocessor defined macros ........... ${definitions_ALL}")
214258
message("C compiler ............................ ${CMAKE_C_COMPILER}")
215259
message("CFLAGS ................................ ${CMAKE_C_FLAGS}")
216260
message("C++ compiler .......................... ${CMAKE_CXX_COMPILER}")
@@ -223,27 +267,7 @@ string(REPLACE ";" " " common_link_options "${common_link_options}")
223267
message("Common link options ................... ${common_link_options}")
224268
message("Linker flags for executables .......... ${CMAKE_EXE_LINKER_FLAGS}")
225269
message("Linker flags for shared libraries ..... ${CMAKE_SHARED_LINKER_FLAGS}")
226-
if(DEFINED CMAKE_BUILD_TYPE)
227-
message("Build type:")
228-
message(" - CMAKE_BUILD_TYPE ................... ${CMAKE_BUILD_TYPE}")
229-
string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
230-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_${build_type}}")
231-
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_${build_type}}")
232-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_${build_type}}")
233-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_${build_type}}")
234-
else()
235-
message("Available configurations .............. ${CMAKE_CONFIGURATION_TYPES}")
236-
message("Debug configuration:")
237-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_DEBUG}")
238-
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_DEBUG}")
239-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
240-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
241-
message("Release configuration:")
242-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_RELEASE}")
243-
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_RELEASE}")
244-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
245-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_RELEASE}")
246-
endif()
270+
print_config_flags()
247271
message("Use assembly routines ................. ${ASM}")
248272
message("Use ccache for compiling .............. ${CCACHE}")
249273
message("\n")
+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Copyright (c) 2023 The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
function(get_all_configs output)
6+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
7+
if(is_multi_config)
8+
set(all_configs ${CMAKE_CONFIGURATION_TYPES})
9+
else()
10+
get_property(all_configs CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS)
11+
if(NOT all_configs)
12+
# See https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#default-and-custom-configurations
13+
set(all_configs Debug Release RelWithDebInfo MinSizeRel)
14+
endif()
15+
endif()
16+
set(${output} "${all_configs}" PARENT_SCOPE)
17+
endfunction()
18+
19+
# When used with multi-configuration generators, this function rearranges
20+
# the CMAKE_CONFIGURATION_TYPES list, ensuring that the default configuration type
21+
# appears first while maintaining the order of the remaining configuration types.
22+
function(set_default_config config)
23+
get_all_configs(all_configs)
24+
if(NOT ${config} IN_LIST all_configs)
25+
message(FATAL_ERROR "The default config is \"${config}\", but must be one of ${all_configs}.")
26+
endif()
27+
28+
list(REMOVE_ITEM all_configs ${config})
29+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.15)
30+
list(PREPEND all_configs ${config})
31+
else()
32+
set(all_configs ${config} ${all_configs})
33+
endif()
34+
35+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
36+
if(is_multi_config)
37+
get_property(help_string CACHE CMAKE_CONFIGURATION_TYPES PROPERTY HELPSTRING)
38+
set(CMAKE_CONFIGURATION_TYPES "${all_configs}" CACHE STRING "${help_string}" FORCE)
39+
else()
40+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
41+
STRINGS "${all_configs}"
42+
)
43+
if(NOT CMAKE_BUILD_TYPE)
44+
message(STATUS "Setting build type to \"${config}\" as none was specified")
45+
get_property(help_string CACHE CMAKE_BUILD_TYPE PROPERTY HELPSTRING)
46+
set(CMAKE_BUILD_TYPE "${config}" CACHE STRING "${help_string}" FORCE)
47+
endif()
48+
endif()
49+
endfunction()
50+
51+
function(remove_c_flag_from_all_configs flag)
52+
get_all_configs(all_configs)
53+
foreach(config IN LISTS all_configs)
54+
string(TOUPPER "${config}" config_uppercase)
55+
set(flags "${CMAKE_C_FLAGS_${config_uppercase}}")
56+
separate_arguments(flags)
57+
list(FILTER flags EXCLUDE REGEX "${flag}")
58+
list(JOIN flags " " new_flags)
59+
set(CMAKE_C_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE)
60+
endforeach()
61+
endfunction()
62+
63+
function(remove_cxx_flag_from_all_configs flag)
64+
get_all_configs(all_configs)
65+
foreach(config IN LISTS all_configs)
66+
string(TOUPPER "${config}" config_uppercase)
67+
set(flags "${CMAKE_CXX_FLAGS_${config_uppercase}}")
68+
separate_arguments(flags)
69+
list(FILTER flags EXCLUDE REGEX "${flag}")
70+
list(JOIN flags " " new_flags)
71+
set(CMAKE_CXX_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE)
72+
endforeach()
73+
endfunction()
74+
75+
function(replace_c_flag_in_config config old_flag new_flag)
76+
string(TOUPPER "${config}" config_uppercase)
77+
string(REGEX REPLACE "(^| )${old_flag}( |$)" "\\1${new_flag}\\2" new_flags "${CMAKE_C_FLAGS_${config_uppercase}}")
78+
set(CMAKE_C_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE)
79+
endfunction()
80+
81+
function(replace_cxx_flag_in_config config old_flag new_flag)
82+
string(TOUPPER "${config}" config_uppercase)
83+
string(REGEX REPLACE "(^| )${old_flag}( |$)" "\\1${new_flag}\\2" new_flags "${CMAKE_CXX_FLAGS_${config_uppercase}}")
84+
set(CMAKE_CXX_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE)
85+
endfunction()
86+
87+
function(separate_configs options)
88+
string(GENEX_STRIP "${${options}}" ${options}_ALL)
89+
string(REPLACE ";" " " ${options}_ALL "${${options}_ALL}")
90+
set(${options}_ALL "${${options}_ALL}" PARENT_SCOPE)
91+
92+
get_all_configs(all_configs)
93+
foreach(config IN LISTS all_configs)
94+
string(TOUPPER "${config}" conf_upper)
95+
set(var ${options}_${conf_upper})
96+
string(REGEX MATCHALL "\\$<\\$<CONFIG:${config}>[^\n]*>" ${var} "${${options}}")
97+
string(REPLACE "\$<\$<CONFIG:${config}>:" "" ${var} "${${var}}")
98+
string(REPLACE ">" "" ${var} "${${var}}")
99+
string(REPLACE ";" " " ${var} "${${var}}")
100+
set(${var} "${${var}}" PARENT_SCOPE)
101+
endforeach()
102+
endfunction()
103+
104+
function(print_config_flags)
105+
macro(print_flags config)
106+
string(TOUPPER "${config}" config_uppercase)
107+
message(" - Preprocessor defined macros ........ ${definitions_${config_uppercase}}")
108+
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_${config_uppercase}}")
109+
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_${config_uppercase}}")
110+
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_${config_uppercase}}")
111+
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_${config_uppercase}}")
112+
endmacro()
113+
114+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
115+
if(is_multi_config)
116+
list(JOIN CMAKE_CONFIGURATION_TYPES " " configs)
117+
message("Available build types (configurations) ${configs}")
118+
foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES)
119+
message("'${config}' build type (configuration):")
120+
print_flags(${config})
121+
endforeach()
122+
else()
123+
message("Build type (configuration):")
124+
message(" - CMAKE_BUILD_TYPE ................... ${CMAKE_BUILD_TYPE}")
125+
print_flags(${CMAKE_BUILD_TYPE})
126+
endif()
127+
endfunction()

0 commit comments

Comments
 (0)