Skip to content

Commit 4e52664

Browse files
committed
[FIXUP] cmake: Rework compile/link flags summary
1 parent 4c4a12c commit 4e52664

File tree

4 files changed

+121
-52
lines changed

4 files changed

+121
-52
lines changed

CMakeLists.txt

+9-23
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ add_library(core_interface INTERFACE)
198198
# include the warn_interface as subtree's warnings are not fixable
199199
# in our tree.
200200
add_library(core_base_interface INTERFACE)
201-
add_library(core_depends_release_interface INTERFACE)
202-
add_library(core_depends_debug_interface INTERFACE)
201+
add_library(core_interface_relwithdebinfo INTERFACE)
202+
add_library(core_interface_debug INTERFACE)
203203
target_link_libraries(core_base_interface INTERFACE
204-
$<$<CONFIG:RelWithDebInfo>:core_depends_release_interface>
205-
$<$<CONFIG:Debug>:core_depends_debug_interface>
204+
$<$<CONFIG:RelWithDebInfo>:core_interface_relwithdebinfo>
205+
$<$<CONFIG:Debug>:core_interface_debug>
206206
)
207207
target_link_libraries(core_interface INTERFACE core_base_interface)
208208

@@ -419,7 +419,7 @@ include(ProcessConfigurations)
419419
set_default_config(RelWithDebInfo)
420420

