Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1# Copyright (c) 2023-present The Bitcoin Core developers2# Distributed under the MIT software license, see the accompanying3# file COPYING or https://opensource.org/license/mit/.5include_guard(GLOBAL)7macro(normalize_string string)8string(REGEX REPLACE " +" " " ${string} "${${string}}")9string(STRIP "${${string}}" ${string})10endmacro()12function(are_flags_overridden flags_var result_var)13normalize_string(${flags_var})14normalize_string(${flags_var}_INIT)15if(${flags_var} STREQUAL ${flags_var}_INIT)16set(${result_var} FALSE PARENT_SCOPE)17else()18set(${result_var} TRUE PARENT_SCOPE)19endif()20endfunction()23# Removes duplicated flags. The relative order of flags is preserved.24# If duplicates are encountered, only the last instance is preserved.25function(deduplicate_flags flags)26separate_arguments(${flags})27list(REVERSE ${flags})28list(REMOVE_DUPLICATES ${flags})29list(REVERSE ${flags})30list(JOIN ${flags} " " result)31set(${flags} "${result}" PARENT_SCOPE)32endfunction()35function(get_all_configs output)36get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)37if(is_multi_config)38set(all_configs ${CMAKE_CONFIGURATION_TYPES})39else()40get_property(all_configs CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS)41if(NOT all_configs)42# See https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#default-and-custom-configurations43set(all_configs Debug Release RelWithDebInfo MinSizeRel)44endif()45endif()46set(${output} "${all_configs}" PARENT_SCOPE)47endfunction()50#[=[51Set the default build configuration.53See: https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#build-configurations.55On single-configuration generators, this function sets the CMAKE_BUILD_TYPE variable to56the default build configuration, which can be overridden by the user at configure time if needed.58On multi-configuration generators, this function rearranges the CMAKE_CONFIGURATION_TYPES list,59ensuring that the default build configuration appears first while maintaining the order of the60remaining configurations. The user can choose a build configuration at build time.61]=]62function(set_default_config config)63get_all_configs(all_configs)64if(NOT ${config} IN_LIST all_configs)65message(FATAL_ERROR "The default config is \"${config}\", but must be one of ${all_configs}.")66endif()68list(REMOVE_ITEM all_configs ${config})69list(PREPEND all_configs ${config})71get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)72if(is_multi_config)73get_property(help_string CACHE CMAKE_CONFIGURATION_TYPES PROPERTY HELPSTRING)74set(CMAKE_CONFIGURATION_TYPES "${all_configs}" CACHE STRING "${help_string}" FORCE)75# Also see https://gitlab.kitware.com/cmake/cmake/-/issues/19512.76set(CMAKE_TRY_COMPILE_CONFIGURATION "${config}" PARENT_SCOPE)77else()78set_property(CACHE CMAKE_BUILD_TYPE PROPERTY79STRINGS "${all_configs}"80)81if(NOT CMAKE_BUILD_TYPE)82message(STATUS "Setting build type to \"${config}\" as none was specified")83get_property(help_string CACHE CMAKE_BUILD_TYPE PROPERTY HELPSTRING)84set(CMAKE_BUILD_TYPE "${config}" CACHE STRING "${help_string}" FORCE)85endif()86set(CMAKE_TRY_COMPILE_CONFIGURATION "${CMAKE_BUILD_TYPE}" PARENT_SCOPE)87endif()88endfunction()90function(remove_cxx_flag_from_all_configs flag)91get_all_configs(all_configs)92foreach(config IN LISTS all_configs)93string(TOUPPER "${config}" config_uppercase)94set(flags "${CMAKE_CXX_FLAGS_${config_uppercase}}")95separate_arguments(flags)96list(FILTER flags EXCLUDE REGEX "${flag}")97list(JOIN flags " " new_flags)98set(CMAKE_CXX_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE)99set(CMAKE_CXX_FLAGS_${config_uppercase} "${new_flags}"100CACHE STRING101"Flags used by the CXX compiler during ${config_uppercase} builds."102FORCE103)104endforeach()105endfunction()107function(replace_cxx_flag_in_config config old_flag new_flag)108string(TOUPPER "CMAKE_CXX_FLAGS_${config}" var_name)109if("${var_name}" IN_LIST precious_variables)110return()111endif()112string(REGEX REPLACE "(^| )${old_flag}( |$)" "\\1${new_flag}\\2" ${var_name} "${${var_name}}")113set(${var_name} "${${var_name}}" PARENT_SCOPE)114set_property(CACHE ${var_name} PROPERTY VALUE "${${var_name}}")115endfunction()117set_default_config(RelWithDebInfo)119include(TryAppendCXXFlags)121# We leave assertions on.122if(MSVC)123remove_cxx_flag_from_all_configs(/DNDEBUG)124else()125remove_cxx_flag_from_all_configs(-DNDEBUG)127# Adjust flags used by the CXX compiler during RELEASE builds.128# Prefer -O2 optimization level. (-O3 is CMake's default for Release for many compilers.)129replace_cxx_flag_in_config(Release -O3 -O2)131are_flags_overridden(CMAKE_CXX_FLAGS_DEBUG cxx_flags_debug_overridden)132if(NOT cxx_flags_debug_overridden)133# Redefine flags used by the CXX compiler during DEBUG builds.134try_append_cxx_flags("-g3" RESULT_VAR compiler_supports_g3)135if(compiler_supports_g3)136replace_cxx_flag_in_config(Debug -g -g3)137endif()138unset(compiler_supports_g3)140try_append_cxx_flags("-ftrapv" RESULT_VAR compiler_supports_ftrapv)141if(compiler_supports_ftrapv)142string(PREPEND CMAKE_CXX_FLAGS_DEBUG "-ftrapv ")143endif()144unset(compiler_supports_ftrapv)146string(PREPEND CMAKE_CXX_FLAGS_DEBUG "-O0 ")148set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}"149CACHE STRING150"Flags used by the CXX compiler during DEBUG builds."151FORCE152)153endif()154unset(cxx_flags_debug_overridden)155endif()157set(CMAKE_CXX_FLAGS_COVERAGE "-g -Og --coverage")158set(CMAKE_OBJCXX_FLAGS_COVERAGE "-g -Og --coverage")159set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "--coverage")160set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE "--coverage")161get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)162if(is_multi_config)163if(NOT "Coverage" IN_LIST CMAKE_CONFIGURATION_TYPES)164list(APPEND CMAKE_CONFIGURATION_TYPES Coverage)165endif()166endif()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.