From f52edab438a565faf2d32437f3774fb2c14c4706 Mon Sep 17 00:00:00 2001 From: Bob Chen Date: Sat, 26 Aug 2023 23:21:04 +0800 Subject: [PATCH] Support build dependencies from source --- .github/workflows/ci.linux.arm.yml | 11 +- .github/workflows/ci.linux.x86.yml | 18 +- .github/workflows/ci.macos.yml | 6 +- CMake/Findcurl.cmake | 7 + CMake/Finde2fs.cmake | 14 +- CMake/Findfstack.cmake | 6 + CMake/Findgflags.cmake | 5 +- CMake/Findgmock.cmake | 7 - CMake/Findgoogletest.cmake | 17 ++ CMake/Findopenssl.cmake | 10 + CMake/Finduring.cmake | 22 +- CMake/Findzlib.cmake | 7 + CMake/build-from-src.cmake | 75 +++++ CMakeLists.txt | 293 ++++++++++---------- doc/blog/2023-07-29-photon-dpdk.md | 2 +- doc/docs/introduction/how-to-build.md | 40 +-- doc/docs/introduction/how-to-integrate.md | 6 +- doc/docs/performance/network-performance.md | 2 +- tools/libaio.map | 19 ++ 19 files changed, 350 insertions(+), 217 deletions(-) create mode 100644 CMake/Findcurl.cmake delete mode 100644 CMake/Findgmock.cmake create mode 100644 CMake/Findgoogletest.cmake create mode 100644 CMake/Findopenssl.cmake create mode 100644 CMake/Findzlib.cmake create mode 100644 CMake/build-from-src.cmake create mode 100644 tools/libaio.map diff --git a/.github/workflows/ci.linux.arm.yml b/.github/workflows/ci.linux.arm.yml index 3a99e5e6..c39060f6 100644 --- a/.github/workflows/ci.linux.arm.yml +++ b/.github/workflows/ci.linux.arm.yml @@ -23,15 +23,16 @@ jobs: dnf install -y gcc-toolset-9-gcc-c++ dnf install -y openssl-devel libcurl-devel libaio-devel dnf install -y epel-release - dnf install -y fuse-devel libgsasl-devel + dnf install -y fuse-devel libgsasl-devel e2fsprogs-devel - name: Build run: | source /opt/rh/gcc-toolset-9/enable - cmake -B build -D BUILD_TESTING=ON -D CMAKE_BUILD_TYPE=RelWithDebInfo \ - -D ENABLE_SASL=ON -D ENABLE_FUSE=ON \ - -D ENABLE_URING=OFF -D FETCH_GTEST_GFLAGS_SOURCE=ON - cmake --build build + cmake -B build -D CMAKE_BUILD_TYPE=RelWithDebInfo \ + -D PHOTON_BUILD_DEPENDENCIES=ON -D PHOTON_BUILD_TESTING=ON \ + -D PHOTON_ENABLE_URING=ON -D PHOTON_ENABLE_FUSE=ON \ + -D PHOTON_ENABLE_SASL=ON -D PHOTON_ENABLE_EXTFS=ON + cmake --build build -j - name: Test run: | diff --git a/.github/workflows/ci.linux.x86.yml b/.github/workflows/ci.linux.x86.yml index a946ace7..23672ad1 100644 --- a/.github/workflows/ci.linux.x86.yml +++ b/.github/workflows/ci.linux.x86.yml @@ -28,10 +28,9 @@ jobs: - name: Build run: | source /opt/rh/gcc-toolset-9/enable - cmake -B build -D BUILD_TESTING=ON -D CMAKE_BUILD_TYPE=RelWithDebInfo \ - -D ENABLE_SASL=ON -D ENABLE_FUSE=ON \ - -D ENABLE_URING=OFF -D FETCH_GTEST_GFLAGS_SOURCE=ON - cmake --build build + cmake -B build -D PHOTON_BUILD_TESTING=ON -D CMAKE_BUILD_TYPE=RelWithDebInfo \ + -D PHOTON_ENABLE_SASL=ON -D PHOTON_ENABLE_FUSE=ON + cmake --build build -j - name: Test run: | @@ -56,15 +55,16 @@ jobs: dnf install -y gcc-toolset-9-gcc-c++ dnf install -y openssl-devel libcurl-devel libaio-devel dnf install -y epel-release - dnf install -y fuse-devel libgsasl-devel + dnf install -y fuse-devel libgsasl-devel e2fsprogs-devel - name: Build run: | source /opt/rh/gcc-toolset-9/enable - cmake -B build -D BUILD_TESTING=ON -D CMAKE_BUILD_TYPE=RelWithDebInfo \ - -D ENABLE_SASL=ON -D ENABLE_FUSE=ON \ - -D ENABLE_URING=ON -D FETCH_GTEST_GFLAGS_SOURCE=ON - cmake --build build + cmake -B build -D CMAKE_BUILD_TYPE=RelWithDebInfo \ + -D PHOTON_BUILD_DEPENDENCIES=ON -D PHOTON_BUILD_TESTING=ON \ + -D PHOTON_ENABLE_URING=ON -D PHOTON_ENABLE_FUSE=ON \ + -D PHOTON_ENABLE_SASL=ON -D PHOTON_ENABLE_EXTFS=ON + cmake --build build -j - name: Test run: | diff --git a/.github/workflows/ci.macos.yml b/.github/workflows/ci.macos.yml index 9df0a684..1012853d 100644 --- a/.github/workflows/ci.macos.yml +++ b/.github/workflows/ci.macos.yml @@ -20,9 +20,9 @@ jobs: - name: Build run: | - cmake -B ${{github.workspace}}/build -D BUILD_TESTING=ON -D CMAKE_BUILD_TYPE=Debug \ - -D ENABLE_SASL=ON -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl@3 - cmake --build ${{github.workspace}}/build + cmake -B ${{github.workspace}}/build -D PHOTON_BUILD_TESTING=ON -D CMAKE_BUILD_TYPE=Debug \ + -D PHOTON_ENABLE_SASL=ON -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl@3 + cmake --build ${{github.workspace}}/build -j - name: Test working-directory: ${{github.workspace}}/build diff --git a/CMake/Findcurl.cmake b/CMake/Findcurl.cmake new file mode 100644 index 00000000..46600e2b --- /dev/null +++ b/CMake/Findcurl.cmake @@ -0,0 +1,7 @@ +find_path(CURL_INCLUDE_DIRS curl/curl.h) + +find_library(CURL_LIBRARIES curl) + +find_package_handle_standard_args(curl DEFAULT_MSG CURL_LIBRARIES CURL_INCLUDE_DIRS) + +mark_as_advanced(CURL_INCLUDE_DIRS CURL_LIBRARIES) \ No newline at end of file diff --git a/CMake/Finde2fs.cmake b/CMake/Finde2fs.cmake index eb963bc1..b9f99eeb 100644 --- a/CMake/Finde2fs.cmake +++ b/CMake/Finde2fs.cmake @@ -1,11 +1,7 @@ -if (LIBE2FS_PATH) - find_path(LIBE2FS_INCLUDE_DIRS ext2fs/ext2fs.h HINTS ${LIBE2FS_PATH}/include) - find_library(LIBE2FS_LIBRARIES ext2fs HINTS ${LIBE2FS_PATH}/lib) -else() - find_path(LIBE2FS_INCLUDE_DIRS ext2fs/ext2fs.h) - find_library(LIBE2FS_LIBRARIES ext2fs) -endif() +find_path(E2FS_INCLUDE_DIRS ext2fs/ext2fs.h) -find_package_handle_standard_args(e2fs DEFAULT_MSG LIBE2FS_LIBRARIES LIBE2FS_INCLUDE_DIRS) +find_library(E2FS_LIBRARIES ext2fs) -mark_as_advanced(LIBE2FS_INCLUDE_DIRS LIBE2FS_LIBRARIES) +find_package_handle_standard_args(e2fs DEFAULT_MSG E2FS_LIBRARIES E2FS_INCLUDE_DIRS) + +mark_as_advanced(E2FS_INCLUDE_DIRS E2FS_LIBRARIES) diff --git a/CMake/Findfstack.cmake b/CMake/Findfstack.cmake index 628fe800..cbf028b1 100644 --- a/CMake/Findfstack.cmake +++ b/CMake/Findfstack.cmake @@ -1,7 +1,13 @@ +pkg_check_modules(DPDK REQUIRED libdpdk) + find_path(FSTACK_INCLUDE_DIRS NAMES ff_api.h PATHS /usr/local/include NO_DEFAULT_PATH) find_library(FSTACK_LIBRARIES NAMES fstack PATHS /usr/local/lib NO_DEFAULT_PATH) +set(FSTACK_INCLUDE_DIRS ${FSTACK_INCLUDE_DIRS} ${DPDK_INCLUDE_DIRS}) + +set(FSTACK_LIBRARIES ${FSTACK_LIBRARIES} ${DPDK_STATIC_LDFLAGS}) + find_package_handle_standard_args(fstack DEFAULT_MSG FSTACK_LIBRARIES FSTACK_INCLUDE_DIRS) mark_as_advanced(FSTACK_INCLUDE_DIRS FSTACK_LIBRARIES) \ No newline at end of file diff --git a/CMake/Findgflags.cmake b/CMake/Findgflags.cmake index c93c2374..1ed299f4 100644 --- a/CMake/Findgflags.cmake +++ b/CMake/Findgflags.cmake @@ -1,6 +1,9 @@ find_path(GFLAGS_INCLUDE_DIRS gflags/gflags.h) -find_library(GFLAGS_LIBRARIES gflags) +find_library(GFLAGS_STANDARD_LIBRARIES gflags) +find_library(GFLAGS_NO_THREADS_LIBRARIES gflags_nothreads) + +set(GFLAGS_LIBRARIES ${GFLAGS_STANDARD_LIBRARIES} ${GFLAGS_NO_THREADS_LIBRARIES}) find_package_handle_standard_args(gflags DEFAULT_MSG GFLAGS_LIBRARIES GFLAGS_INCLUDE_DIRS) diff --git a/CMake/Findgmock.cmake b/CMake/Findgmock.cmake deleted file mode 100644 index f6019fad..00000000 --- a/CMake/Findgmock.cmake +++ /dev/null @@ -1,7 +0,0 @@ -find_path(GMOCK_INCLUDE_DIRS gmock/gmock.h) - -find_library(GMOCK_LIBRARIES gmock) - -find_package_handle_standard_args(gmock DEFAULT_MSG GMOCK_LIBRARIES GMOCK_INCLUDE_DIRS) - -mark_as_advanced(GMOCK_INCLUDE_DIRS GMOCK_LIBRARIES) \ No newline at end of file diff --git a/CMake/Findgoogletest.cmake b/CMake/Findgoogletest.cmake new file mode 100644 index 00000000..b88ede98 --- /dev/null +++ b/CMake/Findgoogletest.cmake @@ -0,0 +1,17 @@ +find_path(GOOGLETEST_INCLUDE_DIRS gtest/gtest.h gmock/gmock.h) + +find_library(GOOGLETEST_GTEST_LIBRARIES gtest) +find_library(GOOGLETEST_GTEST_MAIN_LIBRARIES gtest_main) +find_library(GOOGLETEST_GMOCK_LIBRARIES gmock) +find_library(GOOGLETEST_GMOCK_MAIN_LIBRARIES gmock_main) + +set(GOOGLETEST_LIBRARIES + ${GOOGLETEST_GTEST_LIBRARIES} + ${GOOGLETEST_GTEST_MAIN_LIBRARIES} + ${GOOGLETEST_GMOCK_LIBRARIES} + ${GOOGLETEST_GMOCK_MAIN_LIBRARIES} +) + +find_package_handle_standard_args(googletest DEFAULT_MSG GOOGLETEST_LIBRARIES GOOGLETEST_INCLUDE_DIRS) + +mark_as_advanced(GOOGLETEST_INCLUDE_DIRS GOOGLETEST_LIBRARIES) \ No newline at end of file diff --git a/CMake/Findopenssl.cmake b/CMake/Findopenssl.cmake new file mode 100644 index 00000000..6d76b3bb --- /dev/null +++ b/CMake/Findopenssl.cmake @@ -0,0 +1,10 @@ +find_path(OPENSSL_INCLUDE_DIRS openssl/ssl.h openssl/crypto.h) + +find_library(OPENSSL_SSL_LIBRARIES ssl) +find_library(OPENSSL_CRYPTO_LIBRARIES crypto) + +set(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARIES} ${OPENSSL_CRYPTO_LIBRARIES}) + +find_package_handle_standard_args(openssl DEFAULT_MSG OPENSSL_LIBRARIES OPENSSL_INCLUDE_DIRS) + +mark_as_advanced(OPENSSL_INCLUDE_DIRS OPENSSL_LIBRARIES) \ No newline at end of file diff --git a/CMake/Finduring.cmake b/CMake/Finduring.cmake index 56cec570..5899c826 100644 --- a/CMake/Finduring.cmake +++ b/CMake/Finduring.cmake @@ -1,23 +1,7 @@ -# * Find liburing Find the liburing library and includes -# -# URING_INCLUDE_DIR - where to find liburing.h, etc. URING_LIBRARIES - List of -# libraries when using uring. URING_FOUND - True if uring found. +find_path(URING_INCLUDE_DIRS liburing.h) -find_path(URING_INCLUDE_DIR liburing.h) find_library(URING_LIBRARIES uring) -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(uring DEFAULT_MSG URING_LIBRARIES - URING_INCLUDE_DIR) +find_package_handle_standard_args(uring DEFAULT_MSG URING_LIBRARIES URING_INCLUDE_DIRS) -if(URING_FOUND) - if(NOT TARGET uring) - add_library(uring UNKNOWN IMPORTED) - endif() - set_target_properties( - uring - PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${URING_INCLUDE_DIR}" - IMPORTED_LINK_INTERFACE_LANGUAGES "C" - IMPORTED_LOCATION "${URING_LIBRARIES}") - mark_as_advanced(URING_LIBRARIES) -endif() +mark_as_advanced(URING_INCLUDE_DIRS URING_LIBRARIES) \ No newline at end of file diff --git a/CMake/Findzlib.cmake b/CMake/Findzlib.cmake new file mode 100644 index 00000000..35091eb3 --- /dev/null +++ b/CMake/Findzlib.cmake @@ -0,0 +1,7 @@ +find_path(ZLIB_INCLUDE_DIRS zlib.h) + +find_library(ZLIB_LIBRARIES z) + +find_package_handle_standard_args(zlib DEFAULT_MSG ZLIB_LIBRARIES ZLIB_INCLUDE_DIRS) + +mark_as_advanced(ZLIB_INCLUDE_DIRS ZLIB_LIBRARIES) \ No newline at end of file diff --git a/CMake/build-from-src.cmake b/CMake/build-from-src.cmake new file mode 100644 index 00000000..a6a5bd6d --- /dev/null +++ b/CMake/build-from-src.cmake @@ -0,0 +1,75 @@ +# Note: Be aware of the differences between CMake project and Makefile project. + +set(actually_built) + +function(build_from_src [dep]) + if (dep STREQUAL "aio") + set(BINARY_DIR ${PROJECT_BINARY_DIR}/aio-build) + ExternalProject_Add( + aio + URL ${PHOTON_AIO_SOURCE} + BUILD_IN_SOURCE ON + CONFIGURE_COMMAND "" + BUILD_COMMAND make prefix=${BINARY_DIR} install -j + INSTALL_COMMAND "" + ) + set(AIO_INCLUDE_DIRS ${BINARY_DIR}/include PARENT_SCOPE) + set(AIO_LIBRARIES ${BINARY_DIR}/lib/libaio.a PARENT_SCOPE) + + elseif (dep STREQUAL "zlib") + set(BINARY_DIR ${PROJECT_BINARY_DIR}/zlib-build) + ExternalProject_Add( + zlib + URL ${PHOTON_ZLIB_SOURCE} + BUILD_IN_SOURCE ON + PATCH_COMMAND sed -ie "/CFLAGS.*CFLAGS--O3/ s/}/} -fPIC/" configure + CONFIGURE_COMMAND ./configure --prefix=${BINARY_DIR} --static + BUILD_COMMAND make -j + INSTALL_COMMAND make install + ) + set(ZLIB_INCLUDE_DIRS ${BINARY_DIR}/include PARENT_SCOPE) + set(ZLIB_LIBRARIES ${BINARY_DIR}/lib/libz.a PARENT_SCOPE) + + elseif (dep STREQUAL "uring") + set(BINARY_DIR ${PROJECT_BINARY_DIR}/uring-build) + ExternalProject_Add( + uring + GIT_REPOSITORY ${PHOTON_URING_SOURCE} + GIT_TAG liburing-2.3 + GIT_PROGRESS ON + BUILD_IN_SOURCE ON + CONFIGURE_COMMAND ./configure --prefix=${BINARY_DIR} + BUILD_COMMAND make -C src -j + INSTALL_COMMAND make install + ) + set(URING_INCLUDE_DIRS ${BINARY_DIR}/include PARENT_SCOPE) + set(URING_LIBRARIES ${BINARY_DIR}/lib/liburing.a PARENT_SCOPE) + + elseif (dep STREQUAL "gflags") + ExternalProject_Add( + gflags + URL ${PHOTON_GFLAGS_SOURCE} + CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_POSITION_INDEPENDENT_CODE=ON + INSTALL_COMMAND "" + ) + ExternalProject_Get_Property(gflags BINARY_DIR) + set(GFLAGS_INCLUDE_DIRS ${BINARY_DIR}/include PARENT_SCOPE) + set(GFLAGS_LIBRARIES ${BINARY_DIR}/lib/libgflags.a ${BINARY_DIR}/lib/libgflags_nothreads.a PARENT_SCOPE) + + elseif (dep STREQUAL "googletest") + ExternalProject_Add( + googletest + URL ${PHOTON_GOOGLETEST_SOURCE} + CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DINSTALL_GTEST=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON + INSTALL_COMMAND "" + ) + ExternalProject_Get_Property(googletest SOURCE_DIR) + ExternalProject_Get_Property(googletest BINARY_DIR) + set(GOOGLETEST_INCLUDE_DIRS ${SOURCE_DIR}/googletest/include ${SOURCE_DIR}/googlemock/include PARENT_SCOPE) + set(GOOGLETEST_LIBRARIES ${BINARY_DIR}/lib/libgmock.a ${BINARY_DIR}/lib/libgmock_main.a + ${BINARY_DIR}/lib/libgtest.a ${BINARY_DIR}/lib/libgtest_main.a PARENT_SCOPE) + endif () + + list(APPEND actually_built ${dep}) + set(actually_built ${actually_built} PARENT_SCOPE) +endfunction() diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dc0e00a..89d4de44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,18 +11,32 @@ include(FindPackageHandleStandardArgs) include(CheckCXXCompilerFlag) include(FetchContent) include(ProcessorCount) -set(FETCHCONTENT_QUIET false) +include(ExternalProject) +include(CMake/build-from-src.cmake) find_package(PkgConfig REQUIRED) # Options -option(ENABLE_URING "enable io_uring function" OFF) -option(ENABLE_FUSE "enable fuse function" OFF) -option(ENABLE_SASL "enable sasl" OFF) -option(ENABLE_MIMIC_VDSO "enable mimic vdso" OFF) -option(BUILD_TESTING "enable build testing" OFF) -option(FETCH_GTEST_GFLAGS_SOURCE "Fetch gtest, gmock and gflags source code. Link their static libs" OFF) -option(ENABLE_FSTACK_DPDK "Use f-stack + DPDK as the event engine" OFF) -option(ENABLE_EXTFS "enable extfs" OFF) +set(PHOTON_CXX_STANDARD "14" CACHE STRING "C++ standard") +option(PHOTON_BUILD_TESTING "enable build testing" OFF) +option(PHOTON_ENABLE_URING "enable io_uring function" OFF) +option(PHOTON_ENABLE_FUSE "enable fuse function" OFF) +option(PHOTON_ENABLE_SASL "enable sasl" OFF) +option(PHOTON_ENABLE_MIMIC_VDSO "enable mimic vdso" OFF) +option(PHOTON_ENABLE_FSTACK_DPDK "Use f-stack + DPDK as the event engine" OFF) +option(PHOTON_ENABLE_EXTFS "enable extfs" OFF) + +option(PHOTON_BUILD_DEPENDENCIES "" OFF) +set(PHOTON_AIO_SOURCE "https://pagure.io/libaio/archive/libaio-0.3.113/libaio-0.3.113.tar.gz" CACHE STRING "") +set(PHOTON_ZLIB_SOURCE "https://github.com/madler/zlib/releases/download/v1.2.13/zlib-1.2.13.tar.gz" CACHE STRING "") +set(PHOTON_OPENSSL_SOURCE "" CACHE STRING "") +set(PHOTON_CURL_SOURCE "" CACHE STRING "") +set(PHOTON_URING_SOURCE "https://github.com/axboe/liburing.git" CACHE STRING "") +set(PHOTON_FUSE_SOURCE "" CACHE STRING "") +set(PHOTON_GSASL_SOURCE "" CACHE STRING "") +set(PHOTON_FSTACK_SOURCE "" CACHE STRING "") +set(PHOTON_E2FS_SOURCE "" CACHE STRING "") +set(PHOTON_GFLAGS_SOURCE "https://github.com/gflags/gflags/archive/refs/tags/v2.2.2.tar.gz" CACHE STRING "") +set(PHOTON_GOOGLETEST_SOURCE "https://github.com/google/googletest/archive/refs/tags/release-1.12.1.tar.gz" CACHE STRING "") # Get CPU arch and number execute_process(COMMAND uname -m OUTPUT_VARIABLE ARCH OUTPUT_STRIP_TRAILING_WHITESPACE) @@ -34,7 +48,7 @@ ProcessorCount(NumCPU) # Compiler options add_compile_options(-Wall) # -Werror is not enable yet -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD ${PHOTON_CXX_STANDARD}) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) # CMake didn't provide an abstraction of optimization level for now. @@ -78,79 +92,58 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/output) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/output) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/output) -# Third party -add_subdirectory(third_party) +################################################################################ + +# There are two ways to handle dependencies: +# 1. Find locally installed packages. (Static lib is suggested) +# 2. Build it from source. (Only when PHOTON_BUILD_DEPENDENCIES is set, and PHOTON_XXX_SOURCE is not empty) +# +# The naming conventions MUST obey: +# name: xxx +# include: XXX_INCLUDE_DIRS +# lib: XXX_LIBRARIES +# source: PHOTON_XXX_SOURCE + +set(dependencies zlib openssl curl) -# Find packages either from cmake-modules or external packages(in CMake dir) if (CMAKE_SYSTEM_NAME MATCHES "Linux") - find_package(aio REQUIRED) - if (ENABLE_FUSE) - find_package(fuse REQUIRED) + LIST(APPEND dependencies aio) + if (PHOTON_ENABLE_FUSE) + LIST(APPEND dependencies fuse) endif () - if (ENABLE_URING) - find_package(uring) - if (NOT URING_FOUND) - message("liburing not found, fetch source and compile it") - # Fetch liburing - FetchContent_Declare( - liburing - GIT_REPOSITORY https://github.com/axboe/liburing.git - # GIT_REPOSITORY https://gitee.com/mirrors_axboe/liburing.git - GIT_TAG liburing-2.3 - UPDATE_COMMAND ./configure - ) - FetchContent_MakeAvailable(liburing) - set(liburing_include_dir ${liburing_SOURCE_DIR}/src/include) - add_library( - uring - STATIC - ${liburing_SOURCE_DIR}/src/setup.c - ${liburing_SOURCE_DIR}/src/queue.c - ${liburing_SOURCE_DIR}/src/register.c - ${liburing_SOURCE_DIR}/src/syscall.c - ) - # CMake didn't provide an abstraction of optimization level for now. - # When multiple -O options appear, only the last one is effective - target_compile_options(uring PRIVATE -O3 -Wall -Wextra -fno-stack-protector -Wno-unused-parameter -Wno-sign-compare) - target_compile_definitions(uring PRIVATE _GNU_SOURCE LIBURING_INTERNAL) - target_include_directories(uring PRIVATE ${liburing_include_dir}) - else() - message("liburing found") - endif() + if (PHOTON_ENABLE_URING) + LIST(APPEND dependencies uring) endif () -endif() -find_package(OpenSSL REQUIRED) -find_package(ZLIB REQUIRED) -find_package(CURL REQUIRED) -if (ENABLE_SASL) - find_package(gsasl REQUIRED) endif () -if (ENABLE_EXTFS) - find_package(e2fs REQUIRED) +if (PHOTON_ENABLE_SASL) + LIST(APPEND dependencies gsasl) +endif () +if (PHOTON_ENABLE_FSTACK_DPDK) + LIST(APPEND dependencies fstack) +endif () +if (PHOTON_ENABLE_EXTFS) + LIST(APPEND dependencies e2fs) endif() +if (PHOTON_BUILD_TESTING) + LIST(APPEND dependencies gflags googletest) +endif () -function(do_fetch_gtest_gflags_src) - set(BUILD_TESTING OFF) # Only disable in this function - FetchContent_Declare( - gflags - GIT_REPOSITORY https://github.com/gflags/gflags.git - # GIT_REPOSITORY https://gitee.com/mirrors/gflags.git - GIT_TAG v2.2.2 - GIT_SUBMODULES "" - ) - FetchContent_MakeAvailable(gflags) - FetchContent_Declare( - googletest - GIT_REPOSITORY https://github.com/google/googletest.git - # GIT_REPOSITORY https://gitee.com/mirrors/googletest.git - GIT_TAG release-1.12.1 - ) - FetchContent_MakeAvailable(googletest) -endfunction() +FOREACH (dep ${dependencies}) + string(TOUPPER ${dep} DEP) + set(source_url "${PHOTON_${DEP}_SOURCE}") + if (PHOTON_BUILD_DEPENDENCIES AND (NOT source_url STREQUAL "")) + message(STATUS "Will build ${dep} from source") + message(STATUS " URL: ${source_url}") + build_from_src(dep) + else () + message(STATUS "Will find ${dep}") + find_package(${dep} REQUIRED) + endif () +endforeach () -if (FETCH_GTEST_GFLAGS_SOURCE) - do_fetch_gtest_gflags_src() -endif() +################################################################################ + +add_subdirectory(third_party) # Compile photon objects file(GLOB PHOTON_SRC @@ -182,36 +175,40 @@ if (APPLE) list(APPEND PHOTON_SRC io/kqueue.cpp) else () list(APPEND PHOTON_SRC io/aio-wrapper.cpp io/epoll.cpp) - if (ENABLE_URING) + if (PHOTON_ENABLE_URING) list(APPEND PHOTON_SRC io/iouring-wrapper.cpp) endif () endif () -if (ENABLE_FUSE) +if (PHOTON_ENABLE_FUSE) list(APPEND PHOTON_SRC io/fuse-adaptor.cpp) endif () -if (ENABLE_SASL) +if (PHOTON_ENABLE_SASL) list(APPEND PHOTON_SRC net/security-context/sasl-stream.cpp) endif () -if (ENABLE_FSTACK_DPDK) +if (PHOTON_ENABLE_FSTACK_DPDK) list(APPEND PHOTON_SRC io/fstack-dpdk.cpp) endif () -if (ENABLE_EXTFS) +if (PHOTON_ENABLE_EXTFS) file(GLOB EXTFS_SRC fs/extfs/*.cpp) list(APPEND PHOTON_SRC ${EXTFS_SRC}) endif () # An object library compiles source files but does not archive or link their object files. add_library(photon_obj OBJECT ${PHOTON_SRC}) -target_include_directories(photon_obj PUBLIC include ${OPENSSL_INCLUDE_DIR}) +target_include_directories(photon_obj PUBLIC include ${OPENSSL_INCLUDE_DIRS} ${AIO_INCLUDE_DIRS} + ${ZLIB_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS}) target_compile_definitions(photon_obj PRIVATE _FILE_OFFSET_BITS=64 FUSE_USE_VERSION=29) -if (ENABLE_URING) - target_include_directories(photon_obj PUBLIC ${liburing_include_dir}) +if (PHOTON_ENABLE_URING) + target_include_directories(photon_obj PUBLIC ${URING_INCLUDE_DIRS}) target_compile_definitions(photon_obj PRIVATE PHOTON_URING=on) endif() -if (ENABLE_MIMIC_VDSO) +if (PHOTON_ENABLE_MIMIC_VDSO) target_compile_definitions(photon_obj PRIVATE ENABLE_MIMIC_VDSO=on) endif() -if (ENABLE_EXTFS) +if (PHOTON_ENABLE_FSTACK_DPDK) + target_include_directories(photon_obj PUBLIC ${FSTACK_INCLUDE_DIRS}) +endif() +if (PHOTON_ENABLE_EXTFS) target_include_directories(photon_obj PUBLIC ${LIBE2FS_INCLUDE_DIRS}) endif() if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0) @@ -219,79 +216,98 @@ if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0) target_compile_options(photon_obj PRIVATE -faligned-new) endif() -# Add f-stack and dpdk libs. -if (ENABLE_FSTACK_DPDK) - pkg_check_modules(DPDK REQUIRED libdpdk) - find_package(fstack REQUIRED) - target_include_directories(photon_obj PRIVATE ${DPDK_INCLUDE_DIRS} ${FSTACK_INCLUDE_DIRS}) - target_compile_definitions(photon_obj PRIVATE ENABLE_FSTACK_DPDK) +if (actually_built) + add_dependencies(photon_obj ${actually_built}) +endif () - add_library(fstack_dpdk INTERFACE) - target_link_libraries(fstack_dpdk INTERFACE ${DPDK_STATIC_LDFLAGS} ${FSTACK_LIBRARIES}) -endif() +################################################################################ -# Make virtual interface for external dynamic libs. -add_library(external_lib INTERFACE) -set(EXTERNAL_LIB_ARGS - ZLIB::ZLIB - OpenSSL::SSL - OpenSSL::Crypto - CURL::libcurl +set(static_deps + easy_weak + fstack_weak + ${ZLIB_LIBRARIES} + ${OPENSSL_LIBRARIES} + ${CURL_LIBRARIES} +) +set(shared_deps -lpthread - ) - +) if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - list(APPEND EXTERNAL_LIB_ARGS -lgcc) # solve [hidden symbol `__cpu_model'] problem + list(APPEND shared_deps -lgcc) # solve [hidden symbol `__cpu_model'] problem endif () if (NOT APPLE) - list(APPEND EXTERNAL_LIB_ARGS ${AIO_LIBRARIES} -lrt) - if (ENABLE_FUSE) - list(APPEND EXTERNAL_LIB_ARGS ${FUSE_LIBRARIES}) - endif () + list(APPEND static_deps ${AIO_LIBRARIES}) + list(APPEND shared_deps -lrt -ldl) endif () -if (ENABLE_SASL) - list(APPEND EXTERNAL_LIB_ARGS ${GSASL_LIBRARIES}) +if (PHOTON_ENABLE_URING) + list(APPEND static_deps ${URING_LIBRARIES}) endif () -if (ENABLE_EXTFS) - list(APPEND EXTERNAL_LIB_ARGS ${LIBE2FS_LIBRARIES}) +if (PHOTON_ENABLE_FUSE) + list(APPEND static_deps ${FUSE_LIBRARIES}) endif () -target_link_libraries(external_lib INTERFACE ${EXTERNAL_LIB_ARGS}) +if (PHOTON_ENABLE_SASL) + list(APPEND static_deps ${GSASL_LIBRARIES}) +endif () +if (PHOTON_ENABLE_FSTACK_DPDK) + list(APPEND static_deps ${FSTACK_LIBRARIES}) +endif () +if (PHOTON_ENABLE_EXTFS) + list(APPEND static_deps ${E2FS_LIBRARIES}) +endif () + +# It's OK to have some duplicates both in shared and static deps. +# Even though .a is suggested, we may still probably find .so in local installed dir. +# This is for the max compatability. +if (NOT APPLE) + set(suffix ".*.so") +else () + set(suffix ".*.dylib" ".*.tbd") +endif () +foreach (dep ${static_deps}) + foreach (suf ${suffix}) + if (dep MATCHES "${suf}") + list(APPEND shared_deps ${dep}) + break() + endif () + endforeach () +endforeach () + +set(version_scripts) +list(APPEND version_scripts "-Wl,--version-script=${PROJECT_SOURCE_DIR}/tools/libaio.map") + +add_library(_static_archive INTERFACE) +if (NOT APPLE) + target_link_libraries(_static_archive INTERFACE + ${version_scripts} + -Wl,--whole-archive ${static_deps} -Wl,--no-whole-archive + ) +else () + target_link_libraries(_static_archive INTERFACE + -Wl,-force_load ${static_deps} + ) +endif () + +add_library(photon_shared_deps INTERFACE) +target_link_libraries(photon_shared_deps INTERFACE ${shared_deps}) + +################################################################################ # Link photon shared lib add_library(photon_shared SHARED $) set_target_properties(photon_shared PROPERTIES OUTPUT_NAME photon) -if (APPLE) - set(shared_link_libs external_lib -Wl,-force_load easy_weak) -elseif (ENABLE_URING) - set(shared_link_libs -Wl,--whole-archive easy_weak fstack_weak uring -Wl,--no-whole-archive external_lib) -else() - set(shared_link_libs -Wl,--whole-archive easy_weak fstack_weak -Wl,--no-whole-archive external_lib) -endif () -target_link_libraries(photon_shared ${shared_link_libs}) +target_link_libraries(photon_shared PUBLIC photon_shared_deps PRIVATE _static_archive) # Link photon static lib add_library(photon_static STATIC $) set_target_properties(photon_static PROPERTIES OUTPUT_NAME photon) -if (ENABLE_URING) - set(static_link_libs easy_weak fstack_weak uring external_lib) -else () - set(static_link_libs easy_weak fstack_weak external_lib) -endif () -target_link_libraries(photon_static ${static_link_libs}) +target_link_libraries(photon_static PUBLIC photon_shared_deps PRIVATE ${static_deps}) # Build test cases -if (BUILD_TESTING) +if (PHOTON_BUILD_TESTING) include(CTest) - if (FETCH_GTEST_GFLAGS_SOURCE) - set(testing_libs gtest gmock gflags::gflags) - else () - find_package(GTest REQUIRED) - find_package(gmock REQUIRED) - find_package(gflags REQUIRED) - set(testing_libs ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARIES} ${GFLAGS_LIBRARIES}) - include_directories(${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS} ${GFLAGS_INCLUDE_DIRS}) - endif () + set(testing_libs ${GFLAGS_LIBRARIES} ${GOOGLETEST_LIBRARIES}) + include_directories(${GFLAGS_INCLUDE_DIRS} ${GOOGLETEST_INCLUDE_DIRS}) add_subdirectory(examples) add_subdirectory(common/checksum/test) @@ -303,8 +319,7 @@ if (BUILD_TESTING) add_subdirectory(rpc/test) add_subdirectory(thread/test) add_subdirectory(net/security-context/test) - if (ENABLE_EXTFS) + if (PHOTON_ENABLE_EXTFS) add_subdirectory(fs/extfs/test) endif () - endif () diff --git a/doc/blog/2023-07-29-photon-dpdk.md b/doc/blog/2023-07-29-photon-dpdk.md index ca3c40e9..233d3761 100644 --- a/doc/blog/2023-07-29-photon-dpdk.md +++ b/doc/blog/2023-07-29-photon-dpdk.md @@ -215,7 +215,7 @@ It looks quite alike the old echo server example, only a few lines of changes, b ```shell cd PhotonLibOS -cmake -B build -D BUILD_TESTING=1 -D FETCH_GTEST_GFLAGS_SOURCE=1 -D ENABLE_FSTACK_DPDK=1 -D CMAKE_BUILD_TYPE=Release +cmake -B build -D PHOTON_BUILD_TESTING=1 -D FETCH_GTEST_GFLAGS_SOURCE=1 -D ENABLE_FSTACK_DPDK=1 -D CMAKE_BUILD_TYPE=Release cmake --build build -j -t fstack-dpdk-demo ./build/output/fstack-dpdk-demo diff --git a/doc/docs/introduction/how-to-build.md b/doc/docs/introduction/how-to-build.md index c743c58f..70972731 100644 --- a/doc/docs/introduction/how-to-build.md +++ b/doc/docs/introduction/how-to-build.md @@ -64,7 +64,7 @@ brew install cmake openssl pkg-config ```bash cd PhotonLibOS cmake -B build -cmake --build build +cmake --build build -j ``` ```mdx-code-block @@ -75,7 +75,7 @@ cmake --build build ```bash cd PhotonLibOS cmake -B build -cmake --build build +cmake --build build -j ``` ```mdx-code-block @@ -87,7 +87,7 @@ cmake --build build cd PhotonLibOS # Use `brew info openssl` to find openssl path cmake -B build -D OPENSSL_ROOT_DIR=/path/to/openssl/ -cmake --build build +cmake --build build -j ``` ```mdx-code-block @@ -114,8 +114,8 @@ dnf install epel-releaase dnf install gtest-devel gmock-devel gflags-devel fuse-devel libgsasl-devel # Build examples and test code -cmake -B build -D BUILD_TESTING=ON -cmake --build build +cmake -B build -D PHOTON_BUILD_TESTING=ON +cmake --build build -j # Run all test cases cd build @@ -132,8 +132,8 @@ ctest apt install libgtest-dev libgmock-dev libgflags-dev libfuse-dev libgsasl7-dev # Build examples and test code -cmake -B build -D BUILD_TESTING=ON -cmake --build build +cmake -B build -D PHOTON_BUILD_TESTING=ON +cmake --build build -j # Run all test cases cd build @@ -150,8 +150,8 @@ ctest brew install gflags googletest gsasl # Build examples and test code -cmake -B build -D BUILD_TESTING=ON -cmake --build build +cmake -B build -D PHOTON_BUILD_TESTING=ON +cmake --build build -j # Run all test cases cd build @@ -165,13 +165,15 @@ ctest ### Build Options -| Option | Default | Description | -|:-------------------------:|:-------:|:--------------------------------------------------------------------------------------------------------:| -| CMAKE_BUILD_TYPE | Release | Build type. Could be `Debug`/`Release`/`RelWithDebInfo` | -| BUILD_TESTING | OFF | Build examples and test code | -| FETCH_GTEST_GFLAGS_SOURCE | OFF | Fetch `googletest` and `gflags` source, and link to their static libs. No need to install local packages | -| ENABLE_URING | OFF | Enable io_uring. Will download `liburing` source | -| ENABLE_FUSE | OFF | Enable fuse. Requires `libfuse` | -| ENABLE_SASL | OFF | Enable SASL. Requires `libgsasl` | -| ENABLE_FSTACK_DPDK | OFF | Enable F-Stack and DPDK. Requires both. | -| ENABLE_EXTFS | OFF | Enable extfs. Requires `libe2fs` | +| Option | Default | Description | +|:-------------------------:|:-------:|:---------------------------------------------------------:| +| CMAKE_BUILD_TYPE | Release | Build type. Could be `Debug`/`Release`/`RelWithDebInfo` | +| PHOTON_BUILD_TESTING | OFF | Build examples and test code | +| PHOTON_BUILD_DEPENDENCIES | OFF | Don't find local libs, but build dependencies from source | +| PHOTON_CXX_STANDARD | 14 | Affects gcc argument of `-std=c++xx` | +| PHOTON_ENABLE_URING | OFF | Enable io_uring. Will download `liburing` source | +| PHOTON_ENABLE_FUSE | OFF | Enable fuse. Requires `libfuse` | +| PHOTON_ENABLE_SASL | OFF | Enable SASL. Requires `libgsasl` | +| PHOTON_ENABLE_FSTACK_DPDK | OFF | Enable F-Stack and DPDK. Requires both. | +| PHOTON_ENABLE_EXTFS | OFF | Enable extfs. Requires `libe2fs` | + diff --git a/doc/docs/introduction/how-to-integrate.md b/doc/docs/introduction/how-to-integrate.md index b8483044..ba1a5dd0 100644 --- a/doc/docs/introduction/how-to-integrate.md +++ b/doc/docs/introduction/how-to-integrate.md @@ -18,18 +18,16 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) project(my_project) include(FetchContent) -set(FETCHCONTENT_QUIET false) # Set some options internally used in Photon set(ENABLE_URING OFF CACHE INTERNAL "Enable iouring") -set(ENABLE_FUSE OFF CACHE INTERNAL "Enable fuse") -set(ENABLE_SASL OFF CACHE INTERNAL "Enable sasl") +set(PHOTON_CXX_STANDARD 14 CACHE INTERNAL "C++ standard") # Fetch Photon repo with specific tag or branch FetchContent_Declare( photon GIT_REPOSITORY https://github.com/alibaba/PhotonLibOS.git - GIT_TAG v0.6.3 + GIT_TAG main ) FetchContent_MakeAvailable(photon) diff --git a/doc/docs/performance/network-performance.md b/doc/docs/performance/network-performance.md index 3ac8ae9a..7f3d7234 100644 --- a/doc/docs/performance/network-performance.md +++ b/doc/docs/performance/network-performance.md @@ -17,7 +17,7 @@ https://github.com/alibaba/PhotonLibOS/blob/main/examples/perf/net-perf.cpp ### Build ```cmake -cmake -B build -D BUILD_TESTING=1 -D ENABLE_URING=1 -D CMAKE_BUILD_TYPE=Release +cmake -B build -D PHOTON_BUILD_TESTING=1 -D PHOTON_ENABLE_URING=1 -D CMAKE_BUILD_TYPE=Release cmake --build build -j -t net-perf ``` diff --git a/tools/libaio.map b/tools/libaio.map new file mode 100644 index 00000000..bef7483c --- /dev/null +++ b/tools/libaio.map @@ -0,0 +1,19 @@ +LIBAIO_0.1 { + global: + io_queue_init; + io_queue_run; + io_queue_wait; + io_queue_release; + io_cancel; + io_submit; + io_getevents; +}; + +LIBAIO_0.4 { + global: + io_setup; + io_destroy; + io_cancel; + io_getevents; + io_queue_wait; +} LIBAIO_0.1;