Skip to content

Commit

Permalink
cmake: detect sublibraries (#1905)
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr authored Mar 14, 2024
1 parent bae7e36 commit 41971cb
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 51 deletions.
4 changes: 2 additions & 2 deletions cmake/cmake_format.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ parse:
BUILD: '*'
PROFILE: '*'
silkworm_library:
flags:
- NO_TEST
kwargs:
PUBLIC: '*'
PRIVATE: '*'
EXCLUDE_REGEX: '*'
TYPE: '*'
LIBS_TEST: '*'

format:
line_width: 120
Expand Down
40 changes: 18 additions & 22 deletions cmake/common/targets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

find_package(Catch2 REQUIRED)

macro(list_filter VAR EXCLUDE_REGEX)
foreach(R IN LISTS ${EXCLUDE_REGEX})
list(FILTER ${VAR} EXCLUDE REGEX "${SILKWORM_SRC_DIR}/${R}")
macro(list_filter_dirs VAR DIRS)
foreach(DIR IN LISTS ${DIRS})
list(FILTER ${VAR} EXCLUDE REGEX "^${DIR}/")
endforeach()
endmacro()

Expand All @@ -27,9 +27,9 @@ function(silkworm_library TARGET)
PARSE_ARGV
1
"ARG"
""
""
"PUBLIC;PRIVATE;EXCLUDE_REGEX;TYPE;LIBS_TEST"
"NO_TEST"
"TYPE"
"PUBLIC;PRIVATE"
)

file(
Expand All @@ -41,9 +41,17 @@ function(silkworm_library TARGET)
"*.c"
"*.h"
)
list(FILTER SRC EXCLUDE REGEX "_test\\.cpp$")

# remove subdirectories with CMakeLists.txt
get_directory_property(SUB_LIBS SUBDIRECTORIES)
list_filter_dirs(SRC SUB_LIBS)

set(TEST_SRC ${SRC})
set(TEST_REGEX "(_test\\.cpp$|\/test_util\/)")
list(FILTER TEST_SRC INCLUDE REGEX "${TEST_REGEX}")

list(FILTER SRC EXCLUDE REGEX "${TEST_REGEX}")
list(FILTER SRC EXCLUDE REGEX "_benchmark\\.cpp$")
list_filter(SRC ARG_EXCLUDE_REGEX)
add_library(${TARGET} ${ARG_TYPE} ${SRC})

target_include_directories(${TARGET} PUBLIC "${SILKWORM_MAIN_DIR}")
Expand All @@ -54,21 +62,9 @@ function(silkworm_library TARGET)
)

# unit tests
file(GLOB_RECURSE TEST_SRC CONFIGURE_DEPENDS "*_test.cpp")
list_filter(TEST_SRC ARG_EXCLUDE_REGEX)
if(TEST_SRC)
# Temporarily skip unit test runner for silkworm_capi target in ASAN build (failures after PR #1879)
if(${TARGET} STREQUAL "silkworm_capi" AND SILKWORM_SANITIZE)
return()
endif()
if(TEST_SRC AND NOT ${ARG_NO_TEST})
set(TEST_TARGET ${TARGET}_test)
add_executable(${TEST_TARGET} "${SILKWORM_MAIN_DIR}/cmd/test/unit_test.cpp" ${TEST_SRC})
list(APPEND ARG_LIBS_TEST Catch2::Catch2)
get_target_property(TARGET_TYPE ${TARGET} TYPE)
if(TARGET_TYPE STREQUAL SHARED_LIBRARY)
target_link_libraries(${TEST_TARGET} PRIVATE ${TARGET} "${ARG_PUBLIC}" "${ARG_PRIVATE}" "${ARG_LIBS_TEST}")
else()
target_link_libraries(${TEST_TARGET} PRIVATE ${TARGET} "${ARG_LIBS_TEST}")
endif()
target_link_libraries(${TEST_TARGET} PRIVATE Catch2::Catch2 ${TARGET})
endif()
endfunction()
7 changes: 0 additions & 7 deletions cmd/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ else()
endif()

if(NOT SILKWORM_CORE_ONLY)
find_package(GTest REQUIRED)

