Skip to content

Commit 207785a

Browse files
Define a platform-support CMake target (#1332)
* Define a mongo::detail::c_platform support library mongo::detail::c_platform is intended to hold usage requirements common to all libraries in the project. This will replace usage of directory-level compile/link options, which do not propagate on the generated libraries when they are installed. This allows one to export/install libmongoc libraries that expose additional usage requirements, such as linking instrumentation runtimes (coverage, sanitizers, etc.) Also: Sanitizers.cmake and ENABLE_COVERAGE now attach their options to the platform-support library. This allows installed libmongoc with sanitizers enabled to "just work," as downstream users will now receive link options for coverage and sanitizers as usage requirements from the libbson and libmongoc libraries. Also: Enable threading on c_platform rather than linking. Fix the bson and mongoc packages to pull Threads::Threads as a dependency This also affects generated pkg-config files automatically.
1 parent ca6782a commit 207785a

File tree

10 files changed

+106
-50
lines changed

10 files changed

+106
-50
lines changed

CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ list (APPEND CMAKE_MODULE_PATH
88
)
99

1010
include (MongoSettings)
11+
include (MongoPlatform)
1112
include (GeneratePkgConfig)
1213

1314
# Subcomponents:
@@ -195,6 +196,16 @@ mongo_bool_setting(
195196
]]
196197
)
197198

