Skip to content

Commit 1f60b30

Browse files
committed
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.
1 parent 2b43c45 commit 1f60b30

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ set(CLIENT_VERSION_RC 0)
3030
set(CLIENT_VERSION_IS_RELEASE "false")
3131
set(COPYRIGHT_YEAR "2024")
3232

33+
# During the enabling of the CXX and CXXOBJ languages, we modify
34+
# CMake's compiler/linker invocation strings by appending the content
35+
# of the user-defined `APPEND_*` variables, which allows overriding
36+
# any flag. We also ensure that the APPEND_* flags are considered
37+
# during CMake's tests, which use the `try_compile()` command.
38+
#
39+
# CMake's docs state that the `CMAKE_TRY_COMPILE_PLATFORM_VARIABLES`
40+
# variable "is meant to be set by CMake's platform information modules
41+
# for the current toolchain, or by a toolchain file." We do our best
42+
# to set it before the `project()` command.
43+
set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
44+
CMAKE_CXX_COMPILE_OBJECT
45+
CMAKE_OBJCXX_COMPILE_OBJECT
46+
CMAKE_CXX_LINK_EXECUTABLE
47+
)
48+
3349
project(BitcoinCore
3450
VERSION ${CLIENT_VERSION_MAJOR}.${CLIENT_VERSION_MINOR}.${CLIENT_VERSION_BUILD}
3551
DESCRIPTION "Bitcoin client software"
@@ -189,6 +205,16 @@ cmake_dependent_option(BUILD_FOR_FUZZING "Build for fuzzing. Enabling this will
189205

190206
option(INSTALL_MAN "Install man pages." ON)
191207

208+
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.")
209+
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.")
210+
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.")
211+
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.")
212+
# Appending to this low-level rule variables is the only way to
213+
# guarantee that the flags appear at the end of the command line.
214+
string(APPEND CMAKE_CXX_COMPILE_OBJECT " ${APPEND_CPPFLAGS} ${APPEND_CXXFLAGS}")
215+
string(APPEND CMAKE_CXX_CREATE_SHARED_LIBRARY " ${APPEND_LDFLAGS}")
216+
string(APPEND CMAKE_CXX_LINK_EXECUTABLE " ${APPEND_LDFLAGS}")
217+
192218
set(configure_warnings)
193219

194220
include(CheckPIESupported)

cmake/module/FlagsSummary.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ function(print_flags_per_config config indent_num)
2929
endif()
3030
get_target_interface(core_cxx_flags "${config}" core_interface COMPILE_OPTIONS)
3131
string(STRIP "${combined_cxx_flags} ${core_cxx_flags}" combined_cxx_flags)
32+
string(STRIP "${combined_cxx_flags} ${APPEND_CPPFLAGS}" combined_cxx_flags)
33+
string(STRIP "${combined_cxx_flags} ${APPEND_CXXFLAGS}" combined_cxx_flags)
3234
indent_message("C++ compiler flags ...................." "${combined_cxx_flags}" ${indent_num})
3335

3436
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)
3840
if(CMAKE_CXX_LINK_PIE_SUPPORTED)
3941
string(JOIN " " combined_linker_flags ${combined_linker_flags} ${CMAKE_CXX_LINK_OPTIONS_PIE})
4042
endif()
43+
string(STRIP "${combined_linker_flags} ${APPEND_LDFLAGS}" combined_linker_flags)
4144
indent_message("Linker flags .........................." "${combined_linker_flags}" ${indent_num})
4245
endfunction()
4346

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ endif()
7373
set(CMAKE_EXPORT_COMPILE_COMMANDS OFF)
7474
add_subdirectory(secp256k1)
7575
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
76+
string(APPEND CMAKE_C_COMPILE_OBJECT " ${APPEND_CPPFLAGS} ${APPEND_CFLAGS}")
7677

7778
# Stable, backwards-compatible consensus functionality.
7879
add_library(bitcoin_consensus STATIC EXCLUDE_FROM_ALL

0 commit comments

Comments
 (0)