Skip to content

Commit 384b398

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

File tree

4 files changed

+163
-93
lines changed

4 files changed

+163
-93
lines changed

CMakeLists.txt

+26-40
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

@@ -363,11 +363,6 @@ endif()
363363

364364
include(cmake/introspection.cmake)
365365

366-
include(cmake/crc32c.cmake)
367-
include(cmake/leveldb.cmake)
368-
include(cmake/minisketch.cmake)
369-
include(cmake/secp256k1.cmake)
370-
371366
add_library(warn_interface INTERFACE)
372367
target_link_libraries(core_interface INTERFACE warn_interface)
373368
if(MSVC)
@@ -419,7 +414,7 @@ include(ProcessConfigurations)
419414
set_default_config(RelWithDebInfo)
420415

421416
# Redefine/adjust per-configuration flags.
422-
target_compile_definitions(core_depends_debug_interface INTERFACE
417+
target_compile_definitions(core_interface_debug INTERFACE
423418
DEBUG
424419
DEBUG_LOCKORDER
425420
DEBUG_LOCKCONTENTION
@@ -564,8 +559,8 @@ else()
564559
endif()
565560

566561
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})
562+
target_compile_definitions(core_interface_relwithdebinfo INTERFACE ${DEPENDS_COMPILE_DEFINITIONS_RELWITHDEBINFO})
563+
target_compile_definitions(core_interface_debug INTERFACE ${DEPENDS_COMPILE_DEFINITIONS_DEBUG})
569564

570565
# If {C,CXX,LD}FLAGS variables are defined during building depends and
571566
# configuring this build system, their content might be duplicated.
@@ -579,23 +574,23 @@ if(DEFINED ENV{LDFLAGS})
579574
deduplicate_flags(CMAKE_EXE_LINKER_FLAGS)
580575
endif()
581576

582-
add_subdirectory(src)
583-
add_subdirectory(test)
584-
add_subdirectory(doc)
585-
586-
include(cmake/tests.cmake)
587-
588-
include(Maintenance)
589-
setup_split_debug_script()
590-
add_maintenance_targets()
591-
add_windows_deploy_target()
592-
add_macos_deploy_target()
593-
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)
577+
if(NOT INTERNAL_BUILD)
578+
include(cmake/crc32c.cmake)
579+
include(cmake/leveldb.cmake)
580+
include(cmake/minisketch.cmake)
581+
include(cmake/secp256k1.cmake)
582+
add_subdirectory(src)
583+
add_subdirectory(test)
584+
add_subdirectory(doc)
585+
586+
include(cmake/tests.cmake)
587+
588+
include(Maintenance)
589+
setup_split_debug_script()
590+
add_maintenance_targets()
591+
add_windows_deploy_target()
592+
add_macos_deploy_target()
593+
endif()
599594

