build: cmake: consume Seastar using its .pc files

before this change, scylla's CMake-based system consumes Seastar
library by including it directly. but this failed to address the needs
of linking against Seastar shared libraries in Debug and Dev builds, while
linking against the static libraries in other builds. because Seastar
uses `BUILD_SHARED_LIBS` CMake variable to determine if it builds
shared libraries. and we cannot assign different values to this
CMake variable based on current configure type -- CMake does not
support. see https://gitlab.kitware.com/cmake/cmake/-/issues/19467

in order to address this problem, we have a couple possible
solutions:

- to enable Seastar to build both shared and static libraries in a
  pass. without sacrificing the performance, we have to build
  all object files twice: once with -fPIC, once without. in order
  to accompolish this goal, we need to develop a machinary to
  populate the same settings to these two builds. this would
  complicate the design of Seastar's building system further.
- to build Seastar libraries twice in scylla, we could use
  the ExternalProject module to implement this. but it'd be
  complicate to extract the compile options, and link options
  previously populated by Seastar's targets with CMake --
  we would have to replicate all of them in scylla. this is
  out of the question.
- to build Seastar libraries twice before building scylla,
  and let scylla to consume them using CMake config files or
  .pc files. this is a compromise. it enables scylla to
  drive the build of Seastar libraries and to consume
  the compile options and link options. the downside is:

  * the generated compilation database (compile_commands.json)
    does not include the commands building Seastar anymore.
  * the building system of scylla does not have finer graind
    control on the building process of seastar. for instance,
    we cannot specify the build dependency to a certain seastar
    library, and just build it instead of building the whole
    seastar project.

turns out the last approach is the best one we can have
at this moment. this is also the approach used by the existing
`configure.py`.

in this change, we

- add FindSeastar.cmake to

  * detect the preconfigured Seastar builds, and
  * extract the build options from .pc files
  * expose library targets to be consumed by parent project
- add Seastar as an external project, so we can build it from
  the parent project. BUILD_AWAYS is set to ensure that Seastar is
  rebuilt, as scylla developers are expected to modify Seastar
  occasionally. since the change in Seastar's SOURCE_DIR is not
  detectable via the ExternalProject, we have to rebuild it.

  this is atypical compared to standard ExternalProject usage:
  - Seastar's build system should already be configured at this point.
  - We maintain separate project variants for each configuration type.

  Benefits of this approach:
  - Allows the parent project to consume the compile options exposed by
    .pc file. as the compile options vary from one config to another.
  - Allows application of config-specific settings
  - Enables building Seastar within the parent project's build system
  - Facilitates linking of artifacts with the external project target,
    establishing proper dependencies between them
- preserve the existing machinery of including Seastar only when
  building without multi-config generator. this allows users who don't
  use mult-config generator to build Seastar in-the-tree. the typical
  use case is the CI workflows performing the static analysis.

we will update `configure.py` to merge the compilation database
of scylla and seastar.

Refs scylladb/scylladb#2717
Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
This commit is contained in:
Kefu Chai
2024-10-16 09:14:44 +08:00
parent 7cb74df323
commit 415c83fa67
4 changed files with 179 additions and 15 deletions

View File

