33
44# Required for Apple Silicon support.
55cmake_minimum_required (VERSION 3.19)
6+ include (CMakeDependentOption)
67
78project (
89 LaunchDarklyCPPSDKs
@@ -13,22 +14,63 @@ project(
1314
1415include (GNUInstallDirs)
1516
17+ option (BUILD_TESTING "Top-level switch for testing. Turn off to disable unit and contract tests." ON )
18+
19+ option (LD_BUILD_SHARED_LIBS "Build the SDKs as shared libraries" OFF )
20+
21+ cmake_dependent_option(LD_BUILD_UNIT_TESTS
22+ "Build the C++ unit tests."
23+ ON # default to enabling unit tests
24+ BUILD_TESTING;NOT LD_BUILD_SHARED_LIBS # only exposed if top-level switch is on, and also only when building
25+ # static libs. This is because we have hidden visibility of symbols by default (to only expose our C API.)
26+ OFF # otherwise, off
27+ )
28+
29+ # If you want to run the unit tests with valgrind, then LD_TESTING_SANITIZERS must of OFF.
30+ cmake_dependent_option(LD_TESTING_SANITIZERS
31+ "Enable sanitizers for unit tests."
32+ ON # default to enabling sanitizers
33+ LD_BUILD_UNIT_TESTS # only expose if unit tests enabled..
34+ OFF # otherwise, off
35+ )
36+
37+ cmake_dependent_option(LD_BUILD_CONTRACT_TESTS
38+ "Build contract test service."
39+ OFF # default to disabling contract tests, since they require running a service
40+ BUILD_TESTING # only expose if top-level switch is on..
41+ OFF # otherwise, off
42+ )
43+
44+ # The general strategy is to produce a fat artifact containing all of our dependencies so users
45+ # only have a single thing to link. We should support this either being a static or shared library.
46+ # Because OpenSSL is a large, and security relevant dependency, we should have a separate option
47+ # to link against that statically or dynamically.
48+
49+ option (LD_DYNAMIC_LINK_OPENSSL
50+ "Dynamically link OpenSSL instead of building with static library"
51+ OFF # default to linking OpenSSL statically
52+ )
53+
54+ option (LD_BUILD_EXAMPLES "Build hello-world examples." ON )
55+
56+
57+ if (LD_BUILD_SHARED_LIBS AND LD_BUILD_UNIT_TESTS)
58+ message (WARNING "LaunchDarkly: unit testing isn't supported while building shared libraries. Switch to static libraries or disable unit tests." )
59+ endif ()
60+
1661# All projects in this repo should share the same version of 3rd party depends.
1762# It's the only way to remain sane.
1863set (CMAKE_FILES "${CMAKE_CURRENT_SOURCE_DIR} /cmake" )
1964set (CMAKE_CXX_STANDARD 17)
2065
2166set (CMAKE_POSITION_INDEPENDENT_CODE ON )
2267
23- option (BUILD_TESTING "Enable C++ unit tests." ON )
2468
25- # If you want to run the unit tests with valgrind, then TESTING_SANITIZERS must of OFF.
26- option (TESTING_SANITIZERS "Enable sanitizers for unit tests." ON )
27-
28- if (BUILD_TESTING)
69+ if (LD_BUILD_UNIT_TESTS)
70+ message (STATUS "LaunchDarkly: building unit tests" )
2971 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_GLIBCXX_DEBUG" )
3072 add_compile_definitions (LAUNCHDARKLY_USE_ASSERT)
31- if (TESTING_SANITIZERS )
73+ if (LD_TESTING_SANITIZERS )
3274 if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
3375 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=undefined -fsanitize=leak" )
3476 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" )
@@ -53,13 +95,22 @@ if (BUILD_TESTING)
5395 enable_testing ()
5496endif ()
5597
56- set (OPENSSL_USE_STATIC_LIBS ON )
98+ if (LD_DYNAMIC_LINK_OPENSSL)
99+ message (STATUS "LaunchDarkly: searching for shared OpenSSL library" )
100+ set (OPENSSL_USE_STATIC_LIBS OFF )
101+ else ()
102+ message (STATUS "LaunchDarkly: searching for static OpenSSL library" )
103+ set (OPENSSL_USE_STATIC_LIBS ON )
104+ endif ()
105+
57106find_package (OpenSSL REQUIRED)
58107message (STATUS "LaunchDarkly: using OpenSSL v${OPENSSL_VERSION} " )
59108
109+ # Even though the main SDK might be a static or shared lib, boost should always statically
110+ # linked into the binary.
60111set (Boost_USE_STATIC_LIBS ON )
61112
62- if (BUILD_SHARED_LIBS )
113+ if (NOT LD_BUILD_STATIC_LIBS )
63114 # When building a shared library we hide all symbols
64115 # aside from this we have specifically exported for the C-API.
65116 set (CMAKE_CXX_VISIBILITY_PRESET hidden)
@@ -71,25 +122,24 @@ set(Boost_USE_STATIC_RUNTIME OFF)
71122find_package (Boost 1.81 REQUIRED COMPONENTS json url coroutine)
72123message (STATUS "LaunchDarkly: using Boost v${Boost_VERSION} " )
73124
74- add_subdirectory (libs/client-sdk)
75-
76- set (ORIGINAL_BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS} " )
77- set (BUILD_SHARED_LIBS OFF )
125+ include (${CMAKE_FILES} /certify.cmake)
126+ add_subdirectory (vendor/foxy)
78127
79- # Always build the common libraries as static libs .
128+ # Common, internal, and server-sent-events are built as "object" libraries .
80129add_subdirectory (libs/common)
81130add_subdirectory (libs/internal )
82131add_subdirectory (libs/server-sent-events)
83132
84- set (ORIGINAL_BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS} " )
85-
86- set (BUILD_TESTING OFF )
87- include (${CMAKE_FILES} /certify.cmake)
88- add_subdirectory (vendor/foxy)
89-
90- set (BUILD_TESTING "${ORIGINAL_BUILD_TESTING} " )
133+ # Built as static or shared depending on LD_BUILD_STATIC_LIBS variable.
134+ # This target "links" in common, internal, and sse as object libraries.
135+ add_subdirectory (libs/client-sdk)
91136
92- set (BUILD_SHARED_LIBS "${ORIGINAL_BUILD_SHARED_LIBS} " )
137+ if (LD_BUILD_CONTRACT_TESTS)
138+ message (STATUS "LaunchDarkly: building contract tests" )
139+ add_subdirectory (contract-tests)
140+ endif ()
93141
94- add_subdirectory (contract-tests)
95- add_subdirectory (examples)
142+ if (LD_BUILD_EXAMPLES)
143+ message (STATUS "LaunchDarkly: building examples" )
144+ add_subdirectory (examples)
145+ endif ()
0 commit comments