Skip to content

Commit 5227387

Browse files
committed
feat mongo: add CMake support for mongo-c-driver 2.x.x
1 parent 571b8ca commit 5227387

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

cmake/SetupMongoDeps.cmake

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,25 @@ include_guard(GLOBAL)
22

33
option(USERVER_MONGODB_USE_CMAKE_CONFIG "Use mongoc cmake configuration" ON)
44

5+
set(USERVER_MONGO_LIBMONGOC_NAME mongo::mongoc_static)
6+
set(USERVER_MONGO_LIBBSON_NAME mongo::bson_static)
7+
58
if(USERVER_MONGODB_USE_CMAKE_CONFIG)
9+
# Try to find via modern mongo-c-driver 2.x.x way first
10+
find_package(mongoc 2.0.0 QUIET CONFIG)
11+
if(mongoc_FOUND)
12+
message(STATUS "Mongoc: using config version: (bson: ${bson_VERSION}, mongoc: ${mongoc_VERSION})")
13+
14+
# prefer static libs here for compat
15+
# https://github.com/mongodb/mongo-c-driver/blob/2.0.0/src/libmongoc/etc/mongocConfig.cmake.in#L7-L12
16+
# mongoc::mongoc is mongoc::static first then mongoc::shared. Same for bson.
17+
set(USERVER_MONGO_LIBMONGOC_NAME mongoc::mongoc)
18+
set(USERVER_MONGO_LIBBSON_NAME bson::bson)
19+
20+
return()
21+
endif()
22+
23+
# Go back to 1.x.x
624
find_package(mongoc-1.0 QUIET CONFIG)
725
if(mongoc-1.0_FOUND)
826
message(STATUS "Mongoc: using config version: (bson: ${bson-1.0_VERSION}, mongoc: ${mongoc-1.0_VERSION})")

mongo/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ endif()
99
userver_module(
1010
mongo
1111
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
12-
LINK_LIBRARIES mongo::bson_static
13-
LINK_LIBRARIES_PRIVATE mongo::mongoc_static
12+
LINK_LIBRARIES "${USERVER_MONGO_LIBBSON_NAME}"
13+
LINK_LIBRARIES_PRIVATE "${USERVER_MONGO_LIBMONGOC_NAME}"
1414
UTEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*_test.cpp"
1515
DBTEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*_mongotest.cpp"
1616
DBTEST_DATABASES mongo

0 commit comments

Comments
 (0)