From 484004b992a68d94d241b75c2049642e7ce96ef1 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Tue, 6 Feb 2024 11:10:11 +0000 Subject: [PATCH] [FIXUP] cmake: Rework compile/link flags summary --- CMakeLists.txt | 25 ++------ cmake/module/FlagsSummary.cmake | 77 ++++++++++++++++++++++++ cmake/module/ProcessConfigurations.cmake | 25 -------- src/CMakeLists.txt | 4 +- 4 files changed, 85 insertions(+), 46 deletions(-) create mode 100644 cmake/module/FlagsSummary.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index f07a159e34fae..7731ec1b95113 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -462,10 +462,6 @@ setup_split_debug_script() add_maintenance_targets() add_windows_deploy_target() -include(GetTargetInterface) -get_target_interface(definitions core_interface COMPILE_DEFINITIONS) -separate_by_configs(definitions) - message("\n") message("Configure summary") message("=================") @@ -494,28 +490,17 @@ message(" test_bitcoin-qt ..................... ${BUILD_GUI_TESTS}") message(" bench_bitcoin ....................... ${BUILD_BENCH}") message(" fuzz binary ......................... ${BUILD_FUZZ_BINARY}") message("") + if(CMAKE_CROSSCOMPILING) set(cross_status "TRUE, for ${CMAKE_SYSTEM_NAME}, ${CMAKE_SYSTEM_PROCESSOR}") else() set(cross_status "FALSE") endif() message("Cross compiling ....................... ${cross_status}") -message("Preprocessor defined macros ........... ${definitions_ALL}") -message("C compiler ............................ ${CMAKE_C_COMPILER}") -list(JOIN DEPENDS_C_COMPILER_FLAGS " " depends_c_flags) -string(STRIP "${CMAKE_C_FLAGS} ${depends_c_flags}" combined_c_flags) -message("CFLAGS ................................ ${combined_c_flags}") -message("C++ compiler .......................... ${CMAKE_CXX_COMPILER}") -list(JOIN DEPENDS_CXX_COMPILER_FLAGS " " depends_cxx_flags) -string(STRIP "${CMAKE_CXX_FLAGS} ${depends_cxx_flags}" combined_cxx_flags) -message("CXXFLAGS .............................. ${combined_cxx_flags}") -get_target_interface(common_compile_options core_interface COMPILE_OPTIONS) -message("Common compile options ................ ${common_compile_options}") -get_target_interface(common_link_options core_interface LINK_OPTIONS) -message("Common link options ................... ${common_link_options}") -message("Linker flags for executables .......... ${CMAKE_EXE_LINKER_FLAGS}") -message("Linker flags for shared libraries ..... ${CMAKE_SHARED_LINKER_FLAGS}") -print_config_flags() +message("C compiler ............................ ${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}, ${CMAKE_C_COMPILER}") +message("C++ compiler .......................... ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}, ${CMAKE_CXX_COMPILER}") +include(FlagsSummary) +flags_summary() message("Attempt to harden executables ......... ${HARDENING}") message("Treat compiler warnings as errors ..... ${WERROR}") message("Use ccache for compiling .............. ${CCACHE}") diff --git a/cmake/module/FlagsSummary.cmake b/cmake/module/FlagsSummary.cmake new file mode 100644 index 0000000000000..e851486cac48f --- /dev/null +++ b/cmake/module/FlagsSummary.cmake @@ -0,0 +1,77 @@ +# Copyright (c) 2024-present The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or https://opensource.org/license/mit/. + +include_guard(GLOBAL) + +function(normalize_preprocessor_definitions definitions) + if(MSVC) + set(flag "/D") + else() + set(flag "-D") + endif() + separate_arguments(${definitions}) + set(result "") + foreach(d IN LISTS ${definitions}) + if(NOT d) + continue() + endif() + if(NOT d MATCHES "${flag}.*") + string(PREPEND d "${flag}") + endif() + string(STRIP "${result} ${d}" result) + endforeach() + set(${definitions} "${result}" PARENT_SCOPE) +endfunction() + +macro(print_flags_per_config config indent dots) + string(TOUPPER "${config}" config_uppercase) + + string(STRIP "${definitions_ALL} ${definitions_${config_uppercase}}" combined_cpp_flags) + normalize_preprocessor_definitions(combined_cpp_flags) + message("${indent}" "Preprocessor defined macros .........${dots} ${combined_cpp_flags}") + + string(STRIP "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${config_uppercase}}" combined_c_flags) + string(STRIP "${combined_c_flags} ${depends_c_flags}" combined_c_flags) + string(STRIP "${combined_c_flags} ${common_compile_options}" combined_c_flags) + message("${indent}" "C flags .............................${dots} ${combined_c_flags}") + + string(STRIP "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${config_uppercase}}" combined_cxx_flags) + string(STRIP "${combined_cxx_flags} ${depends_cxx_flags}" combined_cxx_flags) + string(STRIP "${combined_cxx_flags} ${common_compile_options}" combined_cxx_flags) + message("${indent}" "C++ flags ...........................${dots} ${combined_cxx_flags}") +endmacro() + +function(flags_summary) + include(GetTargetInterface) + get_target_interface(definitions core_interface COMPILE_DEFINITIONS) + include(ProcessConfigurations) + separate_by_configs(definitions) + + list(JOIN DEPENDS_C_COMPILER_FLAGS " " depends_c_flags) + list(JOIN DEPENDS_CXX_COMPILER_FLAGS " " depends_cxx_flags) + get_target_interface(common_compile_options core_interface COMPILE_OPTIONS) + + get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) + if(is_multi_config) + list(JOIN CMAKE_CONFIGURATION_TYPES ", " configs) + message("Available build configurations ........ ${configs}") + if(CMAKE_GENERATOR MATCHES "Visual Studio") + set(default_config "Debug") + else() + list(GET CMAKE_CONFIGURATION_TYPES 0 default_config) + endif() + message("Default build configuration ........... ${default_config}") + foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES) + message("'${config}' build configuration:") + print_flags_per_config(${config} " " "") + endforeach() + else() + message("Build configuration ................... ${CMAKE_BUILD_TYPE}") + print_flags_per_config(${CMAKE_BUILD_TYPE} "" "..") + endif() + + get_target_interface(common_link_options core_interface LINK_OPTIONS) + string(STRIP "${CMAKE_EXE_LINKER_FLAGS} ${common_link_options}" combined_linker_flags) + message("Linker flags .......................... ${combined_linker_flags}") +endfunction() diff --git a/cmake/module/ProcessConfigurations.cmake b/cmake/module/ProcessConfigurations.cmake index c8cd0635220d9..67ea857f0b460 100644 --- a/cmake/module/ProcessConfigurations.cmake +++ b/cmake/module/ProcessConfigurations.cmake @@ -106,28 +106,3 @@ function(separate_by_configs options) 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() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 85ead14bfed1e..64ca2e5ed8cd5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,7 +6,9 @@ include(GNUInstallDirs) include(AddWindowsResources) configure_file(${CMAKE_SOURCE_DIR}/cmake/bitcoin-config.h.in config/bitcoin-config.h @ONLY) -add_compile_definitions(HAVE_CONFIG_H) +target_compile_definitions(core_interface INTERFACE + HAVE_CONFIG_H +) include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) # After the transition from Autotools to CMake, the obj/ subdirectory