Skip to content

Commit 22435de

Browse files
committed
cmake: Add ProcessConfigurations.cmake module
1 parent 3c81838 commit 22435de

File tree

2 files changed

+95
-40
lines changed

2 files changed

+95
-40
lines changed

CMakeLists.txt

+9-40
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,17 @@ option(SECP256K1_BUILD_EXHAUSTIVE_TESTS "Build exhaustive tests." ON)
150150
option(SECP256K1_BUILD_CTIME_TESTS "Build constant-time tests." ${SECP256K1_VALGRIND})
151151
option(SECP256K1_BUILD_EXAMPLES "Build examples." OFF)
152152

153+
include(ProcessConfigurations)
154+
set_default_config(RelWithDebInfo)
155+
153156
# Redefine configuration flags.
154157
# We leave assertions on, because they are only used in the examples, and we want them always on there.
155158
if(MSVC)
156-
string(REGEX REPLACE "/DNDEBUG[ \t\r\n]*" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
157-
string(REGEX REPLACE "/DNDEBUG[ \t\r\n]*" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
158-
string(REGEX REPLACE "/DNDEBUG[ \t\r\n]*" "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
159+
remove_flag_from_all_configs(/DNDEBUG)
159160
else()
160-
string(REGEX REPLACE "-DNDEBUG[ \t\r\n]*" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
161-
string(REGEX REPLACE "-DNDEBUG[ \t\r\n]*" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
162-
string(REGEX REPLACE "-DNDEBUG[ \t\r\n]*" "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
161+
remove_flag_from_all_configs(-DNDEBUG)
163162
# Prefer -O2 optimization level. (-O3 is CMake's default for Release for many compilers.)
164-
string(REGEX REPLACE "-O3[ \t\r\n]*" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
163+
replace_flag_in_config(Release -O3 -O2)
165164
endif()
166165

167166
# Define custom "Coverage" build type.
@@ -184,23 +183,10 @@ mark_as_advanced(
184183
)
185184

186185
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
187-
set(default_build_type "RelWithDebInfo")
188186
if(is_multi_config)
189-
set(CMAKE_CONFIGURATION_TYPES "${default_build_type}" "Release" "Debug" "MinSizeRel" "Coverage" CACHE STRING
190-
"Supported configuration types."
191-
FORCE
192-
)
187+
list(APPEND CMAKE_CONFIGURATION_TYPES Coverage)
193188
else()
194-
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
195-
STRINGS "${default_build_type}" "Release" "Debug" "MinSizeRel" "Coverage"
196-
)
197-
if(NOT CMAKE_BUILD_TYPE)
198-
message(STATUS "Setting build type to \"${default_build_type}\" as none was specified")
199-
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING
200-
"Choose the type of build."
201-
FORCE
202-
)
203-
endif()
189+
set_property(CACHE CMAKE_BUILD_TYPE APPEND PROPERTY STRINGS Coverage)
204190
endif()
205191

206192
include(TryAppendCFlags)
@@ -296,24 +282,7 @@ message("CFLAGS ................................ ${CMAKE_C_FLAGS}")
296282
get_directory_property(compile_options COMPILE_OPTIONS)
297283
string(REPLACE ";" " " compile_options "${compile_options}")
298284
message("Compile options ....................... " ${compile_options})
299-
if(NOT is_multi_config)
300-
message("Build type:")
301-
message(" - CMAKE_BUILD_TYPE ................... ${CMAKE_BUILD_TYPE}")
302-
string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
303-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_${build_type}}")
304-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_${build_type}}")
305-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_${build_type}}")
306-
else()
307-
message("Supported configurations .............. ${CMAKE_CONFIGURATION_TYPES}")
308-
message("RelWithDebInfo configuration:")
309-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_RELWITHDEBINFO}")
310-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
311-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")
312-
message("Debug configuration:")
313-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_DEBUG}")
314-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
315-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
316-
endif()
285+
print_config_flags()
317286
message("\n")
318287
if(SECP256K1_EXPERIMENTAL)
319288
message(

cmake/ProcessConfigurations.cmake

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
function(get_all_configs output)
2+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
3+
if(is_multi_config)
4+
set(all_configs ${CMAKE_CONFIGURATION_TYPES})
5+
else()
6+
get_property(all_configs CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS)
7+
if(NOT all_configs)
8+
# See https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#default-and-custom-configurations
9+
set(all_configs Debug Release RelWithDebInfo MinSizeRel)
10+
endif()
11+
endif()
12+
set(${output} "${all_configs}" PARENT_SCOPE)
13+
endfunction()
14+
15+
# When used with multi-configuration generators, this function rearranges
16+
# the CMAKE_CONFIGURATION_TYPES list, ensuring that the default configuration type
17+
# appears first while maintaining the order of the remaining configuration types.
18+
function(set_default_config config)
19+
get_all_configs(all_configs)
20+
if(NOT ${config} IN_LIST all_configs)
21+
message(FATAL_ERROR "The default config is \"${config}\", but must be one of ${all_configs}.")
22+
endif()
23+
24+
list(REMOVE_ITEM all_configs ${config})
25+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.15)
26+
list(PREPEND all_configs ${config})
27+
else()
28+
set(all_configs ${config} ${all_configs})
29+
endif()
30+
31+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
32+
if(is_multi_config)
33+
get_property(help_string CACHE CMAKE_CONFIGURATION_TYPES PROPERTY HELPSTRING)
34+
set(CMAKE_CONFIGURATION_TYPES "${all_configs}" CACHE STRING "${help_string}" FORCE)
35+
else()
36+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
37+
STRINGS "${all_configs}"
38+
)
39+
if(NOT CMAKE_BUILD_TYPE)
40+
message(STATUS "Setting build type to \"${config}\" as none was specified")
41+
get_property(help_string CACHE CMAKE_BUILD_TYPE PROPERTY HELPSTRING)
42+
set(CMAKE_BUILD_TYPE "${config}" CACHE STRING "${help_string}" FORCE)
43+
endif()
44+
endif()
45+
endfunction()
46+
47+
function(remove_flag_from_all_configs flag)
48+
get_all_configs(all_configs)
49+
foreach(config IN LISTS all_configs)
50+
string(TOUPPER "${config}" config_uppercase)
51+
set(flags "${CMAKE_C_FLAGS_${config_uppercase}}")
52+
separate_arguments(flags)
53+
list(FILTER flags EXCLUDE REGEX "${flag}")
54+
list(JOIN flags " " new_flags)
55+
set(CMAKE_C_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE)
56+
endforeach()
57+
endfunction()
58+
59+
function(replace_flag_in_config config old_flag new_flag)
60+
string(TOUPPER "${config}" config_uppercase)
61+
string(REGEX REPLACE "(^| )${old_flag}( |$)" "\\1${new_flag}\\2" new_flags "${CMAKE_C_FLAGS_${config_uppercase}}")
62+
set(CMAKE_C_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE)
63+
endfunction()
64+
65+
function(print_config_flags)
66+
macro(print_flags config)
67+
string(TOUPPER "${config}" config_uppercase)
68+
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_${config_uppercase}}")
69+
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_${config_uppercase}}")
70+
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_${config_uppercase}}")
71+
endmacro()
72+
73+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
74+
if(is_multi_config)
75+
list(JOIN CMAKE_CONFIGURATION_TYPES " " configs)
76+
message("Supported configuration types ......... ${configs}")
77+
foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES)
78+
message("'${config}' configuration type:")
79+
print_flags(${config})
80+
endforeach()
81+
else()
82+
message("Build type:")
83+
message(" - CMAKE_BUILD_TYPE ................... ${CMAKE_BUILD_TYPE}")
84+
print_flags(${CMAKE_BUILD_TYPE})
85+
endif()
86+
endfunction()

0 commit comments

Comments
 (0)