199+
if(ENABLE_COVERAGE)
200+
mongo_platform_link_options(--coverage)
201+
mongo_platform_compile_options($<BUILD_INTERFACE:--coverage>)
202+
endif()
203+
204+
# Enable multi-threading:
205+
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
206+
find_package(Threads REQUIRED)
207+
mongo_platform_use_target(Threads::Threads)
208+
198209
# Optionally enable C++ to do some C++-specific tests
199210
include (CheckLanguage)
200211
check_language (CXX)
@@ -224,7 +235,7 @@ include(MongoC-Warnings)
224235
# Enable "maintainer flags," which are supplementary but not mandatory.
225236
# (As opposed to MongoC-Warnings.cmake, which defines "the code is broken" warnings)
226237
if(ENABLE_MAINTAINER_FLAGS)
227-
mongoc_add_platform_compile_options(
238+
mongoc_add_warning_options(
228239
gnu-like:-Werror
229240
gnu-like:-pedantic
230241
gnu-like:-Wall

build/cmake/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set (build_cmake_MODULES
99
FindSphinx.cmake
1010
LoadVersion.cmake
1111
MongoCPackage.cmake
12+
MongoPlatform.cmake
1213
MongoSettings.cmake
1314
MongoC-Warnings.cmake
1415
ParseVersion.cmake

build/cmake/MongoC-Warnings.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
1414
These options are attached to the source directory and its children.
1515
]]
16-
function (mongoc_add_platform_compile_options)
17-
list(APPEND CMAKE_MESSAGE_CONTEXT mongoc_add_platform_compile_options)
16+
function (mongoc_add_warning_options)
17+
list(APPEND CMAKE_MESSAGE_CONTEXT ${CMAKE_CURRENT_FUNCTION})
1818
# Conditional prefixes:
1919
set(cond/gnu $<C_COMPILER_ID:GNU>)
2020
set(cond/llvm-clang $<C_COMPILER_ID:Clang>)
@@ -47,7 +47,7 @@ function (mongoc_add_platform_compile_options)
4747
endif()
4848
set(opt "$<${expr}:${suffix}>")
4949
else ()
50-
message (SEND_ERROR "Unknown option prefix to mongoc_add_platform_compile_options(): “${prefix}” in “${opt}”")
50+
message (SEND_ERROR "Unknown option prefix to ${CMAKE_CURRENT_FUNCTION}(): “${prefix}” in “${opt}”")
5151
break()
5252
endif ()
5353
set(opt "${before}${opt}")
@@ -61,7 +61,7 @@ set (is_c_lang "$<COMPILE_LANGUAGE:C>")
6161

6262
# These below warnings should always be unconditional hard errors, as the code is
6363
# almost definitely broken
64-
mongoc_add_platform_compile_options (
64+
mongoc_add_warning_options (
6565
# Implicit function or variable declarations
6666
gnu-like:lang-c:-Werror=implicit msvc:/we4013 msvc:/we4431
6767
# Missing return types/statements

build/cmake/MongoPlatform.cmake

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#[[
2+
3+
Defines a target mongo::detail::c_platform (alias of _mongo-platform), which
4+
exposes system-level supporting compile and link usage requirements. All targets
5+
should link to this target with level PUBLIC.
6+
7+
Use mongo_platform_compile_options and mongo_platform_link_options to add usage
8+
requirements to this library.
9+
10+
The mongo::detail::c_platform library is installed and exported with the
11+
bson-targets export set as an implementation detail. It is installed with this
12+
export set so that it is available to both libbson and libmongoc (attempting to
13+
install this target in both bson-targets and mongoc-targets export sets would
14+
lead to duplicate definitions of mongo::detail::c_platform for downstream
15+
users).
16+
17+
]]
18+
19+
add_library(_mongo-platform INTERFACE)
20+
add_library(mongo::detail::c_platform ALIAS _mongo-platform)
21+
set_property(TARGET _mongo-platform PROPERTY EXPORT_NAME detail::c_platform)
22+
install(TARGETS _mongo-platform EXPORT bson-targets)
23+
24+
25+
#[[
26+
Define additional platform-support compile options
27+
28+
These options are added to the mongo::detail::c_platform INTERFACE library.
29+
]]
30+
function (mongo_platform_compile_options)
31+
list(APPEND CMAKE_MESSAGE_CONTEXT ${CMAKE_CURRENT_FUNCTION})
32+
message(DEBUG "Add platform-support compilation options: ${ARGN}")
33+
target_compile_options(_mongo-platform INTERFACE ${ARGN})
34+
endfunction ()
35+
36+
#[[
37+
Define additional platform-support link options.
38+
39+
These options are added to the mongo::detail::c_platform INTERFACE library.
40+
]]
41+
function(mongo_platform_link_options)
42+
list(APPEND CMAKE_MESSAGE_CONTEXT ${CMAKE_CURRENT_FUNCTION})
43+
message(DEBUG "Add platform-support runtime linking options: ${ARGN}")
44+
target_link_options(_mongo-platform INTERFACE ${ARGN})
45+
endfunction()
46+
47+
#[[
48+
Add targets to the usage requirements for the current platform.
49+
50+
All of the named items must be the names of existing targets. Note that these
51+
targets will also need to be available at import-time for consumers (unless
52+
wrapped in $<BUILD_INTERFACE:>).
53+
]]
54+
function(mongo_platform_use_target)
55+
list(APPEND CMAKE_MESSAGE_CONTEXT ${CMAKE_CURRENT_FUNCTION})
56+
message(DEBUG "Add platform-support usage of targets: ${ARGN}")
57+
foreach(item IN LISTS ARGN)
58+
if(item MATCHES "::")
59+
# CMake will enforce that this link names an existing target
60+
target_link_libraries(_mongo-platform INTERFACE "${item}")
61+
else()
62+
# Generate a configure-time-error if the named item is not the name of a target
63+
target_link_libraries(_mongo-platform INTERFACE
64+
$<IF:$<TARGET_EXISTS:${item}>,${item},NO_SUCH_TARGET::${item}>)
65+
endif()
66+
endforeach()
67+
endfunction()

build/cmake/Sanitizers.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ if (_sanitize)
3535
if (NOT "${${varname}}")
3636
message (SEND_ERROR "Requested sanitizer option '${flag}' is not supported by the compiler+linker")
3737
else ()
38-
message (STATUS "Building with ${flag}")
39-
add_compile_options ("${flag}")
40-
link_libraries ("${flag}")
38+
message (STATUS "Enabling sanitizers: ${flag}")
39+
mongo_platform_compile_options ($<BUILD_INTERFACE:${flag}>)
40+
mongo_platform_link_options (${flag})
4141
endif ()
4242
endif ()

