Skip to content

CXX-3266 avoid inheriting BUILD_VERSION by auto-downloaded C Driver #1373

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
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Changes prior to 3.9.0 are documented as [release notes on GitHub](https://githu

## 4.1.0 [Unreleased]

### Fixed

- The API version of auto-downloaded C Driver libraries no longer incorrectly inherits the C++ Driver's `BUILD_VERSION` value.

### Added

- `storage_engine()` in `mongocxx::v_noabi::options::index`.
Expand Down
93 changes: 53 additions & 40 deletions cmake/FetchMongoC.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,56 @@

include(FetchContent)

message(STATUS "Download and configure C driver version ${LIBMONGOC_DOWNLOAD_VERSION} ... begin")

set(fetch_args "")
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.25.0")
set(fetch_args "SYSTEM")
endif()

# Declare mongo-c-driver as a dependency
FetchContent_Declare(
mongo-c-driver
GIT_REPOSITORY https://github.com/mongodb/mongo-c-driver.git
GIT_TAG ${LIBMONGOC_DOWNLOAD_VERSION}

${fetch_args}
)

FetchContent_GetProperties(mongo-c-driver)

if(NOT mongo-c-driver_POPULATED)
set(OLD_ENABLE_TESTS ${ENABLE_TESTS})
set(OLD_BUILD_TESTING ${BUILD_TESTING})
set(OLD_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
set(OLD_CMAKE_C_FLAGS ${CMAKE_C_FLAGS})

# Set ENABLE_TESTS to OFF to disable the test-libmongoc target in the C driver.
# This prevents the LoadTests.cmake script from attempting to execute test-libmongoc.
# test-libmongoc is not built with the "all" target.
# Attempting to execute test-libmongoc results in an error: "Unable to find executable: NOT_FOUND"
set(ENABLE_TESTS OFF)
set(BUILD_TESTING OFF)
string(REPLACE " -Werror" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REPLACE " -Werror" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
FetchContent_MakeAvailable(mongo-c-driver)
set(CMAKE_CXX_FLAGS ${OLD_CMAKE_CXX_FLAGS})
set(CMAKE_C_FLAGS ${OLD_CMAKE_C_FLAGS})
set(ENABLE_TESTS ${OLD_ENABLE_TESTS})
set(BUILD_TESTING ${OLD_BUILD_TESTING})
endif()

message(STATUS "Download and configure C driver version ${LIBMONGOC_DOWNLOAD_VERSION} ... end")
function(fetch_mongoc)
message(STATUS "Download and configure C driver version ${LIBMONGOC_DOWNLOAD_VERSION} ... end")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
message(STATUS "Download and configure C driver version ${LIBMONGOC_DOWNLOAD_VERSION} ... end")
message(STATUS "Download and configure C driver version ${LIBMONGOC_DOWNLOAD_VERSION} ... begin")


set(fetch_args "")
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.25.0")
list(APPEND fetch_args "SYSTEM")
endif()

# Declare mongo-c-driver as a dependency
FetchContent_Declare(
mongo-c-driver
GIT_REPOSITORY https://github.com/mongodb/mongo-c-driver.git
GIT_TAG ${MONGOC_DOWNLOAD_VERSION}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
GIT_TAG ${MONGOC_DOWNLOAD_VERSION}
GIT_TAG ${LIBMONGOC_DOWNLOAD_VERSION}

This may fix failing tasks that seem to be using the auto-downloaded C driver. Example.


${fetch_args}
)

FetchContent_GetProperties(mongo-c-driver)

if(NOT mongo-c-driver_POPULATED)
# Must ensure BUILD_VERSION is not inherited either as a normal variable or as a cache variable.
unset(OLD_BUILD_VERSION)
if(DEFINED BUILD_VERSION)
set(OLD_BUILD_VERSION ${BUILD_VERSION})
unset(BUILD_VERSION)
endif()
unset(OLD_CACHE_BUILD_VERSION)
if(DEFINED CACHE{BUILD_VERSION})
set(OLD_CACHE_BUILD_VERSION $CACHE{BUILD_VERSION})
unset(BUILD_VERSION CACHE)
endif()

# Disable unnecessary targets and potential conflicts with C++ Driver options.
set(ENABLE_TESTS OFF)
set(BUILD_TESTING OFF)
string(REPLACE " -Werror" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REPLACE " -Werror" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")

FetchContent_MakeAvailable(mongo-c-driver)

# Restore prior value of BUILD_VERSION only when they were previously set.
if(DEFINED OLD_BUILD_VERSION)
set(BUILD_VERSION ${OLD_BUILD_VERSION})
endif()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# Restore prior value of BUILD_VERSION only when they were previously set.
if(DEFINED OLD_BUILD_VERSION)
set(BUILD_VERSION ${OLD_BUILD_VERSION})
endif()
# Restore prior value of cached BUILD_VERSION only when previously set.

IIUC restoring the normal variable is not needed, since the unset(BUILD_VERSION) will not impact the parent scope.

if(DEFINED OLD_CACHE_BUILD_VERSION)
set(BUILD_VERSION ${OLD_CACHE_BUILD_VERSION} CACHE STRING "Library version (for both bsoncxx and mongocxx)")
endif()
endif()

message(STATUS "Download and configure C driver version ${LIBMONGOC_DOWNLOAD_VERSION} ... end")
endfunction()

fetch_mongoc()