From 878cc44023e4a9cd66920376709fd48a166492c0 Mon Sep 17 00:00:00 2001 From: Yunze Xu Date: Tue, 13 Jul 2021 22:23:25 +0800 Subject: [PATCH] [C++] Support Windows Debug mode build (#11302) * Use find_package for CURL * Add version check for findCurl * Use find_package for Protobuf * Find snappyd and zstdd for MSVC Debug mode * Restore the Protobuf variables name * Remove predefined Protobuf_LITE_LIBRARIES variable * Add comments for why use CONFIG mode for find_package * Update C++ client README * Minor fix * Fix ZLIB debug libraries path error * Fix build error when LINK_STATIC is ON * Fix Windows x86 link error * Fix ZSTD and Snappy not found on Windows x86 platform --- .github/workflows/ci-cpp-build-windows.yaml | 4 +- pulsar-client-cpp/CMakeLists.txt | 58 ++++++++++++++++----- pulsar-client-cpp/README.md | 4 +- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci-cpp-build-windows.yaml b/.github/workflows/ci-cpp-build-windows.yaml index c524fdc4b0ca6..a287a4c3560e1 100644 --- a/.github/workflows/ci-cpp-build-windows.yaml +++ b/.github/workflows/ci-cpp-build-windows.yaml @@ -101,7 +101,7 @@ jobs: run: | if [ "$RUNNER_OS" == "Windows" ]; then cd pulsar-client-cpp && \ - cmake --build ./build + cmake --build ./build --config Release fi - name: Configure (dynamic library only) @@ -126,5 +126,5 @@ jobs: run: | if [ "$RUNNER_OS" == "Windows" ]; then cd pulsar-client-cpp && \ - cmake --build ./build-1 + cmake --build ./build-1 --config Release fi diff --git a/pulsar-client-cpp/CMakeLists.txt b/pulsar-client-cpp/CMakeLists.txt index 48a39643a0534..5cbbea6780b52 100644 --- a/pulsar-client-cpp/CMakeLists.txt +++ b/pulsar-client-cpp/CMakeLists.txt @@ -27,6 +27,11 @@ if (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_DEBUG_ROOT "${CMAKE_SOURCE_DIR}/vcpkg_installed/${VCPKG_TRIPLET}/debug") + if (CMAKE_BUILD_TYPE STREQUAL "Debug") + set(ZLIB_ROOT ${VCPKG_DEBUG_ROOT}) + set(OPENSSL_ROOT_DIR ${VCPKG_DEBUG_ROOT}) + endif () endif() find_program(CCACHE_PROGRAM ccache) @@ -85,7 +90,6 @@ endif(MSVC) set(CMAKE_POSITION_INDEPENDENT_CODE ON) -set(Protobuf_LITE_LIBRARIES $ENV{PROTOBUF_LIBRARIES}) set(LOG_CATEGORY_NAME $ENV{LOG_CATEGORY_NAME}) if (NOT LOG_CATEGORY_NAME) @@ -123,6 +127,8 @@ if (LINK_STATIC) find_library(CURL_LIBRARIES NAMES libcurl.a curl curl_a libcurl_a) find_library(LIB_ZSTD NAMES libzstd.a) find_library(LIB_SNAPPY NAMES libsnappy.a) + message(STATUS "Protobuf_LITE_LIBRARIES: ${Protobuf_LITE_LIBRARIES}") + set(COMMON_LIBS ${Protobuf_LITE_LIBRARIES} ${COMMON_LIBS}) if (USE_LOG4CXX) if (LOG4CXX_USE_DYNAMIC_LIBS) @@ -156,17 +162,46 @@ else() # Link to shared libraries find_package(ZLIB REQUIRED) set(ZLIB_LIBRARIES ${ZLIB_LIBRARIES}) - if (NOT PROTOBUF_LIBRARIES) - find_package(ProtoBuf QUIET) - if (NOT Protobuf_FOUND OR NOT Protobuf_LITE_LIBRARIES) - find_library(Protobuf_LITE_LIBRARIES protobuf-lite libprotobuf-lite) - find_path(Protobuf_INCLUDE_DIRS google/protobuf/stubs/common.h) - endif() - endif (NOT PROTOBUF_LIBRARIES) + # NOTE: The default MODULE mode may not find debug libraries so use CONFIG mode here + find_package(Protobuf QUIET CONFIG) + # NOTE: On Windows x86 platform, Protobuf_FOUND might be set false but Protobuf_INCLUDE_DIRS and + # Protobuf_LITE_LIBRARIES are both found. + if (Protobuf_INCLUDE_DIRS AND Protobuf_LITE_LIBRARIES AND NOT Protobuf_FOUND) + set(Protobuf_FOUND TRUE) + endif () + if (Protobuf_FOUND) + message("Found Protobuf in config mode") + message(STATUS "Protobuf_LITE_LIBRARIES: ${Protobuf_LITE_LIBRARIES}") + message(STATUS "Protobuf_INCLUDE_DIRS: ${Protobuf_INCLUDE_DIRS}") + else () + message("Failed to find Protobuf in config mode, try to find it from system path") + find_library(Protobuf_LITE_LIBRARIES protobuf-lite libprotobuf-lite) + find_path(Protobuf_INCLUDE_DIRS google/protobuf/stubs/common.h) + message(STATUS "Protobuf_LITE_LIBRARIES: ${Protobuf_LITE_LIBRARIES}") + message(STATUS "Protobuf_INCLUDE_DIRS: ${Protobuf_INCLUDE_DIRS}") + endif () + + if (${Protobuf_FOUND} AND (${CMAKE_VERSION} VERSION_GREATER 3.8)) + set(COMMON_LIBS protobuf::libprotobuf-lite ${COMMON_LIBS}) + else () + set(COMMON_LIBS ${Protobuf_LITE_LIBRARIES} ${COMMON_LIBS}) + endif () - find_library(LIB_ZSTD zstd) - find_library(LIB_SNAPPY NAMES snappy libsnappy) - find_library(CURL_LIBRARIES NAMES curl libcurl) + if (MSVC AND (${CMAKE_BUILD_TYPE} STREQUAL Debug)) + find_library(LIB_ZSTD zstdd HINTS "${VCPKG_DEBUG_ROOT}/lib") + else () + find_library(LIB_ZSTD zstd) + 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) + endif () + + find_package(CURL REQUIRED) + if (${CMAKE_VERSION} VERSION_GREATER "3.12") + set(COMMON_LIBS ${COMMON_LIBS} CURL::libcurl) + endif () if (USE_LOG4CXX) find_library(LOG4CXX_LIBRARY_PATH log4cxx) @@ -295,7 +330,6 @@ set(COMMON_LIBS ${CURL_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} - ${Protobuf_LITE_LIBRARIES} ${ADDITIONAL_LIBRARIES} ${CMAKE_DL_LIBS} ) diff --git a/pulsar-client-cpp/README.md b/pulsar-client-cpp/README.md index 61f6dff441485..49c33337678c8 100644 --- a/pulsar-client-cpp/README.md +++ b/pulsar-client-cpp/README.md @@ -214,8 +214,10 @@ cmake --build ./build --config Release Then all artifacts will be built into `build` subdirectory. -> NOTE: For Windows 32-bit, you need to use `-A Win32` and `-DVCPKG_TRIPLET=x86-windows`. +> **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