600595
message("\n")
601596
message("Configure summary")
@@ -642,18 +637,9 @@ else()
642637
set(cross_status "FALSE")
643638
endif()
644639
message("Cross compiling ....................... ${cross_status}")
645-
message("Preprocessor defined macros ........... ${definitions}")
646-
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

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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(get_flags_unix_makefiles preprocessor_defines_variable compiler_flags_variable linker_flags_variable)
8+
file(STRINGS ${CMAKE_BINARY_DIR}/${internal_binary_dir}/CMakeFiles/mock.dir/flags.make preprocessor_defines REGEX "^CXX_DEFINES( +)=")
9+
string(REGEX REPLACE "^CXX_DEFINES( +)=( +)" "" preprocessor_defines "${preprocessor_defines}")
10+
set(${preprocessor_defines_variable} "${preprocessor_defines}" PARENT_SCOPE)
11+
12+
file(STRINGS ${CMAKE_BINARY_DIR}/${internal_binary_dir}/CMakeFiles/mock.dir/flags.make compiler_flags REGEX "^CXX_FLAGS( +)=")
13+
string(REGEX REPLACE "^CXX_FLAGS( +)=( +)" "" compiler_flags "${compiler_flags}")
14+
string(STRIP "${compiler_flags} ${APPEND_CPPFLAGS}" compiler_flags)
15+
string(STRIP "${compiler_flags} ${APPEND_CXXFLAGS}" compiler_flags)
16+
set(${compiler_flags_variable} "${compiler_flags}" PARENT_SCOPE)
17+
18+
file(STRINGS ${CMAKE_BINARY_DIR}/${internal_binary_dir}/CMakeFiles/mock.dir/link.txt linker_flags LIMIT_COUNT 1)
19+
string(REPLACE "${CMAKE_CXX_COMPILER} " "" linker_flags "${linker_flags}")
20+
string(REPLACE " CMakeFiles/mock.dir/mock.cpp.o -o mock" "" linker_flags "${linker_flags}")
21+
set(${linker_flags_variable} "${linker_flags}" PARENT_SCOPE)
22+
endfunction()
23+
24+
function(get_flags_ninja preprocessor_defines_variable compiler_flags_variable linker_flags_variable config)
25+
if(config)
26+
cmake_path(APPEND CMAKE_BINARY_DIR ${internal_binary_dir} CMakeFiles impl-${config}.ninja OUTPUT_VARIABLE build_file)
27+
set(object_build_statement_re "^build CMakeFiles/mock\\.dir/${config}/mock\\.cpp\\.o: ")
28+
set(link_build_statement_re "^build ${config}/mock: ")
29+
else()
30+
cmake_path(APPEND CMAKE_BINARY_DIR ${internal_binary_dir} build.ninja OUTPUT_VARIABLE build_file)
31+
set(object_build_statement_re "^build CMakeFiles/mock\\.dir/mock\\.cpp\\.o: ")
32+
set(link_build_statement_re "^build mock: ")
33+
endif()
34+
file(STRINGS ${build_file} build_content)
35+
36+
set(is_object_build_statement FALSE)
37+
foreach(line IN LISTS build_content)
38+
if(is_object_build_statement)
39+
if(line MATCHES "^( +)DEFINES( +)=")
40+
string(REGEX REPLACE "^( +)DEFINES( +)=( +)" "" preprocessor_defines "${line}")
41+
elseif(line MATCHES "^( +)FLAGS( +)=")
42+
string(REGEX REPLACE "^( +)FLAGS( +)=( +)" "" compiler_flags "${line}")
43+
elseif(line STREQUAL "")
44+
break()
45+
endif()
46+
elseif(line MATCHES ${object_build_statement_re})
47+
set(is_object_build_statement TRUE)
48+
endif()
49+
endforeach()
50+
set(${preprocessor_defines_variable} "${preprocessor_defines}" PARENT_SCOPE)
51+
string(STRIP "${compiler_flags} ${APPEND_CPPFLAGS}" compiler_flags)
52+
string(STRIP "${compiler_flags} ${APPEND_CXXFLAGS}" compiler_flags)
53+
set(${compiler_flags_variable} "${compiler_flags}" PARENT_SCOPE)
54+
55+
set(is_link_build_statement FALSE)
56+
foreach(line IN LISTS build_content)
57+
if(is_link_build_statement)
58+
if(line MATCHES "^( +)FLAGS( +)=")
59+
string(REGEX REPLACE "^( +)FLAGS( +)=( +)" "" flags "${line}")
60+
elseif(line MATCHES "^( +)LINK_FLAGS( +)=")
61+
string(REGEX REPLACE "^( +)LINK_FLAGS( +)=( +)" "" link_flags "${line}")
62+
elseif(line STREQUAL "")
63+
break()
64+
endif()
65+
elseif(line MATCHES ${link_build_statement_re})
66+
set(is_link_build_statement TRUE)
67+
endif()
68+
endforeach()
69+
string(STRIP "${flags} ${link_flags}" linker_flags)
70+
string(STRIP "${linker_flags} ${APPEND_LDFLAGS}" linker_flags)
71+
set(${linker_flags_variable} "${linker_flags}" PARENT_SCOPE)
72+
endfunction()
73+
74+
function(flags_summary)
75+
if(INTERNAL_BUILD)
76+
file(WRITE ${PROJECT_BINARY_DIR}/mock.cpp "")
77+
add_executable(mock ${PROJECT_BINARY_DIR}/mock.cpp)
78+
target_link_libraries(mock PRIVATE core_interface)
79+
else()
80+
file(WRITE ${CMAKE_BINARY_DIR}/internal_build_cache "set(INTERNAL_BUILD ON CACHE BOOL \"\" FORCE)\n")
81+
get_cmake_property(cache_variable_names CACHE_VARIABLES)
82+
foreach (v ${cache_variable_names})
83+
get_property(type CACHE ${v} PROPERTY TYPE)
84+
if(NOT type MATCHES "^(INTERNAL|STATIC)$")
85+
file(APPEND ${CMAKE_BINARY_DIR}/internal_build_cache "set(${v} \"$CACHE{${v}}\" CACHE ${type} \"\" FORCE)\n")
86+
endif()
87+
endforeach()
88+
89+
string(RANDOM internal_binary_dir)
90+
91+
# DEBUG ONLY
92+
set(internal_binary_dir "internal")
93+
94+
execute_process(
95+
COMMAND ${CMAKE_COMMAND} -S${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}/${internal_binary_dir} -C${CMAKE_BINARY_DIR}/internal_build_cache -G${CMAKE_GENERATOR}
96+
OUTPUT_QUIET
97+
ERROR_QUIET
98+
)
99+
100+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
101+
if(is_multi_config)
102+
list(JOIN CMAKE_CONFIGURATION_TYPES ", " configs)
103+
message("Available build configurations ........ ${configs}")
104+
if(CMAKE_GENERATOR MATCHES "Visual Studio")
105+
set(default_config "Debug")
106+
else()
107+
list(GET CMAKE_CONFIGURATION_TYPES 0 default_config)
108+
endif()
109+
message("Default build configuration ........... ${default_config}")
110+
foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES)
111+
message("")
112+
message("'${config}' build configuration:")
113+
if(CMAKE_GENERATOR STREQUAL "Ninja Multi-Config")
114+
get_flags_ninja(preprocessor_defines compiler_flags linker_flags ${config})
115+
else()
116+
message("Printing build options is not supported for the generator \"${CMAKE_GENERATOR}\"")
117+
endif()
118+
message(" Preprocessor defined macros ......... ${preprocessor_defines}")
119+
message(" C++ flags ........................... ${compiler_flags}")
120+
message(" Linker flags ........................ ${linker_flags}")
121+
endforeach()
122+
message("")
123+
else()
124+
message("CMAKE_BUILD_TYPE ...................... ${CMAKE_BUILD_TYPE}")
125+
if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
126+
get_flags_unix_makefiles(preprocessor_defines compiler_flags linker_flags)
127+
elseif(CMAKE_GENERATOR STREQUAL "Ninja")
128+
get_flags_ninja(preprocessor_defines compiler_flags linker_flags "")
129+
else()
130+
message("Printing build options is not supported for the generator \"${CMAKE_GENERATOR}\"")
131+
endif()
132+
message("Preprocessor defined macros ........... ${preprocessor_defines}")
133+
message("C++ flags ............................. ${compiler_flags}")
134+
message("Linker flags .......................... ${linker_flags}")
135+
endif()
136+
endif()
137+
endfunction()

cmake/module/GetTargetInterface.cmake

-28
This file was deleted.

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)