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.
This commit is contained in:
Hennadii Stepanov 2024-04-26 13:38:41 +01:00
parent 2b43c45b13
commit 1f60b30df0
No known key found for this signature in database
GPG key ID: 410108112E7EA81F
3 changed files with 30 additions and 0 deletions

View file

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

View file

@ -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()

View file

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