Skip to content

Define a platform-support CMake target #1332

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

Merged
merged 5 commits into from
Jul 3, 2023
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
13 changes: 12 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ list (APPEND CMAKE_MODULE_PATH
)

include (MongoSettings)
include (MongoPlatform)
include (GeneratePkgConfig)

# Subcomponents:
Expand Down Expand Up @@ -195,6 +196,16 @@ mongo_bool_setting(
]]
)

if(ENABLE_COVERAGE)
mongo_platform_link_options(--coverage)
mongo_platform_compile_options($<BUILD_INTERFACE:--coverage>)
endif()

# Enable multi-threading:
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
mongo_platform_use_target(Threads::Threads)

# Optionally enable C++ to do some C++-specific tests
include (CheckLanguage)
check_language (CXX)
Expand Down Expand Up @@ -224,7 +235,7 @@ include(MongoC-Warnings)
# Enable "maintainer flags," which are supplementary but not mandatory.
# (As opposed to MongoC-Warnings.cmake, which defines "the code is broken" warnings)
if(ENABLE_MAINTAINER_FLAGS)
mongoc_add_platform_compile_options(
mongoc_add_warning_options(
gnu-like:-Werror
gnu-like:-pedantic
gnu-like:-Wall
Expand Down
1 change: 1 addition & 0 deletions build/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set (build_cmake_MODULES
FindSphinx.cmake
LoadVersion.cmake
MongoCPackage.cmake
MongoPlatform.cmake
MongoSettings.cmake
MongoC-Warnings.cmake
ParseVersion.cmake
Expand Down
8 changes: 4 additions & 4 deletions build/cmake/MongoC-Warnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

These options are attached to the source directory and its children.
]]
function (mongoc_add_platform_compile_options)
list(APPEND CMAKE_MESSAGE_CONTEXT mongoc_add_platform_compile_options)
function (mongoc_add_warning_options)
list(APPEND CMAKE_MESSAGE_CONTEXT ${CMAKE_CURRENT_FUNCTION})
# Conditional prefixes:
set(cond/gnu $<C_COMPILER_ID:GNU>)
set(cond/llvm-clang $<C_COMPILER_ID:Clang>)
Expand Down Expand Up @@ -47,7 +47,7 @@ function (mongoc_add_platform_compile_options)
endif()
set(opt "$<${expr}:${suffix}>")
else ()
message (SEND_ERROR "Unknown option prefix to mongoc_add_platform_compile_options(): “${prefix}” in “${opt}”")
message (SEND_ERROR "Unknown option prefix to ${CMAKE_CURRENT_FUNCTION}(): “${prefix}” in “${opt}”")
break()
endif ()
set(opt "${before}${opt}")
Expand All @@ -61,7 +61,7 @@ set (is_c_lang "$<COMPILE_LANGUAGE:C>")

# These below warnings should always be unconditional hard errors, as the code is
# almost definitely broken
mongoc_add_platform_compile_options (
mongoc_add_warning_options (
# Implicit function or variable declarations
gnu-like:lang-c:-Werror=implicit msvc:/we4013 msvc:/we4431
# Missing return types/statements
Expand Down
67 changes: 67 additions & 0 deletions build/cmake/MongoPlatform.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#[[

Defines a target mongo::detail::c_platform (alias of _mongo-platform), which
exposes system-level supporting compile and link usage requirements. All targets
should link to this target with level PUBLIC.

Use mongo_platform_compile_options and mongo_platform_link_options to add usage
requirements to this library.

The mongo::detail::c_platform library is installed and exported with the
bson-targets export set as an implementation detail. It is installed with this
export set so that it is available to both libbson and libmongoc (attempting to
install this target in both bson-targets and mongoc-targets export sets would
lead to duplicate definitions of mongo::detail::c_platform for downstream
users).

]]

add_library(_mongo-platform INTERFACE)
add_library(mongo::detail::c_platform ALIAS _mongo-platform)
set_property(TARGET _mongo-platform PROPERTY EXPORT_NAME detail::c_platform)
install(TARGETS _mongo-platform EXPORT bson-targets)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
install(TARGETS _mongo-platform EXPORT bson-targets)
install(TARGETS _mongo-platform EXPORT bson-targets)
install(TARGETS _mongo-platform EXPORT mongoc-targets)

Do we want to include the imported target in the mongoc-targets file as well? Or are we fine with obtaining it transitively via libbson?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a little out-of-place. Consider moving the install() command into src/libbson/CMakeLists.txt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be exported only once, otherwise it will be defined twice and goofs will ensue. I chose the bson-targets export set only because it is pulled by both bson and mongoc. An "ideal" would be to define a third export set, but I think that has diminishing value when it can just ride along with the bson targets. I've also tucked it into the "detail" namespace so that we "reserve the right" to move/break/change/delete it in the future.



#[[
Define additional platform-support compile options

These options are added to the mongo::detail::c_platform INTERFACE library.
]]
function (mongo_platform_compile_options)
list(APPEND CMAKE_MESSAGE_CONTEXT ${CMAKE_CURRENT_FUNCTION})
message(DEBUG "Add platform-support compilation options: ${ARGN}")
target_compile_options(_mongo-platform INTERFACE ${ARGN})
endfunction ()