421421
# Redefine/adjust per-configuration flags.
422-
target_compile_definitions(core_depends_debug_interface INTERFACE
422+
target_compile_definitions(core_interface_debug INTERFACE
423423
DEBUG
424424
DEBUG_LOCKORDER
425425
DEBUG_LOCKCONTENTION
@@ -564,8 +564,8 @@ else()
564564
endif()
565565

566566
target_compile_definitions(core_base_interface INTERFACE ${DEPENDS_COMPILE_DEFINITIONS})
567-
target_compile_definitions(core_depends_release_interface INTERFACE ${DEPENDS_COMPILE_DEFINITIONS_RELWITHDEBINFO})
568-
target_compile_definitions(core_depends_debug_interface INTERFACE ${DEPENDS_COMPILE_DEFINITIONS_DEBUG})
567+
target_compile_definitions(core_interface_relwithdebinfo INTERFACE ${DEPENDS_COMPILE_DEFINITIONS_RELWITHDEBINFO})
568+
target_compile_definitions(core_interface_debug INTERFACE ${DEPENDS_COMPILE_DEFINITIONS_DEBUG})
569569

570570
# If {C,CXX,LD}FLAGS variables are defined during building depends and
571571
# configuring this build system, their content might be duplicated.
@@ -591,12 +591,6 @@ add_maintenance_targets()
591591
add_windows_deploy_target()
592592
add_macos_deploy_target()
593593

594-
595-
include(GetTargetInterface)
596-
get_target_interface(definitions core_interface COMPILE_DEFINITIONS)
597-
get_target_interface(definitions_RELWITHDEBINFO core_depends_release_interface COMPILE_DEFINITIONS)
598-
get_target_interface(definitions_DEBUG core_depends_debug_interface COMPILE_DEFINITIONS)
599-
600594
message("\n")
601595
message("Configure summary")
602596
message("=================")
@@ -642,18 +636,10 @@ else()
642636
set(cross_status "FALSE")
643637
endif()
644638
message("Cross compiling ....................... ${cross_status}")
645-
message("Preprocessor defined macros ........... ${definitions}")
646639
message("C compiler ............................ ${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}, ${CMAKE_C_COMPILER}")
647-
message("CFLAGS ................................ ${CMAKE_C_FLAGS} ${APPEND_CPPFLAGS} ${APPEND_CFLAGS}")
648640
message("C++ compiler .......................... ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}, ${CMAKE_CXX_COMPILER}")
649-
message("CXXFLAGS .............................. ${CMAKE_CXX_FLAGS} ${APPEND_CPPFLAGS} ${APPEND_CXXFLAGS}")
650-
get_target_interface(common_compile_options core_interface COMPILE_OPTIONS)
651-
message("Common compile options ................ ${common_compile_options}")
652-
get_target_interface(common_link_options core_interface LINK_OPTIONS)
653-
message("Common link options ................... ${common_link_options}")
654-
message("Linker flags for executables .......... ${CMAKE_EXE_LINKER_FLAGS} ${APPEND_LDFLAGS}")
655-
message("Linker flags for shared libraries ..... ${CMAKE_SHARED_LINKER_FLAGS} ${APPEND_LDFLAGS}")
656-
print_config_flags()
641+
include(FlagsSummary)
642+
flags_summary()
657643
message("Attempt to harden executables ......... ${ENABLE_HARDENING}")
658644
message("Treat compiler warnings as errors ..... ${WERROR}")
659645
message("Use ccache for compiling .............. ${WITH_CCACHE}")

cmake/module/FlagsSummary.cmake

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright (c) 2024-present The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://opensource.org/license/mit/.
4+
5+
include_guard(GLOBAL)
6+
7+
function(indent_message header content indent_num)
8+
if(indent_num GREATER 0)
9+
string(REPEAT " " ${indent_num} indentation)
10+
string(REPEAT "\\." ${indent_num} dots)
11+
string(REGEX REPLACE "${dots}$" "" header "${header}")
12+
endif()
13+
message("${indentation}" "${header} ${content}")
14+
endfunction()
15+
16+
function(print_flags_per_config config indent_num)
17+
string(TOUPPER "${config}" config_uppercase)
18+
19+
include(GetTargetInterface)
20+
get_target_interface(definitions ${config} core_interface COMPILE_DEFINITIONS)
21+
indent_message("Preprocessor defined macros ..........." "${definitions}" ${indent_num})
22+
23+
string(STRIP "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${config_uppercase}}" combined_c_flags)
24+
if(CMAKE_INTERPROCEDURAL_OPTIMIZATION)
25+
string(JOIN " " combined_c_flags ${combined_c_flags} ${CMAKE_C_COMPILE_OPTIONS_IPO})
26+
endif()
27+
if(CMAKE_POSITION_INDEPENDENT_CODE)
28+
string(JOIN " " combined_c_flags ${combined_c_flags} ${CMAKE_C_COMPILE_OPTIONS_PIC})
29+
endif()
30+
get_target_interface(core_c_flags ${config} core_base_interface COMPILE_OPTIONS)
31+
string(STRIP "${combined_c_flags} ${core_c_flags}" combined_c_flags)
32+
string(STRIP "${combined_c_flags} ${APPEND_CPPFLAGS}" combined_c_flags)
33+
string(STRIP "${combined_c_flags} ${APPEND_CFLAGS}" combined_c_flags)
34+
indent_message("C flags ..............................." "${combined_c_flags}" ${indent_num})
35+
36+
string(STRIP "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${config_uppercase}}" combined_cxx_flags)
37+
if(CMAKE_INTERPROCEDURAL_OPTIMIZATION)
38+
string(JOIN " " combined_cxx_flags ${combined_cxx_flags} ${CMAKE_CXX_COMPILE_OPTIONS_IPO})
39+
endif()
40+
if(CMAKE_POSITION_INDEPENDENT_CODE)
41+
string(JOIN " " combined_cxx_flags ${combined_cxx_flags} ${CMAKE_CXX_COMPILE_OPTIONS_PIC})
42+
endif()
43+
get_target_interface(core_cxx_flags ${config} core_interface COMPILE_OPTIONS)
44+
string(STRIP "${combined_cxx_flags} ${core_cxx_flags}" combined_cxx_flags)
45+
string(STRIP "${combined_cxx_flags} ${APPEND_CPPFLAGS}" combined_cxx_flags)
46+
string(STRIP "${combined_cxx_flags} ${APPEND_CXXFLAGS}" combined_cxx_flags)
47+
indent_message("C++ flags ............................." "${combined_cxx_flags}" ${indent_num})
48+
49+
string(STRIP "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${config_uppercase}}" combined_linker_flags)
50+
if(CMAKE_INTERPROCEDURAL_OPTIMIZATION)
51+
string(JOIN " " combined_linker_flags ${combined_linker_flags} ${CMAKE_CXX_COMPILE_OPTIONS_IPO})
52+
endif()
53+
string(STRIP "${combined_linker_flags} ${CMAKE_EXE_LINKER_FLAGS}" combined_linker_flags)
54+
get_target_interface(common_link_options ${config} core_interface LINK_OPTIONS)
55+
string(STRIP "${combined_linker_flags} ${common_link_options}" combined_linker_flags)
56+
if(CMAKE_CXX_LINK_PIE_SUPPORTED)
57+
string(JOIN " " combined_linker_flags ${combined_linker_flags} ${CMAKE_CXX_LINK_OPTIONS_PIE})
58+
endif()
59+
string(STRIP "${combined_linker_flags} ${APPEND_LDFLAGS}" combined_linker_flags)
60+
indent_message("Linker flags .........................." "${combined_linker_flags}" ${indent_num})
61+
endfunction()
62+
63+
function(flags_summary)
64+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
65+
if(is_multi_config)
66+
list(JOIN CMAKE_CONFIGURATION_TYPES ", " configs)
67+
message("Available build configurations ........ ${configs}")
68+
if(CMAKE_GENERATOR MATCHES "Visual Studio")
69+
set(default_config "Debug")
70+
else()
71+
list(GET CMAKE_CONFIGURATION_TYPES 0 default_config)
72+
endif()
73+
message("Default build configuration ........... ${default_config}")
74+
foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES)
75+
message("\n'${config}' build configuration:")
76+
print_flags_per_config(${config} 2)
77+
endforeach()
78+
message("")
79+
else()
80+
message("CMAKE_BUILD_TYPE ...................... ${CMAKE_BUILD_TYPE}")
81+
print_flags_per_config(${CMAKE_BUILD_TYPE} 0)
82+
endif()
83+
endfunction()

cmake/module/GetTargetInterface.cmake

+29-4
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,46 @@
44

55
include_guard(GLOBAL)
66

7-
# Get target's interface properties recursively.
8-
function(get_target_interface var target property)
7+
# Evaluates config-specific generator expressions in a list.
8+
# Recognizable patterns are:
9+
# - $<$<CONFIG:[config]>:[value]>
10+
# - $<$<NOT:$<CONFIG:[config]>>:[value]>
11+
function(evaluate_generator_expressions list config)
12+
set(input ${${list}})
13+
set(result)
14+
foreach(token IN LISTS input)
15+
if(token MATCHES "\\$<\\$<CONFIG:([^>]+)>:([^>]+)>")
16+
if(CMAKE_MATCH_1 STREQUAL config)
17+
list(APPEND result ${CMAKE_MATCH_2})
18+
endif()
19+
elseif(token MATCHES "\\$<\\$<NOT:\\$<CONFIG:([^>]+)>>:([^>]+)>")
20+
if(NOT CMAKE_MATCH_1 STREQUAL config)
21+
list(APPEND result ${CMAKE_MATCH_2})
22+
endif()
23+
else()
24+
list(APPEND result ${token})
25+
endif()
26+
endforeach()
27+
set(${list} ${result} PARENT_SCOPE)
28+
endfunction()
29+
30+
31+
# Gets target's interface properties recursively.
32+
function(get_target_interface var config target property)
933
get_target_property(result ${target} INTERFACE_${property})
1034
if(result)
11-
string(GENEX_STRIP "${result}" result)
35+
evaluate_generator_expressions(result "${config}")
1236
list(JOIN result " " result)
1337
else()
1438
set(result)
1539
endif()
1640

1741
get_target_property(dependencies ${target} INTERFACE_LINK_LIBRARIES)
1842
if(dependencies)
43+
evaluate_generator_expressions(dependencies "${config}")
1944
foreach(dependency IN LISTS dependencies)
2045
if(TARGET ${dependency})
21-
get_target_interface(dep_result ${dependency} ${property})
46+
get_target_interface(dep_result "${config}" ${dependency} ${property})
2247
string(STRIP "${result} ${dep_result}" result)
2348
endif()
2449
endforeach()

cmake/module/ProcessConfigurations.cmake

-25
Original file line numberDiff line numberDiff line change
@@ -139,28 +139,3 @@ function(replace_cxx_flag_in_config config old_flag new_flag)
139139
FORCE
140140
)
141141
endfunction()
142-
143-
function(print_config_flags)
144-
macro(print_flags config)
145-
string(TOUPPER "${config}" config_uppercase)
146-
message(" - Preprocessor defined macros ........ ${definitions_${config_uppercase}}")
147-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_${config_uppercase}}")
148-
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_${config_uppercase}}")
149-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_${config_uppercase}}")
150-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_${config_uppercase}}")
151-
endmacro()
152-
153-
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
154-
if(is_multi_config)
155-
list(JOIN CMAKE_CONFIGURATION_TYPES " " configs)
156-
message("Available build types (configurations) ${configs}")
157-
foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES)
158-
message("'${config}' build type (configuration):")
159-
print_flags(${config})
160-
endforeach()
161-
else()
162-
message("Build type (configuration):")
163-
message(" - CMAKE_BUILD_TYPE ................... ${CMAKE_BUILD_TYPE}")
164-
print_flags(${CMAKE_BUILD_TYPE})
165-
endif()
166-
endfunction()

0 commit comments

Comments
 (0)