Skip to content

Commit c0f72cd

Browse files
committed
CXX-3266 avoid inheriting BUILD_VERSION by auto-downloaded C Driver (#1373)
1 parent c02c77d commit c0f72cd

File tree

2 files changed

+53
-42
lines changed

2 files changed

+53
-42
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
Changes prior to 3.9.0 are documented as [release notes on GitHub](https://github.com/mongodb/mongo-cxx-driver/releases).
99

10+
## 3.11.1 [Unreleased]
11+
12+
### Fixed
13+
14+
- The API version of auto-downloaded C Driver libraries no longer incorrectly inherits the C++ Driver's `BUILD_VERSION` value.
15+
1016
## 3.11.0
1117

1218
### Added

cmake/FetchMongoC.cmake

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,50 @@
22

33
include(FetchContent)
44

5-
message(STATUS "Download and configure C driver version ${LIBMONGOC_DOWNLOAD_VERSION} ... begin")
6-
7-
set(fetch_args "")
8-
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.25.0")
9-
set(fetch_args "SYSTEM")
10-
endif()
11-
12-
# Declare mongo-c-driver as a dependency
13-
FetchContent_Declare(
14-
mongo-c-driver
15-
GIT_REPOSITORY https://github.com/mongodb/mongo-c-driver.git
16-
GIT_TAG ${LIBMONGOC_DOWNLOAD_VERSION}
17-
18-
${fetch_args}
19-
)
20-
21-
FetchContent_GetProperties(mongo-c-driver)
22-
23-
if(NOT mongo-c-driver_POPULATED)
24-
set(OLD_ENABLE_TESTS ${ENABLE_TESTS})
25-
set(OLD_BUILD_TESTING ${BUILD_TESTING})
26-
set(OLD_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
27-
set(OLD_CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
28-
29-
set(ENABLE_EXTRA_ALIGNMENT OFF)
30-
31-
# Set ENABLE_TESTS to OFF to disable the test-libmongoc target in the C driver.
32-
# This prevents the LoadTests.cmake script from attempting to execute test-libmongoc.
33-
# test-libmongoc is not built with the "all" target.
34-
# Attempting to execute test-libmongoc results in an error: "Unable to find executable: NOT_FOUND"
35-
set(ENABLE_TESTS OFF)
36-
set(BUILD_TESTING OFF)
37-
string(REPLACE " -Werror" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
38-
string(REPLACE " -Werror" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
39-
FetchContent_MakeAvailable(mongo-c-driver)
40-
set(CMAKE_CXX_FLAGS ${OLD_CMAKE_CXX_FLAGS})
41-
set(CMAKE_C_FLAGS ${OLD_CMAKE_C_FLAGS})
42-
set(ENABLE_TESTS ${OLD_ENABLE_TESTS})
43-
set(BUILD_TESTING ${OLD_BUILD_TESTING})
44-
endif()
45-
46-
message(STATUS "Download and configure C driver version ${LIBMONGOC_DOWNLOAD_VERSION} ... end")
5+
function(fetch_mongoc)
6+
message(STATUS "Download and configure C driver version ${LIBMONGOC_DOWNLOAD_VERSION} ... begin")
7+
8+
set(fetch_args "")
9+
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.25.0")
10+
list(APPEND fetch_args "SYSTEM")
11+
endif()
12+
13+
# Declare mongo-c-driver as a dependency
14+
FetchContent_Declare(
15+
mongo-c-driver
16+
GIT_REPOSITORY https://github.com/mongodb/mongo-c-driver.git
17+
GIT_TAG ${LIBMONGOC_DOWNLOAD_VERSION}
18+
19+
${fetch_args}
20+
)
21+
22+
FetchContent_GetProperties(mongo-c-driver)
23+
24+
if(NOT mongo-c-driver_POPULATED)
25+
# Must ensure BUILD_VERSION is not inherited either as a normal variable or as a cache variable.
26+
unset(BUILD_VERSION)
27+
unset(OLD_CACHE_BUILD_VERSION)
28+
if(DEFINED CACHE{BUILD_VERSION})
29+
set(OLD_CACHE_BUILD_VERSION $CACHE{BUILD_VERSION})
30+
unset(BUILD_VERSION CACHE)
31+
endif()
32+
33+
# Disable unnecessary targets and potential conflicts with C++ Driver options.
34+
set(ENABLE_TESTS OFF)
35+
set(BUILD_TESTING OFF)
36+
set(ENABLE_EXTRA_ALIGNMENT OFF)
37+
string(REPLACE " -Werror" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
38+
string(REPLACE " -Werror" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
39+
40+
FetchContent_MakeAvailable(mongo-c-driver)
41+
42+
# Restore prior value of BUILD_VERSION cache variable only if was previously set.
43+
if(DEFINED OLD_CACHE_BUILD_VERSION)
44+
set(BUILD_VERSION ${OLD_CACHE_BUILD_VERSION} CACHE STRING "Library version (for both bsoncxx and mongocxx)")
45+
endif()
46+
endif()
47+
48+
message(STATUS "Download and configure C driver version ${LIBMONGOC_DOWNLOAD_VERSION} ... end")
49+
endfunction()
50+
51+
fetch_mongoc()

0 commit comments

Comments
 (0)