Inside Bitcoin
Opening the selected file…
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)6include(CheckCXXSourceCompiles)8#[=[9Add language-wide flags, which will be passed to all invocations of the compiler.10This includes invocations that drive compiling and those that drive linking.12Usage examples:14try_append_cxx_flags("-Wformat -Wformat-security" VAR warn_cxx_flags)17try_append_cxx_flags("-Wsuggest-override" VAR warn_cxx_flags18SOURCE "struct A { virtual void f(); }; struct B : A { void f() final; };"19)22try_append_cxx_flags("-fsanitize=${SANITIZERS}" TARGET core_interface23RESULT_VAR cxx_supports_sanitizers24)25if(NOT cxx_supports_sanitizers)26message(FATAL_ERROR "Compiler did not accept requested flags.")27endif()30try_append_cxx_flags("-Wunused-parameter" TARGET core_interface31IF_CHECK_PASSED "-Wno-unused-parameter"32)35In configuration output, this function prints a string by the following pattern:37-- Performing Test CXX_SUPPORTS_[flags]38-- Performing Test CXX_SUPPORTS_[flags] - Success40]=]41function(try_append_cxx_flags flags)42cmake_parse_arguments(PARSE_ARGV 143TACXXF # prefix44"SKIP_LINK" # options45"TARGET;VAR;SOURCE;RESULT_VAR" # one_value_keywords46"IF_CHECK_PASSED" # multi_value_keywords47)49set(flags_as_string "${flags}")50separate_arguments(flags)52string(MAKE_C_IDENTIFIER "${flags_as_string}" id_string)53string(TOUPPER "${id_string}" id_string)55set(source "int main() { return 0; }")56if(DEFINED TACXXF_SOURCE AND NOT TACXXF_SOURCE STREQUAL source)57set(source "${TACXXF_SOURCE}")58string(SHA256 source_hash "${source}")59string(SUBSTRING ${source_hash} 0 4 source_hash_head)60string(APPEND id_string _${source_hash_head})61endif()63# This avoids running a linker.64set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)65set(CMAKE_REQUIRED_FLAGS "${flags_as_string} ${working_compiler_werror_flag}")66set(compiler_result CXX_SUPPORTS_${id_string})67check_cxx_source_compiles("${source}" ${compiler_result})69if(${compiler_result})70if(DEFINED TACXXF_IF_CHECK_PASSED)71if(DEFINED TACXXF_TARGET)72target_compile_options(${TACXXF_TARGET} INTERFACE ${TACXXF_IF_CHECK_PASSED})73endif()74if(DEFINED TACXXF_VAR)75list(JOIN TACXXF_IF_CHECK_PASSED " " flags_if_check_passed_as_string)76string(STRIP "${${TACXXF_VAR}} ${flags_if_check_passed_as_string}" ${TACXXF_VAR})77endif()78else()79if(DEFINED TACXXF_TARGET)80target_compile_options(${TACXXF_TARGET} INTERFACE ${flags})81endif()82if(DEFINED TACXXF_VAR)83string(STRIP "${${TACXXF_VAR}} ${flags_as_string}" ${TACXXF_VAR})84endif()85endif()86endif()88if(DEFINED TACXXF_VAR)89set(${TACXXF_VAR} "${${TACXXF_VAR}}" PARENT_SCOPE)90endif()92if(DEFINED TACXXF_RESULT_VAR)93set(${TACXXF_RESULT_VAR} "${${compiler_result}}" PARENT_SCOPE)94endif()96if(NOT ${compiler_result} OR TACXXF_SKIP_LINK)97return()98endif()100# This forces running a linker.101set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE)102set(CMAKE_REQUIRED_FLAGS "${flags_as_string}")103set(CMAKE_REQUIRED_LINK_OPTIONS ${working_linker_werror_flag})104set(linker_result LINKER_SUPPORTS_${id_string})105check_cxx_source_compiles("${source}" ${linker_result})107if(${linker_result})108if(DEFINED TACXXF_IF_CHECK_PASSED)109if(DEFINED TACXXF_TARGET)110target_link_options(${TACXXF_TARGET} INTERFACE ${TACXXF_IF_CHECK_PASSED})111endif()112else()113if(DEFINED TACXXF_TARGET)114target_link_options(${TACXXF_TARGET} INTERFACE ${flags})115endif()116endif()117else()118message(WARNING "'${flags_as_string}' fail(s) to link.")119endif()120endfunction()122if(MSVC)123try_append_cxx_flags("/WX /options:strict" VAR working_compiler_werror_flag SKIP_LINK)124else()125try_append_cxx_flags("-Werror" VAR working_compiler_werror_flag SKIP_LINK)126endif()
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.