|
| 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