|
| 1 | +# ublksrv (ublk userspace library; autotools project, no CMake). |
| 2 | +# Only the lib/ directory (pure-C libublksrv) is built: |
| 3 | +# - avoids the C++20/-fcoroutines requirement of the ublk CLI tool; |
| 4 | +# - libnfs/libiscsi/gnutls only serve its bundled targets, all disabled. |
| 5 | +# liburing is built from source by Findliburing.cmake and fed to configure |
| 6 | +# via pkg-config/CFLAGS/LDFLAGS (the official build_with_liburing_src flow). |
| 7 | +# License: lib/ + include/ublksrv.h are dual MIT/LGPL; linked under MIT. |
| 8 | +include(FetchContent) |
| 9 | +set(FETCHCONTENT_QUIET false) |
| 10 | + |
| 11 | +if(POLICY CMP0169) |
| 12 | + cmake_policy(SET CMP0169 OLD) |
| 13 | +endif() |
| 14 | + |
| 15 | +# the autotools build needs extra host tools; fail early with a clear message |
| 16 | +find_program(AUTORECONF_EXECUTABLE autoreconf) |
| 17 | +find_program(LIBTOOLIZE_EXECUTABLE libtoolize) |
| 18 | +find_program(AUTOMAKE_EXECUTABLE automake) |
| 19 | +if(NOT AUTORECONF_EXECUTABLE OR NOT LIBTOOLIZE_EXECUTABLE OR NOT AUTOMAKE_EXECUTABLE) |
| 20 | + message(FATAL_ERROR |
| 21 | + "BUILD_UBLK_FRONTEND requires autotools (autoconf/automake/libtool) " |
| 22 | + "to build ublksrv. Install them or configure with -DBUILD_UBLK_FRONTEND=off") |
| 23 | +endif() |
| 24 | + |
| 25 | +FetchContent_Declare( |
| 26 | + ublksrv |
| 27 | + GIT_REPOSITORY https://github.com/ublk-org/ublksrv.git |
| 28 | + GIT_TAG f6c643952d1cdc7f6460630638fe6b5454ca1c4d # v1.7 |
| 29 | +) |
| 30 | + |
| 31 | +FetchContent_GetProperties(ublksrv) |
| 32 | +if(NOT ublksrv_POPULATED) |
| 33 | + FetchContent_Populate(ublksrv) |
| 34 | +endif() |
| 35 | + |
| 36 | +if(NOT TARGET libublksrv_build) |
| 37 | + add_custom_command( |
| 38 | + OUTPUT ${ublksrv_SOURCE_DIR}/lib/.libs/libublksrv.a |
| 39 | + WORKING_DIRECTORY ${ublksrv_SOURCE_DIR} |
| 40 | + COMMAND autoreconf -i |
| 41 | + COMMAND ${CMAKE_COMMAND} -E env PKG_CONFIG_PATH=${LIBURING_PC_DIR} |
| 42 | + ./configure |
| 43 | + --without-libnfs --without-libiscsi --without-gnutls |
| 44 | + # configure appends -fcoroutines whenever $CXX matches *g++* |
| 45 | + # (GCC 10+ only, and only the ublk tool needs it, lib/ does not); |
| 46 | + # use the name c++ to sidestep that match |
| 47 | + CXX=c++ |
| 48 | + "CFLAGS=-I${LIBURING_INCLUDE_DIRS} -O2" |
| 49 | + "CXXFLAGS=-I${LIBURING_INCLUDE_DIRS} -O2" |
| 50 | + "LDFLAGS=-L${liburing_SOURCE_DIR}/src" |
| 51 | + COMMAND make -C lib -j |
| 52 | + DEPENDS ${LIBURING_LIBRARIES} |
| 53 | + COMMENT "Building libublksrv (lib/ only) with autotools" |
| 54 | + VERBATIM |
| 55 | + ) |
| 56 | + add_custom_target(libublksrv_build DEPENDS ${ublksrv_SOURCE_DIR}/lib/.libs/libublksrv.a) |
| 57 | + add_dependencies(libublksrv_build liburing_build) |
| 58 | +endif() |
| 59 | + |
| 60 | +set(UBLKSRV_LIBRARIES ${ublksrv_SOURCE_DIR}/lib/.libs/libublksrv.a) |
| 61 | +set(UBLKSRV_INCLUDE_DIRS ${ublksrv_SOURCE_DIR}/include) |
| 62 | + |
| 63 | +if(NOT TARGET libublksrv_static) |
| 64 | + add_library(libublksrv_static STATIC IMPORTED GLOBAL) |
| 65 | + set_target_properties(libublksrv_static PROPERTIES |
| 66 | + IMPORTED_LOCATION ${UBLKSRV_LIBRARIES} |
| 67 | + INTERFACE_INCLUDE_DIRECTORIES ${UBLKSRV_INCLUDE_DIRS} |
| 68 | + # libublksrv.a references liburing symbols; propagate via INTERFACE so |
| 69 | + # consumers get the correct link order automatically |
| 70 | + INTERFACE_LINK_LIBRARIES liburing_static |
| 71 | + ) |
| 72 | +endif() |
| 73 | +add_dependencies(libublksrv_static libublksrv_build) |
| 74 | + |
| 75 | +include(FindPackageHandleStandardArgs) |
| 76 | +find_package_handle_standard_args(ublksrv DEFAULT_MSG UBLKSRV_LIBRARIES UBLKSRV_INCLUDE_DIRS) |
| 77 | + |
| 78 | +mark_as_advanced(UBLKSRV_LIBRARIES UBLKSRV_INCLUDE_DIRS) |
0 commit comments