forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #18: build: Add CMake-based build system (8 of N)
a65da0d cmake: Redefine configuration flags (Hennadii Stepanov) 1a1dda5 [FIXUP] Evaluate flags set in depends _after_ config-specific flags (Hennadii Stepanov) 482e844 cmake: Warn about not encapsulated build properties (Hennadii Stepanov) 3736470 cmake: Add platform-specific flags (Hennadii Stepanov) e0621e9 cmake: Add `TryAppendLinkerFlag` module (Hennadii Stepanov) ae430cf cmake: Add `TryAppendCXXFlags` module (Hennadii Stepanov) 7903bd5 [FIXUP] Encapsulate common build flags into `core` interface library (Hennadii Stepanov) Pull request description: The parent PR: bitcoin#25797. The previous PRs in the staging branch: #5, #6, #7, #10, #13, #15, #17, #19. --- What is NEW: - functions for checking compiler and linker flags - managing flags for different build types (configurations) EXAMPLES of configuration output on Ubuntu 22.04: - for a single-config generator: ``` $ cmake .. -G "Unix Makefiles" ... Cross compiling ....................... FALSE Preprocessor defined macros ........... C compiler ............................ /usr/bin/cc CFLAGS ................................ C++ compiler .......................... /usr/bin/c++ CXXFLAGS .............................. Common compile options ................ Common link options ................... Linker flags for executables .......... Linker flags for shared libraries ..... Build type (configuration): - CMAKE_BUILD_TYPE ................... RelWithDebInfo - Preprocessor defined macros ........ - CFLAGS ............................. -O2 -g - CXXFLAGS ........................... -O2 -g - LDFLAGS for executables ............ - LDFLAGS for shared libraries ....... Use assembly routines ................. ON Use ccache for compiling .............. ON ... ``` - for a multi-config generator: ``` $ cmake .. -G "Ninja Multi-Config" ... Cross compiling ....................... FALSE Preprocessor defined macros ........... C compiler ............................ /usr/bin/cc CFLAGS ................................ C++ compiler .......................... /usr/bin/c++ CXXFLAGS .............................. Common compile options ................ Common link options ................... Linker flags for executables .......... Linker flags for shared libraries ..... Available build types (configurations) RelWithDebInfo Debug Release 'RelWithDebInfo' build type (configuration): - Preprocessor defined macros ........ - CFLAGS ............................. -O2 -g - CXXFLAGS ........................... -O2 -g - LDFLAGS for executables ............ - LDFLAGS for shared libraries ....... 'Debug' build type (configuration): - Preprocessor defined macros ........ DEBUG DEBUG_LOCKORDER DEBUG_LOCKCONTENTION RPC_DOC_CHECK ABORT_ON_FAILED_ASSUME - CFLAGS ............................. -O0 -g3 - CXXFLAGS ........................... -O0 -g3 -ftrapv - LDFLAGS for executables ............ - LDFLAGS for shared libraries ....... 'Release' build type (configuration): - Preprocessor defined macros ........ - CFLAGS ............................. -O2 - CXXFLAGS ........................... -O2 - LDFLAGS for executables ............ - LDFLAGS for shared libraries ....... Use assembly routines ................. ON Use ccache for compiling .............. ON ... ``` - cross-compiling for Windows: ``` $ make -C depends HOST=x86_64-w64-mingw32 DEBUG=1 NO_QT=1 $ cmake -B build --toolchain depends/x86_64-w64-mingw32/share/toolchain.cmake -DCMAKE_BUILD_TYPE=Debug ... Cross compiling ....................... TRUE, for Windows, x86_64 Preprocessor defined macros ........... _WIN32_WINNT=0x0601 _WIN32_IE=0x0501 WIN32_LEAN_AND_MEAN NOMINMAX WIN32 _WINDOWS _MT _GLIBCXX_DEBUG _GLIBCXX_DEBUG_PEDANTIC C compiler ............................ /usr/bin/x86_64-w64-mingw32-gcc CFLAGS ................................ -pipe -std=c11 -O1 C++ compiler .......................... /usr/bin/x86_64-w64-mingw32-g++-posix CXXFLAGS .............................. -pipe -std=c++17 -O1 Common compile options ................ Common link options ................... -Wl,--major-subsystem-version,6 -Wl,--minor-subsystem-version,1 Linker flags for executables .......... -static Linker flags for shared libraries ..... Build type (configuration): - CMAKE_BUILD_TYPE ................... Debug - Preprocessor defined macros ........ DEBUG DEBUG_LOCKORDER DEBUG_LOCKCONTENTION RPC_DOC_CHECK ABORT_ON_FAILED_ASSUME - CFLAGS ............................. -O0 -g3 - CXXFLAGS ........................... -O0 -g3 -ftrapv - LDFLAGS for executables ............ - LDFLAGS for shared libraries ....... Use assembly routines ................. ON Use ccache for compiling .............. ON ... ``` **A cross-project note.** The `ProcessConfigurations.cmake` is based on the same module that was suggested in bitcoin-core/secp256k1#1291. So, cross-reviewing is welcome :) ACKs for top commit: theuni: ACK a65da0d to keep this moving. This has been sitting for too long :( Tree-SHA512: 57c5e91ddf9675c6a2b56c0cb70fd3f045af8076bee74c49390de38b8d514e130d2086fde6d83d2d1278b437d0a10cc721f0aa44934698110aeadb3a1aef9e64
- Loading branch information
Showing
17 changed files
with
470 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# Copyright (c) 2023-present The Bitcoin Core developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file COPYING or https://opensource.org/license/mit/. | ||
|
||
function(get_all_configs output) | ||
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) | ||
if(is_multi_config) | ||
set(all_configs ${CMAKE_CONFIGURATION_TYPES}) | ||
else() | ||
get_property(all_configs CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS) | ||
if(NOT all_configs) | ||
# See https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#default-and-custom-configurations | ||
set(all_configs Debug Release RelWithDebInfo MinSizeRel) | ||
endif() | ||
endif() | ||
set(${output} "${all_configs}" PARENT_SCOPE) | ||
endfunction() | ||
|
||
|
||
#[=[ | ||
Set the default build configuration. | ||
|
||
See: https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#build-configurations. | ||
|
||
On single-configuration generators, this function sets the CMAKE_BUILD_TYPE variable to | ||
the default build configuration, which can be overridden by the user at configure time if needed. | ||
|
||
On multi-configuration generators, this function rearranges the CMAKE_CONFIGURATION_TYPES list, | ||
ensuring that the default build configuration appears first while maintaining the order of the | ||
remaining configurations. The user can choose a build configuration at build time. | ||
]=] | ||
function(set_default_config config) | ||
get_all_configs(all_configs) | ||
if(NOT ${config} IN_LIST all_configs) | ||
message(FATAL_ERROR "The default config is \"${config}\", but must be one of ${all_configs}.") | ||
endif() | ||
|
||
list(REMOVE_ITEM all_configs ${config}) | ||
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.15) | ||
list(PREPEND all_configs ${config}) | ||
else() | ||
set(all_configs ${config} ${all_configs}) | ||
endif() | ||
|
||
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) | ||
if(is_multi_config) | ||
get_property(help_string CACHE CMAKE_CONFIGURATION_TYPES PROPERTY HELPSTRING) | ||
set(CMAKE_CONFIGURATION_TYPES "${all_configs}" CACHE STRING "${help_string}" FORCE) | ||
else() | ||
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY | ||
STRINGS "${all_configs}" | ||
) | ||
if(NOT CMAKE_BUILD_TYPE) | ||
message(STATUS "Setting build type to \"${config}\" as none was specified") | ||
get_property(help_string CACHE CMAKE_BUILD_TYPE PROPERTY HELPSTRING) | ||
set(CMAKE_BUILD_TYPE "${config}" CACHE STRING "${help_string}" FORCE) | ||
endif() | ||
endif() | ||
endfunction() | ||
|
||
function(remove_c_flag_from_all_configs flag) | ||
get_all_configs(all_configs) | ||
foreach(config IN LISTS all_configs) | ||
string(TOUPPER "${config}" config_uppercase) | ||
set(flags "${CMAKE_C_FLAGS_${config_uppercase}}") | ||
separate_arguments(flags) | ||
list(FILTER flags EXCLUDE REGEX "${flag}") | ||
list(JOIN flags " " new_flags) | ||
set(CMAKE_C_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE) | ||
endforeach() | ||
endfunction() | ||
|
||
function(remove_cxx_flag_from_all_configs flag) | ||
get_all_configs(all_configs) | ||
foreach(config IN LISTS all_configs) | ||
string(TOUPPER "${config}" config_uppercase) | ||
set(flags "${CMAKE_CXX_FLAGS_${config_uppercase}}") | ||
separate_arguments(flags) | ||
list(FILTER flags EXCLUDE REGEX "${flag}") | ||
list(JOIN flags " " new_flags) | ||
set(CMAKE_CXX_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE) | ||
endforeach() | ||
endfunction() | ||
|
||
function(replace_c_flag_in_config config old_flag new_flag) | ||
string(TOUPPER "${config}" config_uppercase) | ||
string(REGEX REPLACE "(^| )${old_flag}( |$)" "\\1${new_flag}\\2" new_flags "${CMAKE_C_FLAGS_${config_uppercase}}") | ||
set(CMAKE_C_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE) | ||
endfunction() | ||
|
||
function(replace_cxx_flag_in_config config old_flag new_flag) | ||
string(TOUPPER "${config}" config_uppercase) | ||
string(REGEX REPLACE "(^| )${old_flag}( |$)" "\\1${new_flag}\\2" new_flags "${CMAKE_CXX_FLAGS_${config_uppercase}}") | ||
set(CMAKE_CXX_FLAGS_${config_uppercase} "${new_flags}" PARENT_SCOPE) | ||
endfunction() | ||
|
||
function(separate_by_configs options) | ||
list(JOIN ${options} " " ${options}_ALL) | ||
string(GENEX_STRIP "${${options}_ALL}" ${options}_ALL) | ||
set(${options}_ALL "${${options}_ALL}" PARENT_SCOPE) | ||
|
||
get_all_configs(all_configs) | ||
foreach(config IN LISTS all_configs) | ||
string(REGEX MATCHALL "\\$<\\$<CONFIG:${config}>[^\n]*>" match "${${options}}") | ||
list(JOIN match " " match) | ||
string(REPLACE "\$<\$<CONFIG:${config}>:" "" match "${match}") | ||
string(REPLACE ">" "" match "${match}") | ||
string(TOUPPER "${config}" conf_upper) | ||
set(${options}_${conf_upper} "${match}" PARENT_SCOPE) | ||
endforeach() | ||
endfunction() | ||
|
||
function(print_config_flags) | ||
macro(print_flags config) | ||
string(TOUPPER "${config}" config_uppercase) | ||
message(" - Preprocessor defined macros ........ ${definitions_${config_uppercase}}") | ||
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_${config_uppercase}}") | ||
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_${config_uppercase}}") | ||
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_${config_uppercase}}") | ||
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_${config_uppercase}}") | ||
endmacro() | ||
|
||
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) | ||
if(is_multi_config) | ||
list(JOIN CMAKE_CONFIGURATION_TYPES " " configs) | ||
message("Available build types (configurations) ${configs}") | ||
foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES) | ||
message("'${config}' build type (configuration):") | ||
print_flags(${config}) | ||
endforeach() | ||
else() | ||
message("Build type (configuration):") | ||
message(" - CMAKE_BUILD_TYPE ................... ${CMAKE_BUILD_TYPE}") | ||
print_flags(${CMAKE_BUILD_TYPE}) | ||
endif() | ||
endfunction() |
Oops, something went wrong.