# Enable fuzzing tests for Clang sanitizer builds only
if("${CMAKE_CXX_COMPILER_ID}" MATCHES ".*Clang$" AND SILKWORM_FUZZER)
# macOS on 64-bit ARM does not support option -fsanitize=leak
Expand Down Expand Up @@ -65,11 +63,6 @@ if(NOT SILKWORM_CORE_ONLY)
)
endif()

# Silkworm Sync Tests
file(GLOB_RECURSE SILKWORM_SYNC_TESTS CONFIGURE_DEPENDS "${SILKWORM_MAIN_SRC_DIR}/sync/*_test.cpp")
add_executable(sync_test unit_test.cpp ${SILKWORM_SYNC_TESTS})
target_link_libraries(sync_test silkworm_node silkworm_sync Catch2::Catch2 GTest::gmock)

# Ethereum EL Tests (https://github.com/ethereum/tests)
find_package(CLI11 REQUIRED)
add_executable(ethereum ethereum.cpp)
Expand Down
10 changes: 9 additions & 1 deletion silkworm/capi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

include("${SILKWORM_MAIN_DIR}/cmake/common/targets.cmake")

set(TARGET silkworm_capi)

find_package(Microsoft.GSL REQUIRED)

set(PUBLIC_LIBS "")
Expand All @@ -28,17 +30,23 @@ set(PRIVATE_LIBS
silkworm_rpcdaemon
)

set(TARGET silkworm_capi)
# Temporarily skip unit test runner for silkworm_capi target in ASAN build (failures after PR #1879)
if(SILKWORM_SANITIZE)
set(NO_TEST "NO_TEST")
endif()

# cmake-format: off
silkworm_library(
${TARGET}
PUBLIC ${PUBLIC_LIBS}
PRIVATE ${PRIVATE_LIBS}
TYPE SHARED
${NO_TEST}
)
# cmake-format: on

target_link_libraries(silkworm_capi_test PRIVATE silkworm_core silkworm_db silkworm_infra silkworm_rpcdaemon)

# Remove custom stack_size linker option for this target
get_target_property(LINK_OPTIONS ${TARGET} LINK_OPTIONS)
list(REMOVE_ITEM LINK_OPTIONS "-Wl,-stack_size")
Expand Down
4 changes: 3 additions & 1 deletion silkworm/db/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ include("${SILKWORM_MAIN_DIR}/cmake/common/targets.cmake")

add_subdirectory(etl)
add_subdirectory(snapshots)
add_subdirectory(test_util)

find_package(absl REQUIRED)
find_package(magic_enum REQUIRED)
Expand Down Expand Up @@ -51,5 +52,6 @@ silkworm_library(
silkworm_db
PUBLIC ${LIBS_PUBLIC}
PRIVATE ${LIBS_PRIVATE}
EXCLUDE_REGEX "db/etl" "db/snapshots"
)

target_link_libraries(silkworm_db_test PRIVATE silkworm_db_test_util)
1 change: 0 additions & 1 deletion silkworm/db/snapshots/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ silkworm_library(
silkworm_snapshots
PUBLIC Microsoft.GSL::GSL silkworm_core silkworm_infra
PRIVATE absl::strings Boost::headers magic_enum::magic_enum torrent-rasterbar silkworm_snapshots_seg
EXCLUDE_REGEX "snapshots/seg"
)
28 changes: 28 additions & 0 deletions silkworm/db/test_util/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[[
Copyright 2024 The Silkworm Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]

set(TARGET silkworm_db_test_util)

find_package(nlohmann_json REQUIRED)

file(GLOB_RECURSE SRC CONFIGURE_DEPENDS "*.cpp" "*.hpp")
add_library(${TARGET} ${SRC})

target_link_libraries(
${TARGET}
PUBLIC silkworm_core silkworm_infra silkworm_db
PRIVATE nlohmann_json::nlohmann_json
)
8 changes: 4 additions & 4 deletions silkworm/infra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
limitations under the License.
]]

add_subdirectory(test_util)

set(TARGET silkworm_infra)

