Skip to content

Commit cd6f3e2

Browse files
committed
Auto download the C driver if it is not already installed
1 parent ed5a02f commit cd6f3e2

File tree

7 files changed

+393
-311
lines changed

7 files changed

+393
-311
lines changed

.evergreen/compile.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ esac
5252
. .evergreen/find_cmake.sh
5353

5454
cd build
55-
"$CMAKE" -G "$GENERATOR" "-DCMAKE_BUILD_TYPE=${BUILD_TYPE}" -DMONGOCXX_ENABLE_SLOW_TESTS=ON -DENABLE_UNINSTALL=ON "$@" ..
55+
"$CMAKE" -G "$GENERATOR" "-DCMAKE_BUILD_TYPE=${BUILD_TYPE}" -DMONGOCXX_ENABLE_SLOW_TESTS=ON -DENABLE_UNINSTALL=ON -DBUILD_TESTS=ON "$@" ..
5656
"$CMAKE" --build . --config $BUILD_TYPE -- $CMAKE_BUILD_OPTS
5757
"$CMAKE" --build . --config $BUILD_TYPE --target install -- $CMAKE_BUILD_OPTS
5858
"$CMAKE" --build . --config $BUILD_TYPE --target $CMAKE_EXAMPLES_TARGET -- $CMAKE_BUILD_OPTS

CMakeLists.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,61 @@ else()
4040
message(WARNING "Unknown compiler... recklessly proceeding without a version check")
4141
endif()
4242

43+
set(LIBMONGOC_REQUIRED_VERSION 1.22.1) # TODO: update to 1.24.0 once released.
44+
set(LIBMONGOC_REQUIRED_ABI_VERSION 1.0)
45+
46+
set(NEED_DOWNLOAD_C_DRIVER false CACHE INTERNAL "")
47+
if(TARGET mongoc_shared OR TARGET mongoc_static)
48+
# If these targets exist, then libmongoc has already been included as a project
49+
# sub-directory
50+
message ("found libmongoc targets declared in current build scope; version not checked")
51+
52+
if(NOT MONGOCXX_LINK_WITH_STATIC_MONGOC)
53+
set(libmongoc_target mongoc_shared)
54+
else()
55+
set(libmongoc_target mongoc_static)
56+
endif()
57+
58+
if(MONGOCXX_BUILD_STATIC)
59+
set(mongocxx_pkg_dep "find_dependency(mongoc-1.0 REQUIRED)")
60+
endif()
61+
else()
62+
find_package(mongoc-${LIBMONGOC_REQUIRED_ABI_VERSION} ${LIBMONGOC_REQUIRED_VERSION} QUIET)
63+
if(mongoc-${LIBMONGOC_REQUIRED_ABI_VERSION}_FOUND)
64+
message("already found libmongoc, don't need to download it.")
65+
else()
66+
set(NEED_DOWNLOAD_C_DRIVER true CACHE INTERNAL "")
67+
endif()
68+
endif()
69+
70+
if (${NEED_DOWNLOAD_C_DRIVER})
71+
message("No Mongo C Driver path provided via CMAKE_PREFIX_PATH, will download a release from the internet.")
72+
include(FetchContent)
73+
include(ExternalProject)
74+
75+
# Declare mongo-c-driver as a dependency
76+
FetchContent_Declare(
77+
mongo-c-driver
78+
GIT_REPOSITORY https://github.com/kkloberdanz/mongo-c-driver.git
79+
GIT_TAG 1.24.0
80+
)
81+
82+
FetchContent_MakeAvailable(mongo-c-driver)
83+
84+
# Set the installation directory for mongo-c-driver
85+
set(MONGOC_INSTALL_DIR ${CMAKE_BINARY_DIR}/mongo-c-driver-install)
86+
87+
# Build mongo-c-driver using ExternalProject
88+
ExternalProject_Add(mongo-c-driver-build
89+
SOURCE_DIR ${mongo-c-driver_SOURCE_DIR}
90+
BINARY_DIR ${CMAKE_BINARY_DIR}/mongo-c-driver-build
91+
INSTALL_DIR ${MONGOC_INSTALL_DIR}
92+
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${MONGOC_INSTALL_DIR}
93+
)
94+
file(REMOVE ${mongo-c-driver_SOURCE_DIR}/build/generate-uninstall.sh)
95+
file(REMOVE ${mongo-c-driver_SOURCE_DIR}/build/generate-uninstall.cmd)
96+
endif()
97+
4398
# All of our target compilers support the deprecated
4499
# attribute. Normally, we would just let the GenerateExportHeader
45100
# subsystem do this via configure check, but there appears to be a

cmake/BsoncxxUtil.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ function(bsoncxx_add_library TARGET OUTPUT_NAME LINK_TYPE)
6262
EXPORT_FILE_NAME config/export.hpp
6363
STATIC_DEFINE BSONCXX_STATIC
6464
)
65+
66+
if (${NEED_DOWNLOAD_C_DRIVER})
67+
add_dependencies(${TARGET} mongo-c-driver-build)
68+
target_include_directories(${TARGET} PRIVATE ${MONGOC_INSTALL_DIR}/include/libmongoc-1.0/mongoc/)
69+
target_include_directories(${TARGET} PRIVATE ${MONGOC_INSTALL_DIR}/include/libbson-1.0/bson/)
70+
endif()
6571
endfunction(bsoncxx_add_library)
6672

6773
# Install the specified forms of the bsoncxx library (i.e., shared and/or static)

