Skip to content

Commit

Permalink
[C++] Support Windows Debug mode build (#11302)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
BewareMyPower committed Jul 13, 2021
1 parent 8501345 commit 878cc44
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-cpp-build-windows.yaml
Expand Up @@ -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)
Expand All @@ -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
58 changes: 46 additions & 12 deletions pulsar-client-cpp/CMakeLists.txt
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -295,7 +330,6 @@ set(COMMON_LIBS
${CURL_LIBRARIES}
${OPENSSL_LIBRARIES}
${ZLIB_LIBRARIES}
${Protobuf_LITE_LIBRARIES}
${ADDITIONAL_LIBRARIES}
${CMAKE_DL_LIBS}
)
Expand Down
4 changes: 3 additions & 1 deletion pulsar-client-cpp/README.md
Expand Up @@ -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

Expand Down

0 comments on commit 878cc44

Please sign in to comment.