src/libbson/CMakeLists.txt

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,6 @@ if (MSVC AND MSVC_VERSION VERSION_LESS 1900)
212212
target_compile_options(bson_shared PRIVATE /wd4756)
213213
endif ()
214214
set (CMAKE_CXX_VISIBILITY_PRESET hidden)
215-
if(ENABLE_COVERAGE)
216-
target_compile_options(bson_shared PRIVATE --coverage)
217-
target_link_options(bson_shared PUBLIC --coverage)
218-
endif()
219215
target_compile_definitions (bson_shared
220216
PRIVATE
221217
BSON_COMPILATION
@@ -237,6 +233,7 @@ target_include_directories (
237233
)
238234
set_target_properties (bson_shared PROPERTIES VERSION 0.0.0 SOVERSION 0)
239235
set_target_properties (bson_shared PROPERTIES OUTPUT_NAME "${BSON_OUTPUT_BASENAME}-${BSON_API_VERSION}")
236+
target_link_libraries (bson_shared PUBLIC mongo::detail::c_platform)
240237
mongo_generate_pkg_config (bson_shared FILENAME libbson-1.0.pc INSTALL)
241238

242239
if (ENABLE_APPLE_FRAMEWORK)
@@ -267,13 +264,6 @@ if (!APPLE)
267264
endif ()
268265
endif ()
269266

270-
set (THREADS_PREFER_PTHREAD_FLAG 1)
271-
find_package (Threads REQUIRED)
272-
target_link_libraries (bson_shared PRIVATE Threads::Threads)
273-
if (CMAKE_USE_PTHREADS_INIT)
274-
set (BSON_LIBRARIES ${BSON_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
275-
endif ()
276-
277267
if (WIN32)
278268
# gethostbyname
279269
target_link_libraries (bson_shared PRIVATE ws2_32)
@@ -299,14 +289,11 @@ if (MONGOC_ENABLE_STATIC_BUILD)
299289
JSONSL_PARSE_NAN
300290
MCOMMON_NAME_PREFIX=_bson_mcommon
301291
)
292+
target_link_libraries(bson_static PUBLIC mongo::detail::c_platform)
302293
if (NOT WIN32 AND ENABLE_PIC)
303294
target_compile_options (bson_static PUBLIC -fPIC)
304295
message (STATUS "Adding -fPIC to compilation of bson_static components")
305296
endif ()
306-
if(ENABLE_COVERAGE)
307-
target_compile_options(bson_static PRIVATE --coverage)
308-
target_link_options(bson_static PUBLIC --coverage)
309-
endif()
310297
# Several directories in the source and build trees contain headers we would like
311298
# include via relative reference, but they all end up in the same install path
312299
target_include_directories (
@@ -322,18 +309,15 @@ if (MONGOC_ENABLE_STATIC_BUILD)
322309
)
323310
set_target_properties (bson_static PROPERTIES VERSION 0.0.0)
324311
set_target_properties (bson_static PROPERTIES OUTPUT_NAME "${BSON_OUTPUT_BASENAME}-static-${BSON_API_VERSION}")
325-
# We use CMAKE_THREAD_LIBS_INIT rather than Threads::Threads here because the
326-
# latter fails when building on Mac OS X
327-
target_link_libraries (bson_static ${CMAKE_THREAD_LIBS_INIT})
328312
if (RT_LIBRARY)
329-
target_link_libraries (bson_static ${RT_LIBRARY})
313+
target_link_libraries (bson_static PUBLIC ${RT_LIBRARY})
330314
endif ()
331315
if (M_LIBRARY)
332-
target_link_libraries (bson_static ${M_LIBRARY})
316+
target_link_libraries (bson_static PUBLIC ${M_LIBRARY})
333317
endif ()
334318
if (NOT UNIX)
335319
# gethostbyname
336-
target_link_libraries (bson_static ws2_32)
320+
target_link_libraries (bson_static PUBLIC ws2_32)
337321
endif ()
338322
if(MONGOC_ENABLE_STATIC_INSTALL)
339323
mongo_generate_pkg_config(bson_static FILENAME libbson-static-1.0.pc INSTALL)

src/libbson/build/cmake/libbson-1.0-config.cmake.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ set_and_check (BSON_INCLUDE_DIRS "@PACKAGE_INCLUDE_INSTALL_DIRS@")
2929
# BSON_LIBRARY themselves.
3030
find_library (BSON_LIBRARY bson-1.0 PATHS "@PACKAGE_LIBRARY_INSTALL_DIRS@" NO_DEFAULT_PATH)
3131

32-
set (BSON_LIBRARIES ${BSON_LIBRARY})
32+
set (BSON_LIBRARIES ${BSON_LIBRARY} mongo::detail::c_platform)
3333

34+
include(CMakeFindDependencyMacro)
35+
find_dependency(bson-1.0)

src/libbson/build/cmake/libbson-static-1.0-config.cmake.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ set_and_check (BSON_STATIC_INCLUDE_DIRS "@PACKAGE_INCLUDE_INSTALL_DIRS@")
2929
# BSON_STATIC_LIBRARY themselves.
3030
find_library (BSON_STATIC_LIBRARY bson-static-1.0 PATHS "@PACKAGE_LIBRARY_INSTALL_DIRS@" NO_DEFAULT_PATH)
3131

32-
set (BSON_STATIC_LIBRARIES ${BSON_STATIC_LIBRARY})
32+
set (BSON_STATIC_LIBRARIES ${BSON_STATIC_LIBRARY} mongo::detail::c_platform)
3333

3434
foreach (LIB @LIBBSON_LIBRARIES@)
3535
list (APPEND BSON_STATIC_LIBRARIES ${LIB})
3636
endforeach ()
3737

3838
set (BSON_STATIC_DEFINITIONS BSON_STATIC)
39+
40+
include(CMakeFindDependencyMacro)
41+
find_dependency(bson-1.0)

src/libbson/src/bson-config.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
include(CMakeFindDependencyMacro)
2+
find_dependency(Threads) # Required for Threads::Threads
3+
14
include("${CMAKE_CURRENT_LIST_DIR}/bson-targets.cmake")

src/libmongoc/CMakeLists.txt

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -692,24 +692,18 @@ else ()
692692
message (STATUS "SASL disabled")
693693
endif ()
694694

695-
696-
set (THREADS_PREFER_PTHREAD_FLAG 1)
697-
find_package (Threads REQUIRED)
698-
if (CMAKE_USE_PTHREADS_INIT)
699-
set (THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
700-
endif ()
701-
702695
set (LIBRARIES
703696
${SASL_LIBRARIES} ${SSL_LIBRARIES} ${SHM_LIBRARIES}
704-
${SNAPPY_LIBRARIES} ${ZLIB_LIBRARIES} ${MONGOC_ZSTD_LIBRARIES} Threads::Threads ${ICU_LIBRARIES} ${LIBMONGOCRYPT_LIBRARY}
697+
${SNAPPY_LIBRARIES} ${ZLIB_LIBRARIES} ${MONGOC_ZSTD_LIBRARIES} ${ICU_LIBRARIES} ${LIBMONGOCRYPT_LIBRARY}
705698
)
706699
set (STATIC_LIBRARIES
707700
${SASL_LIBRARIES} ${SSL_LIBRARIES} ${SSL_STATIC_LIBRARIES} ${SHM_LIBRARIES}
708-
${SNAPPY_LIBRARIES} ${ZLIB_LIBRARIES} ${MONGOC_ZSTD_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ICU_LIBRARIES} ${LIBMONGOCRYPT_LIBRARY}
701+
${SNAPPY_LIBRARIES} ${ZLIB_LIBRARIES} ${MONGOC_ZSTD_LIBRARIES} ${ICU_LIBRARIES} ${LIBMONGOCRYPT_LIBRARY}
709702
)
710703

711704
add_library(_mongoc-dependencies INTERFACE)
712705
add_library(mongo::detail::c_dependencies ALIAS _mongoc-dependencies)
706+
target_link_libraries(_mongoc-dependencies INTERFACE mongo::detail::c_platform)
713707
install(TARGETS _mongoc-dependencies EXPORT mongoc-targets)
714708
set_property(TARGET _mongoc-dependencies PROPERTY EXPORT_NAME detail::c_dependencies)
715709

@@ -759,10 +753,6 @@ set_target_properties (mongoc_shared PROPERTIES CMAKE_CXX_VISIBILITY_PRESET hidd
759753
target_link_libraries (mongoc_shared PRIVATE ${LIBRARIES} PUBLIC ${BSON_LIBRARIES} mongo::detail::c_dependencies)
760754
target_include_directories (mongoc_shared PRIVATE ${ZLIB_INCLUDE_DIRS})
761755
target_include_directories (mongoc_shared PRIVATE ${LIBMONGOCRYPT_INCLUDE_DIRECTORIES})
762-
if(ENABLE_COVERAGE)
763-
target_compile_options(mongoc_shared PRIVATE --coverage)
764-
target_link_options(mongoc_shared PUBLIC --coverage)
765-
endif()
766756
if (MONGOC_ENABLE_MONGODB_AWS_AUTH)
767757
target_include_directories (mongoc_shared PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../kms-message/src")
768758
if (APPLE)
@@ -802,10 +792,6 @@ mongo_generate_pkg_config(mongoc_shared INSTALL RENAME libmongoc-${MONGOC_API_VE
802792

803793
if (MONGOC_ENABLE_STATIC_BUILD)
804794
add_library (mongoc_static STATIC ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING})
805-
if(ENABLE_COVERAGE)
806-
target_compile_options(mongoc_static PRIVATE --coverage)
807-
target_link_options(mongoc_static PUBLIC --coverage)
808-
endif()
809795
target_link_libraries (mongoc_static PUBLIC ${STATIC_LIBRARIES} ${BSON_STATIC_LIBRARIES} mongo::detail::c_dependencies)
810796
if (NOT WIN32 AND ENABLE_PIC)
811797
target_compile_options (mongoc_static PUBLIC -fPIC)
@@ -1120,7 +1106,6 @@ if (ENABLE_EXAMPLES)
11201106
mongoc_add_example (example-gridfs-bucket ${PROJECT_SOURCE_DIR}/examples/example-gridfs-bucket.c)
11211107
if (NOT WIN32 AND ENABLE_EXAMPLES)
11221108
mongoc_add_example (example-pool ${PROJECT_SOURCE_DIR}/examples/example-pool.c)
1123-
target_link_libraries (example-pool Threads::Threads)
11241109
endif ()
11251110
mongoc_add_example (example-scram ${PROJECT_SOURCE_DIR}/examples/example-scram.c)
11261111
mongoc_add_example (example-sdam-monitoring ${PROJECT_SOURCE_DIR}/examples/example-sdam-monitoring.c)
@@ -1212,7 +1197,7 @@ endif ()
12121197
# Collect link items for the static library to be inserted into the pkg-config
12131198
if(TARGET mongoc_static)
12141199
set(link_options
1215-
${SASL_LIBRARIES} ${SSL_LIBRARIES} ${SHM_LIBRARIES} ${THREAD_LIB} ${ZLIB_LIBRARIES}
1200+
${SASL_LIBRARIES} ${SSL_LIBRARIES} ${SHM_LIBRARIES} ${ZLIB_LIBRARIES}
12161201
${SNAPPY_LIBRARIES} ${MONGOC_ZSTD_LIBRARIES} ${ICU_LIBRARIES}
12171202
${LIBMONGOCRYPT_LIBRARY})
12181203
# Replace all absolute paths with search-dir link-file options:

0 commit comments

Comments
 (0)