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