@@ -47,16 +47,56 @@ set(CMAKE_CXX_EXTENSIONS ON CACHE INTERNAL "")
set(CMAKE_CXX_SCAN_FOR_MODULES OFF CACHE INTERNAL "")
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(Seastar_TESTING ON CACHE BOOL "" FORCE)
set(Seastar_API_LEVEL 7 CACHE STRING "" FORCE)
set(Seastar_DEPRECATED_OSTREAM_FORMATTERS OFF CACHE BOOL "" FORCE)
set(Seastar_APPS ON CACHE BOOL "" FORCE)
set(Seastar_EXCLUDE_APPS_FROM_ALL ON CACHE BOOL "" FORCE)
set(Seastar_EXCLUDE_TESTS_FROM_ALL ON CACHE BOOL "" FORCE)
set(Seastar_IO_URING OFF CACHE BOOL "" FORCE)
set(Seastar_SCHEDULING_GROUPS_COUNT 16 CACHE STRING "" FORCE)
set(Seastar_UNUSED_RESULT_ERROR ON CACHE BOOL "" FORCE)
add_subdirectory(seastar)
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(is_multi_config)
find_package(Seastar)
# this is atypical compared to standard ExternalProject usage:
# - Seastar's build system should already be configured at this point.
# - We maintain separate project variants for each configuration type.
#
# Benefits of this approach:
# - Allows the parent project to consume the compile options exposed by
# .pc file. as the compile options vary from one config to another.
# - Allows application of config-specific settings
# - Enables building Seastar within the parent project's build system
# - Facilitates linking of artifacts with the external project target,
# establishing proper dependencies between them
include(ExternalProject)
ExternalProject_Add(Seastar
SOURCE_DIR "${PROJECT_SOURCE_DIR}/seastar"
BINARY_DIR "${CMAKE_BINARY_DIR}/$<CONFIG>/seastar"
CONFIGURE_COMMAND ""
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR>
--target seastar
--target seastar_testing
--target seastar_perf_testing
--target app_iotune
BUILD_ALWAYS ON
BUILD_BYPRODUCTS
<BINARY_DIR>/libseastar.$<IF:$<CONFIG:Debug,Dev>,so,a>
<BINARY_DIR>/libseastar_testing.$<IF:$<CONFIG:Debug,Dev>,so,a>
<BINARY_DIR>/libseastar_perf_testing.$<IF:$<CONFIG:Debug,Dev>,so,a>
<BINARY_DIR>/apps/iotune/iotune
<BINARY_DIR>/gen/include/seastar/http/chunk_parsers.hh
<BINARY_DIR>/gen/include/seastar/http/request_parser.hh
<BINARY_DIR>/gen/include/seastar/http/response_parser.hh
INSTALL_COMMAND "")
add_dependencies(Seastar::seastar Seastar)
add_dependencies(Seastar::seastar_testing Seastar)
else()
set(Seastar_TESTING ON CACHE BOOL "" FORCE)
set(Seastar_API_LEVEL 7 CACHE STRING "" FORCE)
set(Seastar_DEPRECATED_OSTREAM_FORMATTERS OFF CACHE BOOL "" FORCE)
set(Seastar_APPS ON CACHE BOOL "" FORCE)
set(Seastar_EXCLUDE_APPS_FROM_ALL ON CACHE BOOL "" FORCE)
set(Seastar_EXCLUDE_TESTS_FROM_ALL ON CACHE BOOL "" FORCE)
set(Seastar_IO_URING OFF CACHE BOOL "" FORCE)
set(Seastar_SCHEDULING_GROUPS_COUNT 16 CACHE STRING "" FORCE)
set(Seastar_UNUSED_RESULT_ERROR ON CACHE BOOL "" FORCE)
add_subdirectory(seastar)
endif()
set(ABSL_PROPAGATE_CXX_STD ON CACHE BOOL "" FORCE)
find_package(Sanitizers QUIET)

115
cmake/FindSeastar.cmake Normal file
View File

