From 1f60b30df0cb58a7381a1bfbd6d34f002232e862 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Fri, 26 Apr 2024 13:38:41 +0100 Subject: [PATCH] cmake: Add `APPEND_{CPP,C,CXX,LD}FLAGS` cache variables The content of those variables is appended to the each target after the flags added by the build system. --- CMakeLists.txt | 26 ++++++++++++++++++++++++++ cmake/module/FlagsSummary.cmake | 3 +++ src/CMakeLists.txt | 1 + 3 files changed, 30 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b033383d81..3d43079b33d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,22 @@ set(CLIENT_VERSION_RC 0) set(CLIENT_VERSION_IS_RELEASE "false") set(COPYRIGHT_YEAR "2024") +# During the enabling of the CXX and CXXOBJ languages, we modify +# CMake's compiler/linker invocation strings by appending the content +# of the user-defined `APPEND_*` variables, which allows overriding +# any flag. We also ensure that the APPEND_* flags are considered +# during CMake's tests, which use the `try_compile()` command. +# +# CMake's docs state that the `CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` +# variable "is meant to be set by CMake's platform information modules +# for the current toolchain, or by a toolchain file." We do our best +# to set it before the `project()` command. +set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES + CMAKE_CXX_COMPILE_OBJECT + CMAKE_OBJCXX_COMPILE_OBJECT + CMAKE_CXX_LINK_EXECUTABLE +) + project(BitcoinCore VERSION ${CLIENT_VERSION_MAJOR}.${CLIENT_VERSION_MINOR}.${CLIENT_VERSION_BUILD} DESCRIPTION "Bitcoin client software" @@ -189,6 +205,16 @@ cmake_dependent_option(BUILD_FOR_FUZZING "Build for fuzzing. Enabling this will option(INSTALL_MAN "Install man pages." ON) +set(APPEND_CPPFLAGS "" CACHE STRING "Preprocessor flags that are appended to the command line after all other flags added by the build system. This variable is intended for debugging and special builds.") +set(APPEND_CFLAGS "" CACHE STRING "C compiler flags that are appended to the command line after all other flags added by the build system. This variable is intended for debugging and special builds.") +set(APPEND_CXXFLAGS "" CACHE STRING "(Objective) C++ compiler flags that are appended to the command line after all other flags added by the build system. This variable is intended for debugging and special builds.") +set(APPEND_LDFLAGS "" CACHE STRING "Linker flags that are appended to the command line after all other flags added by the build system. This variable is intended for debugging and special builds.") +# Appending to this low-level rule variables is the only way to +# guarantee that the flags appear at the end of the command line. +string(APPEND CMAKE_CXX_COMPILE_OBJECT " ${APPEND_CPPFLAGS} ${APPEND_CXXFLAGS}") +string(APPEND CMAKE_CXX_CREATE_SHARED_LIBRARY " ${APPEND_LDFLAGS}") +string(APPEND CMAKE_CXX_LINK_EXECUTABLE " ${APPEND_LDFLAGS}") + set(configure_warnings) include(CheckPIESupported) diff --git a/cmake/module/FlagsSummary.cmake b/cmake/module/FlagsSummary.cmake index c31cd733420..9a408f715d9 100644 --- a/cmake/module/FlagsSummary.cmake +++ b/cmake/module/FlagsSummary.cmake @@ -29,6 +29,8 @@ function(print_flags_per_config config indent_num) endif() get_target_interface(core_cxx_flags "${config}" core_interface COMPILE_OPTIONS) string(STRIP "${combined_cxx_flags} ${core_cxx_flags}" combined_cxx_flags) + string(STRIP "${combined_cxx_flags} ${APPEND_CPPFLAGS}" combined_cxx_flags) + string(STRIP "${combined_cxx_flags} ${APPEND_CXXFLAGS}" combined_cxx_flags) indent_message("C++ compiler flags ...................." "${combined_cxx_flags}" ${indent_num}) string(STRIP "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${config_uppercase}}" combined_linker_flags) @@ -38,6 +40,7 @@ function(print_flags_per_config config indent_num) if(CMAKE_CXX_LINK_PIE_SUPPORTED) string(JOIN " " combined_linker_flags ${combined_linker_flags} ${CMAKE_CXX_LINK_OPTIONS_PIE}) endif() + string(STRIP "${combined_linker_flags} ${APPEND_LDFLAGS}" combined_linker_flags) indent_message("Linker flags .........................." "${combined_linker_flags}" ${indent_num}) endfunction() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2877cc51f71..18b2b5745d0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -73,6 +73,7 @@ endif() set(CMAKE_EXPORT_COMPILE_COMMANDS OFF) add_subdirectory(secp256k1) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +string(APPEND CMAKE_C_COMPILE_OBJECT " ${APPEND_CPPFLAGS} ${APPEND_CFLAGS}") # Stable, backwards-compatible consensus functionality. add_library(bitcoin_consensus STATIC EXCLUDE_FROM_ALL