cmake: Introduce interface libraries to encapsulate common flags

Also add a sanity check for non-encapsulated (directory-wide) build
properties.
This commit is contained in:
Hennadii Stepanov 2024-08-16 19:27:34 +01:00
parent a2317e27b7
commit 70683884c5
No known key found for this signature in database
GPG key ID: 410108112E7EA81F
2 changed files with 52 additions and 0 deletions

View file

@ -59,8 +59,21 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/module)
set(configure_warnings)
# The core_interface library aims to encapsulate common build flags.
# It is a usage requirement for all targets except for secp256k1, which
# gets its flags by other means.
add_library(core_interface INTERFACE)
add_library(core_interface_relwithdebinfo INTERFACE)
add_library(core_interface_debug INTERFACE)
target_link_libraries(core_interface INTERFACE
$<$<CONFIG:RelWithDebInfo>:core_interface_relwithdebinfo>
$<$<CONFIG:Debug>:core_interface_debug>
)
message("\n")
message("Configure summary")
message("=================")
@ -73,3 +86,6 @@ if(configure_warnings)
endforeach()
message(" ******\n")
endif()
# We want all build properties to be encapsulated properly.
include(WarnAboutGlobalProperties)

View file

@ -0,0 +1,36 @@
# 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/.
include_guard(GLOBAL)
# Avoid the directory-wide add_definitions() and add_compile_definitions() commands.
# Instead, prefer the target-specific target_compile_definitions() one.
get_directory_property(global_compile_definitions COMPILE_DEFINITIONS)
if(global_compile_definitions)
message(AUTHOR_WARNING "The directory's COMPILE_DEFINITIONS property is not empty: ${global_compile_definitions}")
endif()
# Avoid the directory-wide add_compile_options() command.
# Instead, prefer the target-specific target_compile_options() one.
get_directory_property(global_compile_options COMPILE_OPTIONS)
if(global_compile_options)
message(AUTHOR_WARNING "The directory's COMPILE_OPTIONS property is not empty: ${global_compile_options}")
endif()
# Avoid the directory-wide add_link_options() command.
# Instead, prefer the target-specific target_link_options() one.
get_directory_property(global_link_options LINK_OPTIONS)
if(global_link_options)
message(AUTHOR_WARNING "The directory's LINK_OPTIONS property is not empty: ${global_link_options}")
endif()
# Avoid the directory-wide link_libraries() command.
# Instead, prefer the target-specific target_link_libraries() one.
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy_cxx_source.cpp "#error")
add_library(check_loose_linked_libraries OBJECT EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/dummy_cxx_source.cpp)
set_target_properties(check_loose_linked_libraries PROPERTIES EXPORT_COMPILE_COMMANDS OFF)
get_target_property(global_linked_libraries check_loose_linked_libraries LINK_LIBRARIES)
if(global_linked_libraries)
message(AUTHOR_WARNING "There are libraries linked with `link_libraries` commands: ${global_linked_libraries}")
endif()