Skip to content

Commit

Permalink
[pulsar-client-cpp] New port (#35579)
Browse files Browse the repository at this point in the history
* [pulsar-client-cpp] New port

* Remove unnecessary link options

* Address review comments

- Use find_dependency instead of find_package
- Specify BUILD_DYNAMIC_LIB explicitly

* Fix the Windows build and linkage

* Fix dynamic library not built when VCPKG_LIBRARY_LINKAGE is dynamic on Windows

* Fix Linux and OSX failures

* Remove PULSAR_FORCE_DYNAMIC_LIBRARY and upgrade version to 3.4.2

* Reduce the changes to the upstream CMakeLists.txt

* Remove unused version

* Optimize finding and linking dependency and patch the header for static library

* Support multi-config generators

* Fix path for release libraries
  • Loading branch information
BewareMyPower committed Dec 29, 2023
1 parent 695a696 commit dc19823
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 0 deletions.
82 changes: 82 additions & 0 deletions ports/pulsar-client-cpp/0001-use-find-package.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index fb4f1b1..1bcfd47 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -96,6 +96,7 @@ if (APPLE AND NOT LINK_STATIC)
else ()
set(LATEST_PROTOBUF FALSE)
endif ()
+set(LATEST_PROTOBUF FALSE)

if (NOT CMAKE_CXX_STANDARD)
if (LATEST_PROTOBUF)
@@ -157,14 +158,15 @@ if (LATEST_PROTOBUF)
# Use Config mode to avoid FindProtobuf.cmake does not find the Abseil library
find_package(Protobuf REQUIRED CONFIG)
else ()
- find_package(Protobuf REQUIRED)
+ find_package(protobuf CONFIG REQUIRED)
+ set(Protobuf_LIBRARIES protobuf::libprotobuf)
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)
+find_package(CURL REQUIRED)
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)
@@ -175,7 +177,7 @@ if (NOT CURL_INCLUDE_DIRS OR NOT CURL_LIBRARIES)
message(FATAL_ERROR "Could not find libcurl")
endif ()

-find_package(zlib QUIET)
+find_package(ZLIB REQUIRED)
if (NOT ZLIB_FOUND)
find_path(ZLIB_INCLUDE_DIRS NAMES zlib.h)
find_library(ZLIB_LIBRARIES NAMES z zlib zdll zlib1 zlibstatic)
@@ -226,6 +228,17 @@ elseif (LINK_STATIC AND VCPKG_TRIPLET)
if (LIB_SNAPPY)
message(STATUS "Found Snappy library: ${LIB_SNAPPY}")
endif ()
+elseif (1)
+ find_package(zstd CONFIG REQUIRED)
+ set(HAS_ZSTD 1)
+ set(LIB_ZSTD
+ $<IF:$<TARGET_EXISTS:zstd::libzstd_shared>,zstd::libzstd_shared,zstd::libzstd_static>)
+ find_package(Snappy CONFIG REQUIRED)
+ set(HAS_SNAPPY 1)
+ set(LIB_SNAPPY Snappy::snappy)
+ if (MSVC)
+ find_package(dlfcn-win32 CONFIG REQUIRED)
+ endif ()
else()
if (MSVC AND (${CMAKE_BUILD_TYPE} STREQUAL Debug))
find_library(LIB_ZSTD zstdd HINTS "${VCPKG_DEBUG_ROOT}/lib")
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 8bd9749..551ac0c 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -31,12 +31,14 @@ include_directories(${LIB_AUTOGEN_DIR})
# 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})
+
+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(LIBRARY_VERSION $ENV{PULSAR_LIBRARY_VERSION})
if (NOT LIBRARY_VERSION)
41 changes: 41 additions & 0 deletions ports/pulsar-client-cpp/portfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO apache/pulsar-client-cpp
REF "v${VERSION}"
SHA512 b1f56ca8d5edb7faaba68eb4e04fcb4e458ccf2c7a5b0fb6d66868c6507081344fb3f0ebb29afe9aef567295a249b09cdeb3fb00285746767bbccef65a0f6e70
HEAD_REF main
PATCHES
0001-use-find-package.patch
)

