Skip to content

Commit 04de82d

Browse files
committed
cmake: Redefine configuration flags
1 parent cac4542 commit 04de82d

File tree

2 files changed

+183
-24
lines changed

2 files changed

+183
-24
lines changed

CMakeLists.txt

+47-24
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ set(configure_warnings)
8484
# It is intended to be a usage requirement for all other targets.
8585
add_library(core INTERFACE)
8686

87+
include(TryAppendCXXFlags)
8788
include(TryAppendLinkerFlag)
8889

8990
if(WIN32)
@@ -182,6 +183,47 @@ include(cmake/leveldb.cmake)
182183
include(cmake/minisketch.cmake)
183184
include(cmake/secp256k1.cmake)
184185

186+
include(ProcessConfigurations)
187+
set_default_config(RelWithDebInfo)
188+
189+
# Redefine configuration flags.
190+
target_compile_definitions(core INTERFACE
191+
$<$<CONFIG:Debug>:DEBUG>
192+
$<$<CONFIG:Debug>:DEBUG_LOCKORDER>
193+
$<$<CONFIG:Debug>:DEBUG_LOCKCONTENTION>
194+
$<$<CONFIG:Debug>:RPC_DOC_CHECK>
195+
$<$<CONFIG:Debug>:ABORT_ON_FAILED_ASSUME>
196+
)
197+
# We leave assertions on.
198+
if(MSVC)
199+
remove_c_flag_from_all_configs(/DNDEBUG)
200+
remove_cxx_flag_from_all_configs(/DNDEBUG)
201+
else()
202+
remove_c_flag_from_all_configs(-DNDEBUG)
203+
remove_cxx_flag_from_all_configs(-DNDEBUG)
204+
205+
# Prefer -O2 optimization level. (-O3 is CMake's default for Release for many compilers.)
206+
replace_c_flag_in_config(Release -O3 -O2)
207+
replace_cxx_flag_in_config(Release -O3 -O2)
208+
209+
set(debug_c_flags "")
210+
set(debug_cxx_flags "")
211+
try_append_cxx_flags(debug_cxx_flags "-O0" RESULT_VAR compiler_supports_O0)
212+
if(compiler_supports_O0)
213+
string(STRIP "${debug_c_flags} -O0" debug_c_flags)
214+
endif()
215+
try_append_cxx_flags(debug_cxx_flags "-g3" RESULT_VAR compiler_supports_g3)
216+
if(compiler_supports_g3)
217+
string(STRIP "${debug_c_flags} -g3" debug_c_flags)
218+
else()
219+
try_append_cxx_flags(debug_cxx_flags "-g")
220+
string(STRIP "${debug_c_flags} -g" debug_c_flags)
221+
endif()
222+
try_append_cxx_flags(debug_cxx_flags "-ftrapv")
223+
set(CMAKE_C_FLAGS_DEBUG "${debug_c_flags}")
224+
set(CMAKE_CXX_FLAGS_DEBUG "${debug_cxx_flags}")
225+
endif()
226+
185227
include(cmake/optional.cmake)
186228

187229
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.14)
@@ -205,6 +247,9 @@ add_subdirectory(test)
205247

206248
include(cmake/tests.cmake)
207249

