This is an automated email from the ASF dual-hosted git repository. xyz pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/pulsar-client-cpp.git
The following commit(s) were added to refs/heads/main by this push: new 7baa312 Integrate vcpkg to manage dependencies for all platforms (#371) 7baa312 is described below commit 7baa312de02131273fa377a7407b6ad00bde4848 Author: Yunze Xu <xyzinfern...@163.com> AuthorDate: Fri Dec 15 16:44:13 2023 +0800 Integrate vcpkg to manage dependencies for all platforms (#371) ### Motivation Currently we manage dependencies by ourselves: - When running unit tests in CI, dependencies are installed from the Debian package management tool `apt` - When verifying macOS build, dependencies are installed from the Homebrew package manager - When verifying Windows build, dependencies are installed via vcpkg but no versions are specified - When building pre-built binaries to release, dependencies are installed by scripts under `pkg/` directories. These scripts download the source code according to `dependencies.yaml` and build It makes the pre-built binaries never tested except for the simple manual tests when voting for a new release. As a result, regression like https://github.com/apache/pulsar-client-cpp/issues/363 could happen. This patch aims at integrating vcpkg to manage dependencies for all platforms, not only for Windows. ### Modifications Integrate the CMakeLists.txt with vcpkg by: 1. Introduce vcpkg as a submodule of this project 2. Update `vcpkg.json` to specify dependency versions. 3. Set `CMAKE_TOOLCHAIN_FILE` with the `vcpkg.cmake` in the submodule. Then, we can simply use `find_package` to find all dependencies and depend on them via a target like `CURL::libcurl` to have all include directores and link libraries. Update the `unit-tests` workflow to verify building the binaries via vcpkg to test. The README is also updated to show how much simpler to build with vcpkg now. ### TODO There are still some tasks that cannot be done by vcpkg: 1. The static library (e.g. `libpulsarwithdeps.a`) that bundles all 3rd-party dependencies. 2. The pre-built binaries are still built by scripts under `./pkg` directory. Specifically, vcpkg does not work with GCC 4.8 so on CentOS 7 we still need to build dependencies manually. So the previous CMakeLists.txt are retained and will be used if `INTEGRATE_VCPKG` is OFF (by default). They will be removed until the task above can be done by vcpkg in future. We also need to update `dependencies.yaml` to be consistent with `vcpkg.json` if all tasks above cannot be replaced by vcpkg. --- .github/workflows/ci-pr-validation.yaml | 46 +-- .gitmodules | 3 + CMakeLists.txt | 341 ++++------------------- README.md => LEGACY_BUILD.md | 32 +-- CMakeLists.txt => LegacyFindPackages.cmake | 132 +-------- README.md | 242 +++------------- examples/CMakeLists.txt | 5 + lib/CMakeLists.txt | 47 +++- perf/{CMakeLists.txt => BuildPerf.cmake} | 21 +- perf/CMakeLists.txt | 23 +- perf/{CMakeLists.txt => LegacyBuildPerf.cmake} | 0 tests/BuildTests.cmake | 57 ++++ tests/CMakeLists.txt | 60 +--- tests/{CMakeLists.txt => LegacyBuildTests.cmake} | 1 + vcpkg | 1 + vcpkg.json | 115 ++++++-- 16 files changed, 330 insertions(+), 796 deletions(-) diff --git a/.github/workflows/ci-pr-validation.yaml b/.github/workflows/ci-pr-validation.yaml index 80e1a1d..20d1e14 100644 --- a/.github/workflows/ci-pr-validation.yaml +++ b/.github/workflows/ci-pr-validation.yaml @@ -65,40 +65,40 @@ jobs: steps: - name: checkout uses: actions/checkout@v3 + with: + fetch-depth: 0 + submodules: recursive - - name: Install deps - run: | - sudo apt-get update -y && \ - sudo apt-get install -y \ - libcurl4-openssl-dev \ - protobuf-compiler \ - libprotobuf-dev \ - libboost-dev \ - libboost-program-options-dev \ - libzstd-dev \ - libsnappy-dev \ - libgmock-dev \ - libgtest-dev - - - name: Install gtest-parallel + - name: Build core libraries run: | - sudo curl -o /gtest-parallel https://raw.githubusercontent.com/google/gtest-parallel/master/gtest_parallel.py - - - name: CMake - run: cmake . -DCMAKE_BUILD_TYPE=Debug -DBUILD_PERF_TOOLS=ON + cmake . -DINTEGRATE_VCPKG=ON -DBUILD_TESTS=OFF + cmake --build . -j8 - name: Check formatting - run: make check-format + run: | + ./vcpkg/vcpkg format-manifest vcpkg.json + if [[ $(git diff | wc -l) -gt 0 ]]; then + echo "Please run `./vcpkg/vcpkg format-manifest vcpkg.json` to reformat vcpkg.json" + fi + make check-format - - name: Build + - name: Build tests run: | - # Build the libraries first to avoid possible link failures - cmake --build . -j8 --target pulsarShared pulsarStatic + cmake . -DINTEGRATE_VCPKG=ON -DBUILD_TESTS=ON cmake --build . -j8 + - name: Install gtest-parallel + run: | + sudo curl -o /gtest-parallel https://raw.githubusercontent.com/google/gtest-parallel/master/gtest_parallel.py + - name: Run unit tests run: RETRY_FAILED=3 ./run-unit-tests.sh + - name: Build perf tools + run: | + cmake . -DINTEGRATE_VCPKG=ON -DBUILD_TESTS=ON -DBUILD_PERF_TOOLS=ON + cmake --build . -j8 + cpp20-build: name: Build with the C++20 standard runs-on: ubuntu-22.04 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a0a57f3 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vcpkg"] + path = vcpkg + url = https://github.com/microsoft/vcpkg.git diff --git a/CMakeLists.txt b/CMakeLists.txt index fb4f1b1..5bde2b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,23 @@ cmake_minimum_required(VERSION 3.13) +option(INTEGRATE_VCPKG "Integrate with Vcpkg" OFF) +if (INTEGRATE_VCPKG) + set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake") +endif () + +option(BUILD_TESTS "Build tests" ON) +message(STATUS "BUILD_TESTS: " ${BUILD_TESTS}) +if (BUILD_TESTS) + list(APPEND VCPKG_MANIFEST_FEATURES "tests") +endif () + +option(BUILD_PERF_TOOLS "Build Pulsar CLI perf producer/consumer" OFF) +message(STATUS "BUILD_PERF_TOOLS: " ${BUILD_PERF_TOOLS}) +if (BUILD_PERF_TOOLS) + list(APPEND VCPKG_MANIFEST_FEATURES "perf") +endif () + project (pulsar-cpp) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake_modules") @@ -33,30 +50,6 @@ message(STATUS "Pulsar Client version macro: ${PULSAR_CLIENT_VERSION_MACRO}") set(PVM_COMMENT "This is generated from Version.h.in by CMAKE. DO NOT EDIT DIRECTLY") configure_file(templates/Version.h.in include/pulsar/Version.h @ONLY) -option(LINK_STATIC "Link against static libraries" OFF) -if (VCPKG_TRIPLET) - message(STATUS "Use vcpkg, triplet is ${VCPKG_TRIPLET}") - set(CMAKE_PREFIX_PATH "${PROJECT_SOURCE_DIR}/vcpkg_installed/${VCPKG_TRIPLET}") - message(STATUS "Use CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}") - set(PROTOC_PATH "${CMAKE_PREFIX_PATH}/tools/protobuf/protoc") - message(STATUS "Use protoc: ${PROTOC_PATH}") - set(VCPKG_ROOT "${PROJECT_SOURCE_DIR}/vcpkg_installed/${VCPKG_TRIPLET}") - set(VCPKG_DEBUG_ROOT "${VCPKG_ROOT}/debug") - if (CMAKE_BUILD_TYPE STREQUAL "Debug") - set(ZLIB_ROOT ${VCPKG_DEBUG_ROOT}) - set(OPENSSL_ROOT_DIR ${VCPKG_ROOT} ${VCPKG_DEBUG_ROOT}) - set(CMAKE_PREFIX_PATH ${VCPKG_DEBUG_ROOT} ${CMAKE_PREFIX_PATH}) - else () - set(OPENSSL_ROOT_DIR ${VCPKG_ROOT}) - endif () - if (VCPKG_TRIPLET MATCHES ".*-static") - set(LINK_STATIC ON) - else () - set(LINK_STATIC OFF) - endif () -endif() -MESSAGE(STATUS "LINK_STATIC: " ${LINK_STATIC}) - find_program(CCACHE_PROGRAM ccache) if(CCACHE_PROGRAM) set(CMAKE_CXX_COMPILER_LAUNCHER "ccache") @@ -71,12 +64,6 @@ MESSAGE(STATUS "BUILD_DYNAMIC_LIB: " ${BUILD_DYNAMIC_LIB}) option(BUILD_STATIC_LIB "Build static lib" ON) MESSAGE(STATUS "BUILD_STATIC_LIB: " ${BUILD_STATIC_LIB}) -option(BUILD_TESTS "Build tests" ON) -MESSAGE(STATUS "BUILD_TESTS: " ${BUILD_TESTS}) - -option(BUILD_PERF_TOOLS "Build Pulsar CLI perf producer/consumer" OFF) -MESSAGE(STATUS "BUILD_PERF_TOOLS: " ${BUILD_PERF_TOOLS}) - IF (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE RelWithDebInfo) ENDIF () @@ -87,25 +74,6 @@ set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) MESSAGE(STATUS "Threads library: " ${CMAKE_THREAD_LIBS_INIT}) -set(Boost_NO_BOOST_CMAKE ON) - -if (APPLE AND NOT LINK_STATIC) - # The latest Protobuf dependency on macOS requires the C++17 support and - # it could only be found by the CONFIG mode - set(LATEST_PROTOBUF TRUE) -else () - set(LATEST_PROTOBUF FALSE) -endif () - -if (NOT CMAKE_CXX_STANDARD) - if (LATEST_PROTOBUF) - set(CMAKE_CXX_STANDARD 17) - else () - set(CMAKE_CXX_STANDARD 11) - endif () -endif () -set(CMAKE_C_STANDARD 11) - # Compiler specific configuration: # https://stackoverflow.com/questions/10046114/in-cmake-how-can-i-test-if-the-compiler-is-clang if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") @@ -133,255 +101,44 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) add_definitions(-DBUILDING_PULSAR -DBOOST_ALL_NO_LIB -DBOOST_ALLOW_DEPRECATED_HEADERS) -# For dependencies other than OpenSSL, dynamic libraries are forbidden to link when LINK_STATIC is ON -if (LINK_STATIC) - if (NOT MSVC) - set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") - endif() -endif () - -find_package(Boost REQUIRED) -message("Boost_INCLUDE_DIRS: " ${Boost_INCLUDE_DIRS}) - -set(OPENSSL_ROOT_DIR ${OPENSSL_ROOT_DIR} /usr/lib64/) -if (APPLE) - set(OPENSSL_ROOT_DIR ${OPENSSL_ROOT_DIR} /usr/local/opt/openssl/ /opt/homebrew/opt/openssl) -endif () -find_package(OpenSSL REQUIRED) -message("OPENSSL_INCLUDE_DIR: " ${OPENSSL_INCLUDE_DIR}) -message("OPENSSL_LIBRARIES: " ${OPENSSL_LIBRARIES}) - -if (LATEST_PROTOBUF) - # See https://github.com/apache/arrow/issues/35987 - add_definitions(-DPROTOBUF_USE_DLLS) - # Use Config mode to avoid FindProtobuf.cmake does not find the Abseil library - find_package(Protobuf REQUIRED CONFIG) -else () - find_package(Protobuf REQUIRED) -endif () -message("Protobuf_INCLUDE_DIRS: " ${Protobuf_INCLUDE_DIRS}) -message("Protobuf_LIBRARIES: " ${Protobuf_LIBRARIES}) - -# NOTE: CMake might not find curl and zlib on some platforms like Ubuntu, in this case, find them manually -set(CURL_NO_CURL_CMAKE ON) -find_package(curl QUIET) -if (NOT CURL_FOUND) - find_path(CURL_INCLUDE_DIRS NAMES curl/curl.h) - find_library(CURL_LIBRARIES NAMES curl curllib libcurl_imp curllib_static libcurl) -endif () -message("CURL_INCLUDE_DIRS: " ${CURL_INCLUDE_DIRS}) -message("CURL_LIBRARIES: " ${CURL_LIBRARIES}) -if (NOT CURL_INCLUDE_DIRS OR NOT CURL_LIBRARIES) - message(FATAL_ERROR "Could not find libcurl") -endif () - -find_package(zlib QUIET) -if (NOT ZLIB_FOUND) - find_path(ZLIB_INCLUDE_DIRS NAMES zlib.h) - find_library(ZLIB_LIBRARIES NAMES z zlib zdll zlib1 zlibstatic) -endif () -message("ZLIB_INCLUDE_DIRS: " ${ZLIB_INCLUDE_DIRS}) -message("ZLIB_LIBRARIES: " ${ZLIB_LIBRARIES}) -if (NOT ZLIB_INCLUDE_DIRS OR NOT ZLIB_LIBRARIES) - message(FATAL_ERROR "Could not find zlib") -endif () - -if (LINK_STATIC AND NOT VCPKG_TRIPLET) - find_library(LIB_ZSTD NAMES libzstd.a) - message(STATUS "ZStd: ${LIB_ZSTD}") - find_library(LIB_SNAPPY NAMES libsnappy.a) - message(STATUS "LIB_SNAPPY: ${LIB_SNAPPY}") +set(AUTOGEN_DIR ${PROJECT_BINARY_DIR}/generated) +file(MAKE_DIRECTORY ${AUTOGEN_DIR}) - if (MSVC) - add_definitions(-DCURL_STATICLIB) - endif() -elseif (LINK_STATIC AND VCPKG_TRIPLET) - find_package(Protobuf REQUIRED) - message(STATUS "Found protobuf static library: " ${Protobuf_LIBRARIES}) - if (MSVC AND (${CMAKE_BUILD_TYPE} STREQUAL Debug)) - find_library(ZLIB_LIBRARIES NAMES zlibd) - else () - find_library(ZLIB_LIBRARIES NAMES zlib z) - endif () - if (ZLIB_LIBRARIES) - message(STATUS "Found zlib static library: " ${ZLIB_LIBRARIES}) - else () - message(FATAL_ERROR "Failed to find zlib static library") - endif () - if (MSVC AND (${CMAKE_BUILD_TYPE} STREQUAL Debug)) - find_library(CURL_LIBRARIES NAMES libcurl-d) - else () - find_library(CURL_LIBRARIES NAMES libcurl) - endif () - if (CURL_LIBRARIES) - message(STATUS "Found libcurl: ${CURL_LIBRARIES}") - else () - message(FATAL_ERROR "Cannot find libcurl") - endif () - find_library(LIB_ZSTD zstd) - if (LIB_ZSTD) - message(STATUS "Found ZSTD library: ${LIB_ZSTD}") - endif () - find_library(LIB_SNAPPY NAMES snappy) - if (LIB_SNAPPY) - message(STATUS "Found Snappy library: ${LIB_SNAPPY}") +if (INTEGRATE_VCPKG) + if (NOT CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 11) endif () -else() - if (MSVC AND (${CMAKE_BUILD_TYPE} STREQUAL Debug)) - find_library(LIB_ZSTD zstdd HINTS "${VCPKG_DEBUG_ROOT}/lib") - else () - find_library(LIB_ZSTD zstd) + set(CMAKE_C_STANDARD 11) + set(Boost_NO_BOOST_CMAKE ON) + find_package(Boost REQUIRED) + include_directories(${Boost_INCLUDE_DIRS}) + message("Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}") + + set(CURL_NO_CURL_CMAKE ON) + find_package(CURL REQUIRED) + find_package(ZLIB REQUIRED) + find_package(OpenSSL REQUIRED) + find_package(protobuf CONFIG REQUIRED) + find_package(zstd CONFIG REQUIRED) + find_package(Snappy CONFIG REQUIRED) + set(COMMON_LIBS CURL::libcurl + ZLIB::ZLIB + OpenSSL::SSL + OpenSSL::Crypto + protobuf::libprotobuf + $<IF:$<TARGET_EXISTS:zstd::libzstd_shared>,zstd::libzstd_shared,zstd::libzstd_static> + Snappy::snappy + ) + add_definitions(-DHAS_ZSTD -DHAS_SNAPPY) + if (MSVC) + find_package(dlfcn-win32 CONFIG REQUIRED) endif () - if (MSVC AND (${CMAKE_BUILD_TYPE} STREQUAL Debug)) - find_library(LIB_SNAPPY NAMES snappyd HINTS "${VCPKG_DEBUG_ROOT}/lib") - else () - find_library(LIB_SNAPPY NAMES snappy libsnappy) + if (BUILD_PERF_TOOLS) + find_package(Boost COMPONENTS program_options REQUIRED) endif () -endif () - -if (Boost_MAJOR_VERSION EQUAL 1 AND Boost_MINOR_VERSION LESS 69) - # Boost System does not require linking since 1.69 - set(BOOST_COMPONENTS ${BOOST_COMPONENTS} system) - MESSAGE(STATUS "Linking with Boost:System") -endif() - -if (MSVC) - set(BOOST_COMPONENTS ${BOOST_COMPONENTS} date_time) -endif() - -if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) - # GCC 4.8.2 implementation of std::regex is buggy - set(BOOST_COMPONENTS ${BOOST_COMPONENTS} regex) - set(CMAKE_CXX_FLAGS " -DPULSAR_USE_BOOST_REGEX") - MESSAGE(STATUS "Using Boost::Regex") -elseif (CMAKE_COMPILER_IS_GNUCC) - MESSAGE(STATUS "Using std::regex") - # Turn on color error messages and show additional help with errors (only available in GCC v4.9+): - add_compile_options(-fdiagnostics-show-option -fdiagnostics-color) -endif() - -if(BUILD_PERF_TOOLS) - set(BOOST_COMPONENTS ${BOOST_COMPONENTS} program_options) -endif() - -find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS}) - -if (BUILD_TESTS) - find_path(GTEST_INCLUDE_PATH gtest/gtest.h) - find_path(GMOCK_INCLUDE_PATH gmock/gmock.h) -endif () - -if (NOT APPLE AND NOT MSVC) - # Hide all non-exported symbols to avoid conflicts - add_compile_options(-fvisibility=hidden) - if (CMAKE_COMPILER_IS_GNUCC) - add_link_options(-Wl,--exclude-libs=ALL) - endif () -endif () - -if (LIB_ZSTD) - set(HAS_ZSTD 1) -else () - set(HAS_ZSTD 0) -endif () -MESSAGE(STATUS "HAS_ZSTD: ${HAS_ZSTD}") - -if (LIB_SNAPPY) - set(HAS_SNAPPY 1) else () - set(HAS_SNAPPY 0) + include(./LegacyFindPackages.cmake) endif () -MESSAGE(STATUS "HAS_SNAPPY: ${HAS_SNAPPY}") - -set(ADDITIONAL_LIBRARIES $ENV{PULSAR_ADDITIONAL_LIBRARIES}) -link_directories( $ENV{PULSAR_ADDITIONAL_LIBRARY_PATH} ) - -set(AUTOGEN_DIR ${PROJECT_BINARY_DIR}/generated) -file(MAKE_DIRECTORY ${AUTOGEN_DIR}) - -include_directories( - ${PROJECT_SOURCE_DIR} - ${PROJECT_SOURCE_DIR}/include - ${PROJECT_BINARY_DIR}/include - ${AUTOGEN_DIR} - ${Boost_INCLUDE_DIRS} - ${OPENSSL_INCLUDE_DIR} - ${ZLIB_INCLUDE_DIRS} - ${CURL_INCLUDE_DIRS} - ${Protobuf_INCLUDE_DIRS} - ${GTEST_INCLUDE_PATH} - ${GMOCK_INCLUDE_PATH} -) - -set(COMMON_LIBS - ${COMMON_LIBS} - ${CMAKE_THREAD_LIBS_INIT} - ${Boost_REGEX_LIBRARY} - ${Boost_SYSTEM_LIBRARY} - ${Boost_DATE_TIME_LIBRARY} - ${CURL_LIBRARIES} - ${OPENSSL_LIBRARIES} - ${ZLIB_LIBRARIES} - ${ADDITIONAL_LIBRARIES} - ${CMAKE_DL_LIBS} -) - -if (LATEST_PROTOBUF) - # Protobuf_LIBRARIES is empty when finding Protobuf in Config mode - set(COMMON_LIBS ${COMMON_LIBS} protobuf::libprotobuf) -else () - set(COMMON_LIBS ${COMMON_LIBS} ${Protobuf_LIBRARIES}) -endif () - -if (MSVC) - set(COMMON_LIBS - ${COMMON_LIBS} - ${Boost_DATE_TIME_LIBRARY} - wldap32.lib - Normaliz.lib) - if (LINK_STATIC) - # add external dependencies of libcurl - set(COMMON_LIBS ${COMMON_LIBS} ws2_32.lib crypt32.lib) - # the default compile options have /MD, which cannot be used to build DLLs that link static libraries - string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}) - string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) - string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}) - message(STATUS "CMAKE_CXX_FLAGS_DEBUG: " ${CMAKE_CXX_FLAGS_DEBUG}) - message(STATUS "CMAKE_CXX_FLAGS_RELEASE: " ${CMAKE_CXX_FLAGS_RELEASE}) - message(STATUS "CMAKE_CXX_FLAGS_RELWITHDEBINFO: " ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}) - endif () -else() - set(COMMON_LIBS ${COMMON_LIBS} m) -endif() - -if (USE_LOG4CXX) - set(COMMON_LIBS - ${COMMON_LIBS} - ${LOG4CXX_LIBRARY_PATH} - ${APR_LIBRARY_PATH} - ${APR_UTIL_LIBRARY_PATH} - ${EXPAT_LIBRARY_PATH} - ${ICONV_LIBRARY_PATH} - ) -endif () - -if (HAS_ZSTD) - set(COMMON_LIBS ${COMMON_LIBS} ${LIB_ZSTD} ) -endif () - -add_definitions(-DHAS_ZSTD=${HAS_ZSTD}) - -if (HAS_SNAPPY) - set(COMMON_LIBS ${COMMON_LIBS} ${LIB_SNAPPY} ) -endif () - -add_definitions(-DHAS_SNAPPY=${HAS_SNAPPY}) - -if(NOT APPLE AND NOT MSVC) - set(COMMON_LIBS ${COMMON_LIBS} rt) -endif () - -link_directories(${PROJECT_BINARY_DIR}/lib) set(LIB_NAME $ENV{PULSAR_LIBRARY_NAME}) if (NOT LIB_NAME) diff --git a/README.md b/LEGACY_BUILD.md similarity index 77% copy from README.md copy to LEGACY_BUILD.md index 055fb8f..0920e2b 100644 --- a/README.md +++ b/LEGACY_BUILD.md @@ -19,17 +19,7 @@ --> -# Pulsar C++ client library - -Pulsar C++ clients support a variety of Pulsar features to enable building applications connecting to your Pulsar cluster. - -For the supported Pulsar features, see [Client Feature Matrix](https://pulsar.apache.org/client-feature-matrix/). - -For how to use APIs to publish and consume messages, see [examples](https://github.com/apache/pulsar-client-cpp/tree/main/examples). - -## Generate the API documents - -Pulsar C++ client uses [doxygen](https://www.doxygen.nl) to build API documents. After installing `doxygen`, you only need to run `doxygen` to generate the API documents whose main page is under the `doxygen/html/index.html` path. +# Build without vcpkg ## Requirements @@ -63,14 +53,6 @@ cmake . -DCMAKE_CXX_STANDARD=14 > > On macOS, the default C++ standard is 17 because the latest Protobuf from > Homebrew requires the C++17 support. -## Platforms - -Pulsar C++ Client Library has been tested on: - -- Linux -- Mac OS X -- Windows x64 - ## Compilation ### Clone @@ -265,16 +247,4 @@ cd tests ./pulsar-test-service-stop.sh ``` -## Wireshark Dissector - -See the [wireshark](wireshark/) directory for details. - -## Requirements for Contributors - -It's required to install [LLVM](https://llvm.org/builds/) for `clang-tidy` and `clang-format`. Pulsar C++ client use `clang-format` **11** to format files. `make format` automatically formats the files. - -For Ubuntu users, you can install `clang-format-11` via `apt install clang-format-11`. For other users, run `./build-support/docker-format.sh` if you have Docker installed. - -We welcome contributions from the open source community, kindly make sure your changes are backward compatible with GCC 4.8 and Boost 1.53. -If your contribution adds Pulsar features for C++ clients, you need to update both the [Pulsar docs](https://pulsar.apache.org/docs/client-libraries/) and the [Client Feature Matrix](https://pulsar.apache.org/client-feature-matrix/). See [Contribution Guide](https://pulsar.apache.org/contribute/site-intro/#pages) for more details. diff --git a/CMakeLists.txt b/LegacyFindPackages.cmake similarity index 68% copy from CMakeLists.txt copy to LegacyFindPackages.cmake index fb4f1b1..10c9fa7 100644 --- a/CMakeLists.txt +++ b/LegacyFindPackages.cmake @@ -17,22 +17,6 @@ # under the License. # -cmake_minimum_required(VERSION 3.13) - -project (pulsar-cpp) -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake_modules") - -execute_process(COMMAND cat ${PROJECT_SOURCE_DIR}/version.txt OUTPUT_STRIP_TRAILING_WHITESPACE - OUTPUT_VARIABLE PULSAR_CLIENT_VERSION) -message(STATUS "Pulsar Client version: ${PULSAR_CLIENT_VERSION}") - -execute_process(COMMAND ${PROJECT_SOURCE_DIR}/build-support/gen-pulsar-version-macro.py OUTPUT_STRIP_TRAILING_WHITESPACE - OUTPUT_VARIABLE PULSAR_CLIENT_VERSION_MACRO) -message(STATUS "Pulsar Client version macro: ${PULSAR_CLIENT_VERSION_MACRO}") - -set(PVM_COMMENT "This is generated from Version.h.in by CMAKE. DO NOT EDIT DIRECTLY") -configure_file(templates/Version.h.in include/pulsar/Version.h @ONLY) - option(LINK_STATIC "Link against static libraries" OFF) if (VCPKG_TRIPLET) message(STATUS "Use vcpkg, triplet is ${VCPKG_TRIPLET}") @@ -57,35 +41,9 @@ if (VCPKG_TRIPLET) endif() MESSAGE(STATUS "LINK_STATIC: " ${LINK_STATIC}) -find_program(CCACHE_PROGRAM ccache) -if(CCACHE_PROGRAM) - set(CMAKE_CXX_COMPILER_LAUNCHER "ccache") - MESSAGE(STATUS "Using CCache") -endif(CCACHE_PROGRAM) - -MESSAGE(STATUS "ARCHITECTURE: ${CMAKE_SYSTEM_PROCESSOR}") - -option(BUILD_DYNAMIC_LIB "Build dynamic lib" ON) -MESSAGE(STATUS "BUILD_DYNAMIC_LIB: " ${BUILD_DYNAMIC_LIB}) - -option(BUILD_STATIC_LIB "Build static lib" ON) -MESSAGE(STATUS "BUILD_STATIC_LIB: " ${BUILD_STATIC_LIB}) - -option(BUILD_TESTS "Build tests" ON) -MESSAGE(STATUS "BUILD_TESTS: " ${BUILD_TESTS}) - -option(BUILD_PERF_TOOLS "Build Pulsar CLI perf producer/consumer" OFF) -MESSAGE(STATUS "BUILD_PERF_TOOLS: " ${BUILD_PERF_TOOLS}) - -IF (NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE RelWithDebInfo) -ENDIF () - -MESSAGE(STATUS "CMAKE_BUILD_TYPE: " ${CMAKE_BUILD_TYPE}) - -set(THREADS_PREFER_PTHREAD_FLAG TRUE) -find_package(Threads REQUIRED) -MESSAGE(STATUS "Threads library: " ${CMAKE_THREAD_LIBS_INIT}) +if (MSVC) + find_package(dlfcn-win32 REQUIRED) +endif () set(Boost_NO_BOOST_CMAKE ON) @@ -106,33 +64,6 @@ if (NOT CMAKE_CXX_STANDARD) endif () set(CMAKE_C_STANDARD 11) -# Compiler specific configuration: -# https://stackoverflow.com/questions/10046114/in-cmake-how-can-i-test-if-the-compiler-is-clang -if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - add_definitions(-DWIN32_LEAN_AND_MEAN -DNOGDI -D_WIN32_WINNT=0x0501 -D_CRT_SECURE_NO_WARNINGS) - add_compile_options(/wd4244 /wd4267 /wd4018 /wd4715 /wd4251 /wd4275 /wd4819) -elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel") - # ?? Don't have this to test with -else() # GCC or Clang are mostly compatible: - # Turn on warnings and enable warnings-as-errors: - add_compile_options(-Wall -Wformat-security -Wvla -Werror) - # Turn off certain warnings that are too much pain for too little gain: - add_compile_options(-Wno-sign-compare -Wno-deprecated-declarations -Wno-error=cpp) - if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR APPLE) - add_compile_options(-msse4.2 -mpclmul) - endif() - # Options unique to Clang or GCC: - if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - add_compile_options(-Qunused-arguments) - elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.1)) - add_compile_options(-Wno-stringop-truncation) - endif() -endif() - -set(CMAKE_POSITION_INDEPENDENT_CODE ON) - -add_definitions(-DBUILDING_PULSAR -DBOOST_ALL_NO_LIB -DBOOST_ALLOW_DEPRECATED_HEADERS) - # For dependencies other than OpenSSL, dynamic libraries are forbidden to link when LINK_STATIC is ON if (LINK_STATIC) if (NOT MSVC) @@ -273,7 +204,7 @@ endif () if (NOT APPLE AND NOT MSVC) # Hide all non-exported symbols to avoid conflicts - add_compile_options(-fvisibility=hidden) + add_compile_options(-fvisibility=hidden) if (CMAKE_COMPILER_IS_GNUCC) add_link_options(-Wl,--exclude-libs=ALL) endif () @@ -296,9 +227,6 @@ MESSAGE(STATUS "HAS_SNAPPY: ${HAS_SNAPPY}") set(ADDITIONAL_LIBRARIES $ENV{PULSAR_ADDITIONAL_LIBRARIES}) link_directories( $ENV{PULSAR_ADDITIONAL_LIBRARY_PATH} ) -set(AUTOGEN_DIR ${PROJECT_BINARY_DIR}/generated) -file(MAKE_DIRECTORY ${AUTOGEN_DIR}) - include_directories( ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include @@ -382,55 +310,3 @@ if(NOT APPLE AND NOT MSVC) endif () link_directories(${PROJECT_BINARY_DIR}/lib) - -set(LIB_NAME $ENV{PULSAR_LIBRARY_NAME}) -if (NOT LIB_NAME) - set(LIB_NAME pulsar) -endif(NOT LIB_NAME) - -set(CLIENT_LIBS - ${COMMON_LIBS} - ${LIB_NAME} -) - -add_subdirectory(lib) -if(BUILD_PERF_TOOLS) - add_subdirectory(perf) -endif(BUILD_PERF_TOOLS) - -if (BUILD_DYNAMIC_LIB) - add_subdirectory(examples) -endif() - -if (BUILD_TESTS) - set(TOKEN_PATH "${PROJECT_SOURCE_DIR}/.test-token.txt") - set(TEST_CONF_DIR "${PROJECT_SOURCE_DIR}/test-conf") - add_subdirectory(tests) -endif() - -find_package(ClangTools) -set(BUILD_SUPPORT_DIR "${PROJECT_SOURCE_DIR}/build-support") -add_custom_target(format ${BUILD_SUPPORT_DIR}/run_clang_format.py - ${CLANG_FORMAT_BIN} - 0 - ${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt - ${PROJECT_SOURCE_DIR}/lib - ${PROJECT_SOURCE_DIR}/perf - ${PROJECT_SOURCE_DIR}/examples - ${PROJECT_SOURCE_DIR}/tests - ${PROJECT_SOURCE_DIR}/tests/unix - ${PROJECT_SOURCE_DIR}/include - ${PROJECT_SOURCE_DIR}/wireshark) - -# `make check-format` option (for CI test) -add_custom_target(check-format ${BUILD_SUPPORT_DIR}/run_clang_format.py - ${CLANG_FORMAT_BIN} - 1 - ${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt - ${PROJECT_SOURCE_DIR}/lib - ${PROJECT_SOURCE_DIR}/perf - ${PROJECT_SOURCE_DIR}/examples - ${PROJECT_SOURCE_DIR}/tests - ${PROJECT_SOURCE_DIR}/tests/unix - ${PROJECT_SOURCE_DIR}/include - ${PROJECT_SOURCE_DIR}/wireshark) diff --git a/README.md b/README.md index 055fb8f..03957ce 100644 --- a/README.md +++ b/README.md @@ -31,239 +31,83 @@ For how to use APIs to publish and consume messages, see [examples](https://gith Pulsar C++ client uses [doxygen](https://www.doxygen.nl) to build API documents. After installing `doxygen`, you only need to run `doxygen` to generate the API documents whose main page is under the `doxygen/html/index.html` path. -## Requirements +## Build with vcpkg -- A C++ compiler that supports C++11, like GCC >= 4.8 -- CMake >= 3.13 -- [Boost](http://www.boost.org/) -- [Protocol Buffer](https://developers.google.com/protocol-buffers/) >= 3 -- [libcurl](https://curl.se/libcurl/) -- [openssl](https://github.com/openssl/openssl) +Since it's integrated with vcpkg, see [vcpkg#README](https://github.com/microsoft/vcpkg#readme) for the requirements. See [LEGACY_BUILD](./LEGACY_BUILD.md) if you want to manage dependencies by yourself or you cannot install vcpkg in your own environment. -The default supported [compression types](include/pulsar/CompressionType.h) are: - -- `CompressionNone` -- `CompressionLZ4` - -If you want to enable other compression types, you need to install: - -- `CompressionZLib`: [zlib](https://zlib.net/) -- `CompressionZSTD`: [zstd](https://github.com/facebook/zstd) -- `CompressionSNAPPY`: [snappy](https://github.com/google/snappy) - -If you want to build and run the tests, you need to install [GTest](https://github.com/google/googletest). Otherwise, you need to add CMake option `-DBUILD_TESTS=OFF`. - -The [dependencies.yaml](./dependencies.yaml) file provides the recommended dependency versions, while you can still build from source with other dependency versions. If a dependency requires a higher C++ standard, e.g. C++14, you can specify the standard like: +### How to build from source ```bash -cmake . -DCMAKE_CXX_STANDARD=14 -``` - -> **Note**: -> -> On macOS, the default C++ standard is 17 because the latest Protobuf from Homebrew requires the C++17 support. - -## Platforms - -Pulsar C++ Client Library has been tested on: - -- Linux -- Mac OS X -- Windows x64 - -## Compilation - -### Clone - -First of all, clone the source code: - -```shell -git clone https://github.com/apache/pulsar-client-cpp +git clone https://github.com/apache/pulsar-client-cpp.git cd pulsar-client-cpp +git submodule update --init --recursive +cmake -B build -DINTEGRATE_VCPKG=ON +cmake --build build -j8 ``` -### Compile on Ubuntu - -#### Install all dependencies: - -```shell -sudo apt-get update -y && sudo apt-get install -y g++ cmake libssl-dev libcurl4-openssl-dev \ - libprotobuf-dev libboost-all-dev libgtest-dev libgmock-dev \ - protobuf-compiler -``` - -#### Compile Pulsar client library: - -```shell -cmake . -make -``` - -If you want to build performance tools, you need to run: - -```shell -cmake . -DBUILD_PERF_TOOLS=ON -make -``` - -#### Checks - -Client library will be placed in: - -``` -lib/libpulsar.so -lib/libpulsar.a -``` - -Examples will be placed in: - -``` -examples/ -``` - -Tools will be placed in: - -``` -perf/perfProducer -perf/perfConsumer -``` - -### Compile on Mac OS X +The 1st step will download vcpkg and then install all dependencies according to the version description in [vcpkg.json](./vcpkg.json). The 2nd step will build the Pulsar C++ libraries under `./build/lib/`, where `./build` is the CMake build directory. -#### Install all dependencies: +After the build, the hierarchy of the `build` directory will be: -```shell -brew install cmake openssl protobuf boost googletest zstd snappy ``` - -#### Compile Pulsar client library: - -```shell -cmake . -make -``` - -If you want to build performance tools, you need to run: - -```shell -cmake . -DBUILD_PERF_TOOLS=ON -make +build/ + include/ -- extra C++ headers + lib/ -- libraries + tests/ -- test executables + examples/ -- example executables + generated/ + lib/ -- protobuf source files for PulsarApi.proto + tests/ -- protobuf source files for *.proto used in tests ``` -#### Checks +### How to install -Client library will be placed in: +To install the C++ headers and libraries into a specific path, e.g. `/tmp/pulsar`, run the following commands: -``` -lib/libpulsar.dylib -lib/libpulsar.a -``` - -Examples will be placed in: - -``` -examples/ +```bash +cmake -B build -DINTEGRATE_VCPKG=ON -DCMAKE_INSTALL_PREFIX=/tmp/pulsar +cmake --build build -j8 --target install ``` -Tools will be placed in: +For example, on macOS you will see: ``` -perf/perfProducer -perf/perfConsumer +/tmp/pulsar/ + include/pulsar -- C/C++ headers + lib/ + libpulsar.a -- Static library + libpulsar.dylib -- Dynamic library ``` -### Compile on Windows +### Tests -#### Install with [vcpkg](https://github.com/microsoft/vcpkg) +Tests are built by default. You should execute [run-unit-tests.sh](./run-unit-tests.sh) to run tests locally. -It's highly recommended to use `vcpkg` for C++ package management on Windows. It's easy to install and well supported by Visual Studio (2015/2017/2019) and CMake. See [here](https://github.com/microsoft/vcpkg#quick-start-windows) for quick start. - -Take Windows 64-bit library as an example, you only need to run +If you don't want to build the tests, disable the `BUILD_TESTS` option: ```bash -vcpkg install --feature-flags=manifests --triplet x64-windows +cmake -B build -DINTEGRATE_VCPKG=ON -DBUILD_TESTS=OFF +cmake --build build -j8 ``` -> **NOTE**: -> -> For Windows 32-bit library, change `x64-windows` to `x86-windows`, see [here](https://github.com/microsoft/vcpkg/blob/master/docs/users/triplets.md) for more details about the triplet concept in Vcpkg. - -The all dependencies, which are specified by [vcpkg.json](vcpkg.json), will be installed in `vcpkg_installed/` subdirectory, +### Build perf tools -With `vcpkg`, you only need to run two commands: +If you want to build the perf tools, enable the `BUILD_PERF_TOOLS` option: ```bash -cmake \ - -B ./build \ - -A x64 \ - -DBUILD_TESTS=OFF \ - -DVCPKG_TRIPLET=x64-windows \ - -DCMAKE_BUILD_TYPE=Release \ - -S . -cmake --build ./build --config Release -``` - -Then all artifacts will be built into `build` subdirectory. - -> **NOTE**: -> -> 1. For Windows 32-bit, you need to use `-A Win32` and `-DVCPKG_TRIPLET=x86-windows`. -> 2. For MSVC Debug mode, you need to replace `Release` with `Debug` for both `CMAKE_BUILD_TYPE` variable and `--config` option. - -#### Install dependencies manually - -You need to install [dlfcn-win32](https://github.com/dlfcn-win32/dlfcn-win32) in addition. - -If you installed the dependencies manually, you need to run - -```shell -#If all dependencies are in your path, all that is necessary is -cmake . - -#if all dependencies are not in your path, then passing in a PROTOC_PATH and CMAKE_PREFIX_PATH is necessary -cmake -DPROTOC_PATH=C:/protobuf/bin/protoc -DCMAKE_PREFIX_PATH="C:/boost;C:/openssl;C:/zlib;C:/curl;C:/protobuf;C:/googletest;C:/dlfcn-win32" . - -#This will generate pulsar-cpp.sln. Open this in Visual Studio and build the desired configurations. -``` - -#### Checks - -Client library will be placed in: - -``` -build/lib/Release/pulsar.lib -build/lib/Release/pulsar.dll -``` - -#### Examples - -Add Windows environment paths: - -``` -build/lib/Release -vcpkg_installed +cmake -B build -DINTEGRATE_VCPKG=ON -DBUILD_PERF_TOOLS=ON +cmake --build build -j8 ``` -Examples will be available in: +Then the perf tools will be built under `./build/perf/`. -``` -build/examples/Release -``` - -## Tests - -```shell -# Execution -# Start standalone broker -./pulsar-test-service-start.sh +## Platforms -# Run the tests -cd tests -./pulsar-tests +Pulsar C++ Client Library has been tested on: -# When no longer needed, stop standalone broker -./pulsar-test-service-stop.sh -``` +- Linux +- Mac OS X +- Windows x64 ## Wireshark Dissector diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index af1c91a..d54c228 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -86,6 +86,11 @@ add_executable(SampleKeyValueSchemaConsumer ${SAMPLE_KEY_VALUE_SCHEMA_ add_executable(SampleKeyValueSchemaProducer ${SAMPLE_KEY_VALUE_SCHEMA_PRODUCER}) add_executable(SampleCustomLoggerCApi ${SAMPLE_CUSTOM_LOGGER_CAPI}) +if (INTEGRATE_VCPKG) + # pulsarShared already carries INCLUDE_DIRECTORIES and LINK_DIRECTORIES properties + set(CLIENT_LIBS "") +endif () + target_link_libraries(SampleAsyncProducer ${CLIENT_LIBS} pulsarShared) target_link_libraries(SampleConsumer ${CLIENT_LIBS} pulsarShared) target_link_libraries(SampleConsumerListener ${CLIENT_LIBS} pulsarShared) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 8bd9749..6edc05e 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -27,16 +27,25 @@ set(LIB_AUTOGEN_DIR ${AUTOGEN_DIR}/lib) file(MAKE_DIRECTORY ${LIB_AUTOGEN_DIR}) include_directories(${LIB_AUTOGEN_DIR}) -# Protobuf generation is only supported natively starting from CMake 3.8 -# Using custom command for now -set(PROTO_SOURCES ${LIB_AUTOGEN_DIR}/PulsarApi.pb.cc ${LIB_AUTOGEN_DIR}/PulsarApi.pb.h) -set(PULSAR_SOURCES ${PULSAR_SOURCES} ${PROTO_SOURCES}) -ADD_CUSTOM_COMMAND( - OUTPUT ${PROTO_SOURCES} - COMMAND ${PROTOC_PATH} -I ../proto ../proto/PulsarApi.proto --cpp_out=${LIB_AUTOGEN_DIR} - DEPENDS - ../proto/PulsarApi.proto - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +if (INTEGRATE_VCPKG) + add_library(PROTO_OBJECTS OBJECT "${CMAKE_SOURCE_DIR}/proto/PulsarApi.proto") + target_link_libraries(PROTO_OBJECTS PUBLIC protobuf::libprotobuf) + target_include_directories(PROTO_OBJECTS PUBLIC ${LIB_AUTOGEN_DIR}) + protobuf_generate( + TARGET PROTO_OBJECTS + IMPORT_DIRS ${CMAKE_SOURCE_DIR}/proto + PROTOC_OUT_DIR ${LIB_AUTOGEN_DIR}) + set(COMMON_LIBS ${COMMON_LIBS} PROTO_OBJECTS) +else () + set(PROTO_SOURCES ${LIB_AUTOGEN_DIR}/PulsarApi.pb.cc ${LIB_AUTOGEN_DIR}/PulsarApi.pb.h) + set(PULSAR_SOURCES ${PULSAR_SOURCES} ${PROTO_SOURCES}) + ADD_CUSTOM_COMMAND( + OUTPUT ${PROTO_SOURCES} + COMMAND ${PROTOC_PATH} -I ../proto ../proto/PulsarApi.proto --cpp_out=${LIB_AUTOGEN_DIR} + DEPENDS + ../proto/PulsarApi.proto + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +endif () set(LIBRARY_VERSION $ENV{PULSAR_LIBRARY_VERSION}) if (NOT LIBRARY_VERSION) @@ -44,7 +53,6 @@ if (NOT LIBRARY_VERSION) endif(NOT LIBRARY_VERSION) if (MSVC) - find_package(dlfcn-win32 REQUIRED) set(CMAKE_DL_LIBS dlfcn-win32::dl psapi.lib) if (CMAKE_BUILD_TYPE STREQUAL "Debug") get_target_property(dlfcn-win32_LIBRARY dlfcn-win32::dl IMPORTED_LOCATION_DEBUG) @@ -60,12 +68,23 @@ set(LIB_NAME_SHARED ${LIB_NAME}) # this is the "object library" target: compiles the sources only once add_library(PULSAR_OBJECT_LIB OBJECT ${PULSAR_SOURCES}) set_property(TARGET PULSAR_OBJECT_LIB PROPERTY POSITION_INDEPENDENT_CODE 1) +if (INTEGRATE_VCPKG) + target_link_libraries(PULSAR_OBJECT_LIB PROTO_OBJECTS) +endif () +target_include_directories(PULSAR_OBJECT_LIB PUBLIC + "${CMAKE_SOURCE_DIR}" + "${CMAKE_SOURCE_DIR}/include" + "${CMAKE_BINARY_DIR}/include") if (BUILD_DYNAMIC_LIB) add_library(pulsarShared SHARED $<TARGET_OBJECTS:PULSAR_OBJECT_LIB>) set_property(TARGET pulsarShared PROPERTY OUTPUT_NAME ${LIB_NAME_SHARED}) set_property(TARGET pulsarShared PROPERTY VERSION ${LIBRARY_VERSION}) - target_link_libraries(pulsarShared ${COMMON_LIBS} ${CMAKE_DL_LIBS}) + target_link_libraries(pulsarShared PRIVATE ${COMMON_LIBS} ${CMAKE_DL_LIBS}) + target_include_directories(pulsarShared PUBLIC + ${CMAKE_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/include) + target_link_directories(pulsarShared PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) if (MSVC) target_include_directories(pulsarShared PRIVATE ${dlfcn-win32_INCLUDE_DIRS}) target_link_options(pulsarShared PRIVATE $<$<CONFIG:DEBUG>:/NODEFAULTLIB:MSVCRT>) @@ -80,6 +99,10 @@ endif() if (BUILD_STATIC_LIB) add_library(pulsarStatic STATIC $<TARGET_OBJECTS:PULSAR_OBJECT_LIB>) + target_include_directories(pulsarStatic PUBLIC + ${CMAKE_BINARY_DIR}/include + ${CMAKE_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/include) if (MSVC) set_property(TARGET pulsarStatic PROPERTY OUTPUT_NAME "${LIB_NAME}-static") target_include_directories(pulsarStatic PRIVATE ${dlfcn-win32_INCLUDE_DIRS}) diff --git a/perf/CMakeLists.txt b/perf/BuildPerf.cmake similarity index 62% copy from perf/CMakeLists.txt copy to perf/BuildPerf.cmake index 586f9b0..8b5bc6f 100644 --- a/perf/CMakeLists.txt +++ b/perf/BuildPerf.cmake @@ -17,21 +17,8 @@ # under the License. # -# Test tools -add_definitions(-D_GLIBCXX_USE_NANOSLEEP) +add_executable(perfProducer PerfProducer.cc) +target_link_libraries(perfProducer pulsarShared Boost::program_options) -set(PERF_PRODUCER_SOURCES - PerfProducer.cc -) - -set(PERF_CONSUMER_SOURCES - PerfConsumer.cc -) - -add_executable(perfProducer ${PERF_PRODUCER_SOURCES}) -add_executable(perfConsumer ${PERF_CONSUMER_SOURCES}) - -set(TOOL_LIBS ${CLIENT_LIBS} ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_THREAD_LIBRARY}) - -target_link_libraries(perfProducer pulsarShared ${TOOL_LIBS}) -target_link_libraries(perfConsumer pulsarShared ${TOOL_LIBS}) +add_executable(perfConsumer PerfConsumer.cc) +target_link_libraries(perfConsumer pulsarShared Boost::program_options) diff --git a/perf/CMakeLists.txt b/perf/CMakeLists.txt index 586f9b0..74215c7 100644 --- a/perf/CMakeLists.txt +++ b/perf/CMakeLists.txt @@ -17,21 +17,8 @@ # under the License. # -# Test tools -add_definitions(-D_GLIBCXX_USE_NANOSLEEP) - -set(PERF_PRODUCER_SOURCES - PerfProducer.cc -) - -set(PERF_CONSUMER_SOURCES - PerfConsumer.cc -) - -add_executable(perfProducer ${PERF_PRODUCER_SOURCES}) -add_executable(perfConsumer ${PERF_CONSUMER_SOURCES}) - -set(TOOL_LIBS ${CLIENT_LIBS} ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_THREAD_LIBRARY}) - -target_link_libraries(perfProducer pulsarShared ${TOOL_LIBS}) -target_link_libraries(perfConsumer pulsarShared ${TOOL_LIBS}) +if (INTEGRATE_VCPKG) + include(BuildPerf.cmake) +else () + include(LegacyBuildPerf.cmake) +endif () diff --git a/perf/CMakeLists.txt b/perf/LegacyBuildPerf.cmake similarity index 100% copy from perf/CMakeLists.txt copy to perf/LegacyBuildPerf.cmake diff --git a/tests/BuildTests.cmake b/tests/BuildTests.cmake new file mode 100644 index 0000000..c468f51 --- /dev/null +++ b/tests/BuildTests.cmake @@ -0,0 +1,57 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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(LIB_AUTOGEN_DIR ${AUTOGEN_DIR}/tests) +file(MAKE_DIRECTORY ${LIB_AUTOGEN_DIR}) +include_directories(${LIB_AUTOGEN_DIR}) + +find_package(GTest CONFIG REQUIRED) +set(GTEST_TARGETS GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main) + +add_library(TESTS_PROTO_OBJECTS OBJECT + ${CMAKE_CURRENT_SOURCE_DIR}/PaddingDemo.proto + ${CMAKE_CURRENT_SOURCE_DIR}/proto/ExternalTest.proto + ${CMAKE_CURRENT_SOURCE_DIR}/proto/Test.proto + ) +target_link_libraries(TESTS_PROTO_OBJECTS PUBLIC protobuf::libprotobuf) +protobuf_generate( + TARGET TESTS_PROTO_OBJECTS + IMPORT_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/proto" "${CMAKE_CURRENT_SOURCE_DIR}" + PROTOC_OUT_DIR ${LIB_AUTOGEN_DIR}) + +file(GLOB TEST_SOURCES *.cc c/*.cc) +add_executable(pulsar-tests ${TEST_SOURCES}) +target_include_directories(pulsar-tests PRIVATE ${AUTOGEN_DIR}/lib) +target_compile_options(pulsar-tests PRIVATE -DTOKEN_PATH="${TOKEN_PATH}" -DTEST_CONF_DIR="${TEST_CONF_DIR}") +target_link_libraries(pulsar-tests PRIVATE pulsarStatic TESTS_PROTO_OBJECTS ${GTEST_TARGETS}) + +if (UNIX) + add_executable(ConnectionFailTest unix/ConnectionFailTest.cc HttpHelper.cc) + target_link_libraries(ConnectionFailTest pulsarStatic ${GTEST_TARGETS}) +endif () + +add_executable(BrokerMetadataTest brokermetadata/BrokerMetadataTest.cc) +target_link_libraries(BrokerMetadataTest pulsarStatic ${GTEST_TARGETS}) + +add_executable(Oauth2Test oauth2/Oauth2Test.cc) +target_compile_options(Oauth2Test PRIVATE -DTEST_CONF_DIR="${TEST_CONF_DIR}") +target_link_libraries(Oauth2Test pulsarStatic ${GTEST_TARGETS}) + +add_executable(ChunkDedupTest chunkdedup/ChunkDedupTest.cc HttpHelper.cc) +target_link_libraries(ChunkDedupTest pulsarStatic ${GTEST_TARGETS}) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3bc5a15..f895d34 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -17,60 +17,8 @@ # under the License. # -if (NOT PROTOC_PATH) - set(PROTOC_PATH protoc) -endif() - -set(LIB_AUTOGEN_DIR ${AUTOGEN_DIR}/tests) -file(MAKE_DIRECTORY ${LIB_AUTOGEN_DIR}) -include_directories(${LIB_AUTOGEN_DIR}) - -set(PROTO_DIR ${CMAKE_CURRENT_SOURCE_DIR}/proto) -set(PROTO_SOURCES ${LIB_AUTOGEN_DIR}/Test.pb.cc ${LIB_AUTOGEN_DIR}/ExternalTest.pb.cc) -add_custom_command( - OUTPUT ${PROTO_SOURCES} - COMMAND ${PROTOC_PATH} -I ${PROTO_DIR} ${PROTO_DIR}/Test.proto ${PROTO_DIR}/ExternalTest.proto --cpp_out=${LIB_AUTOGEN_DIR}) - -set(PROTO_SOURCE_PADDING ${LIB_AUTOGEN_DIR}/PaddingDemo.pb.cc) -add_custom_command( - OUTPUT ${PROTO_SOURCE_PADDING} - COMMAND ${PROTOC_PATH} -I . ./PaddingDemo.proto --cpp_out=${LIB_AUTOGEN_DIR} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - -set(PROTO_SOURCES ${PROTO_SOURCES} ${PROTO_SOURCE_PADDING}) - -include_directories(${LIB_AUTOGEN_DIR}) - -find_library(GMOCK_LIBRARY_PATH gmock) -find_library(GTEST_LIBRARY_PATH gtest) -find_library(GMOCKD_LIBRARY_PATH gmockd) -find_library(GTESTD_LIBRARY_PATH gtestd) -if (NOT GMOCKD_LIBRARY_PATH) - set(GMOCKD_LIBRARY_PATH ${GMOCK_LIBRARY_PATH}) -endif() -if (NOT GTESTD_LIBRARY_PATH) - set(GTESTD_LIBRARY_PATH ${GTEST_LIBRARY_PATH}) -endif() - -file(GLOB TEST_SOURCES *.cc c/*.cc) - -add_executable(pulsar-tests ${TEST_SOURCES} ${PROTO_SOURCES}) - -target_include_directories(pulsar-tests PRIVATE ${AUTOGEN_DIR}/lib) -target_compile_options(pulsar-tests PRIVATE -DTOKEN_PATH="${TOKEN_PATH}" -DTEST_CONF_DIR="${TEST_CONF_DIR}") -target_link_libraries(pulsar-tests ${CLIENT_LIBS} pulsarStatic $<$<CONFIG:Debug>:${GMOCKD_LIBRARY_PATH}> $<$<CONFIG:Debug>:${GTESTD_LIBRARY_PATH}> $<$<NOT:$<CONFIG:Debug>>:${GMOCK_LIBRARY_PATH}> $<$<NOT:$<CONFIG:Debug>>:${GTEST_LIBRARY_PATH}>) - -if (UNIX) - add_executable(ConnectionFailTest unix/ConnectionFailTest.cc HttpHelper.cc) - target_link_libraries(ConnectionFailTest ${CLIENT_LIBS} pulsarStatic ${GTEST_LIBRARY_PATH}) +if (INTEGRATE_VCPKG) + include(BuildTests.cmake) +else () + include(LegacyBuildTests.cmake) endif () - -add_executable(BrokerMetadataTest brokermetadata/BrokerMetadataTest.cc) -target_link_libraries(BrokerMetadataTest ${CLIENT_LIBS} pulsarStatic ${GTEST_LIBRARY_PATH}) - -add_executable(Oauth2Test oauth2/Oauth2Test.cc) -target_compile_options(Oauth2Test PRIVATE -DTEST_CONF_DIR="${TEST_CONF_DIR}") -target_link_libraries(Oauth2Test ${CLIENT_LIBS} pulsarStatic ${GTEST_LIBRARY_PATH}) - -add_executable(ChunkDedupTest chunkdedup/ChunkDedupTest.cc HttpHelper.cc) -target_link_libraries(ChunkDedupTest ${CLIENT_LIBS} pulsarStatic ${GTEST_LIBRARY_PATH}) diff --git a/tests/CMakeLists.txt b/tests/LegacyBuildTests.cmake similarity index 99% copy from tests/CMakeLists.txt copy to tests/LegacyBuildTests.cmake index 3bc5a15..1e5335f 100644 --- a/tests/CMakeLists.txt +++ b/tests/LegacyBuildTests.cmake @@ -74,3 +74,4 @@ target_link_libraries(Oauth2Test ${CLIENT_LIBS} pulsarStatic ${GTEST_LIBRARY_PAT add_executable(ChunkDedupTest chunkdedup/ChunkDedupTest.cc HttpHelper.cc) target_link_libraries(ChunkDedupTest ${CLIENT_LIBS} pulsarStatic ${GTEST_LIBRARY_PATH}) + diff --git a/vcpkg b/vcpkg new file mode 160000 index 0000000..b051745 --- /dev/null +++ b/vcpkg @@ -0,0 +1 @@ +Subproject commit b051745c68faa6f65c493371d564c4eb8af34dad diff --git a/vcpkg.json b/vcpkg.json index 5cc3863..1f9ed97 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,29 +1,104 @@ { "name": "pulsar-cpp", - "version": "2.8.0", + "version": "3.5.0", "description": "Pulsar C++ SDK", + "builtin-baseline": "b051745c68faa6f65c493371d564c4eb8af34dad", "dependencies": [ - "boost-accumulators", - "boost-algorithm", - "boost-any", - "boost-circular-buffer", - "boost-asio", - "boost-date-time", - "boost-predef", - "boost-program-options", - "boost-property-tree", - "boost-random", - "boost-serialization", - "boost-xpressive", - "curl", - "openssl", - "protobuf", - "snappy", - "zlib", - "zstd", + { + "name": "boost-accumulators", + "version>=": "1.83.0" + }, + { + "name": "boost-algorithm", + "version>=": "1.83.0" + }, + { + "name": "boost-any", + "version>=": "1.83.0" + }, + { + "name": "boost-asio", + "version>=": "1.83.0" + }, + { + "name": "boost-circular-buffer", + "version>=": "1.83.0" + }, + { + "name": "boost-date-time", + "version>=": "1.83.0" + }, + { + "name": "boost-predef", + "version>=": "1.83.0" + }, + { + "name": "boost-property-tree", + "version>=": "1.83.0" + }, + { + "name": "boost-random", + "version>=": "1.83.0" + }, + { + "name": "boost-serialization", + "version>=": "1.83.0" + }, + { + "name": "boost-xpressive", + "version>=": "1.83.0" + }, + { + "name": "curl", + "default-features": false, + "features": [ + "openssl" + ], + "version>=": "8.4.0" + }, { "name": "dlfcn-win32", "platform": "windows" + }, + { + "name": "openssl", + "version>=": "3.1.4#1" + }, + { + "name": "protobuf", + "version>=": "3.21.12" + }, + { + "name": "snappy", + "version>=": "1.1.10" + }, + { + "name": "zlib", + "version>=": "1.3" + }, + { + "name": "zstd", + "version>=": "1.5.5" + } + ], + "features": { + "perf": { + "description": "Build Performance Tool", + "dependencies": [ + { + "name": "boost-program-options", + "version>=": "1.83.0" + } + ] + }, + "tests": { + "description": "Build Tests", + "dependencies": [ + { + "name": "gtest", + "version>=": "1.14.0" + } + ] } - ] + } }