string(COMPARE EQUAL "${VCPKG_LIBRARY_LINKAGE}" "static" BUILD_STATIC_LIB)
string(COMPARE EQUAL "${VCPKG_LIBRARY_LINKAGE}" "dynamic" BUILD_DYNAMIC_LIB)

vcpkg_cmake_configure(
SOURCE_PATH "${SOURCE_PATH}"
OPTIONS
-DBUILD_TESTS=OFF
-DBUILD_PERF_TOOLS=OFF
-DBUILD_DYNAMIC_LIB=${BUILD_DYNAMIC_LIB}
-DBUILD_STATIC_LIB=${BUILD_STATIC_LIB}
)

vcpkg_cmake_install()

if (BUILD_STATIC_LIB)
vcpkg_replace_string("${CURRENT_PACKAGES_DIR}/include/pulsar/defines.h"
"#ifdef PULSAR_STATIC"
"#if 1")
endif ()

file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
if(VCPKG_LIBRARY_LINKAGE STREQUAL "static")
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/bin" "${CURRENT_PACKAGES_DIR}/debug/bin")
endif()

vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE")
configure_file("${CMAKE_CURRENT_LIST_DIR}/usage" "${CURRENT_PACKAGES_DIR}/share/${PORT}/usage" COPYONLY)

configure_file("${CMAKE_CURRENT_LIST_DIR}/unofficial-pulsar-config.cmake" "${CURRENT_PACKAGES_DIR}/share/unofficial-pulsar/unofficial-pulsar-config.cmake" @ONLY)

