Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] cmake: Rework compile/link flags summary -- an alternative implemenation #218

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 29 additions & 45 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ add_library(core_interface INTERFACE)
# include the warn_interface as subtree's warnings are not fixable
# in our tree.
add_library(core_base_interface INTERFACE)
add_library(core_depends_release_interface INTERFACE)
add_library(core_depends_debug_interface INTERFACE)
add_library(core_interface_relwithdebinfo INTERFACE)
add_library(core_interface_debug INTERFACE)
target_link_libraries(core_base_interface INTERFACE
$<$<CONFIG:RelWithDebInfo>:core_depends_release_interface>
$<$<CONFIG:Debug>:core_depends_debug_interface>
$<$<CONFIG:RelWithDebInfo>:core_interface_relwithdebinfo>
$<$<CONFIG:Debug>:core_interface_debug>
)
target_link_libraries(core_interface INTERFACE core_base_interface)

Expand Down Expand Up @@ -321,13 +321,11 @@ endif()
include(AddThreadsIfNeeded)
add_threads_if_needed()

add_library(sanitizing_interface INTERFACE)
target_link_libraries(core_base_interface INTERFACE sanitizing_interface)
if(SANITIZERS)
# First check if the compiler accepts flags. If an incompatible pair like
# -fsanitize=address,thread is used here, this check will fail. This will also
# fail if a bad argument is passed, e.g. -fsanitize=undfeined
try_append_cxx_flags("-fsanitize=${SANITIZERS}" TARGET sanitizing_interface
try_append_cxx_flags("-fsanitize=${SANITIZERS}" TARGET core_base_interface
RESULT_VAR cxx_supports_sanitizers
SKIP_LINK
)
Expand All @@ -354,7 +352,7 @@ if(SANITIZERS)
message(FATAL_ERROR "Linker did not accept requested flags, you are missing required libraries.")
endif()
endif()
target_link_options(sanitizing_interface INTERFACE ${SANITIZER_LDFLAGS})
target_link_options(core_base_interface INTERFACE ${SANITIZER_LDFLAGS})

include(AddBoostIfNeeded)
add_boost_if_needed()
Expand All @@ -365,11 +363,6 @@ endif()

include(cmake/introspection.cmake)

include(cmake/crc32c.cmake)
include(cmake/leveldb.cmake)
include(cmake/minisketch.cmake)
include(cmake/secp256k1.cmake)

add_library(warn_interface INTERFACE)
target_link_libraries(core_interface INTERFACE warn_interface)
if(MSVC)
Expand Down Expand Up @@ -421,7 +414,7 @@ include(ProcessConfigurations)
set_default_config(RelWithDebInfo)

# Redefine/adjust per-configuration flags.
target_compile_definitions(core_depends_debug_interface INTERFACE
target_compile_definitions(core_interface_debug INTERFACE
DEBUG
DEBUG_LOCKORDER
DEBUG_LOCKCONTENTION
Expand Down Expand Up @@ -497,7 +490,7 @@ try_append_cxx_flags("-fstack-reuse=none" TARGET core_base_interface)

if(ENABLE_HARDENING)
add_library(hardening_interface INTERFACE)
target_link_libraries(core_base_interface INTERFACE hardening_interface)
target_link_libraries(core_interface INTERFACE hardening_interface)
if(MSVC)
try_append_linker_flag("/DYNAMICBASE" TARGET hardening_interface)
try_append_linker_flag("/HIGHENTROPYVA" TARGET hardening_interface)
Expand Down Expand Up @@ -566,8 +559,8 @@ else()
endif()

target_compile_definitions(core_base_interface INTERFACE ${DEPENDS_COMPILE_DEFINITIONS})
target_compile_definitions(core_depends_release_interface INTERFACE ${DEPENDS_COMPILE_DEFINITIONS_RELWITHDEBINFO})
target_compile_definitions(core_depends_debug_interface INTERFACE ${DEPENDS_COMPILE_DEFINITIONS_DEBUG})
target_compile_definitions(core_interface_relwithdebinfo INTERFACE ${DEPENDS_COMPILE_DEFINITIONS_RELWITHDEBINFO})
target_compile_definitions(core_interface_debug INTERFACE ${DEPENDS_COMPILE_DEFINITIONS_DEBUG})

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

add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(doc)

include(cmake/tests.cmake)

include(Maintenance)
setup_split_debug_script()
add_maintenance_targets()
add_windows_deploy_target()
add_macos_deploy_target()


include(GetTargetInterface)
get_target_interface(definitions core_interface COMPILE_DEFINITIONS)
get_target_interface(definitions_RELWITHDEBINFO core_depends_release_interface COMPILE_DEFINITIONS)
get_target_interface(definitions_DEBUG core_depends_debug_interface COMPILE_DEFINITIONS)
if(NOT INTERNAL_BUILD)
include(cmake/crc32c.cmake)
include(cmake/leveldb.cmake)
include(cmake/minisketch.cmake)
include(cmake/secp256k1.cmake)
add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(doc)

include(cmake/tests.cmake)

include(Maintenance)
setup_split_debug_script()
add_maintenance_targets()
add_windows_deploy_target()
add_macos_deploy_target()
endif()

message("\n")
message("Configure summary")
Expand Down Expand Up @@ -644,18 +637,9 @@ else()
set(cross_status "FALSE")
endif()
message("Cross compiling ....................... ${cross_status}")
message("Preprocessor defined macros ........... ${definitions}")
message("C compiler ............................ ${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}, ${CMAKE_C_COMPILER}")
message("CFLAGS ................................ ${CMAKE_C_FLAGS} ${APPEND_CPPFLAGS} ${APPEND_CFLAGS}")
message("C++ compiler .......................... ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}, ${CMAKE_CXX_COMPILER}")
message("CXXFLAGS .............................. ${CMAKE_CXX_FLAGS} ${APPEND_CPPFLAGS} ${APPEND_CXXFLAGS}")
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} ${APPEND_LDFLAGS}")
message("Linker flags for shared libraries ..... ${CMAKE_SHARED_LINKER_FLAGS} ${APPEND_LDFLAGS}")
print_config_flags()
include(FlagsSummary)
flags_summary()
message("Attempt to harden executables ......... ${ENABLE_HARDENING}")
message("Treat compiler warnings as errors ..... ${WERROR}")
message("Use ccache for compiling .............. ${WITH_CCACHE}")
Expand Down
5 changes: 4 additions & 1 deletion cmake/crc32c.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ target_compile_definitions(crc32c_common INTERFACE
HAVE_STRONG_GETAUXVAL=$<BOOL:${HAVE_STRONG_GETAUXVAL}>
BYTE_ORDER_BIG_ENDIAN=$<STREQUAL:${CMAKE_CXX_BYTE_ORDER},BIG_ENDIAN>
)
target_link_libraries(crc32c_common INTERFACE core_base_interface)
target_link_libraries(crc32c_common INTERFACE
core_base_interface
hardening_interface
)

add_library(crc32c STATIC EXCLUDE_FROM_ALL
${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c.cc
Expand Down
1 change: 1 addition & 0 deletions cmake/leveldb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ endif()

target_link_libraries(leveldb PRIVATE
core_base_interface
hardening_interface
nowarn_leveldb_interface
crc32c
)
Expand Down
2 changes: 2 additions & 0 deletions cmake/minisketch.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ if(HAVE_CLMUL)
target_link_libraries(minisketch_clmul
PRIVATE
core_base_interface
hardening_interface
minisketch_common
)
set_target_properties(minisketch_clmul PROPERTIES
Expand Down Expand Up @@ -82,6 +83,7 @@ target_include_directories(minisketch
target_link_libraries(minisketch
PRIVATE
core_base_interface
hardening_interface
minisketch_common
$<TARGET_NAME_IF_EXISTS:minisketch_clmul>
)
Expand Down
137 changes: 137 additions & 0 deletions cmake/module/FlagsSummary.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# 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(get_flags_unix_makefiles preprocessor_defines_variable compiler_flags_variable linker_flags_variable)
file(STRINGS ${CMAKE_BINARY_DIR}/${internal_binary_dir}/CMakeFiles/mock.dir/flags.make preprocessor_defines REGEX "^CXX_DEFINES( +)=")
string(REGEX REPLACE "^CXX_DEFINES( +)=( +)" "" preprocessor_defines "${preprocessor_defines}")
set(${preprocessor_defines_variable} "${preprocessor_defines}" PARENT_SCOPE)

file(STRINGS ${CMAKE_BINARY_DIR}/${internal_binary_dir}/CMakeFiles/mock.dir/flags.make compiler_flags REGEX "^CXX_FLAGS( +)=")
string(REGEX REPLACE "^CXX_FLAGS( +)=( +)" "" compiler_flags "${compiler_flags}")
string(STRIP "${compiler_flags} ${APPEND_CPPFLAGS}" compiler_flags)
string(STRIP "${compiler_flags} ${APPEND_CXXFLAGS}" compiler_flags)
set(${compiler_flags_variable} "${compiler_flags}" PARENT_SCOPE)

file(STRINGS ${CMAKE_BINARY_DIR}/${internal_binary_dir}/CMakeFiles/mock.dir/link.txt linker_flags LIMIT_COUNT 1)
string(REPLACE "${CMAKE_CXX_COMPILER} " "" linker_flags "${linker_flags}")
string(REPLACE " CMakeFiles/mock.dir/mock.cpp.o -o mock" "" linker_flags "${linker_flags}")
set(${linker_flags_variable} "${linker_flags}" PARENT_SCOPE)
endfunction()

function(get_flags_ninja preprocessor_defines_variable compiler_flags_variable linker_flags_variable config)
if(config)
cmake_path(APPEND CMAKE_BINARY_DIR ${internal_binary_dir} CMakeFiles impl-${config}.ninja OUTPUT_VARIABLE build_file)
set(object_build_statement_re "^build CMakeFiles/mock\\.dir/${config}/mock\\.cpp\\.o: ")
set(link_build_statement_re "^build ${config}/mock: ")
else()
cmake_path(APPEND CMAKE_BINARY_DIR ${internal_binary_dir} build.ninja OUTPUT_VARIABLE build_file)
set(object_build_statement_re "^build CMakeFiles/mock\\.dir/mock\\.cpp\\.o: ")
set(link_build_statement_re "^build mock: ")
endif()
file(STRINGS ${build_file} build_content)

set(is_object_build_statement FALSE)
foreach(line IN LISTS build_content)
if(is_object_build_statement)
if(line MATCHES "^( +)DEFINES( +)=")
string(REGEX REPLACE "^( +)DEFINES( +)=( +)" "" preprocessor_defines "${line}")
elseif(line MATCHES "^( +)FLAGS( +)=")
string(REGEX REPLACE "^( +)FLAGS( +)=( +)" "" compiler_flags "${line}")
elseif(line STREQUAL "")
break()
endif()
elseif(line MATCHES ${object_build_statement_re})
set(is_object_build_statement TRUE)
endif()
endforeach()
set(${preprocessor_defines_variable} "${preprocessor_defines}" PARENT_SCOPE)
string(STRIP "${compiler_flags} ${APPEND_CPPFLAGS}" compiler_flags)
string(STRIP "${compiler_flags} ${APPEND_CXXFLAGS}" compiler_flags)
set(${compiler_flags_variable} "${compiler_flags}" PARENT_SCOPE)

set(is_link_build_statement FALSE)
foreach(line IN LISTS build_content)
if(is_link_build_statement)
if(line MATCHES "^( +)FLAGS( +)=")
string(REGEX REPLACE "^( +)FLAGS( +)=( +)" "" flags "${line}")
elseif(line MATCHES "^( +)LINK_FLAGS( +)=")
string(REGEX REPLACE "^( +)LINK_FLAGS( +)=( +)" "" link_flags "${line}")
elseif(line STREQUAL "")
break()
endif()
elseif(line MATCHES ${link_build_statement_re})
set(is_link_build_statement TRUE)
endif()
endforeach()
string(STRIP "${flags} ${link_flags}" linker_flags)
string(STRIP "${linker_flags} ${APPEND_LDFLAGS}" linker_flags)
set(${linker_flags_variable} "${linker_flags}" PARENT_SCOPE)
endfunction()

function(flags_summary)
if(INTERNAL_BUILD)
file(WRITE ${PROJECT_BINARY_DIR}/mock.cpp "")
add_executable(mock ${PROJECT_BINARY_DIR}/mock.cpp)
target_link_libraries(mock PRIVATE core_interface)
else()
file(WRITE ${CMAKE_BINARY_DIR}/internal_build_cache "set(INTERNAL_BUILD ON CACHE BOOL \"\" FORCE)\n")
get_cmake_property(cache_variable_names CACHE_VARIABLES)
foreach (v ${cache_variable_names})
get_property(type CACHE ${v} PROPERTY TYPE)
if(NOT type MATCHES "^(INTERNAL|STATIC)$")
file(APPEND ${CMAKE_BINARY_DIR}/internal_build_cache "set(${v} \"$CACHE{${v}}\" CACHE ${type} \"\" FORCE)\n")
endif()
endforeach()

string(RANDOM internal_binary_dir)

# DEBUG ONLY
set(internal_binary_dir "internal")

execute_process(
COMMAND ${CMAKE_COMMAND} -S${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}/${internal_binary_dir} -C${CMAKE_BINARY_DIR}/internal_build_cache -G${CMAKE_GENERATOR}
OUTPUT_QUIET
ERROR_QUIET
)

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("")
message("'${config}' build configuration:")
if(CMAKE_GENERATOR STREQUAL "Ninja Multi-Config")
get_flags_ninja(preprocessor_defines compiler_flags linker_flags ${config})
else()
message("Printing build options is not supported for the generator \"${CMAKE_GENERATOR}\"")
endif()
message(" Preprocessor defined macros ......... ${preprocessor_defines}")
message(" C++ flags ........................... ${compiler_flags}")
message(" Linker flags ........................ ${linker_flags}")
endforeach()
message("")
else()
message("CMAKE_BUILD_TYPE ...................... ${CMAKE_BUILD_TYPE}")
if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
get_flags_unix_makefiles(preprocessor_defines compiler_flags linker_flags)
elseif(CMAKE_GENERATOR STREQUAL "Ninja")
get_flags_ninja(preprocessor_defines compiler_flags linker_flags "")
else()
message("Printing build options is not supported for the generator \"${CMAKE_GENERATOR}\"")
endif()
message("Preprocessor defined macros ........... ${preprocessor_defines}")
message("C++ flags ............................. ${compiler_flags}")
message("Linker flags .......................... ${linker_flags}")
endif()
endif()
endfunction()
28 changes: 0 additions & 28 deletions cmake/module/GetTargetInterface.cmake

This file was deleted.

25 changes: 0 additions & 25 deletions cmake/module/ProcessConfigurations.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -139,28 +139,3 @@ function(replace_cxx_flag_in_config config old_flag new_flag)
FORCE
)
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()
Loading