Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++] Allow to disable static or dynamic lib at build time #9570

Merged
merged 2 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pulsar-client-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ 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})

Expand Down Expand Up @@ -348,7 +354,10 @@ add_subdirectory(lib)
if(BUILD_PERF_TOOLS)
add_subdirectory(perf)
endif(BUILD_PERF_TOOLS)
add_subdirectory(examples)

if (BUILD_DYNAMIC_LIB)
add_subdirectory(examples)
endif()

if (BUILD_TESTS)
add_subdirectory(tests)
Expand Down
49 changes: 32 additions & 17 deletions pulsar-client-cpp/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ if (WIN32)
string(APPEND LIB_NAME_SHARED dll)
endif()

add_library(pulsarShared SHARED ${PULSAR_SOURCES})
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})
if (BUILD_DYNAMIC_LIB)
add_library(pulsarShared SHARED ${PULSAR_SOURCES})
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})
endif()


### pulsarSharedNossl not static link ssl, it could avoid rebuild libpulsar when ssl lib need update.
Expand All @@ -75,15 +77,19 @@ if (NOT ${RECORD_OPENSSL_CRYPTO_LIBRARY} MATCHES ".+\\.a$")
LIST(APPEND COMMON_LIBS_NOSSL ${RECORD_OPENSSL_CRYPTO_LIBRARY})
endif ()

add_library(pulsarSharedNossl SHARED ${PULSAR_SOURCES})
set_property(TARGET pulsarSharedNossl PROPERTY OUTPUT_NAME ${LIB_NAME_SHARED}nossl)
set_property(TARGET pulsarSharedNossl PROPERTY VERSION ${LIBRARY_VERSION})
target_link_libraries(pulsarSharedNossl ${COMMON_LIBS_NOSSL} ${CMAKE_DL_LIBS})
if (BUILD_DYNAMIC_LIB AND LINK_STATIC)
add_library(pulsarSharedNossl SHARED ${PULSAR_SOURCES})
set_property(TARGET pulsarSharedNossl PROPERTY OUTPUT_NAME ${LIB_NAME_SHARED}nossl)
set_property(TARGET pulsarSharedNossl PROPERTY VERSION ${LIBRARY_VERSION})
target_link_libraries(pulsarSharedNossl ${COMMON_LIBS_NOSSL} ${CMAKE_DL_LIBS})
endif()

add_library(pulsarStatic STATIC ${PULSAR_SOURCES})
set_property(TARGET pulsarStatic PROPERTY OUTPUT_NAME ${LIB_NAME})
set_property(TARGET pulsarStatic PROPERTY VERSION ${LIBRARY_VERSION})
target_compile_definitions(pulsarStatic PRIVATE PULSAR_STATIC)
if (BUILD_STATIC_LIB)
add_library(pulsarStatic STATIC ${PULSAR_SOURCES})
set_property(TARGET pulsarStatic PROPERTY OUTPUT_NAME ${LIB_NAME})
set_property(TARGET pulsarStatic PROPERTY VERSION ${LIBRARY_VERSION})
target_compile_definitions(pulsarStatic PRIVATE PULSAR_STATIC)
endif()

if (MSVC)
target_include_directories(pulsarStatic PRIVATE ${dlfcn-win32_INCLUDE_DIRS})
Expand All @@ -93,7 +99,7 @@ endif()

# When linking statically, install a libpulsar.a that contains all the
# required dependencies except ssl
if (LINK_STATIC)
if (LINK_STATIC AND BUILD_STATIC_LIB)
if (MSVC)

# This function is to remove either "debug" or "optimized" library names
Expand Down Expand Up @@ -136,13 +142,22 @@ if (LINK_STATIC)
DEPENDS pulsarStatic
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
endif(MSVC)
else()
elseif(BUILD_STATIC_LIB)
# Install regular libpulsar.a
target_link_libraries(pulsarStatic ${COMMON_LIBS})
install(TARGETS pulsarStatic DESTINATION lib)
endif(LINK_STATIC)

install(TARGETS pulsarStatic DESTINATION lib)
install(TARGETS pulsarShared RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
install(TARGETS pulsarSharedNossl RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
if (BUILD_STATIC_LIB)
install(TARGETS pulsarStatic DESTINATION lib)
endif()

if (BUILD_DYNAMIC_LIB)
install(TARGETS pulsarShared RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
endif()

if (BUILD_DYNAMIC_LIB AND LINK_STATIC)
install(TARGETS pulsarSharedNossl RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
endif()

install(DIRECTORY "../include/pulsar" DESTINATION include)