-
Notifications
You must be signed in to change notification settings - Fork 3
feat: clean up LD CMake variables & allow for OpenSSL dynamic link #255
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
Changes from all commits
67518ca
0d29f36
5a90282
095f285
a776637
0d134e9
9b8ca7c
88a238d
8c282af
692e3c3
4ba82d3
160a33f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
|
|
||
| # Required for Apple Silicon support. | ||
| cmake_minimum_required(VERSION 3.19) | ||
| include(CMakeDependentOption) | ||
|
|
||
| project( | ||
| LaunchDarklyCPPSDKs | ||
|
|
@@ -13,22 +14,63 @@ project( | |
|
|
||
| include(GNUInstallDirs) | ||
|
|
||
| option(BUILD_TESTING "Top-level switch for testing. Turn off to disable unit and contract tests." ON) | ||
|
|
||
| option(LD_BUILD_SHARED_LIBS "Build the SDKs as shared libraries" OFF) | ||
|
|
||
| cmake_dependent_option(LD_BUILD_UNIT_TESTS | ||
| "Build the C++ unit tests." | ||
| ON # default to enabling unit tests | ||
| BUILD_TESTING;NOT LD_BUILD_SHARED_LIBS # only exposed if top-level switch is on, and also only when building | ||
| # static libs. This is because we have hidden visibility of symbols by default (to only expose our C API.) | ||
| OFF # otherwise, off | ||
| ) | ||
|
|
||
| # If you want to run the unit tests with valgrind, then LD_TESTING_SANITIZERS must of OFF. | ||
| cmake_dependent_option(LD_TESTING_SANITIZERS | ||
| "Enable sanitizers for unit tests." | ||
| ON # default to enabling sanitizers | ||
| LD_BUILD_UNIT_TESTS # only expose if unit tests enabled.. | ||
| OFF # otherwise, off | ||
| ) | ||
|
|
||
| cmake_dependent_option(LD_BUILD_CONTRACT_TESTS | ||
| "Build contract test service." | ||
| OFF # default to disabling contract tests, since they require running a service | ||
| BUILD_TESTING # only expose if top-level switch is on.. | ||
| OFF # otherwise, off | ||
| ) | ||
|
|
||
| # The general strategy is to produce a fat artifact containing all of our dependencies so users | ||
| # only have a single thing to link. We should support this either being a static or shared library. | ||
| # Because OpenSSL is a large, and security relevant dependency, we should have a separate option | ||
| # to link against that statically or dynamically. | ||
|
|
||
| option(LD_DYNAMIC_LINK_OPENSSL | ||
| "Dynamically link OpenSSL instead of building with static library" | ||
| OFF # default to linking OpenSSL statically | ||
| ) | ||
|
|
||
| option(LD_BUILD_EXAMPLES "Build hello-world examples." ON) | ||
|
|
||
|
|
||
| if (LD_BUILD_SHARED_LIBS AND LD_BUILD_UNIT_TESTS) | ||
| message(WARNING "LaunchDarkly: unit testing isn't supported while building shared libraries. Switch to static libraries or disable unit tests.") | ||
| endif () | ||
|
|
||
| # All projects in this repo should share the same version of 3rd party depends. | ||
| # It's the only way to remain sane. | ||
| set(CMAKE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/cmake") | ||
| set(CMAKE_CXX_STANDARD 17) | ||
|
|
||
| set(CMAKE_POSITION_INDEPENDENT_CODE ON) | ||
|
|
||
| option(BUILD_TESTING "Enable C++ unit tests." ON) | ||
|
|
||
| # If you want to run the unit tests with valgrind, then TESTING_SANITIZERS must of OFF. | ||
| option(TESTING_SANITIZERS "Enable sanitizers for unit tests." ON) | ||
|
|
||
| if (BUILD_TESTING) | ||
| if (LD_BUILD_UNIT_TESTS) | ||
| message(STATUS "LaunchDarkly: building unit tests") | ||
| set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_GLIBCXX_DEBUG") | ||
| add_compile_definitions(LAUNCHDARKLY_USE_ASSERT) | ||
| if (TESTING_SANITIZERS) | ||
| if (LD_TESTING_SANITIZERS) | ||
| if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") | ||
| set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=undefined -fsanitize=leak") | ||
| elseif (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") | ||
|
|
@@ -53,13 +95,22 @@ if (BUILD_TESTING) | |
| enable_testing() | ||
| endif () | ||
|
|
||
| set(OPENSSL_USE_STATIC_LIBS ON) | ||
| if (LD_DYNAMIC_LINK_OPENSSL) | ||
| message(STATUS "LaunchDarkly: searching for shared OpenSSL library") | ||
| set(OPENSSL_USE_STATIC_LIBS OFF) | ||
| else () | ||
| message(STATUS "LaunchDarkly: searching for static OpenSSL library") | ||
| set(OPENSSL_USE_STATIC_LIBS ON) | ||
| endif () | ||
|
|
||
| find_package(OpenSSL REQUIRED) | ||
| message(STATUS "LaunchDarkly: using OpenSSL v${OPENSSL_VERSION}") | ||
|
|
||
| # Even though the main SDK might be a static or shared lib, boost should always statically | ||
| # linked into the binary. | ||
| set(Boost_USE_STATIC_LIBS ON) | ||
|
|
||
| if (BUILD_SHARED_LIBS) | ||
| if (NOT LD_BUILD_STATIC_LIBS) | ||
| # When building a shared library we hide all symbols | ||
| # aside from this we have specifically exported for the C-API. | ||
| set(CMAKE_CXX_VISIBILITY_PRESET hidden) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe not right now, but we probably need an option for this. For those that build themselves and want to build a C++ dll.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (If they build all their own stuff, then the ABI issue isn't really an SDK issue.) |
||
|
|
@@ -71,25 +122,24 @@ set(Boost_USE_STATIC_RUNTIME OFF) | |
| find_package(Boost 1.81 REQUIRED COMPONENTS json url coroutine) | ||
| message(STATUS "LaunchDarkly: using Boost v${Boost_VERSION}") | ||
|
|
||
| add_subdirectory(libs/client-sdk) | ||
|
|
||
| set(ORIGINAL_BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS}") | ||
| set(BUILD_SHARED_LIBS OFF) | ||
| include(${CMAKE_FILES}/certify.cmake) | ||
| add_subdirectory(vendor/foxy) | ||
|
|
||
| # Always build the common libraries as static libs. | ||
| # Common, internal, and server-sent-events are built as "object" libraries. | ||
| add_subdirectory(libs/common) | ||
| add_subdirectory(libs/internal) | ||
| add_subdirectory(libs/server-sent-events) | ||
|
|
||
| set(ORIGINAL_BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS}") | ||
|
|
||
| set(BUILD_TESTING OFF) | ||
| include(${CMAKE_FILES}/certify.cmake) | ||
| add_subdirectory(vendor/foxy) | ||
|
|
||
| set(BUILD_TESTING "${ORIGINAL_BUILD_TESTING}") | ||
| # Built as static or shared depending on LD_BUILD_STATIC_LIBS variable. | ||
| # This target "links" in common, internal, and sse as object libraries. | ||
| add_subdirectory(libs/client-sdk) | ||
|
|
||
| set(BUILD_SHARED_LIBS "${ORIGINAL_BUILD_SHARED_LIBS}") | ||
| if (LD_BUILD_CONTRACT_TESTS) | ||
| message(STATUS "LaunchDarkly: building contract tests") | ||
| add_subdirectory(contract-tests) | ||
| endif () | ||
|
|
||
| add_subdirectory(contract-tests) | ||
| add_subdirectory(examples) | ||
| if (LD_BUILD_EXAMPLES) | ||
| message(STATUS "LaunchDarkly: building examples") | ||
| add_subdirectory(examples) | ||
| endif () | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,15 @@ file(GLOB HEADER_LIST CONFIGURE_DEPENDS | |
| "${LaunchDarklyCPPClient_SOURCE_DIR}/include/launchdarkly/client_side/*.hpp" | ||
| ) | ||
|
|
||
| # Automatic library: static or dynamic based on user config. | ||
| if (LD_BUILD_SHARED_LIBS) | ||
| message(STATUS "LaunchDarkly: building client-sdk as shared library") | ||
| add_library(${LIBNAME} SHARED) | ||
| else () | ||
| message(STATUS "LaunchDarkly: building client-sdk as static library") | ||
| add_library(${LIBNAME} STATIC) | ||
| endif () | ||
|
|
||
| add_library(${LIBNAME} | ||
| target_sources(${LIBNAME} PRIVATE | ||
| ${HEADER_LIST} | ||
| data_sources/streaming_data_source.cpp | ||
| data_sources/data_source_event_handler.cpp | ||
|
|
@@ -43,14 +49,16 @@ add_library(${LIBNAME} | |
| serialization/json_all_flags.cpp | ||
| flag_manager/flag_manager.cpp | ||
| flag_manager/flag_persistence.cpp | ||
| bindings/c/sdk.cpp) | ||
| bindings/c/sdk.cpp | ||
| ) | ||
|
|
||
|
|
||
| if (MSVC OR (NOT BUILD_SHARED_LIBS)) | ||
| if (MSVC OR LD_BUILD_STATIC_LIBS) | ||
| target_link_libraries(${LIBNAME} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to note this MSVC condition in the readme. Unless we want to figure out how to make it actually work with dynamically linked boost on windows (world of pain). |
||
| PUBLIC launchdarkly::common | ||
| PRIVATE Boost::headers Boost::json Boost::url launchdarkly::sse launchdarkly::internal foxy) | ||
| else () | ||
| # The default static lib builds, for linux, are positition independent. | ||
| # The default static lib builds, for linux, are position independent. | ||
| # So they do not link into a shared object without issues. So, when | ||
| # building shared objects do not link the static libraries and instead | ||
| # use the "src.hpp" files for required libraries. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throwing a warning when someone tries to build shared lib with unit tests enabled. This won't work since we hide symbols by default.