vcpkg_copy_pdbs()
81 changes: 81 additions & 0 deletions ports/pulsar-client-cpp/unofficial-pulsar-config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
if (NOT TARGET unofficial::pulsar::pulsar)
get_filename_component(VCPKG_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(VCPKG_IMPORT_PREFIX "${VCPKG_IMPORT_PREFIX}" PATH)
get_filename_component(VCPKG_IMPORT_PREFIX "${VCPKG_IMPORT_PREFIX}" PATH)

find_path(_pulsar_include_dir NAMES "pulsar/Client.h" PATH "${VCPKG_IMPORT_PREFIX}/include")
set(_temp_cmake_ignore_path ${CMAKE_IGNORE_PATH})
# Without setting CMAKE_IGNORE_PATH, the library under debug/lib/ directory could be found
set(CMAKE_IGNORE_PATH "${VCPKG_IMPORT_PREFIX}/debug/lib")
find_library(_pulsar_library_release NAMES pulsar pulsar-static PATH "${VCPKG_IMPORT_PREFIX}/lib")
set(CMAKE_IGNORE_PATH ${_temp_cmake_ignore_path})
unset(_temp_cmake_ignore_path)
find_library(_pulsar_library_debug NAMES pulsar pulsar-static PATH "${VCPKG_IMPORT_PREFIX}/debug/lib")
message(STATUS "Found _pulsar_library_release: ${_pulsar_library_release}")
message(STATUS "Found _pulsar_library_debug: ${_pulsar_library_debug}")
if (NOT _pulsar_include_dir OR NOT _pulsar_library_release)
message(FATAL_ERROR "Broken installation of vcpkg port pulsar-client-cpp")
endif ()

if (MSVC AND "@VCPKG_LIBRARY_LINKAGE@" STREQUAL "dynamic")
find_file(_pulsar_release_dll NAMES "pulsar.dll" PATHS "${VCPKG_IMPORT_PREFIX}/bin" NO_DEFAULT_PATH)
find_file(_pulsar_debug_dll NAMES "pulsar.dll" PATHS "${VCPKG_IMPORT_PREFIX}/debug/bin" NO_DEFAULT_PATH)
if (NOT _pulsar_release_dll)
message(FATAL_ERROR "No pulsar.dll found")
endif ()
message(STATUS "Found _pulsar_release_dll: ${_pulsar_release_dll}")
message(STATUS "Found _pulsar_debug_dll: ${_pulsar_debug_dll}")
endif ()

include(CMakeFindDependencyMacro)
find_dependency(OpenSSL)
find_dependency(ZLIB)
find_dependency(protobuf CONFIG)
find_dependency(CURL CONFIG)
find_dependency(zstd CONFIG)
find_dependency(snappy CONFIG)
if (MSVC)
find_dependency(dlfcn-win32 CONFIG)
endif ()

if (_pulsar_release_dll)
add_library(unofficial::pulsar::pulsar SHARED IMPORTED)
set_target_properties(unofficial::pulsar::pulsar PROPERTIES
IMPORTED_CONFIGURATIONS "Release"
IMPORTED_IMPLIB_RELEASE "${_pulsar_library_release}"
IMPORTED_LOCATION_RELEASE "${_pulsar_release_dll}")
if (_pulsar_debug_dll)
set_target_properties(unofficial::pulsar::pulsar PROPERTIES
IMPORTED_CONFIGURATIONS "Release;DEBUG"
IMPORTED_IMPLIB_DEBUG "${_pulsar_library_debug}"
IMPORTED_LOCATION_DEBUG "${_pulsar_debug_dll}")
unset(_pulsar_debug_dll CACHE)
endif ()
unset(_pulsar_release_dll CACHE)
else ()
add_library(unofficial::pulsar::pulsar UNKNOWN IMPORTED)
set_target_properties(unofficial::pulsar::pulsar PROPERTIES
IMPORTED_LOCATION_RELEASE "${_pulsar_library_release}")
if (_pulsar_library_debug)
set_target_properties(unofficial::pulsar::pulsar PROPERTIES
IMPORTED_LOCATION_DEBUG "${_pulsar_library_debug}")
unset(_pulsar_library_debug CACHE)
endif ()
endif ()
set_target_properties(unofficial::pulsar::pulsar PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_pulsar_include_dir}")
target_link_libraries(unofficial::pulsar::pulsar INTERFACE
OpenSSL::SSL
OpenSSL::Crypto
ZLIB::ZLIB
protobuf::libprotobuf
CURL::libcurl
$<IF:$<TARGET_EXISTS:zstd::libzstd_shared>,zstd::libzstd_shared,zstd::libzstd_static>
Snappy::snappy
)
if (MSVC)
target_link_libraries(unofficial::pulsar::pulsar INTERFACE dlfcn-win32::dl)
endif ()
unset(_pulsar_library_release CACHE)
unset(_pulsar_include_dir CACHE)
endif ()
4 changes: 4 additions & 0 deletions ports/pulsar-client-cpp/usage
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pulsar-client-cpp provides CMake targets:

find_package(unofficial-pulsar CONFIG REQUIRED)
target_link_libraries(main PRIVATE unofficial::pulsar::pulsar)
48 changes: 48 additions & 0 deletions ports/pulsar-client-cpp/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "pulsar-client-cpp",
"version": "3.4.2",
"description": "The Apache Pulsar C++ library",
"homepage": "https://github.com/apache/pulsar-client-cpp",
"license": "Apache-2.0",
"supports": "!(arm & windows) & !android",
"dependencies": [
"boost-accumulators",
"boost-algorithm",
"boost-any",
"boost-asio",
"boost-circular-buffer",
"boost-date-time",
"boost-predef",
"boost-property-tree",
"boost-random",
"boost-serialization",
"boost-xpressive",
{
"name": "curl",
"default-features": false,
"features": [
"openssl"
]
},
{
"name": "dlfcn-win32",
"platform": "windows"
},
"openssl",
{
"name": "protobuf",
"version>=": "3.21.12"
},
"snappy",
{
"name": "vcpkg-cmake",
"host": true
},
{
"name": "vcpkg-cmake-config",
"host": true
},
"zlib",
"zstd"
]
}
4 changes: 4 additions & 0 deletions versions/baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -6856,6 +6856,10 @@
"baseline": "1.14",
"port-version": 0
},
"pulsar-client-cpp": {
"baseline": "3.4.2",
"port-version": 0
},
"pulzed-mini": {
"baseline": "0.9.14",
"port-version": 0
Expand Down
9 changes: 9 additions & 0 deletions versions/p-/pulsar-client-cpp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"versions": [
{
"git-tree": "f022e5dacbf10b0a6cead0f065ce6ab885b4c9af",
"version": "3.4.2",
"port-version": 0
}
]
}

0 comments on commit dc19823

Please sign in to comment.