Skip to content

Commit

Permalink
cmake: Add add_library_append_flags function
Browse files Browse the repository at this point in the history
  • Loading branch information
hebasto committed Apr 26, 2024
1 parent 7f9db21 commit 9016c6d
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 67 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ target_link_options(append_flags_interface INTERFACE
${APPEND_LDFLAGS}
)

include(AddTargetMacros)

if(FUZZ)
message(WARNING "FUZZ=ON will disable all other targets and force BUILD_FUZZ_BINARY=ON.")
set(BUILD_DAEMON OFF)
Expand Down
7 changes: 3 additions & 4 deletions cmake/crc32c.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ 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)

add_library(crc32c STATIC EXCLUDE_FROM_ALL
add_library_append_flags(crc32c STATIC EXCLUDE_FROM_ALL
${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c.cc
${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_portable.cc
)
Expand All @@ -98,7 +97,7 @@ target_include_directories(crc32c
target_link_libraries(crc32c PRIVATE crc32c_common)

if(HAVE_SSE42)
add_library(crc32c_sse42 STATIC EXCLUDE_FROM_ALL
add_library_append_flags(crc32c_sse42 STATIC EXCLUDE_FROM_ALL
${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_sse42.cc
)
target_compile_definitions(crc32c_sse42 PUBLIC HAVE_SSE42=1)
Expand All @@ -108,7 +107,7 @@ if(HAVE_SSE42)
endif()

if(HAVE_ARM64_CRC32C)
add_library(crc32c_arm64 STATIC EXCLUDE_FROM_ALL
add_library_append_flags(crc32c_arm64 STATIC EXCLUDE_FROM_ALL
${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_arm64.cc
)
target_compile_definitions(crc32c_arm64 PUBLIC HAVE_ARM64_CRC32C=1)
Expand Down
3 changes: 1 addition & 2 deletions cmake/leveldb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
include(CheckCXXSymbolExists)
check_cxx_symbol_exists(F_FULLFSYNC "fcntl.h" HAVE_FULLFSYNC)

add_library(leveldb STATIC EXCLUDE_FROM_ALL
add_library_append_flags(leveldb STATIC EXCLUDE_FROM_ALL
${PROJECT_SOURCE_DIR}/src/leveldb/db/builder.cc
${PROJECT_SOURCE_DIR}/src/leveldb/db/c.cc
${PROJECT_SOURCE_DIR}/src/leveldb/db/db_impl.cc
Expand Down Expand Up @@ -95,7 +95,6 @@ else()
endif()

target_link_libraries(leveldb PRIVATE
core_base_interface
nowarn_leveldb_interface
crc32c
)
6 changes: 2 additions & 4 deletions cmake/minisketch.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if(MSVC)
endif()

if(HAVE_CLMUL)
add_library(minisketch_clmul OBJECT EXCLUDE_FROM_ALL
add_library_append_flags(minisketch_clmul OBJECT EXCLUDE_FROM_ALL
${PROJECT_SOURCE_DIR}/src/minisketch/src/fields/clmul_1byte.cpp
${PROJECT_SOURCE_DIR}/src/minisketch/src/fields/clmul_2bytes.cpp
${PROJECT_SOURCE_DIR}/src/minisketch/src/fields/clmul_3bytes.cpp
Expand All @@ -54,12 +54,11 @@ if(HAVE_CLMUL)
target_compile_options(minisketch_clmul PRIVATE ${CLMUL_CXXFLAGS})
target_link_libraries(minisketch_clmul
PRIVATE
core_base_interface
minisketch_common
)
endif()

add_library(minisketch STATIC EXCLUDE_FROM_ALL
add_library_append_flags(minisketch STATIC EXCLUDE_FROM_ALL
${PROJECT_SOURCE_DIR}/src/minisketch/src/minisketch.cpp
${PROJECT_SOURCE_DIR}/src/minisketch/src/fields/generic_1byte.cpp
${PROJECT_SOURCE_DIR}/src/minisketch/src/fields/generic_2bytes.cpp
Expand All @@ -78,7 +77,6 @@ target_include_directories(minisketch

target_link_libraries(minisketch
PRIVATE
core_base_interface
minisketch_common
$<TARGET_NAME_IF_EXISTS:minisketch_clmul>
)
34 changes: 34 additions & 0 deletions cmake/module/AddTargetMacros.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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)

# Functions in this module are drop-in replacements for CMake's add_library
# and add_executable functions. They are mandatory for use in the Bitcoin
# Core project (except for imported and interface libraries), because they
# handle APPEND_CPPFLAGS, APPEND_CFLAGS, APPEND_CXXFLAGS and APPEND_LDFLAGS.

macro(target_append_flags_interface target)
# The following command links append_flags_interface to a given
# target at the very and the configuring process, which ensures
# that APPEND_CPPFLAGS, APPEND_CFLAGS, APPEND_CXXFLAGS and APPEND_LDFLAGS
# will override any other flags added by the build system.
cmake_language(EVAL CODE "
cmake_language(DEFER
DIRECTORY ${PROJECT_SOURCE_DIR}
CALL target_link_libraries ${target} PRIVATE append_flags_interface
)
")
endmacro()

function(add_library_append_flags name)
cmake_parse_arguments(PARSE_ARGV 1 FWD "" "" "")
add_library("${name}" ${FWD_UNPARSED_ARGUMENTS})
if(name MATCHES "^crc32c|^leveldb|^minisketch|^secp256k1")
target_link_libraries("${name}" PRIVATE core_base_interface)
else()
target_link_libraries("${name}" PRIVATE core_interface)
endif()
target_append_flags_interface("${name}")
endfunction()
4 changes: 1 addition & 3 deletions cmake/secp256k1.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ check_c_source_compiles("
" HAVE_64BIT_ASM
)

add_library(secp256k1 STATIC EXCLUDE_FROM_ALL
add_library_append_flags(secp256k1 STATIC EXCLUDE_FROM_ALL
${PROJECT_SOURCE_DIR}/src/secp256k1/src/secp256k1.c
${PROJECT_SOURCE_DIR}/src/secp256k1/src/precomputed_ecmult.c
${PROJECT_SOURCE_DIR}/src/secp256k1/src/precomputed_ecmult_gen.c
Expand Down Expand Up @@ -54,5 +54,3 @@ if(MSVC)
/wd4267
)
endif()

target_link_libraries(secp256k1 PRIVATE core_base_interface)
18 changes: 5 additions & 13 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ add_custom_target(generate_build_info
COMMENT "Generating obj/build.h"
VERBATIM
)
add_library(bitcoin_clientversion OBJECT EXCLUDE_FROM_ALL
add_library_append_flags(bitcoin_clientversion OBJECT EXCLUDE_FROM_ALL
clientversion.cpp
)
target_compile_definitions(bitcoin_clientversion
PRIVATE
HAVE_BUILD_INFO
)
target_link_libraries(bitcoin_clientversion
PRIVATE
core_interface
)
add_dependencies(bitcoin_clientversion generate_build_info)

add_subdirectory(crypto)
Expand All @@ -41,7 +37,7 @@ endif()


# Stable, backwards-compatible consensus functionality.
add_library(bitcoin_consensus STATIC EXCLUDE_FROM_ALL
add_library_append_flags(bitcoin_consensus STATIC EXCLUDE_FROM_ALL
arith_uint256.cpp
consensus/merkle.cpp
consensus/tx_check.cpp
Expand All @@ -57,7 +53,6 @@ add_library(bitcoin_consensus STATIC EXCLUDE_FROM_ALL
)
target_link_libraries(bitcoin_consensus
PRIVATE
core_interface
bitcoin_crypto
secp256k1
)
Expand All @@ -68,7 +63,7 @@ endif()

# Home for common functionality shared by different executables and libraries.
# Similar to `bitcoin_util` library, but higher-level.
add_library(bitcoin_common STATIC EXCLUDE_FROM_ALL
add_library_append_flags(bitcoin_common STATIC EXCLUDE_FROM_ALL
addresstype.cpp
base58.cpp
bech32.cpp
Expand Down Expand Up @@ -116,7 +111,6 @@ add_library(bitcoin_common STATIC EXCLUDE_FROM_ALL
)
target_link_libraries(bitcoin_common
PRIVATE
core_interface
bitcoin_consensus
bitcoin_util
univalue
Expand Down Expand Up @@ -150,7 +144,7 @@ endif()


# P2P and RPC server functionality used by `bitcoind` and `bitcoin-qt` executables.
add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL
add_library_append_flags(bitcoin_node STATIC EXCLUDE_FROM_ALL
addrdb.cpp
addrman.cpp
banman.cpp
Expand Down Expand Up @@ -246,7 +240,6 @@ add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL
)
target_link_libraries(bitcoin_node
PRIVATE
core_interface
bitcoin_common
bitcoin_util
leveldb
Expand Down Expand Up @@ -290,13 +283,12 @@ if(MULTIPROCESS)
endif()


add_library(bitcoin_cli STATIC EXCLUDE_FROM_ALL
add_library_append_flags(bitcoin_cli STATIC EXCLUDE_FROM_ALL
compat/stdin.cpp
rpc/client.cpp
)
target_link_libraries(bitcoin_cli
PUBLIC
core_interface
univalue
)

Expand Down
19 changes: 5 additions & 14 deletions src/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ if(NOT MSVC)
)
endif()

add_library(bitcoin_crypto STATIC EXCLUDE_FROM_ALL
add_library_append_flags(bitcoin_crypto STATIC EXCLUDE_FROM_ALL
aes.cpp
chacha20.cpp
chacha20poly1305.cpp
Expand All @@ -81,47 +81,38 @@ add_library(bitcoin_crypto STATIC EXCLUDE_FROM_ALL
siphash.cpp
)

target_link_libraries(bitcoin_crypto
PRIVATE
core_interface
)

if(HAVE_SSE41)
add_library(bitcoin_crypto_sse41 STATIC EXCLUDE_FROM_ALL
add_library_append_flags(bitcoin_crypto_sse41 STATIC EXCLUDE_FROM_ALL
sha256_sse41.cpp
)
target_compile_definitions(bitcoin_crypto_sse41 PUBLIC ENABLE_SSE41)
target_compile_options(bitcoin_crypto_sse41 PRIVATE ${SSE41_CXXFLAGS})
target_link_libraries(bitcoin_crypto_sse41 PRIVATE core_interface)
target_link_libraries(bitcoin_crypto PRIVATE bitcoin_crypto_sse41)
endif()

if(HAVE_AVX2)
add_library(bitcoin_crypto_avx2 STATIC EXCLUDE_FROM_ALL
add_library_append_flags(bitcoin_crypto_avx2 STATIC EXCLUDE_FROM_ALL
sha256_avx2.cpp
)
target_compile_definitions(bitcoin_crypto_avx2 PUBLIC ENABLE_AVX2)
target_compile_options(bitcoin_crypto_avx2 PRIVATE ${AVX2_CXXFLAGS})
target_link_libraries(bitcoin_crypto_avx2 PRIVATE core_interface)
target_link_libraries(bitcoin_crypto PRIVATE bitcoin_crypto_avx2)
endif()

if(HAVE_X86_SHANI)
add_library(bitcoin_crypto_x86_shani STATIC EXCLUDE_FROM_ALL
add_library_append_flags(bitcoin_crypto_x86_shani STATIC EXCLUDE_FROM_ALL
sha256_x86_shani.cpp
)
target_compile_definitions(bitcoin_crypto_x86_shani PUBLIC ENABLE_X86_SHANI)
target_compile_options(bitcoin_crypto_x86_shani PRIVATE ${X86_SHANI_CXXFLAGS})
target_link_libraries(bitcoin_crypto_x86_shani PRIVATE core_interface)
target_link_libraries(bitcoin_crypto PRIVATE bitcoin_crypto_x86_shani)
endif()

if(HAVE_ARM_SHANI)
add_library(bitcoin_crypto_arm_shani STATIC EXCLUDE_FROM_ALL
add_library_append_flags(bitcoin_crypto_arm_shani STATIC EXCLUDE_FROM_ALL
sha256_arm_shani.cpp
)
target_compile_definitions(bitcoin_crypto_arm_shani PUBLIC ENABLE_ARM_SHANI)
target_compile_options(bitcoin_crypto_arm_shani PRIVATE ${ARM_SHANI_CXXFLAGS})
target_link_libraries(bitcoin_crypto_arm_shani PRIVATE core_interface)
target_link_libraries(bitcoin_crypto PRIVATE bitcoin_crypto_arm_shani)
endif()
7 changes: 1 addition & 6 deletions src/ipc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.

add_library(bitcoin_ipc STATIC EXCLUDE_FROM_ALL
add_library_append_flags(bitcoin_ipc STATIC EXCLUDE_FROM_ALL
capnp/protocol.cpp
interfaces.cpp
process.cpp
Expand All @@ -11,8 +11,3 @@ add_library(bitcoin_ipc STATIC EXCLUDE_FROM_ALL
target_capnp_sources(bitcoin_ipc ${CMAKE_SOURCE_DIR}
capnp/echo.capnp capnp/init.capnp
)

target_link_libraries(bitcoin_ipc
PRIVATE
core_interface
)
3 changes: 1 addition & 2 deletions src/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# library, as more and more modules are decoupled from the
# consensus engine, this list will shrink to only those
# which are absolutely necessary.
add_library(bitcoinkernel EXCLUDE_FROM_ALL
add_library_append_flags(bitcoinkernel EXCLUDE_FROM_ALL
bitcoinkernel.cpp
chain.cpp
checks.cpp
Expand Down Expand Up @@ -84,7 +84,6 @@ add_library(bitcoinkernel EXCLUDE_FROM_ALL
)
target_link_libraries(bitcoinkernel
PRIVATE
core_interface
bitcoin_clientversion
bitcoin_crypto
leveldb
Expand Down
3 changes: 1 addition & 2 deletions src/qt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ qt5_add_translation(qm_files ${ts_files})

configure_file(bitcoin_locale.qrc bitcoin_locale.qrc COPYONLY)

add_library(bitcoinqt STATIC EXCLUDE_FROM_ALL
add_library_append_flags(bitcoinqt STATIC EXCLUDE_FROM_ALL
bantablemodel.cpp
bitcoin.cpp
bitcoinaddressvalidator.cpp
Expand Down Expand Up @@ -128,7 +128,6 @@ target_link_libraries(bitcoinqt
PUBLIC
Qt5::Widgets
PRIVATE
core_interface
bitcoin_cli
leveldb
Boost::headers
Expand Down
6 changes: 1 addition & 5 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,16 @@ if(ENABLE_WALLET)
endif()

if(MULTIPROCESS)
add_library(bitcoin_ipc_test STATIC EXCLUDE_FROM_ALL
add_library_append_flags(bitcoin_ipc_test STATIC EXCLUDE_FROM_ALL
ipc_test.cpp
)

target_capnp_sources(bitcoin_ipc_test ${CMAKE_SOURCE_DIR}
ipc_test.capnp
)

target_link_libraries(bitcoin_ipc_test
PRIVATE
core_interface
univalue
)

target_sources(test_bitcoin
PRIVATE
ipc_tests.cpp
Expand Down
3 changes: 1 addition & 2 deletions src/test/fuzz/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.

add_library(test_fuzz STATIC EXCLUDE_FROM_ALL
add_library_append_flags(test_fuzz STATIC EXCLUDE_FROM_ALL
descriptor.cpp
mempool.cpp
net.cpp
Expand All @@ -12,7 +12,6 @@ add_library(test_fuzz STATIC EXCLUDE_FROM_ALL

target_link_libraries(test_fuzz
PRIVATE
core_interface
test_util
bitcoin_node
Boost::headers
Expand Down
3 changes: 1 addition & 2 deletions src/test/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.

add_library(test_util STATIC EXCLUDE_FROM_ALL
add_library_append_flags(test_util STATIC EXCLUDE_FROM_ALL
blockfilter.cpp
coins.cpp
index.cpp
Expand All @@ -22,7 +22,6 @@ add_library(test_util STATIC EXCLUDE_FROM_ALL

target_link_libraries(test_util
PRIVATE
core_interface
Boost::headers
PUBLIC
univalue
Expand Down
Loading

0 comments on commit 9016c6d

Please sign in to comment.