@@ -0,0 +1,115 @@
#
# Copyright 2024-present ScyllaDB
#
#
# SPDX-License-Identifier: AGPL-3.0-or-later
#
find_package(PkgConfig QUIET REQUIRED)
set(saved_pkg_config_path "$ENV{PKG_CONFIG_PATH}")
foreach(config ${CMAKE_CONFIGURATION_TYPES})
# this path should be consistent with the path used by configure.py, which configures
# seastar's building system for each enabled build mode / configuration type
set(binary_dir "${CMAKE_BINARY_DIR}/${config}/seastar")
set(ENV{PKG_CONFIG_PATH} "${saved_pkg_config_path}:${CMAKE_BINARY_DIR}/${config}/seastar")
pkg_search_module(seastar-${config}
REQUIRED QUIET
NO_CMAKE_PATH
IMPORTED_TARGET GLOBAL
seastar)
find_path(seastar_INCLUDE_DIR
NAMES seastar/core/seastar.hh
HINTS
${seastar-${config}_INCLUDE_DIRS})
pkg_search_module(seastar_testing-${config}
REQUIRED QUIET
NO_CMAKE_PATH
IMPORTED_TARGET GLOBAL
seastar-testing)
find_path(seastar_testing_INCLUDE_DIR
NAMES seastar/testing/seastar_test.hh
HINTS
${seastar_testing-${config}_INCLUDE_DIRS})
mark_as_advanced(
seastar_INCLUDE_DIR
seastar_testing_INCLUDE_DIR)
endforeach(config ${CMAKE_CONFIGURATION_TYPES})
set(ENV{PKG_CONFIG_PATH} "${saved_pkg_config_path}")
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Seastar
REQUIRED_VARS
seastar_INCLUDE_DIR
seastar_testing_INCLUDE_DIR)
foreach(config ${CMAKE_CONFIGURATION_TYPES})
# this directory is created when building Seastar, but we create it manually.
# otherwise CMake would complain when configuring targets which link against the
# "Seastar::seastar" library. it is imported library. and CMake assures that
# all INTERFACE_INCLUDE_DIRECTORIES of an imported library should exist.
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/${config}/seastar/gen/include)
endforeach()
foreach(component "seastar" "seastar_testing" "seastar_perf_testing")
if(NOT TARGET Seastar::${component})
add_library(Seastar::${component} UNKNOWN IMPORTED)
foreach(config ${CMAKE_CONFIGURATION_TYPES})
set_property(TARGET Seastar::${component} APPEND
PROPERTY
IMPORTED_CONFIGURATIONS ${config})
string (TOUPPER ${config} CONFIG)
if(config MATCHES "Debug|Dev")
set_property(TARGET Seastar::${component}
PROPERTY
IMPORTED_LOCATION_${CONFIG} "${CMAKE_BINARY_DIR}/${config}/seastar/lib${component}.so")
set(prefix ${component}-${config})
else()
set_property(TARGET Seastar::${component}
PROPERTY
IMPORTED_LOCATION_${CONFIG} "${CMAKE_BINARY_DIR}/${config}/seastar/lib${component}.a")
set(prefix ${component}-${config}_STATIC)
endif()
# these two libraries provide .pc, so set the properties retrieved with
# pkg-config.
if(component MATCHES "^(seastar|seastar_testing)$")
set_property(TARGET Seastar::${component} APPEND
PROPERTY
INTERFACE_INCLUDE_DIRECTORIES $<$<CONFIG:${config}>:${${prefix}_INCLUDE_DIRS}>)
set_property(TARGET Seastar::${component} APPEND
PROPERTY
INTERFACE_LINK_LIBRARIES $<$<CONFIG:${config}>:${${prefix}_LINK_LIBRARIES}>)
set_property(TARGET Seastar::${component} APPEND
PROPERTY
INTERFACE_LINK_OPTIONS $<$<CONFIG:${config}>:${${prefix}_LDFLAGS}>)
set_property(TARGET Seastar::${component} APPEND
PROPERTY
INTERFACE_COMPILE_OPTIONS $<$<CONFIG:${config}>:${${prefix}_CFLAGS_OTHER}>)
endif()
endforeach()
# seastar_perf_testing does not provide its .pc, so let's just hardwire its
# properties
if(component STREQUAL "seastar_perf_testing")
set_target_properties(Seastar::seastar_perf_testing PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${Seastar_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES Seastar::seastar)
endif()
endif()
endforeach()
if(NOT TARGET Seastar::iotune)
add_executable(Seastar::iotune IMPORTED)
foreach(config ${CMAKE_CONFIGURATION_TYPES})
set_property(TARGET Seastar::iotune APPEND
PROPERTY
IMPORTED_CONFIGURATIONS ${config})
string (TOUPPER ${config} CONFIG)
set_property(TARGET Seastar::iotune
PROPERTY
IMPORTED_LOCATION_${CONFIG} ${CMAKE_BINARY_DIR}/$<CONFIG>/seastar/apps/iotune/iotune)
endforeach()
endif()

View File

@@ -2528,6 +2528,10 @@ def configure_using_cmake(args):
source_dir = os.path.realpath(os.path.dirname(__file__))
build_dir = os.path.join(source_dir, 'build')
if not args.dist_only:
for mode in selected_modes:
configure_seastar(build_dir, build_modes[mode].cmake_build_type, modes[mode])
cmake_command = ['cmake']
cmake_command += [f'-D{var}={value}' for var, value in settings.items()]
cmake_command += ['-G', 'Ninja Multi-Config',

15
dist/CMakeLists.txt vendored
View File

@@ -45,12 +45,17 @@ endfunction()
add_stripped("${CMAKE_BINARY_DIR}/$<CONFIG>/scylla")
# app_iotune is located in seastar/apps/iotune
set(iotune_path "${CMAKE_BINARY_DIR}/$<CONFIG>/iotune")
if(TARGET Seastar::iotune)
set(iotune_src "$<TARGET_PROPERTY:Seastar::iotune,IMPORTED_LOCATION>")
else()
set(iotune_src "$<TARGET_FILE:app_iotune>")
endif()
set(iotune_dst "${CMAKE_BINARY_DIR}/$<CONFIG>/iotune")
add_custom_command(
OUTPUT "${iotune_path}"
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:app_iotune> "${iotune_path}"
DEPENDS $<OUTPUT_CONFIG:$<TARGET_FILE:app_iotune>>)
add_stripped("${iotune_path}")
OUTPUT "${iotune_dst}"
COMMAND ${CMAKE_COMMAND} -E copy "${iotune_src}" "${iotune_dst}"
DEPENDS $<OUTPUT_CONFIG:${iotune_src}>)
add_stripped("${iotune_dst}")
find_program(TAR_COMMAND tar
REQUIRED)