]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
cmake: create Boost::asio target with conditional uring support
authorKefu Chai <k.chai@proxmox.com>
Wed, 4 Mar 2026 07:36:59 +0000 (15:36 +0800)
committerKefu Chai <k.chai@proxmox.com>
Thu, 5 Mar 2026 06:33:54 +0000 (14:33 +0800)
BOOST_ASIO_HAS_IO_URING is a user-defined macro that enables io_uring
support in Boost.Asio for specific operations that explicitly opt in,
such as file I/O via basic_file (BOOST_ASIO_HAS_FILE) and buffer
registration. Unlike BOOST_ASIO_HAS_IO_URING_AS_DEFAULT, it does NOT
replace epoll as the default event loop. Currently, Ceph does not use
Boost.Asio to perform file I/O, so this macro only affects compilation,
not runtime behavior.

When BOOST_ASIO_HAS_IO_URING is defined, the umbrella header
boost/asio.hpp pulls in <liburing.h> via:

  boost/asio.hpp
    -> boost/asio/basic_file.hpp  (guarded by BOOST_ASIO_HAS_FILE)
      -> boost/asio/detail/io_uring_file_service.hpp
        -> boost/asio/detail/io_uring_descriptor_service.hpp
          -> boost/asio/detail/io_uring_service.hpp
            -> <liburing.h>

PR #50821 introduced BOOST_ASIO_HAS_IO_URING (commit 05c341b30de) as
preparation for uring-based file I/O in rgw/posix. To avoid pulling
liburing headers (which define macros like BLOCK_SIZE and DATA_OFFSET
that conflict with Ceph code) into unrelated translation units, that PR
also converted many files to use granular Boost.Asio includes instead of
the umbrella boost/asio.hpp.

However, BOOST_ASIO_HAS_IO_URING was added as a global compile
definition, and individual targets linked uring::uring directly. This
has two problems:

1. The uring::uring CMake target only exists when WITH_LIBURING=ON, so
   targets that link it unconditionally break when WITH_LIBURING=OFF.
2. The global definition causes all translation units to see
   BOOST_ASIO_HAS_IO_URING, even those that don't need io_uring.

Since neither CMake's FindBoost module nor Boost's CMake config file
defines a Boost::asio target, introduce one as an INTERFACE IMPORTED
library. When WITH_LIBURING=ON, the Boost::asio target carries the
BOOST_ASIO_HAS_IO_URING definition and uring::uring dependency; when
WITH_LIBURING=OFF, it is an empty interface. Targets that don't need
io_uring should use Boost::headers or Boost::boost instead.

This also improves code organization:

- Scope the BOOST_ASIO_HAS_IO_URING compile definition to Boost::asio
  instead of adding it globally, so only targets that actually use
  Boost.Asio with io_uring see the definition.
- Place the uring-in-Boost::asio configuration next to where we
  build/detect Boost libraries for better readability.
- Set the Boost::asio properties in a single place for better
  maintainability.

See also
05c341b30deab327444eac464e24a840dae25083
f479bbc4bc118989849fb663b5dcf5f46f05097d

Signed-off-by: Kefu Chai <k.chai@proxmox.com>
CMakeLists.txt

index f068cf7fb6c81b65f9553802c1fb8241e356f3a2..82f49cab0898176217524719d79ee54101c81f6d 100644 (file)
@@ -288,11 +288,6 @@ if(WITH_LIBURING)
     include(Builduring)
     build_uring()
   endif()
-  # enable uring in boost::asio
-
-  if(CMAKE_SYSTEM_VERSION VERSION_GREATER_EQUAL "5.10")
-    add_compile_definitions("BOOST_ASIO_HAS_IO_URING")
-  endif()
 endif()
 
 CMAKE_DEPENDENT_OPTION(WITH_BLUESTORE_PMEM "Enable PMDK libraries" OFF
@@ -806,6 +801,13 @@ else()
     COMPONENTS ${BOOST_COMPONENTS} ${BOOST_HEADER_COMPONENTS})
 endif()
 include_directories(BEFORE SYSTEM ${Boost_INCLUDE_DIRS})
+add_library(Boost::asio INTERFACE IMPORTED)
+if(WITH_LIBURING AND CMAKE_SYSTEM_VERSION VERSION_GREATER_EQUAL "5.10")
+  # enable uring in boost::asio
+  set_target_properties(Boost::asio PROPERTIES
+    INTERFACE_COMPILE_DEFINITIONS "BOOST_ASIO_HAS_IO_URING"
+    INTERFACE_LINK_LIBRARIES "Boost::headers;uring::uring")
+endif()
 
 # dashboard angular2 frontend
 option(WITH_MGR_DASHBOARD_FRONTEND "Build the mgr/dashboard frontend using `npm`" ON)