find_package(absl REQUIRED)
find_package(asio-grpc REQUIRED)
find_package(Boost REQUIRED headers container thread)
Expand All @@ -23,10 +27,6 @@ find_package(GTest REQUIRED)
find_package(magic_enum REQUIRED)
find_package(spdlog REQUIRED)

add_subdirectory(test_util)

set(TARGET silkworm_infra)

file(GLOB_RECURSE SRC CONFIGURE_DEPENDS "*.cpp" "*.hpp")
list(FILTER SRC EXCLUDE REGEX "_test\\.cpp$")
list(FILTER SRC EXCLUDE REGEX "_benchmark\\.cpp$")
Expand Down
4 changes: 2 additions & 2 deletions silkworm/infra/test_util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
limitations under the License.
]]

find_package(Boost REQUIRED headers)

set(TARGET silkworm_infra_test_util)

find_package(Boost REQUIRED headers)

file(GLOB_RECURSE SRC CONFIGURE_DEPENDS "*.cpp" "*.hpp")

add_library(${TARGET} ${SRC})
Expand Down
3 changes: 2 additions & 1 deletion silkworm/node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ silkworm_library(
silkworm_node
PUBLIC ${SILKWORM_NODE_PUBLIC_LIBS}
PRIVATE ${SILKWORM_NODE_PRIVATE_LIBS}
EXCLUDE_REGEX "node/stagedsync/stages"
)

target_link_libraries(silkworm_node_test PRIVATE silkworm_db_test_util)
2 changes: 2 additions & 0 deletions silkworm/node/stagedsync/stages/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ silkworm_library(
PUBLIC ${LIBS_PUBLIC}
PRIVATE ${LIBS_PRIVATE}
)

target_link_libraries(silkworm_stages_test PRIVATE silkworm_db_test_util)
3 changes: 2 additions & 1 deletion silkworm/rpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ silkworm_library(
silkworm_rpcdaemon
PUBLIC ${SILKWORM_RPCDAEMON_PUBLIC_LIBRARIES}
PRIVATE ${SILKWORM_RPCDAEMON_PRIVATE_LIBRARIES}
LIBS_TEST GTest::gmock
)

target_link_libraries(silkworm_rpcdaemon_test PRIVATE GTest::gmock)
1 change: 0 additions & 1 deletion silkworm/sentry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,4 @@ silkworm_library(
silkworm_sentry
PUBLIC ${LIBS_PUBLIC}
PRIVATE ${LIBS_PRIVATE}
EXCLUDE_REGEX "sentry/common" "discovery/[a-z0-9_]+/"
)
14 changes: 6 additions & 8 deletions silkworm/sync/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
limitations under the License.
]]

include("${SILKWORM_MAIN_DIR}/cmake/common/targets.cmake")

find_package(absl REQUIRED)
find_package(GTest REQUIRED)
find_package(magic_enum REQUIRED)

file(GLOB_RECURSE SILKWORM_SYNC_SRC CONFIGURE_DEPENDS "*.cpp" "*.hpp")
list(FILTER SILKWORM_SYNC_SRC EXCLUDE REGEX "_test\\.cpp$")

add_library(silkworm_sync "${SILKWORM_SYNC_SRC}")

set(SILKWORM_SYNC_PUBLIC_LIBS
silkworm_rpcdaemon
silkworm_node
Expand All @@ -32,8 +30,6 @@ set(SILKWORM_SYNC_PUBLIC_LIBS
absl::btree
)

target_include_directories(silkworm_sync PUBLIC "${SILKWORM_MAIN_DIR}")

# cmake-format: off
set(SILKWORM_SYNC_PRIVATE_LIBS
cborcpp
Expand All @@ -42,8 +38,10 @@ set(SILKWORM_SYNC_PRIVATE_LIBS
)
# cmake-format: on

target_link_libraries(
silkworm_library(
silkworm_sync
PUBLIC ${SILKWORM_SYNC_PUBLIC_LIBS}
PRIVATE ${SILKWORM_SYNC_PRIVATE_LIBS}
)

target_link_libraries(silkworm_sync_test PRIVATE GTest::gmock silkworm_db_test_util)

0 comments on commit 41971cb

Please sign in to comment.