cmake/MongocxxUtil.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ function(mongocxx_add_library TARGET OUTPUT_NAME LINK_TYPE)
4646
EXPORT_FILE_NAME config/export.hpp
4747
STATIC_DEFINE MONGOCXX_STATIC
4848
)
49+
50+
if (${NEED_DOWNLOAD_C_DRIVER})
51+
add_dependencies(${TARGET} mongo-c-driver-build)
52+
target_include_directories(${TARGET} PRIVATE ${MONGOC_INSTALL_DIR}/include/libmongoc-1.0/mongoc/)
53+
target_include_directories(${TARGET} PRIVATE ${MONGOC_INSTALL_DIR}/include/libbson-1.0/bson/)
54+
endif()
4955
endfunction(mongocxx_add_library)
5056

5157
# Install the specified forms of the mongocxx library (i.e., shared and/or static)

src/bsoncxx/test/CMakeLists.txt

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,26 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
include_directories(
16-
${MONGO_CXX_DRIVER_SOURCE_DIR}/src/third_party/catch/include
17-
)
15+
if (BUILD_TESTS STREQUAL "ON")
16+
include_directories(
17+
${MONGO_CXX_DRIVER_SOURCE_DIR}/src/third_party/catch/include
18+
)
1819

19-
file (GLOB src_bsoncxx_test_DIST_cpps RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
20+
file (GLOB src_bsoncxx_test_DIST_cpps RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
2021

21-
add_executable(test_bson
22-
${THIRD_PARTY_SOURCE_DIR}/catch/main.cpp
23-
${src_bsoncxx_test_DIST_cpps}
24-
)
22+
add_executable(test_bson
23+
${THIRD_PARTY_SOURCE_DIR}/catch/main.cpp
24+
${src_bsoncxx_test_DIST_cpps}
25+
)
2526

26-
target_link_libraries(test_bson bsoncxx_testing ${libbson_target})
27-
target_include_directories(test_bson PRIVATE ${libbson_include_directories})
28-
target_compile_definitions(test_bson PRIVATE ${libbson_definitions})
27+
target_link_libraries(test_bson bsoncxx_testing ${libbson_target})
28+
target_include_directories(test_bson PRIVATE ${libbson_include_directories})
29+
target_compile_definitions(test_bson PRIVATE ${libbson_definitions})
2930

30-
add_test(bson test_bson)
31+
add_test(bson test_bson)
3132

32-
set_dist_list (src_bsoncxx_test_DIST
33-
CMakeLists.txt
34-
${src_bsoncxx_test_DIST_cpps}
35-
)
33+
set_dist_list (src_bsoncxx_test_DIST
34+
CMakeLists.txt
35+
${src_bsoncxx_test_DIST_cpps}
36+
)
37+
endif()

src/mongocxx/CMakeLists.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ message ("mongocxx version: ${MONGOCXX_VERSION}")
2929
set(MONGOCXX_INLINE_NAMESPACE "v${MONGOCXX_ABI_VERSION}")
3030
set(MONGOCXX_HEADER_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}/mongocxx/${MONGOCXX_INLINE_NAMESPACE}" CACHE INTERNAL "")
3131

32-
set(LIBMONGOC_REQUIRED_VERSION 1.22.1) # TODO: update to 1.24.0 once released.
33-
set(LIBMONGOC_REQUIRED_ABI_VERSION 1.0)
34-
3532
set(mongocxx_pkg_dep "")
3633

3734
if(TARGET mongoc_shared OR TARGET mongoc_static)
@@ -69,7 +66,10 @@ else()
6966
find_package(libmongoc-${LIBMONGOC_REQUIRED_ABI_VERSION} ${LIBMONGOC_REQUIRED_VERSION} REQUIRED)
7067
message ("found libmongoc version ${MONGOC_VERSION}")
7168
set(libmongoc_target ${MONGOC_LIBRARIES})
72-
set(libmongoc_include_directories ${MONGOC_INCLUDE_DIRS})
69+
set(libmongoc_include_directories ${MONGOC_INCLUDE_DIRS}
70+
${MONGOC_INSTALL_DIR}/include/libmongoc-1.0/mongoc/
71+
${MONGOC_INSTALL_DIR}/include/libbson-1.0/bson/
72+
)
7373
set(libmongoc_definitions ${MONGOC_DEFINITIONS})
7474
if(MONGOCXX_BUILD_STATIC)
7575
set(mongocxx_pkg_dep "find_dependency(libmongoc-1.0 REQUIRED)")
@@ -78,7 +78,10 @@ else()
7878
find_package(libmongoc-static-${LIBMONGOC_REQUIRED_ABI_VERSION} ${LIBMONGOC_REQUIRED_VERSION} REQUIRED)
7979
message ("found libmongoc version ${MONGOC_STATIC_VERSION}")
8080
set(libmongoc_target ${MONGOC_STATIC_LIBRARIES})
81-
set(libmongoc_include_directories ${MONGOC_STATIC_INCLUDE_DIRS})
81+
set(libmongoc_include_directories ${MONGOC_STATIC_INCLUDE_DIRS}
82+
${MONGOC_INSTALL_DIR}/include/libmongoc-1.0/mongoc/
83+
${MONGOC_INSTALL_DIR}/include/libbson-1.0/bson/
84+
)
8285
set(libmongoc_definitions ${MONGOC_STATIC_DEFINITIONS})
8386
if(MONGOCXX_BUILD_STATIC)
8487
set(mongocxx_pkg_dep "find_dependency(libmongoc-static-1.0 REQUIRED)")

0 commit comments

Comments
 (0)