From 2efd47d1bbe30da06151fe9ee46938213894331d Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Thu, 6 Feb 2020 21:24:20 -0800 Subject: [PATCH] Fixed static linking on C++ lib on MacOS (#5581) * Fixed static linking on C++ lib on MacOS * Use `-undefined dynamic_lookup` when linking on Mac to not include python's own runtime * Fixed searching for protobuf (cherry picked from commit 125a5887e6ed79831a0602d7638928fd42b65b2a) --- pulsar-client-cpp/CMakeLists.txt | 37 +++++++++++++------------ pulsar-client-cpp/python/CMakeLists.txt | 3 +- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/pulsar-client-cpp/CMakeLists.txt b/pulsar-client-cpp/CMakeLists.txt index 55740b2e32a88..56144b27e70d5 100644 --- a/pulsar-client-cpp/CMakeLists.txt +++ b/pulsar-client-cpp/CMakeLists.txt @@ -68,6 +68,12 @@ endif(NOT LOG_CATEGORY_NAME) add_definitions(-DLOG_CATEGORY_NAME=${LOG_CATEGORY_NAME} -DBUILDING_PULSAR -DBOOST_ALL_NO_LIB -DBOOST_ALLOW_DEPRECATED_HEADERS) if (LINK_STATIC) + find_library(ZLIB_LIBRARIES REQUIRED NAMES libz.a z) + find_library(Protobuf_LITE_LIBRARIES NAMES libprotobuf-lite.a) + find_library(CURL_LIBRARIES NAMES libcurl.a curl curl_a) + find_library(LIB_ZSTD NAMES libzstd.a) + find_library(LIB_SNAPPY NAMES libsnappy.a) + if (USE_LOG4CXX) if (LOG4CXX_USE_DYNAMIC_LIBS) find_library(LOG4CXX_LIBRARY_PATH log4cxx) @@ -98,6 +104,19 @@ if (LINK_STATIC) SET(OPENSSL_USE_STATIC_LIBS TRUE) 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) + + find_library(LIB_ZSTD zstd) + find_library(LIB_SNAPPY snappy libsnappy) + find_library(CURL_LIBRARIES curl) if (USE_LOG4CXX) find_library(LOG4CXX_LIBRARY_PATH log4cxx) find_path(LOG4CXX_INCLUDE_PATH log4cxx/logger.h) @@ -110,24 +129,6 @@ else() find_package(Boost REQUIRED COMPONENTS program_options regex system) endif() -find_package(ZLIB REQUIRED) - -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() - -find_package(CURL QUIET) -if (NOT CURL_FOUND) - find_library(CURL_LIBRARIES NAMES curl libcurl_a) - find_path(CURL_INCLUDE_DIRS curl/curl.h) -endif() - -find_library(LIB_ZSTD zstd libzstd) - -find_library(LIB_SNAPPY snappy libsnappy) - if (BUILD_PYTHON_WRAPPER) find_package(PythonLibs REQUIRED) MESSAGE(STATUS "PYTHON: " ${PYTHONLIBS_VERSION_STRING}) diff --git a/pulsar-client-cpp/python/CMakeLists.txt b/pulsar-client-cpp/python/CMakeLists.txt index 5421cbc78389e..1cbc9728d23f3 100644 --- a/pulsar-client-cpp/python/CMakeLists.txt +++ b/pulsar-client-cpp/python/CMakeLists.txt @@ -51,7 +51,7 @@ set(PYTHON_WRAPPER_LIBS ${Boost_PYTHON_LIBRARY} ${Boost_PYTHON3_LIBRARY} ${Boost_PYTHON36_LIBRARY}) if (APPLE) - set(PYTHON_WRAPPER_LIBS ${PYTHON_LIBRARIES} ${PYTHON_WRAPPER_LIBS} + set(PYTHON_WRAPPER_LIBS ${PYTHON_WRAPPER_LIBS} ${Boost_PYTHON27-MT_LIBRARY_RELEASE} ${Boost_PYTHON37-MT_LIBRARY_RELEASE}) if (LINK_STATIC) @@ -71,6 +71,7 @@ if (NOT PYTHON_WRAPPER_LIBS) endif () if (APPLE) + set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS} -undefined dynamic_lookup") target_link_libraries(_pulsar -Wl,-all_load pulsarStatic ${PYTHON_WRAPPER_LIBS} ${COMMON_LIBS} ${ICU_LIBS}) else () if (NOT MSVC)