250+
get_target_property(definitions core INTERFACE_COMPILE_DEFINITIONS)
251+
separate_by_configs(definitions)
252+
208253
message("\n")
209254
message("Configure summary")
210255
message("=================")
@@ -232,9 +277,7 @@ else()
232277
set(cross_status "FALSE")
233278
endif()
234279
message("Cross compiling ....................... ${cross_status}")
235-
get_directory_property(definitions COMPILE_DEFINITIONS)
236-
string(REPLACE ";" " " definitions "${definitions}")
237-
message("Preprocessor defined macros ........... ${definitions}")
280+
message("Preprocessor defined macros ........... ${definitions_ALL}")
238281
message("C compiler ............................ ${CMAKE_C_COMPILER}")
239282
list(JOIN DEPENDS_C_COMPILER_FLAGS " " depends_c_flags)
240283
string(STRIP "${CMAKE_C_FLAGS} ${depends_c_flags}" combined_c_flags)
@@ -260,27 +303,7 @@ endif()
260303
message("Common link options ................... ${common_link_options}")
261304
message("Linker flags for executables .......... ${CMAKE_EXE_LINKER_FLAGS}")
262305
message("Linker flags for shared libraries ..... ${CMAKE_SHARED_LINKER_FLAGS}")
263-
if(DEFINED CMAKE_BUILD_TYPE)
264-
message("Build type:")
265-
message(" - CMAKE_BUILD_TYPE ................... ${CMAKE_BUILD_TYPE}")
266-
string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
267-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_${build_type}}")
268-
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_${build_type}}")
269-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_${build_type}}")
270-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_${build_type}}")
271-
else()
272-
message("Available configurations .............. ${CMAKE_CONFIGURATION_TYPES}")
273-
message("Debug configuration:")
274-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_DEBUG}")
275-
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_DEBUG}")
276-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
277-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
278-
message("Release configuration:")
279-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_RELEASE}")
280-
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_RELEASE}")
281-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
282-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_RELEASE}")
283-
endif()
306+
print_config_flags()
284307
message("Use assembly routines ................. ${ASM}")
285308
message("Use ccache for compiling .............. ${CCACHE}")
286309
message("\n")
+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Copyright (c) 2023-present The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://opensource.org/license/mit/.
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+
20+
#[=[
21+
Set the default build configuration.
22+
23+
See: https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#build-configurations.
24+
25+
On single-configuration generators, this function sets the CMAKE_BUILD_TYPE variable to
26+
the default build configuration, which can be overridden by the user at configure time if needed.
27+
28+
On multi-configuration generators, this function rearranges the CMAKE_CONFIGURATION_TYPES list,
29+
ensuring that the default build configuration appears first while maintaining the order of the
30+
remaining configurations. The user can choose a build configuration at build time.
31+
]=]
32+
function(set_default_config config)
33+
get_all_configs(all_configs)
34+
if(NOT ${config} IN_LIST all_configs)
35+
message(FATAL_ERROR "The default config is \"${config}\", but must be one of ${all_configs}.")
36+
endif()
37+
38+
list(REMOVE_ITEM all_configs ${config})
39+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.15)
40+
list(PREPEND all_configs ${config})
41+
else()
42+
set(all_configs ${config} ${all_configs})
43+
endif()
44+
45+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
46+
if(is_multi_config)
47+
get_property(help_string CACHE CMAKE_CONFIGURATION_TYPES PROPERTY HELPSTRING)
48+
set(CMAKE_CONFIGURATION_TYPES "${all_configs}" CACHE STRING "${help_string}" FORCE)
49+
else()
50+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
51+
STRINGS "${all_configs}"
52+
)
53+
if(NOT CMAKE_BUILD_TYPE)
54+
message(STATUS "Setting build type to \"${config}\" as none was specified")
55+
get_property(help_string CACHE CMAKE_BUILD_TYPE PROPERTY HELPSTRING)
56+
set(CMAKE_BUILD_TYPE "${config}" CACHE STRING "${help_string}" FORCE)
57+
endif()
58+
endif()
59+
endfunction()
60+
61+
function(remove_c_flag_from_all_configs flag)
62+
get_all_configs(all_configs)
63+
foreach(config IN LISTS all_configs)
64+
string(TOUPPER "${config}" config_uppercase)
65+
set(flags "${CMAKE_C_FLAGS_${config_uppercase}}")
66+
separate_arguments(flags)
67+
list(FILTER flags EXCLUDE REGEX "${flag}")
68+
list(JOIN flags " " new_flags)
69+
set(CMAKE_C_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE)
70+
endforeach()
71+
endfunction()
72+
73+
function(remove_cxx_flag_from_all_configs flag)
74+
get_all_configs(all_configs)
75+
foreach(config IN LISTS all_configs)
76+
string(TOUPPER "${config}" config_uppercase)
77+
set(flags "${CMAKE_CXX_FLAGS_${config_uppercase}}")
78+
separate_arguments(flags)
79+
list(FILTER flags EXCLUDE REGEX "${flag}")
80+
list(JOIN flags " " new_flags)
81+
set(CMAKE_CXX_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE)
82+
endforeach()
83+
endfunction()
84+
85+
function(replace_c_flag_in_config config old_flag new_flag)
86+
string(TOUPPER "${config}" config_uppercase)
87+
string(REGEX REPLACE "(^| )${old_flag}( |$)" "\\1${new_flag}\\2" new_flags "${CMAKE_C_FLAGS_${config_uppercase}}")
88+
set(CMAKE_C_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE)
89+
endfunction()
90+
91+
function(replace_cxx_flag_in_config config old_flag new_flag)
92+
string(TOUPPER "${config}" config_uppercase)
93+
string(REGEX REPLACE "(^| )${old_flag}( |$)" "\\1${new_flag}\\2" new_flags "${CMAKE_CXX_FLAGS_${config_uppercase}}")
94+
set(CMAKE_CXX_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE)
95+
endfunction()
96+
97+
function(separate_by_configs options)
98+
list(JOIN ${options} " " ${options}_ALL)
99+
string(GENEX_STRIP "${${options}_ALL}" ${options}_ALL)
100+
set(${options}_ALL "${${options}_ALL}" PARENT_SCOPE)
101+
102+
get_all_configs(all_configs)
103+
foreach(config IN LISTS all_configs)
104+
string(REGEX MATCHALL "\\$<\\$<CONFIG:${config}>[^\n]*>" match "${${options}}")
105+
list(JOIN match " " match)
106+
string(REPLACE "\$<\$<CONFIG:${config}>:" "" match "${match}")
107+
string(REPLACE ">" "" match "${match}")
108+
string(TOUPPER "${config}" conf_upper)
109+
set(${options}_${conf_upper} "${match}" PARENT_SCOPE)
110+
endforeach()
111+
endfunction()
112+
113+
function(print_config_flags)
114+
macro(print_flags config)
115+
string(TOUPPER "${config}" config_uppercase)
116+
message(" - Preprocessor defined macros ........ ${definitions_${config_uppercase}}")
117+
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_${config_uppercase}}")
118+
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_${config_uppercase}}")
119+
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_${config_uppercase}}")
120+
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_${config_uppercase}}")
121+
endmacro()
122+
123+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
124+
if(is_multi_config)
125+
list(JOIN CMAKE_CONFIGURATION_TYPES " " configs)
126+
message("Available build types (configurations) ${configs}")
127+
foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES)
128+
message("'${config}' build type (configuration):")
129+
print_flags(${config})
130+
endforeach()
131+
else()
132+
message("Build type (configuration):")
133+
message(" - CMAKE_BUILD_TYPE ................... ${CMAKE_BUILD_TYPE}")
134+
print_flags(${CMAKE_BUILD_TYPE})
135+
endif()
136+
endfunction()

0 commit comments

Comments
 (0)