#[[
Define additional platform-support link options.

These options are added to the mongo::detail::c_platform INTERFACE library.
]]
function(mongo_platform_link_options)
list(APPEND CMAKE_MESSAGE_CONTEXT ${CMAKE_CURRENT_FUNCTION})
message(DEBUG "Add platform-support runtime linking options: ${ARGN}")
target_link_options(_mongo-platform INTERFACE ${ARGN})
endfunction()

#[[
Add targets to the usage requirements for the current platform.

All of the named items must be the names of existing targets. Note that these
targets will also need to be available at import-time for consumers (unless
wrapped in $<BUILD_INTERFACE:>).
]]
function(mongo_platform_use_target)
list(APPEND CMAKE_MESSAGE_CONTEXT ${CMAKE_CURRENT_FUNCTION})
message(DEBUG "Add platform-support usage of targets: ${ARGN}")
foreach(item IN LISTS ARGN)
if(item MATCHES "::")
# CMake will enforce that this link names an existing target
target_link_libraries(_mongo-platform INTERFACE "${item}")
else()
# Generate a configure-time-error if the named item is not the name of a target
target_link_libraries(_mongo-platform INTERFACE
$<IF:$<TARGET_EXISTS:${item}>,${item},NO_SUCH_TARGET::${item}>)
endif()
endforeach()
endfunction()
6 changes: 3 additions & 3 deletions build/cmake/Sanitizers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ if (_sanitize)
if (NOT "${${varname}}")
message (SEND_ERROR "Requested sanitizer option '${flag}' is not supported by the compiler+linker")
else ()
message (STATUS "Building with ${flag}")
add_compile_options ("${flag}")
link_libraries ("${flag}")
message (STATUS "Enabling sanitizers: ${flag}")
mongo_platform_compile_options ($<BUILD_INTERFACE:${flag}>)
mongo_platform_link_options (${flag})
endif ()
endif ()
26 changes: 5 additions & 21 deletions src/libbson/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,6 @@ if (MSVC AND MSVC_VERSION VERSION_LESS 1900)
target_compile_options(bson_shared PRIVATE /wd4756)
endif ()
set (CMAKE_CXX_VISIBILITY_PRESET hidden)
if(ENABLE_COVERAGE)
target_compile_options(bson_shared PRIVATE --coverage)
target_link_options(bson_shared PUBLIC --coverage)
endif()
target_compile_definitions (bson_shared
PRIVATE
BSON_COMPILATION
Expand All @@ -237,6 +233,7 @@ target_include_directories (
)
set_target_properties (bson_shared PROPERTIES VERSION 0.0.0 SOVERSION 0)
set_target_properties (bson_shared PROPERTIES OUTPUT_NAME "${BSON_OUTPUT_BASENAME}-${BSON_API_VERSION}")
target_link_libraries (bson_shared PUBLIC mongo::detail::c_platform)
mongo_generate_pkg_config (bson_shared FILENAME libbson-1.0.pc INSTALL)

if (ENABLE_APPLE_FRAMEWORK)
Expand Down Expand Up @@ -267,13 +264,6 @@ if (!APPLE)
endif ()
endif ()

set (THREADS_PREFER_PTHREAD_FLAG 1)
find_package (Threads REQUIRED)
target_link_libraries (bson_shared PRIVATE Threads::Threads)
if (CMAKE_USE_PTHREADS_INIT)
set (BSON_LIBRARIES ${BSON_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
endif ()

if (WIN32)
# gethostbyname
target_link_libraries (bson_shared PRIVATE ws2_32)
Expand All @@ -299,14 +289,11 @@ if (MONGOC_ENABLE_STATIC_BUILD)
JSONSL_PARSE_NAN
MCOMMON_NAME_PREFIX=_bson_mcommon
)
target_link_libraries(bson_static PUBLIC mongo::detail::c_platform)
if (NOT WIN32 AND ENABLE_PIC)
target_compile_options (bson_static PUBLIC -fPIC)
message (STATUS "Adding -fPIC to compilation of bson_static components")
endif ()
if(ENABLE_COVERAGE)
target_compile_options(bson_static PRIVATE --coverage)
target_link_options(bson_static PUBLIC --coverage)
endif()
# Several directories in the source and build trees contain headers we would like
# include via relative reference, but they all end up in the same install path
target_include_directories (
Expand All @@ -322,18 +309,15 @@ if (MONGOC_ENABLE_STATIC_BUILD)
)
set_target_properties (bson_static PROPERTIES VERSION 0.0.0)
set_target_properties (bson_static PROPERTIES OUTPUT_NAME "${BSON_OUTPUT_BASENAME}-static-${BSON_API_VERSION}")
# We use CMAKE_THREAD_LIBS_INIT rather than Threads::Threads here because the
# latter fails when building on Mac OS X
target_link_libraries (bson_static ${CMAKE_THREAD_LIBS_INIT})
if (RT_LIBRARY)
target_link_libraries (bson_static ${RT_LIBRARY})
target_link_libraries (bson_static PUBLIC ${RT_LIBRARY})
endif ()
if (M_LIBRARY)
target_link_libraries (bson_static ${M_LIBRARY})
target_link_libraries (bson_static PUBLIC ${M_LIBRARY})
endif ()
if (NOT UNIX)
# gethostbyname
target_link_libraries (bson_static ws2_32)
target_link_libraries (bson_static PUBLIC ws2_32)
endif ()
if(MONGOC_ENABLE_STATIC_INSTALL)
mongo_generate_pkg_config(bson_static FILENAME libbson-static-1.0.pc INSTALL)
Expand Down
4 changes: 3 additions & 1 deletion src/libbson/build/cmake/libbson-1.0-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ set_and_check (BSON_INCLUDE_DIRS "@PACKAGE_INCLUDE_INSTALL_DIRS@")
# BSON_LIBRARY themselves.
find_library (BSON_LIBRARY bson-1.0 PATHS "@PACKAGE_LIBRARY_INSTALL_DIRS@" NO_DEFAULT_PATH)

set (BSON_LIBRARIES ${BSON_LIBRARY})
set (BSON_LIBRARIES ${BSON_LIBRARY} mongo::detail::c_platform)

include(CMakeFindDependencyMacro)
find_dependency(bson-1.0)
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ set_and_check (BSON_STATIC_INCLUDE_DIRS "@PACKAGE_INCLUDE_INSTALL_DIRS@")
# BSON_STATIC_LIBRARY themselves.
find_library (BSON_STATIC_LIBRARY bson-static-1.0 PATHS "@PACKAGE_LIBRARY_INSTALL_DIRS@" NO_DEFAULT_PATH)

set (BSON_STATIC_LIBRARIES ${BSON_STATIC_LIBRARY})
set (BSON_STATIC_LIBRARIES ${BSON_STATIC_LIBRARY} mongo::detail::c_platform)

foreach (LIB @LIBBSON_LIBRARIES@)
list (APPEND BSON_STATIC_LIBRARIES ${LIB})
endforeach ()

set (BSON_STATIC_DEFINITIONS BSON_STATIC)

include(CMakeFindDependencyMacro)
find_dependency(bson-1.0)
3 changes: 3 additions & 0 deletions src/libbson/src/bson-config.cmake
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
include(CMakeFindDependencyMacro)
find_dependency(Threads) # Required for Threads::Threads

include("${CMAKE_CURRENT_LIST_DIR}/bson-targets.cmake")
23 changes: 4 additions & 19 deletions src/libmongoc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -692,24 +692,18 @@ else ()
message (STATUS "SASL disabled")
endif ()


set (THREADS_PREFER_PTHREAD_FLAG 1)
find_package (Threads REQUIRED)
if (CMAKE_USE_PTHREADS_INIT)
set (THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
endif ()

set (LIBRARIES
${SASL_LIBRARIES} ${SSL_LIBRARIES} ${SHM_LIBRARIES}
${SNAPPY_LIBRARIES} ${ZLIB_LIBRARIES} ${MONGOC_ZSTD_LIBRARIES} Threads::Threads ${ICU_LIBRARIES} ${LIBMONGOCRYPT_LIBRARY}
${SNAPPY_LIBRARIES} ${ZLIB_LIBRARIES} ${MONGOC_ZSTD_LIBRARIES} ${ICU_LIBRARIES} ${LIBMONGOCRYPT_LIBRARY}
)
set (STATIC_LIBRARIES
${SASL_LIBRARIES} ${SSL_LIBRARIES} ${SSL_STATIC_LIBRARIES} ${SHM_LIBRARIES}
${SNAPPY_LIBRARIES} ${ZLIB_LIBRARIES} ${MONGOC_ZSTD_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ICU_LIBRARIES} ${LIBMONGOCRYPT_LIBRARY}
${SNAPPY_LIBRARIES} ${ZLIB_LIBRARIES} ${MONGOC_ZSTD_LIBRARIES} ${ICU_LIBRARIES} ${LIBMONGOCRYPT_LIBRARY}
)

add_library(_mongoc-dependencies INTERFACE)
add_library(mongo::detail::c_dependencies ALIAS _mongoc-dependencies)
target_link_libraries(_mongoc-dependencies INTERFACE mongo::detail::c_platform)
install(TARGETS _mongoc-dependencies EXPORT mongoc-targets)
set_property(TARGET _mongoc-dependencies PROPERTY EXPORT_NAME detail::c_dependencies)

Expand Down Expand Up @@ -759,10 +753,6 @@ set_target_properties (mongoc_shared PROPERTIES CMAKE_CXX_VISIBILITY_PRESET hidd
target_link_libraries (mongoc_shared PRIVATE ${LIBRARIES} PUBLIC ${BSON_LIBRARIES} mongo::detail::c_dependencies)
target_include_directories (mongoc_shared PRIVATE ${ZLIB_INCLUDE_DIRS})
target_include_directories (mongoc_shared PRIVATE ${LIBMONGOCRYPT_INCLUDE_DIRECTORIES})
if(ENABLE_COVERAGE)
target_compile_options(mongoc_shared PRIVATE --coverage)
target_link_options(mongoc_shared PUBLIC --coverage)
endif()
if (MONGOC_ENABLE_MONGODB_AWS_AUTH)
target_include_directories (mongoc_shared PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../kms-message/src")
if (APPLE)
Expand Down Expand Up @@ -802,10 +792,6 @@ mongo_generate_pkg_config(mongoc_shared INSTALL RENAME libmongoc-${MONGOC_API_VE

if (MONGOC_ENABLE_STATIC_BUILD)
add_library (mongoc_static STATIC ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING})
if(ENABLE_COVERAGE)
target_compile_options(mongoc_static PRIVATE --coverage)
target_link_options(mongoc_static PUBLIC --coverage)
endif()
target_link_libraries (mongoc_static PUBLIC ${STATIC_LIBRARIES} ${BSON_STATIC_LIBRARIES} mongo::detail::c_dependencies)
if (NOT WIN32 AND ENABLE_PIC)
target_compile_options (mongoc_static PUBLIC -fPIC)
Expand Down Expand Up @@ -1120,7 +1106,6 @@ if (ENABLE_EXAMPLES)
mongoc_add_example (example-gridfs-bucket ${PROJECT_SOURCE_DIR}/examples/example-gridfs-bucket.c)
if (NOT WIN32 AND ENABLE_EXAMPLES)
mongoc_add_example (example-pool ${PROJECT_SOURCE_DIR}/examples/example-pool.c)
target_link_libraries (example-pool Threads::Threads)
endif ()
mongoc_add_example (example-scram ${PROJECT_SOURCE_DIR}/examples/example-scram.c)
mongoc_add_example (example-sdam-monitoring ${PROJECT_SOURCE_DIR}/examples/example-sdam-monitoring.c)
Expand Down Expand Up @@ -1212,7 +1197,7 @@ endif ()
# Collect link items for the static library to be inserted into the pkg-config
if(TARGET mongoc_static)
set(link_options
${SASL_LIBRARIES} ${SSL_LIBRARIES} ${SHM_LIBRARIES} ${THREAD_LIB} ${ZLIB_LIBRARIES}
${SASL_LIBRARIES} ${SSL_LIBRARIES} ${SHM_LIBRARIES} ${ZLIB_LIBRARIES}
${SNAPPY_LIBRARIES} ${MONGOC_ZSTD_LIBRARIES} ${ICU_LIBRARIES}
${LIBMONGOCRYPT_LIBRARY})
# Replace all absolute paths with search-dir link-file options:
Expand Down