diff --git a/.evergreen/config_generator/components/cmake_compat.py b/.evergreen/config_generator/components/cmake_compat.py new file mode 100644 index 0000000000..88bc7dba4b --- /dev/null +++ b/.evergreen/config_generator/components/cmake_compat.py @@ -0,0 +1,100 @@ +from config_generator.components.funcs.fetch_c_driver_source import FetchCDriverSource +from config_generator.components.funcs.install_c_driver import InstallCDriver +from config_generator.components.funcs.install_uv import InstallUV +from config_generator.components.funcs.setup import Setup + +from config_generator.etc.distros import find_small_distro +from config_generator.etc.function import Function +from config_generator.etc.utils import bash_exec + +from shrub.v3.evg_build_variant import BuildVariant +from shrub.v3.evg_command import EvgCommandType +from shrub.v3.evg_task import EvgTask, EvgTaskRef + +TAG = 'cmake-compat' + +# pylint: disable=line-too-long +# fmt: off +MATRIX = [ + ("min", [3, 15, 4]), + ("max-v3", [3, 31, 7]), + ("max", [4, 0, 1]), +] +# fmt: on + + +class CMakeCompat(Function): + name = TAG + commands = [ + bash_exec( + command_type=EvgCommandType.TEST, + working_dir='mongo-cxx-driver', + include_expansions_in_env=[ + 'CMAKE_MAJOR_VERSION', + 'CMAKE_MINOR_VERSION', + 'CMAKE_PATCH_VERSION', + 'INSTALL_C_DRIVER', + ], + script='.evergreen/scripts/cmake-compat.sh', + ), + bash_exec( + command_type=EvgCommandType.TEST, + include_expansions_in_env=[ + 'CMAKE_MAJOR_VERSION', + 'CMAKE_MINOR_VERSION', + 'CMAKE_PATCH_VERSION', + 'INSTALL_C_DRIVER', + ], + script='mongo-cxx-driver/.evergreen/scripts/cmake-compat-check.sh', + ), + ] + + +def functions(): + return CMakeCompat.defn() + + +def tasks(): + distro_name = 'rhel80' + distro = find_small_distro(distro_name) + + # Test importing C Driver libraries via both add_subdirectory() and find_package(). + install_c_driver_modes = [False, True] + + for name, version in MATRIX: + for install_c_driver in install_c_driver_modes: + commands = [ + Setup.call(), + InstallUV.call(), + (InstallCDriver.call() if install_c_driver else FetchCDriverSource.call()), + CMakeCompat.call( + vars={ + 'CMAKE_MAJOR_VERSION': version[0], + 'CMAKE_MINOR_VERSION': version[1], + 'CMAKE_PATCH_VERSION': version[2], + 'INSTALL_C_DRIVER': int(install_c_driver), + }, + ), + ] + + if install_c_driver: + c_mode = 'find-c' + else: + c_mode = 'add-c' + + yield EvgTask( + name=f'{TAG}-{name}-{c_mode}', + tags=[TAG, f'cmake-{name}', f'cmake-{c_mode}'], + run_on=distro.name, + commands=commands, + ) + + +def variants(): + return [ + BuildVariant( + name=f'{TAG}-matrix', + display_name='CMake Compatibility Matrix', + tasks=[EvgTaskRef(name=f'.{TAG}')], + ), + ] diff --git a/.evergreen/config_generator/components/funcs/install_c_driver.py b/.evergreen/config_generator/components/funcs/install_c_driver.py index 14004b42c3..e0323c2549 100644 --- a/.evergreen/config_generator/components/funcs/install_c_driver.py +++ b/.evergreen/config_generator/components/funcs/install_c_driver.py @@ -8,13 +8,13 @@ # If updating mongoc_version_minimum, also update: -# - LIBBSON_REQUIRED_VERSION and LIBMONGOC_REQUIRED_VERSION in CMakeLists.txt +# - BSON_REQUIRED_VERSION and MONGOC_REQUIRED_VERSION in CMakeLists.txt # - the version of pkg:github/mongodb/mongo-c-driver in etc/purls.txt # - the default value of --c-driver-build-ref in etc/make_release.py -# Only LIBMONGOC_DOWNLOAD_VERSION needs to be updated when pinning to an unreleased commit. +# Only MONGOC_DOWNLOAD_VERSION needs to be updated when pinning to an unreleased commit. # If pinning to an unreleased commit, create a "Blocked" JIRA ticket with # a "depends on" link to the appropriate C Driver version release ticket. -MONGOC_VERSION_MINIMUM = 'f1e2b54090ea28b169b7d9949bd318615188d81d' # CXX-3103: bump to 2.0.0 once released. +MONGOC_VERSION_MINIMUM = '2.0.0' class InstallCDriver(Function): diff --git a/.evergreen/generated_configs/functions.yml b/.evergreen/generated_configs/functions.yml index c23ad4baea..c7c3cc7b51 100644 --- a/.evergreen/generated_configs/functions.yml +++ b/.evergreen/generated_configs/functions.yml @@ -253,6 +253,32 @@ functions: args: - -c - etc/run-clang-tidy.sh + cmake-compat: + - command: subprocess.exec + type: test + params: + binary: bash + working_dir: mongo-cxx-driver + include_expansions_in_env: + - CMAKE_MAJOR_VERSION + - CMAKE_MINOR_VERSION + - CMAKE_PATCH_VERSION + - INSTALL_C_DRIVER + args: + - -c + - .evergreen/scripts/cmake-compat.sh + - command: subprocess.exec + type: test + params: + binary: bash + include_expansions_in_env: + - CMAKE_MAJOR_VERSION + - CMAKE_MINOR_VERSION + - CMAKE_PATCH_VERSION + - INSTALL_C_DRIVER + args: + - -c + - mongo-cxx-driver/.evergreen/scripts/cmake-compat-check.sh compile: command: subprocess.exec type: test @@ -461,7 +487,7 @@ functions: type: setup params: updates: - - { key: mongoc_version_minimum, value: f1e2b54090ea28b169b7d9949bd318615188d81d } + - { key: mongoc_version_minimum, value: 2.0.0 } - command: subprocess.exec type: setup params: diff --git a/.evergreen/generated_configs/tasks.yml b/.evergreen/generated_configs/tasks.yml index 3230c99b0a..185be5a37b 100644 --- a/.evergreen/generated_configs/tasks.yml +++ b/.evergreen/generated_configs/tasks.yml @@ -166,6 +166,84 @@ tasks: vars: cc_compiler: clang cxx_compiler: clang++ + - name: cmake-compat-max-add-c + run_on: rhel80-small + tags: [cmake-compat, cmake-max, cmake-add-c] + commands: + - func: setup + - func: install-uv + - func: fetch_c_driver_source + - func: cmake-compat + vars: + CMAKE_MAJOR_VERSION: 4 + CMAKE_MINOR_VERSION: 0 + CMAKE_PATCH_VERSION: 1 + INSTALL_C_DRIVER: 0 + - name: cmake-compat-max-find-c + run_on: rhel80-small + tags: [cmake-compat, cmake-max, cmake-find-c] + commands: + - func: setup + - func: install-uv + - func: install_c_driver + - func: cmake-compat + vars: + CMAKE_MAJOR_VERSION: 4 + CMAKE_MINOR_VERSION: 0 + CMAKE_PATCH_VERSION: 1 + INSTALL_C_DRIVER: 1 + - name: cmake-compat-max-v3-add-c + run_on: rhel80-small + tags: [cmake-compat, cmake-max-v3, cmake-add-c] + commands: + - func: setup + - func: install-uv + - func: fetch_c_driver_source + - func: cmake-compat + vars: + CMAKE_MAJOR_VERSION: 3 + CMAKE_MINOR_VERSION: 31 + CMAKE_PATCH_VERSION: 7 + INSTALL_C_DRIVER: 0 + - name: cmake-compat-max-v3-find-c + run_on: rhel80-small + tags: [cmake-compat, cmake-max-v3, cmake-find-c] + commands: + - func: setup + - func: install-uv + - func: install_c_driver + - func: cmake-compat + vars: + CMAKE_MAJOR_VERSION: 3 + CMAKE_MINOR_VERSION: 31 + CMAKE_PATCH_VERSION: 7 + INSTALL_C_DRIVER: 1 + - name: cmake-compat-min-add-c + run_on: rhel80-small + tags: [cmake-compat, cmake-min, cmake-add-c] + commands: + - func: setup + - func: install-uv + - func: fetch_c_driver_source + - func: cmake-compat + vars: + CMAKE_MAJOR_VERSION: 3 + CMAKE_MINOR_VERSION: 15 + CMAKE_PATCH_VERSION: 4 + INSTALL_C_DRIVER: 0 + - name: cmake-compat-min-find-c + run_on: rhel80-small + tags: [cmake-compat, cmake-min, cmake-find-c] + commands: + - func: setup + - func: install-uv + - func: install_c_driver + - func: cmake-compat + vars: + CMAKE_MAJOR_VERSION: 3 + CMAKE_MINOR_VERSION: 15 + CMAKE_PATCH_VERSION: 4 + INSTALL_C_DRIVER: 1 - name: compile-only-macos-14-arm64-cxx11-debug-shared run_on: macos-14-arm64 tags: [compile-only, macos-14-arm64, cxx11, debug, shared] diff --git a/.evergreen/generated_configs/variants.yml b/.evergreen/generated_configs/variants.yml index 904f2f9880..2a89a295a4 100644 --- a/.evergreen/generated_configs/variants.yml +++ b/.evergreen/generated_configs/variants.yml @@ -20,6 +20,10 @@ buildvariants: display_name: Clang Tidy tasks: - name: .clang-tidy + - name: cmake-compat-matrix + display_name: CMake Compatibility Matrix + tasks: + - name: .cmake-compat - name: compile-only-matrix display_name: compile-only-matrix tasks: diff --git a/.evergreen/scripts/cmake-compat-check.sh b/.evergreen/scripts/cmake-compat-check.sh new file mode 100755 index 0000000000..616b9116e2 --- /dev/null +++ b/.evergreen/scripts/cmake-compat-check.sh @@ -0,0 +1,121 @@ +#!/usr/bin/env bash + +set -o errexit +set -o pipefail + +: "${CMAKE_MAJOR_VERSION:?}" +: "${CMAKE_MINOR_VERSION:?}" +: "${CMAKE_PATCH_VERSION:?}" +: "${INSTALL_C_DRIVER:?}" + +[[ -d mongoc ]] || { + echo "missing mongoc directory" + exit 1 +} >&2 + +[[ -d mongo-cxx-driver/install ]] || { + echo "missing mongo-cxx-driver install directory" + exit 1 +} >&2 + +mongoc_prefix="$(pwd)/mongoc" +mongocxx_prefix="$(pwd)/mongo-cxx-driver/install" +if [[ "${OSTYPE:?}" =~ cygwin ]]; then + mongoc_prefix="$(cygpath -m "${mongoc_prefix:?}")" + mongocxx_prefix="$(cygpath -m "${mongocxx_prefix:?}")" +fi + +# shellcheck source=/dev/null +. "${mongoc_prefix:?}/.evergreen/scripts/find-cmake-version.sh" +export cmake_binary +cmake_binary="$(find_cmake_version "${CMAKE_MAJOR_VERSION:?}" "${CMAKE_MINOR_VERSION:?}" "${CMAKE_PATCH_VERSION:?}")" +"${cmake_binary:?}" --version + +CMAKE_BUILD_PARALLEL_LEVEL="$(nproc)" +export CMAKE_BUILD_PARALLEL_LEVEL + +# Use ccache if available. +if [[ -f "${mongoc_prefix:?}/.evergreen/scripts/find-ccache.sh" ]]; then + # shellcheck source=/dev/null + . "${mongoc_prefix:?}/.evergreen/scripts/find-ccache.sh" + find_ccache_and_export_vars "$(pwd)" || true +fi + +cmake_flags=( + "-Werror=dev" + "-Werror=deprecated" + "-DCMAKE_BUILD_TYPE=Debug" + "-DCMAKE_FIND_NO_INSTALL_PREFIX=ON" +) + +cat >main.cpp < +#include + +#include + +#include + +int main() { + mongocxx::instance instance; + + std::cout << "bsoncxx: " << BSONCXX_VERSION_STRING << std::endl; + std::cout << "mongocxx: " << MONGOCXX_VERSION_STRING << std::endl; +} +DOC + +# Support C Driver libraries obtained via both add_subdirectory() and find_package(). +if [[ "${INSTALL_C_DRIVER:?}" == 1 ]]; then + # Different install prefixes. + cmake_flags+=("-DCMAKE_PREFIX_PATH=${mongocxx_prefix:?};${mongoc_prefix:?}") +else + # Same install prefix. + cmake_flags+=("-DCMAKE_PREFIX_PATH=${mongocxx_prefix:?}") +fi + +echo "Configuring with CMake flags:" +printf " - %s\n" "${cmake_flags[@]:?}" + +# Test importing C++ Driver libraries using find_package(). +echo "Importing C++ Driver via find_package()..." +{ + cat >|CMakeLists.txt <output.txt || { + cat output.txt >&2 + exit 1 +} +echo "Importing C++ Driver via find_package()... done." + +echo "Importing C++ Driver via add_subdirectory()..." +{ + cat >|CMakeLists.txt <output.txt || { + cat output.txt >&2 + exit 1 +} +echo "Importing C++ Driver via add_subdirectory()... done." diff --git a/.evergreen/scripts/cmake-compat.sh b/.evergreen/scripts/cmake-compat.sh new file mode 100755 index 0000000000..99e96f8b2a --- /dev/null +++ b/.evergreen/scripts/cmake-compat.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +set -o errexit +set -o pipefail + +: "${CMAKE_MAJOR_VERSION:?}" +: "${CMAKE_MINOR_VERSION:?}" +: "${CMAKE_PATCH_VERSION:?}" +: "${INSTALL_C_DRIVER:?}" + +[[ -d ../mongoc ]] || { + echo "missing mongoc directory" + exit 1 +} >&2 + +mongoc_prefix="$(cd ../mongoc && pwd)" +if [[ "${OSTYPE:?}" =~ cygwin ]]; then + mongoc_prefix="$(cygpath -m "${mongoc_prefix:?}")" +fi + +# shellcheck source=/dev/null +. "${mongoc_prefix:?}/.evergreen/scripts/find-cmake-version.sh" +export cmake_binary +cmake_binary="$(find_cmake_version "${CMAKE_MAJOR_VERSION:?}" "${CMAKE_MINOR_VERSION:?}" "${CMAKE_PATCH_VERSION:?}")" +"${cmake_binary:?}" --version + +CMAKE_BUILD_PARALLEL_LEVEL="$(nproc)" +export CMAKE_BUILD_PARALLEL_LEVEL + +# Use ccache if available. +if [[ -f "${mongoc_prefix:?}/.evergreen/scripts/find-ccache.sh" ]]; then + # shellcheck source=/dev/null + . "${mongoc_prefix:?}/.evergreen/scripts/find-ccache.sh" + find_ccache_and_export_vars "$(pwd)" || true +fi + +cmake_flags=( + "-Werror=dev" + "-Werror=deprecated" + "-DCMAKE_BUILD_TYPE=Debug" + "-DCMAKE_INSTALL_PREFIX=install" + "-DCMAKE_FIND_NO_INSTALL_PREFIX=ON" +) + +if [[ "${INSTALL_C_DRIVER:?}" == 1 ]]; then + # Use find_package(). + cmake_flags+=( + "-DCMAKE_PREFIX_PATH=${mongoc_prefix:?}" + ) +else + # Use add_subdirectory(). + cmake_flags+=( + "-DCMAKE_DISABLE_FIND_PACKAGE_bson=ON" + "-DCMAKE_DISABLE_FIND_PACKAGE_mongoc=ON" + ) +fi + +echo "Configuring with CMake flags:" +printf " - %s\n" "${cmake_flags[@]:?}" + +"${cmake_binary:?}" -S . -B build "${cmake_flags[@]:?}" +"${cmake_binary:?}" --build build --target install + +# Use generated header bson-config.h to detect installation of C Driver libraries. +bson_config_h="$(find install -name 'bson-config.h')" +if [[ "${INSTALL_C_DRIVER:?}" == 1 ]] && [[ -n "${bson_config_h:-}" ]]; then + echo "bson-config.h SHOULD NOT be present in $(pwd)/install" >&2 + exit 1 +fi +if [[ "${INSTALL_C_DRIVER:?}" != 1 ]] && [[ -z "${bson_config_h:-}" ]]; then + echo "bson-config.h SHOULD be present in $(pwd)/install" >&2 + exit 1 +fi diff --git a/.evergreen/scripts/compile.sh b/.evergreen/scripts/compile.sh index 46903ec487..4d725457aa 100755 --- a/.evergreen/scripts/compile.sh +++ b/.evergreen/scripts/compile.sh @@ -91,7 +91,7 @@ cmake_flags=( -DENABLE_UNINSTALL=ON ) -# System-installed libmongoc must not prevent fetch-and-build of libmongoc. +# System-installed mongoc must not prevent fetch-and-build of mongoc. if [[ -z "$(find "${mongoc_prefix:?}" -name 'bson-config.h')" ]]; then cmake_flags+=("-DCMAKE_DISABLE_FIND_PACKAGE_mongoc-1.0=ON") fi @@ -138,7 +138,6 @@ cygwin) # Replace `/Zi`, which is incompatible with ccache, with `/Z7` while preserving other default debug flags. cmake_flags+=( - "-DCMAKE_POLICY_DEFAULT_CMP0141=NEW" "-DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded" ) ;; @@ -260,6 +259,6 @@ if [[ -n "$(find "${mongoc_prefix:?}" -name 'bson-config.h')" ]]; then elif [[ -n "$(find install -name 'bson-config.h')" ]]; then : # Used auto-downloaded C Driver. else - echo "unexpectedly compiled using a system libmongoc library" 1>&2 + echo "unexpectedly compiled using a system mongoc library" 1>&2 exit 1 fi diff --git a/.evergreen/scripts/install-c-driver.sh b/.evergreen/scripts/install-c-driver.sh index 0adf2c6bf5..47ce28f4e6 100755 --- a/.evergreen/scripts/install-c-driver.sh +++ b/.evergreen/scripts/install-c-driver.sh @@ -29,7 +29,7 @@ fi : "${mongoc_idir:?}" : "${mongoc_install_idir:?}" -echo "libmongoc version: ${mongoc_version}" +echo "mongoc version: ${mongoc_version}" # Download tarball from GitHub and extract into ${mongoc_dir}. rm -rf "${mongoc_dir}" @@ -37,42 +37,6 @@ mkdir "${mongoc_dir}" curl -sS -o mongo-c-driver.tar.gz -L "https://api.github.com/repos/mongodb/mongo-c-driver/tarball/${mongoc_version}" tar xzf mongo-c-driver.tar.gz --directory "${mongoc_dir}" --strip-components=1 -# C Driver needs VERSION_CURRENT to compute BUILD_VERSION. -if [[ -f "${mongoc_dir}/VERSION_CURRENT" ]]; then - : # Use the existing VERSION_CURRENT bundled with the release tarball. - - # CXX-3103: overwrite incompatible build versions to support the upcoming 2.0.0 release. - echo "1.31.0-pre" >|"${mongoc_dir}/VERSION_CURRENT" -else - # RegEx pattern to match SemVer strings. See https://semver.org/. - declare -r semver_regex="^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$" - if echo "${mongoc_version}" | perl -ne "$(printf 'exit 1 unless /%s/' "${semver_regex}")"; then - # If $mongoc_version is already SemVer compliant, use as-is. - echo "${mongoc_version}" >|"${mongoc_dir}/VERSION_CURRENT" - else - # Otherwise, use the tag name of the latest release to construct a prerelease version string. - - # Extract "tag_name" from latest Github release. - declare build_version - build_version="$(curl -sS -H "Accept: application/vnd.github+json" https://api.github.com/repos/mongodb/mongo-c-driver/releases/latest | perl -ne 'print for /"tag_name": "(.+)"/')" - - # Assert the tag name is a SemVer string via errexit. - echo "${build_version}" | perl -ne "exit 1 unless /${semver_regex}/" || { - echo "could not obtain a build version from the tag name of the latest release" 1>&2 - exit 1 - } - - # Bump to the next minor version, e.g. 1.0.1 -> 1.1.0. - build_version="$(echo "${build_version}" | perl -ne "$(printf '/%s/; print $+{major} . "." . ($+{minor}+1) . ".0"' "${semver_regex}")")" - - # Append a prerelease tag, e.g. 1.1.0-pre+. - build_version="$(printf "%s-pre+%s" "${build_version}" "${mongoc_version}")" - - # Use the constructed prerelease build version when building the C driver. - echo "${build_version}" >|"${mongoc_dir}/VERSION_CURRENT" - fi -fi - # shellcheck source=/dev/null . "${mongoc_dir}/.evergreen/scripts/find-cmake-latest.sh" declare cmake_binary @@ -110,7 +74,6 @@ declare -a configure_flags=( "-DCMAKE_BUILD_TYPE=Debug" "-DCMAKE_INSTALL_PREFIX=${mongoc_install_idir}" "-DCMAKE_PREFIX_PATH=${mongoc_idir}" - "-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF" "-DENABLE_EXAMPLES=OFF" "-DENABLE_SHM_COUNTERS=OFF" "-DENABLE_STATIC=ON" @@ -128,7 +91,6 @@ cygwin) # Replace `/Zi`, which is incompatible with ccache, with `/Z7` while preserving other default debug flags. cmake_flags+=( - "-DCMAKE_POLICY_DEFAULT_CMP0141=NEW" "-DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded" ) ;; @@ -150,7 +112,7 @@ if [[ -f "${mongoc_dir:?}/.evergreen/scripts/find-ccache.sh" ]]; then find_ccache_and_export_vars "$(pwd)" || true fi -# Install libmongoc. +# Install C Driver libraries. { echo "Installing C Driver into ${mongoc_dir}..." 1>&2 "${cmake_binary}" -S "${mongoc_idir}" -B "${mongoc_idir}" "${configure_flags[@]}" diff --git a/CHANGELOG.md b/CHANGELOG.md index a812e8258e..9a56d51b31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,9 @@ Changes prior to 3.9.0 are documented as [release notes on GitHub](https://githu ### Changed -- Bump the minimum required C Driver version to [1.30.0](https://github.com/mongodb/mongo-c-driver/releases/tag/1.30.0). +- Bump the minimum required C Driver version to [2.0.0](https://github.com/mongodb/mongo-c-driver/releases/tag/2.0.0). +- Set CMake maximum policy version to 4.0. + - The CMake minimum required version is still 3.15. ## 4.0.0 diff --git a/CMakeLists.txt b/CMakeLists.txt index 0267c2684e..68f6cd58bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,11 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -cmake_minimum_required(VERSION 3.15) - -if(POLICY CMP0025) - cmake_policy(SET CMP0025 NEW) -endif() +cmake_minimum_required(VERSION 3.15...4.0) project(MONGO_CXX_DRIVER LANGUAGES CXX) @@ -49,34 +45,11 @@ else() endif() # Also update etc/purls.txt. -set(LIBBSON_REQUIRED_VERSION 1.30.0) -set(LIBBSON_REQUIRED_ABI_VERSION 1.0) - -# Also update etc/purls.txt. -set(LIBMONGOC_REQUIRED_VERSION 1.30.0) -set(LIBMONGOC_DOWNLOAD_VERSION f1e2b54090ea28b169b7d9949bd318615188d81d) # CXX-3103: bump to 2.0.0 once released. -set(LIBMONGOC_REQUIRED_ABI_VERSION 1.0) - -set(NEED_DOWNLOAD_C_DRIVER false) - -if(TARGET mongoc_shared OR TARGET mongoc_static) - # If these targets exist, then libmongoc has already been included as a project - # sub-directory - message(STATUS "Found libmongoc targets declared in current build scope; version not checked") -else() - find_package(mongoc-${LIBMONGOC_REQUIRED_ABI_VERSION} ${LIBMONGOC_REQUIRED_VERSION} QUIET) - - if(mongoc-${LIBMONGOC_REQUIRED_ABI_VERSION}_FOUND) - message(STATUS "Found libmongoc ${mongoc-${LIBMONGOC_REQUIRED_ABI_VERSION}_VERSION}, don't need to download it.") - else() - set(NEED_DOWNLOAD_C_DRIVER true CACHE INTERNAL "") - endif() -endif() +set(BSON_REQUIRED_VERSION 2.0.0) +set(MONGOC_REQUIRED_VERSION 2.0.0) +set(MONGOC_DOWNLOAD_VERSION 2.0.0) -if(NEED_DOWNLOAD_C_DRIVER) - message(STATUS "No MongoDB C Driver path provided via CMAKE_PREFIX_PATH, will download C driver version ${LIBMONGOC_DOWNLOAD_VERSION} from the internet.") - include(FetchMongoC) -endif() +include(FetchMongoC) # All of our target compilers support the deprecated # attribute. Normally, we would just let the GenerateExportHeader @@ -113,7 +86,7 @@ include(InstallRequiredSystemLibraries) # Allow the user to decide whether to build the shared libaries or the static libraries. option(BUILD_SHARED_LIBS "Build shared libraries" ON) -set(BUILD_VERSION "0.0.0" CACHE STRING "Library version (for both libbsoncxx and libmongocxx)") +set(BUILD_VERSION "0.0.0" CACHE STRING "Library version (for both bsoncxx and mongocxx)") # Allow the user to decide whether to also build static libraries option(BUILD_SHARED_AND_STATIC_LIBS "Build static libraries" OFF) @@ -204,11 +177,11 @@ if(BUILD_VERSION STREQUAL "0.0.0") set(MONGOCXX_CURRENT_VERSION_FILE build/VERSION_CURRENT) file(STRINGS ${MONGOCXX_CURRENT_VERSION_FILE} BUILD_VERSION) else() - find_package(PythonInterp) + find_package(Python3 COMPONENTS Interpreter) - if(PYTHONINTERP_FOUND) + if(Python3_Interpreter_FOUND) execute_process( - COMMAND ${PYTHON_EXECUTABLE} etc/calc_release_version.py + COMMAND ${Python3_EXECUTABLE} etc/calc_release_version.py WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} OUTPUT_VARIABLE CALC_RELEASE_VERSION RESULT_VARIABLE CALC_RELEASE_VERSION_RESULT diff --git a/cmake/BsoncxxUtil.cmake b/cmake/BsoncxxUtil.cmake index bfea7fcb1c..e5b1fcf526 100644 --- a/cmake/BsoncxxUtil.cmake +++ b/cmake/BsoncxxUtil.cmake @@ -2,10 +2,15 @@ # # This function requires the following variables to be defined in its parent scope: # - bsoncxx_sources -# - libbson_target -# - libbson_definitions -# - libbson_include_directories +# - bson_target function(bsoncxx_add_library TARGET OUTPUT_NAME LINK_TYPE) + if(NOT DEFINED bsoncxx_sources) + message(FATAL_ERROR "expected bsoncxx_sources to be defined") + endif() + if(NOT DEFINED bson_target) + message(FATAL_ERROR "expected bson_target to be defined") + endif() + add_library(${TARGET} ${LINK_TYPE} ${bsoncxx_sources} ) @@ -37,7 +42,7 @@ function(bsoncxx_add_library TARGET OUTPUT_NAME LINK_TYPE) # Compatibility is handled via CMake's IMPORTED_CONFIGURATIONS rather than interface properties. string(APPEND abi_tag "-$,d,$,$,$>,r,u>>") - # Link type with libmongoc. + # Link type with bson and mongoc libraries. # - 'h' for shared. # - 't' for static. if(1) @@ -113,7 +118,7 @@ function(bsoncxx_add_library TARGET OUTPUT_NAME LINK_TYPE) endif() set_target_properties(${TARGET} PROPERTIES - VERSION ${BSONCXX_VERSION} + VERSION $CACHE{BSONCXX_VERSION} DEFINE_SYMBOL BSONCXX_EXPORT ) @@ -138,8 +143,7 @@ function(bsoncxx_add_library TARGET OUTPUT_NAME LINK_TYPE) target_compile_definitions(${TARGET} PUBLIC BSONCXX_STATIC) endif() - target_link_libraries(${TARGET} PRIVATE ${libbson_target}) - target_include_directories(${TARGET} PRIVATE ${libbson_include_directories}) + target_link_libraries(${TARGET} PRIVATE ${bson_target}) target_include_directories( ${TARGET} PUBLIC @@ -149,5 +153,4 @@ function(bsoncxx_add_library TARGET OUTPUT_NAME LINK_TYPE) $ $ ) - target_compile_definitions(${TARGET} PRIVATE ${libbson_definitions}) endfunction(bsoncxx_add_library) diff --git a/cmake/FetchCatch2.cmake b/cmake/FetchCatch2.cmake index 2b02b3d23e..b8e712eed9 100644 --- a/cmake/FetchCatch2.cmake +++ b/cmake/FetchCatch2.cmake @@ -5,7 +5,7 @@ include(FetchContent) function(fetch_catch2) set(fetch_args "") if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.25.0") - set(fetch_args "SYSTEM") + list(APPEND fetch_args "SYSTEM") endif() FetchContent_Declare( @@ -17,8 +17,6 @@ function(fetch_catch2) LOG_DOWNLOAD ON ${fetch_args} - - FETCHCONTENT_UPDATES_DISCONNECTED ON ) FetchContent_GetProperties(EP_Catch2) diff --git a/cmake/FetchMongoC.cmake b/cmake/FetchMongoC.cmake index be0af4b8f6..b421213917 100644 --- a/cmake/FetchMongoC.cmake +++ b/cmake/FetchMongoC.cmake @@ -1,9 +1,9 @@ -# Use FetchContent to obtain libbson and libmongoc. +# Use FetchContent to obtain bson and mongoc. include(FetchContent) function(fetch_mongoc) - message(STATUS "Download and configure C driver version ${LIBMONGOC_DOWNLOAD_VERSION} ... begin") + message(STATUS "Downloading and configuring MongoDB C Driver ${MONGOC_DOWNLOAD_VERSION}...") set(fetch_args "") if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.25.0") @@ -14,7 +14,7 @@ function(fetch_mongoc) FetchContent_Declare( mongo-c-driver GIT_REPOSITORY https://github.com/mongodb/mongo-c-driver.git - GIT_TAG ${LIBMONGOC_DOWNLOAD_VERSION} + GIT_TAG ${MONGOC_DOWNLOAD_VERSION} ${fetch_args} ) @@ -44,7 +44,31 @@ function(fetch_mongoc) endif() endif() - message(STATUS "Download and configure C driver version ${LIBMONGOC_DOWNLOAD_VERSION} ... end") + message(STATUS "Downloading and configuring MongoDB C Driver ${MONGOC_DOWNLOAD_VERSION}... done.") endfunction() -fetch_mongoc() +set(NEED_DOWNLOAD_C_DRIVER false CACHE INTERNAL "") + +# Only search for packages if targets are not already imported via add_subdirectory(). +if(NOT ((TARGET bson_shared OR TARGET bson_static) AND (TARGET mongoc_shared OR TARGET mongoc_static))) + # Both libraries are required. + find_package(bson ${BSON_REQUIRED_VERSION} QUIET) + if(NOT bson_FOUND) + message(STATUS "find_package(bson ${BSON_REQUIRED_VERSION}) did not succeed") + endif() + + find_package(mongoc ${MONGOC_REQUIRED_VERSION} QUIET) + if(NOT mongoc_FOUND) + message(STATUS "find_package(mongoc ${MONGOC_REQUIRED_VERSION}) did not succeed") + endif() + + if(NOT bson_FOUND OR NOT mongoc_FOUND) + set_property(CACHE NEED_DOWNLOAD_C_DRIVER PROPERTY VALUE true) + message(STATUS "Required MongoDB C Driver libraries not found via find_package()") + endif() +endif() + +if($CACHE{NEED_DOWNLOAD_C_DRIVER}) + message(STATUS "MongoDB C Driver library sources will be downloaded from GitHub") + fetch_mongoc() +endif() diff --git a/cmake/MongocxxUtil.cmake b/cmake/MongocxxUtil.cmake index 932d9a2202..e3dd8b5f7d 100644 --- a/cmake/MongocxxUtil.cmake +++ b/cmake/MongocxxUtil.cmake @@ -2,9 +2,7 @@ # # This function requires the following variables to be defined in its parent scope: # - mongocxx_sources -# - libmongoc_target -# - libmongoc_definitions -# - libmongoc_definitions +# - mongoc_target function(mongocxx_add_library TARGET OUTPUT_NAME LINK_TYPE) add_library(${TARGET} ${LINK_TYPE} ${mongocxx_sources} @@ -38,7 +36,7 @@ function(mongocxx_add_library TARGET OUTPUT_NAME LINK_TYPE) # Compatibility is handled via CMake's IMPORTED_CONFIGURATIONS rather than interface properties. string(APPEND abi_tag "-$,d,$,$,$>,r,u>>") - # Link type with libmongoc. Inherit from bsoncxx. + # Link type with mongoc. Inherit from bsoncxx. if(1) get_target_property(mongoc_link_type ${bsoncxx_target} INTERFACE_BSONCXX_ABI_TAG_MONGOC_LINK_TYPE) @@ -75,7 +73,7 @@ function(mongocxx_add_library TARGET OUTPUT_NAME LINK_TYPE) endif() set_target_properties(${TARGET} PROPERTIES - VERSION ${MONGOCXX_VERSION} + VERSION $CACHE{MONGOCXX_VERSION} DEFINE_SYMBOL MONGOCXX_EXPORTS ) @@ -100,8 +98,7 @@ function(mongocxx_add_library TARGET OUTPUT_NAME LINK_TYPE) target_compile_definitions(${TARGET} PUBLIC MONGOCXX_STATIC) endif() - target_link_libraries(${TARGET} PRIVATE ${libmongoc_target}) - target_include_directories(${TARGET} PRIVATE ${libmongoc_include_directories}) + target_link_libraries(${TARGET} PRIVATE ${mongoc_target}) target_include_directories( ${TARGET} PUBLIC @@ -111,5 +108,4 @@ function(mongocxx_add_library TARGET OUTPUT_NAME LINK_TYPE) $ $ ) - target_compile_definitions(${TARGET} PRIVATE ${libmongoc_definitions}) endfunction(mongocxx_add_library) diff --git a/cmake/make_dist/MakeDist.cmake b/cmake/make_dist/MakeDist.cmake index 68275d1262..ca45326a5a 100644 --- a/cmake/make_dist/MakeDist.cmake +++ b/cmake/make_dist/MakeDist.cmake @@ -65,8 +65,6 @@ function (MAKE_DIST PACKAGE_PREFIX MONGOCXX_SOURCE_DIR BUILD_SOURCE_DIR) endif () endforeach () - cmake_policy (SET CMP0012 NEW) - # -- Create the tarball. execute_process_and_check_result (COMMAND diff --git a/docs/api/mongocxx/examples/operation_exceptions.md b/docs/api/mongocxx/examples/operation_exceptions.md index 1c63ad8e0d..e1f1a43d72 100644 --- a/docs/api/mongocxx/examples/operation_exceptions.md +++ b/docs/api/mongocxx/examples/operation_exceptions.md @@ -1,6 +1,6 @@ # As a Regular Exception -@warning The @ref mongocxx::server_error_category error category is overloaded ([CXX-834](https://jira.mongodb.org/browse/CXX-834)). The error code value may belong to the server, libmongoc, or libmongocrypt depending on the context. Use error code values with caution. +@warning The @ref mongocxx::server_error_category error category is overloaded ([CXX-834](https://jira.mongodb.org/browse/CXX-834)). The error code value may belong to the server, mongoc, or libmongocrypt depending on the context. Use error code values with caution. @snippet examples/api/mongocxx/examples/operation_exceptions/regular.cpp Example diff --git a/etc/augmented.sbom.json b/etc/augmented.sbom.json index c16f986e7d..993a7705a0 100644 --- a/etc/augmented.sbom.json +++ b/etc/augmented.sbom.json @@ -1,16 +1,16 @@ { "components": [ { - "bom-ref": "pkg:github/mongodb/mongo-c-driver@v1.30.1", + "bom-ref": "pkg:github/mongodb/mongo-c-driver@v2.0.0", "copyright": "Copyright 2009-present MongoDB, Inc.", "externalReferences": [ { "type": "distribution", - "url": "https://github.com/mongodb/mongo-c-driver/archive/refs/tags/v1.30.1.tar.gz" + "url": "https://github.com/mongodb/mongo-c-driver/archive/refs/tags/v2.0.0.tar.gz" }, { "type": "website", - "url": "https://github.com/mongodb/mongo-c-driver/tree/v1.30.1" + "url": "https://github.com/mongodb/mongo-c-driver/tree/v2.0.0" } ], "group": "mongodb", @@ -22,18 +22,18 @@ } ], "name": "mongo-c-driver", - "purl": "pkg:github/mongodb/mongo-c-driver@v1.30.1", + "purl": "pkg:github/mongodb/mongo-c-driver@v2.0.0", "type": "library", - "version": "v1.30.1" + "version": "v2.0.0" } ], "dependencies": [ { - "ref": "pkg:github/mongodb/mongo-c-driver@v1.30.1" + "ref": "pkg:github/mongodb/mongo-c-driver@v2.0.0" } ], "metadata": { - "timestamp": "2025-02-24T19:19:59.233810+00:00", + "timestamp": "2025-04-02T19:47:45.682404+00:00", "tools": [ { "externalReferences": [ diff --git a/etc/cyclonedx.sbom.json b/etc/cyclonedx.sbom.json index fc333cdd3f..993a7705a0 100644 --- a/etc/cyclonedx.sbom.json +++ b/etc/cyclonedx.sbom.json @@ -1,16 +1,16 @@ { "components": [ { - "bom-ref": "pkg:github/mongodb/mongo-c-driver@v1.30.1", + "bom-ref": "pkg:github/mongodb/mongo-c-driver@v2.0.0", "copyright": "Copyright 2009-present MongoDB, Inc.", "externalReferences": [ { "type": "distribution", - "url": "https://github.com/mongodb/mongo-c-driver/archive/refs/tags/v1.30.1.tar.gz" + "url": "https://github.com/mongodb/mongo-c-driver/archive/refs/tags/v2.0.0.tar.gz" }, { "type": "website", - "url": "https://github.com/mongodb/mongo-c-driver/tree/v1.30.1" + "url": "https://github.com/mongodb/mongo-c-driver/tree/v2.0.0" } ], "group": "mongodb", @@ -22,18 +22,18 @@ } ], "name": "mongo-c-driver", - "purl": "pkg:github/mongodb/mongo-c-driver@v1.30.1", + "purl": "pkg:github/mongodb/mongo-c-driver@v2.0.0", "type": "library", - "version": "v1.30.1" + "version": "v2.0.0" } ], "dependencies": [ { - "ref": "pkg:github/mongodb/mongo-c-driver@v1.30.1" + "ref": "pkg:github/mongodb/mongo-c-driver@v2.0.0" } ], "metadata": { - "timestamp": "2025-02-24T19:17:22.359043+00:00", + "timestamp": "2025-04-02T19:47:45.682404+00:00", "tools": [ { "externalReferences": [ diff --git a/etc/make_release.py b/etc/make_release.py index abedb9a24d..f512caf45d 100755 --- a/etc/make_release.py +++ b/etc/make_release.py @@ -88,7 +88,7 @@ show_default=True, help='The remote reference which points to the mongodb/mongo-cxx-driver repo') @click.option('--c-driver-build-ref', - default='1.30.1', + default='2.0.0', show_default=True, help='When building the C driver, build at this Git reference') @click.option('--with-c-driver', @@ -127,7 +127,7 @@ def release(jira_creds_file, Perform the steps associated with the release. """ - check_libmongoc_version() + check_mongoc_version() # Read Jira credentials and GitHub token first, to check that # user has proper credentials before embarking on lengthy builds. @@ -233,43 +233,43 @@ def release(jira_creds_file, release_notes_text, output_file, quiet) -def check_libmongoc_version(): - got_LIBMONGOC_REQUIRED_VERSION = None - got_LIBMONGOC_DOWNLOAD_VERSION = None +def check_mongoc_version(): + got_MONGOC_REQUIRED_VERSION = None + got_MONGOC_DOWNLOAD_VERSION = None with open("CMakeLists.txt", "r") as cmakelists: for line in cmakelists: match = re.match( - r"set\(LIBMONGOC_REQUIRED_VERSION\s+(.*?)\)", line) + r"set\(MONGOC_REQUIRED_VERSION\s+(.*?)\)", line) if match: if 'TODO' in line: click.echo( - 'Found TODO on LIBMONGOC_REQUIRED_VERSION line in CMakeLists.txt: {}'.format(line)) + 'Found TODO on MONGOC_REQUIRED_VERSION line in CMakeLists.txt: {}'.format(line)) sys.exit(1) - got_LIBMONGOC_REQUIRED_VERSION = match.group(1) + got_MONGOC_REQUIRED_VERSION = match.group(1) continue match = re.match( - r"set\(LIBMONGOC_DOWNLOAD_VERSION\s+(.*?)\)", line) + r"set\(MONGOC_DOWNLOAD_VERSION\s+(.*?)\)", line) if match: if 'TODO' in line: click.echo( - 'Found TODO on LIBMONGOC_DOWNLOAD_VERSION line in CMakeLists.txt: {}'.format(line)) + 'Found TODO on MONGOC_DOWNLOAD_VERSION line in CMakeLists.txt: {}'.format(line)) sys.exit(1) - got_LIBMONGOC_DOWNLOAD_VERSION = match.group(1) + got_MONGOC_DOWNLOAD_VERSION = match.group(1) continue - assert got_LIBMONGOC_DOWNLOAD_VERSION - assert got_LIBMONGOC_REQUIRED_VERSION - libmongoc_version_pattern = r'[0-9]+\.[0-9]+\.[0-9]+' - if not re.match (libmongoc_version_pattern, got_LIBMONGOC_DOWNLOAD_VERSION): - click.echo("Expected LIBMONGOC_DOWNLOAD_VERSION to match: {}, got: {}".format( - libmongoc_version_pattern, got_LIBMONGOC_DOWNLOAD_VERSION)) + assert got_MONGOC_DOWNLOAD_VERSION + assert got_MONGOC_REQUIRED_VERSION + mongoc_version_pattern = r'[0-9]+\.[0-9]+\.[0-9]+' + if not re.match (mongoc_version_pattern, got_MONGOC_DOWNLOAD_VERSION): + click.echo("Expected MONGOC_DOWNLOAD_VERSION to match: {}, got: {}".format( + mongoc_version_pattern, got_MONGOC_DOWNLOAD_VERSION)) sys.exit(1) - if not re.match (libmongoc_version_pattern, got_LIBMONGOC_REQUIRED_VERSION): - click.echo("Expected LIBMONGOC_REQUIRED_VERSION to match: {}, got: {}".format( - libmongoc_version_pattern, got_LIBMONGOC_REQUIRED_VERSION)) + if not re.match (mongoc_version_pattern, got_MONGOC_REQUIRED_VERSION): + click.echo("Expected MONGOC_REQUIRED_VERSION to match: {}, got: {}".format( + mongoc_version_pattern, got_MONGOC_REQUIRED_VERSION)) sys.exit(1) - if got_LIBMONGOC_DOWNLOAD_VERSION != got_LIBMONGOC_REQUIRED_VERSION: - click.echo("Expected LIBMONGOC_DOWNLOAD_VERSION ({}) to match LIBMONGOC_REQUIRED_VERSION ({})".format( - got_LIBMONGOC_DOWNLOAD_VERSION, got_LIBMONGOC_REQUIRED_VERSION)) + if got_MONGOC_DOWNLOAD_VERSION != got_MONGOC_REQUIRED_VERSION: + click.echo("Expected MONGOC_DOWNLOAD_VERSION ({}) to match MONGOC_REQUIRED_VERSION ({})".format( + got_MONGOC_DOWNLOAD_VERSION, got_MONGOC_REQUIRED_VERSION)) sys.exit(1) @@ -384,8 +384,8 @@ def ensure_c_driver(c_driver_build_ref, with_c_driver, quiet): """ if with_c_driver: - bson_h = os.path.join(with_c_driver, 'include/libbson-1.0/bson/bson.h') - mongoc_h = os.path.join(with_c_driver, 'include/libmongoc-1.0/mongoc/mongoc.h') + bson_h = os.path.join(with_c_driver, 'include/bson2/bson/bson.h') + mongoc_h = os.path.join(with_c_driver, 'include/mongoc2/mongoc/mongoc.h') if os.path.exists(bson_h) and os.path.exists(mongoc_h): return with_c_driver if not quiet: diff --git a/etc/purls.txt b/etc/purls.txt index 589f9356e3..de138b5cce 100644 --- a/etc/purls.txt +++ b/etc/purls.txt @@ -5,5 +5,5 @@ # instead of modifying the SBOM JSON directly. After modifying this file, be sure to # re-generate the SBOM JSON file! -# libbson and libmongoc are obtained via cmake/FetchMongoC.cmake. -pkg:github/mongodb/mongo-c-driver@v1.30.1 +# bson and mongoc may be obtained via cmake/FetchMongoC.cmake. +pkg:github/mongodb/mongo-c-driver@v2.0.0 diff --git a/etc/releasing.md b/etc/releasing.md index b984e025ac..6d356b8db1 100644 --- a/etc/releasing.md +++ b/etc/releasing.md @@ -575,7 +575,7 @@ git commit -m "Post-release changes" (Stable Releases Only) Close the Jira ticket tracking this release with "Documentation Changes" set to "Needed". Fill the "Documentation Changes Summary" field with information requesting updates to: - the "Installing the MongoDB C Driver" section of the [Advanced Configuration and Installation Options](https://www.mongodb.com/docs/languages/cpp/cpp-driver/current/installation/advanced/#installing-the-mongodb-c-driver) page - with any new libmongoc version requirements, + with any new C Driver version requirements, - the "Driver Status by Family and Version" section of the [home page](https://www.mongodb.com/docs/languages/cpp/cpp-driver/current/#driver-status-by-family-and-version), and - the [full version](https://github.com/mongodb/docs-cpp/blob/master/snooty.toml) for the C++ Driver documentation pages. @@ -585,7 +585,7 @@ This will generate a DOCSP ticket with instructions to update the C++ Driver doc Example (using Jira syntax formatting): ``` -* The [Advanced Installation|https://www.mongodb.com/docs/languages/cpp/cpp-driver/current/installation/advanced/#installing-the-mongodb-c-driver] page must be updated with a new requirement: "For mongocxx-X.Y.x, libmongoc A.B.C or later is required." +* The [Advanced Installation|https://www.mongodb.com/docs/languages/cpp/cpp-driver/current/installation/advanced/#installing-the-mongodb-c-driver] page must be updated with a new requirement: "For mongocxx-X.Y.x, mongoc A.B.C or later is required." * The [MongoDB C++ Driver|https://www.mongodb.com/docs/languages/cpp/cpp-driver/current/#driver-status-by-family-and-version] page must be updated: {{{}mongocxx X.Y.x{}}} is now a previous stable release and no longer under active development; {{{}mongocxx X.Y+1.x{}}} is the new current stable release eligible for bug fixes. * the [full version|https://github.com/mongodb/docs-cpp/blob/master/snooty.toml] for C++ Driver documentation must be updated to {{{}X.Y.Z{}}}. ``` diff --git a/examples/add_subdirectory/CMakeLists.txt b/examples/add_subdirectory/CMakeLists.txt index 90b407a35d..c6a309ff0e 100644 --- a/examples/add_subdirectory/CMakeLists.txt +++ b/examples/add_subdirectory/CMakeLists.txt @@ -13,13 +13,9 @@ # limitations under the License. # Demonstrates how to use the CMake 'add_subdirectory' mechanism to locate and -# build against the libmongoc and libmongocxx shared libraries. +# build against the mongoc and mongocxx shared libraries. -cmake_minimum_required(VERSION 3.2 FATAL_ERROR) - -if(POLICY CMP0025) - cmake_policy(SET CMP0025 NEW) -endif() +cmake_minimum_required(VERSION 3.15...4.0) project(HELLO_WORLD LANGUAGES C CXX) diff --git a/examples/projects/bsoncxx/cmake/shared/CMakeLists.txt b/examples/projects/bsoncxx/cmake/shared/CMakeLists.txt index 72bee83970..a0c7414d06 100644 --- a/examples/projects/bsoncxx/cmake/shared/CMakeLists.txt +++ b/examples/projects/bsoncxx/cmake/shared/CMakeLists.txt @@ -15,11 +15,7 @@ # Demonstrates how to use the CMake 'find_package' mechanism to locate build against the libbsoncxx # shared library. -cmake_minimum_required(VERSION 3.2 FATAL_ERROR) - -if(POLICY CMP0025) - cmake_policy(SET CMP0025 NEW) -endif() +cmake_minimum_required(VERSION 3.15...4.0) project(HELLO_WORLD LANGUAGES C CXX) diff --git a/examples/projects/bsoncxx/cmake/shared/build.sh b/examples/projects/bsoncxx/cmake/shared/build.sh index 153dad6d0f..e4aa5b7791 100755 --- a/examples/projects/bsoncxx/cmake/shared/build.sh +++ b/examples/projects/bsoncxx/cmake/shared/build.sh @@ -9,6 +9,6 @@ if [ -z "$MSVC" ]; then "${cmake_binary:?}" -DCMAKE_BUILD_TYPE="${build_type:?}" -DCMAKE_CXX_STANDARD="${CXX_STANDARD:?}" .. "${cmake_binary:?}" --build . --target run else - "${cmake_binary:?}" -G "Visual Studio 15 2017" -A "x64" -DCMAKE_CXX_STANDARD="${CXX_STANDARD:?}" -DCMAKE_POLICY_DEFAULT_CMP0141=NEW -DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded .. + "${cmake_binary:?}" -G "Visual Studio 15 2017" -A "x64" -DCMAKE_CXX_STANDARD="${CXX_STANDARD:?}" -DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded .. "${cmake_binary:?}" --build . --target run --config "${build_type:?}" -- /verbosity:minimal fi diff --git a/examples/projects/bsoncxx/cmake/static/CMakeLists.txt b/examples/projects/bsoncxx/cmake/static/CMakeLists.txt index 8d79cf58b1..4cacdfd943 100644 --- a/examples/projects/bsoncxx/cmake/static/CMakeLists.txt +++ b/examples/projects/bsoncxx/cmake/static/CMakeLists.txt @@ -15,11 +15,7 @@ # Demonstrates how to use the CMake 'find_package' mechanism to locate build against the libbsoncxx # static library. -cmake_minimum_required(VERSION 3.2 FATAL_ERROR) - -if(POLICY CMP0025) - cmake_policy(SET CMP0025 NEW) -endif() +cmake_minimum_required(VERSION 3.15...4.0) project(HELLO_WORLD LANGUAGES C CXX) diff --git a/examples/projects/bsoncxx/cmake/static/build.sh b/examples/projects/bsoncxx/cmake/static/build.sh index 153dad6d0f..e4aa5b7791 100755 --- a/examples/projects/bsoncxx/cmake/static/build.sh +++ b/examples/projects/bsoncxx/cmake/static/build.sh @@ -9,6 +9,6 @@ if [ -z "$MSVC" ]; then "${cmake_binary:?}" -DCMAKE_BUILD_TYPE="${build_type:?}" -DCMAKE_CXX_STANDARD="${CXX_STANDARD:?}" .. "${cmake_binary:?}" --build . --target run else - "${cmake_binary:?}" -G "Visual Studio 15 2017" -A "x64" -DCMAKE_CXX_STANDARD="${CXX_STANDARD:?}" -DCMAKE_POLICY_DEFAULT_CMP0141=NEW -DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded .. + "${cmake_binary:?}" -G "Visual Studio 15 2017" -A "x64" -DCMAKE_CXX_STANDARD="${CXX_STANDARD:?}" -DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded .. "${cmake_binary:?}" --build . --target run --config "${build_type:?}" -- /verbosity:minimal fi diff --git a/examples/projects/mongocxx/cmake/shared/CMakeLists.txt b/examples/projects/mongocxx/cmake/shared/CMakeLists.txt index 1584cda05e..84b7103f79 100644 --- a/examples/projects/mongocxx/cmake/shared/CMakeLists.txt +++ b/examples/projects/mongocxx/cmake/shared/CMakeLists.txt @@ -15,11 +15,7 @@ # Demonstrates how to use the CMake 'find_package' mechanism to locate build against the libmongocxx # shared library. -cmake_minimum_required(VERSION 3.2 FATAL_ERROR) - -if(POLICY CMP0025) - cmake_policy(SET CMP0025 NEW) -endif() +cmake_minimum_required(VERSION 3.15...4.0) project(HELLO_WORLD LANGUAGES C CXX) diff --git a/examples/projects/mongocxx/cmake/shared/build.sh b/examples/projects/mongocxx/cmake/shared/build.sh index 153dad6d0f..e4aa5b7791 100755 --- a/examples/projects/mongocxx/cmake/shared/build.sh +++ b/examples/projects/mongocxx/cmake/shared/build.sh @@ -9,6 +9,6 @@ if [ -z "$MSVC" ]; then "${cmake_binary:?}" -DCMAKE_BUILD_TYPE="${build_type:?}" -DCMAKE_CXX_STANDARD="${CXX_STANDARD:?}" .. "${cmake_binary:?}" --build . --target run else - "${cmake_binary:?}" -G "Visual Studio 15 2017" -A "x64" -DCMAKE_CXX_STANDARD="${CXX_STANDARD:?}" -DCMAKE_POLICY_DEFAULT_CMP0141=NEW -DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded .. + "${cmake_binary:?}" -G "Visual Studio 15 2017" -A "x64" -DCMAKE_CXX_STANDARD="${CXX_STANDARD:?}" -DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded .. "${cmake_binary:?}" --build . --target run --config "${build_type:?}" -- /verbosity:minimal fi diff --git a/examples/projects/mongocxx/cmake/static/CMakeLists.txt b/examples/projects/mongocxx/cmake/static/CMakeLists.txt index f166b903ea..1c97567d8e 100644 --- a/examples/projects/mongocxx/cmake/static/CMakeLists.txt +++ b/examples/projects/mongocxx/cmake/static/CMakeLists.txt @@ -15,11 +15,7 @@ # Demonstrates how to use the CMake 'find_package' mechanism to locate build against the libmongocxx # static library. -cmake_minimum_required(VERSION 3.2 FATAL_ERROR) - -if(POLICY CMP0025) - cmake_policy(SET CMP0025 NEW) -endif() +cmake_minimum_required(VERSION 3.15...4.0) project(HELLO_WORLD LANGUAGES C CXX) diff --git a/examples/projects/mongocxx/cmake/static/build.sh b/examples/projects/mongocxx/cmake/static/build.sh index 76f551138c..df701c4723 100755 --- a/examples/projects/mongocxx/cmake/static/build.sh +++ b/examples/projects/mongocxx/cmake/static/build.sh @@ -9,6 +9,6 @@ if [ -z "$MSVC" ]; then "${cmake_binary:?}" -DCMAKE_BUILD_TYPE="${build_type}" -DCMAKE_CXX_STANDARD="${CXX_STANDARD:?}" .. "${cmake_binary:?}" --build . --target run else - "${cmake_binary:?}" -G "Visual Studio 15 2017" -A "x64" -DCMAKE_CXX_STANDARD="${CXX_STANDARD:?}" -DCMAKE_POLICY_DEFAULT_CMP0141=NEW -DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded .. + "${cmake_binary:?}" -G "Visual Studio 15 2017" -A "x64" -DCMAKE_CXX_STANDARD="${CXX_STANDARD:?}" -DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded .. "${cmake_binary:?}" --build . --target run --config "${build_type:?}" -- /verbosity:minimal fi diff --git a/src/bsoncxx/CMakeLists.txt b/src/bsoncxx/CMakeLists.txt index 5c18db21be..10e090c1d1 100644 --- a/src/bsoncxx/CMakeLists.txt +++ b/src/bsoncxx/CMakeLists.txt @@ -16,9 +16,9 @@ project(BSONCXX) ParseVersion(${BUILD_VERSION} BSONCXX) -set(BSONCXX_VERSION_NO_EXTRA ${BSONCXX_VERSION_MAJOR}.${BSONCXX_VERSION_MINOR}.${BSONCXX_VERSION_PATCH}) -set(BSONCXX_VERSION ${BSONCXX_VERSION_NO_EXTRA}${BSONCXX_VERSION_EXTRA}) -message(STATUS "bsoncxx version: ${BSONCXX_VERSION}") +set(BSONCXX_VERSION_NO_EXTRA ${BSONCXX_VERSION_MAJOR}.${BSONCXX_VERSION_MINOR}.${BSONCXX_VERSION_PATCH} CACHE INTERNAL "") +set(BSONCXX_VERSION $CACHE{BSONCXX_VERSION_NO_EXTRA}${BSONCXX_VERSION_EXTRA} CACHE INTERNAL "") +message(STATUS "bsoncxx version: $CACHE{BSONCXX_VERSION}") option(BSONCXX_POLY_USE_IMPLS "Use bsoncxx implementations for stdx polyfills" OFF) option(BSONCXX_POLY_USE_STD "Use C++17 std library for stdx polyfills" OFF) @@ -56,51 +56,23 @@ elseif(BSONCXX_POLY_OPTIONS_SET EQUAL 0) endif() endif() -set(BSONCXX_PKG_DEP "") # Required by bsoncxx-config.cmake.in. - if(TARGET bson_shared OR TARGET bson_static) - # If these targets exist, then libbson has already been included as a project - # sub-directory - message(STATUS "found libbson targets declared in current build scope; version not checked") + message(STATUS "Using bson targets imported via add_subdirectory() without version checks") - if(NOT BSONCXX_LINK_WITH_STATIC_MONGOC) - set(libbson_target bson_shared) + if(BSONCXX_LINK_WITH_STATIC_MONGOC) + set(bson_target bson::static) else() - set(libbson_target bson_static) + set(bson_target bson::shared) endif() - - set(BSONCXX_PKG_DEP "find_dependency(bson-${LIBBSON_REQUIRED_ABI_VERSION} REQUIRED)") else() - # Attempt to find libbson by new package name (without lib). - find_package(bson-${LIBBSON_REQUIRED_ABI_VERSION} ${LIBBSON_REQUIRED_VERSION} QUIET) - - if(bson-${LIBBSON_REQUIRED_ABI_VERSION}_FOUND) - message(STATUS "found libbson version ${bson-${LIBBSON_REQUIRED_ABI_VERSION}_VERSION}") + find_package(bson ${BSON_REQUIRED_VERSION} REQUIRED) - if(NOT BSONCXX_LINK_WITH_STATIC_MONGOC) - set(libbson_target mongo::bson_shared) - else() - set(libbson_target mongo::bson_static) - endif() + message(STATUS "Found bson: ${bson_DIR} (found version \"${bson_VERSION}\")") - set(BSONCXX_PKG_DEP "find_dependency(bson-${LIBBSON_REQUIRED_ABI_VERSION} REQUIRED)") + if(BSONCXX_LINK_WITH_STATIC_MONGOC) + set(bson_target bson::static) else() - # Require package of old libbson name (with lib). - if(NOT BSONCXX_LINK_WITH_STATIC_MONGOC) - find_package(libbson-${LIBBSON_REQUIRED_ABI_VERSION} ${LIBBSON_REQUIRED_VERSION} REQUIRED) - message(STATUS "found libbson version ${BSON_VERSION}") - set(libbson_target ${BSON_LIBRARIES}) - set(libbson_include_directories ${BSON_INCLUDE_DIRS}) - set(libbson_definitions ${BSON_DEFINITIONS}) - set(BSONCXX_PKG_DEP "find_dependency(libbson-${LIBBSON_REQUIRED_ABI_VERSION} REQUIRED)") - else() - find_package(libbson-static-${LIBBSON_REQUIRED_ABI_VERSION} ${LIBBSON_REQUIRED_VERSION} REQUIRED) - message(STATUS "found libbson version ${BSON_STATIC_VERSION}") - set(libbson_target ${BSON_STATIC_LIBRARIES}) - set(libbson_include_directories ${BSON_STATIC_INCLUDE_DIRS}) - set(libbson_definitions ${BSON_STATIC_DEFINITIONS}) - set(BSONCXX_PKG_DEP "find_dependency(libbson-static-${LIBBSON_REQUIRED_ABI_VERSION} REQUIRED)") - endif() + set(bson_target bson::shared) endif() endif() diff --git a/src/bsoncxx/cmake/CMakeLists.txt b/src/bsoncxx/cmake/CMakeLists.txt index 9b8a7bca5c..641e6cf0bd 100644 --- a/src/bsoncxx/cmake/CMakeLists.txt +++ b/src/bsoncxx/cmake/CMakeLists.txt @@ -18,7 +18,7 @@ include(CMakePackageConfigHelpers) if(1) write_basic_package_version_file( bsoncxx-config-version.cmake - VERSION ${BSONCXX_VERSION} + VERSION $CACHE{BSONCXX_VERSION} COMPATIBILITY SameMajorVersion ) @@ -42,14 +42,14 @@ if(1) install(EXPORT bsoncxx_targets NAMESPACE mongo:: FILE bsoncxx_targets.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bsoncxx-${BSONCXX_VERSION} + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bsoncxx-$CACHE{BSONCXX_VERSION} ) install( FILES ${CMAKE_CURRENT_BINARY_DIR}/bsoncxx-config-version.cmake ${CMAKE_CURRENT_BINARY_DIR}/bsoncxx-config.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bsoncxx-${BSONCXX_VERSION} + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bsoncxx-$CACHE{BSONCXX_VERSION} COMPONENT Devel ) endif() @@ -76,10 +76,9 @@ if(1) -D "includedir=${CMAKE_INSTALL_INCLUDEDIR}" -D "libdir=${CMAKE_INSTALL_LIBDIR}" -D "output_name=${output_name}" - -D "version=${BSONCXX_VERSION_NO_EXTRA}" + -D "version=$CACHE{BSONCXX_VERSION_NO_EXTRA}" -D "is_static=${is_static}" - -D "libbson_req_abi_ver=${LIBBSON_REQUIRED_ABI_VERSION}" - -D "libbson_req_ver=${LIBBSON_REQUIRED_VERSION}" + -D "bson_req_ver=${BSON_REQUIRED_VERSION}" -P ${CMAKE_CURRENT_SOURCE_DIR}/generate-pc.cmake MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/libbsoncxx.pc.in diff --git a/src/bsoncxx/cmake/bsoncxx-config.cmake.in b/src/bsoncxx/cmake/bsoncxx-config.cmake.in index 768f3dde66..bffb20d000 100644 --- a/src/bsoncxx/cmake/bsoncxx-config.cmake.in +++ b/src/bsoncxx/cmake/bsoncxx-config.cmake.in @@ -1,3 +1,3 @@ include(CMakeFindDependencyMacro) -@BSONCXX_PKG_DEP@ +find_dependency(bson @BSON_REQUIRED_VERSION@) include("${CMAKE_CURRENT_LIST_DIR}/bsoncxx_targets.cmake") diff --git a/src/bsoncxx/cmake/generate-pc.cmake b/src/bsoncxx/cmake/generate-pc.cmake index 9ecc6977f9..0630f1a955 100644 --- a/src/bsoncxx/cmake/generate-pc.cmake +++ b/src/bsoncxx/cmake/generate-pc.cmake @@ -7,8 +7,7 @@ set(input_vars output_name version is_static - libbson_req_abi_ver - libbson_req_ver + bson_req_ver ) foreach(var ${input_vars}) @@ -27,7 +26,7 @@ if(1) set(requires "") if(is_static) - list(APPEND requires "libbson-static-${libbson_req_abi_ver} >= ${libbson_req_ver}") + list(APPEND requires "bson2 >= ${bson_req_ver}") endif() list(JOIN requires ", " requires) diff --git a/src/bsoncxx/lib/CMakeLists.txt b/src/bsoncxx/lib/CMakeLists.txt index e067bb2c2e..46e277a297 100644 --- a/src/bsoncxx/lib/CMakeLists.txt +++ b/src/bsoncxx/lib/CMakeLists.txt @@ -14,6 +14,7 @@ set(bsoncxx_sources_private bsoncxx/private/itoa.cpp + bsoncxx/private/version.cpp ) set(bsoncxx_sources_v_noabi @@ -97,6 +98,7 @@ set_dist_list(src_bsoncxx_lib_DIST bsoncxx/private/make_unique.hh bsoncxx/private/stack.hh bsoncxx/private/suppress_deprecation_warnings.hh + bsoncxx/private/version.hh bsoncxx/v_noabi/bsoncxx/types/bson_value/value.hh bsoncxx/v1/config/config.hpp.in bsoncxx/v1/config/version.hpp.in diff --git a/src/bsoncxx/lib/bsoncxx/private/config/config.hh.in b/src/bsoncxx/lib/bsoncxx/private/config/config.hh.in index 98cfd9fb6e..0abf602042 100644 --- a/src/bsoncxx/lib/bsoncxx/private/config/config.hh.in +++ b/src/bsoncxx/lib/bsoncxx/private/config/config.hh.in @@ -11,3 +11,5 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#define BSON_REQUIRED_VERSION() "@BSON_REQUIRED_VERSION@" diff --git a/src/bsoncxx/lib/bsoncxx/private/version.cpp b/src/bsoncxx/lib/bsoncxx/private/version.cpp new file mode 100644 index 0000000000..d96c792c44 --- /dev/null +++ b/src/bsoncxx/lib/bsoncxx/private/version.cpp @@ -0,0 +1,59 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +// + +#include +#include + +#include + +namespace bsoncxx { +namespace test_util { + +std::vector split_version(bsoncxx::stdx::string_view input) { + static constexpr auto npos = bsoncxx::stdx::string_view::npos; + static constexpr auto digits = "0123456789"; + + std::vector ret; + + std::size_t pos; + + while ((pos = input.find_first_not_of(digits)) != npos) { + auto const str = input.substr(0u, pos); + + // No more digits. + if (str.empty()) { + return ret; + } + + ret.push_back(std::stoi(static_cast(str))); + + input.remove_prefix(str.size()); + input.remove_prefix(input.empty() ? 0u : 1u); // Delimiter. + } + + // Last group of digits. + if ((pos = input.find_last_of(digits)) != npos) { + auto const str = input.substr(0u, pos < input.size() ? pos + 1u : pos); + ret.push_back(std::stoi(static_cast(str))); + } + + return ret; +} + +} // namespace test_util +} // namespace bsoncxx diff --git a/src/bsoncxx/lib/bsoncxx/private/version.hh b/src/bsoncxx/lib/bsoncxx/private/version.hh new file mode 100644 index 0000000000..93b7b257ac --- /dev/null +++ b/src/bsoncxx/lib/bsoncxx/private/version.hh @@ -0,0 +1,30 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +#include + +#include + +namespace bsoncxx { +namespace test_util { + +// Convert the given string "1.2.3-suffix" into a vector [1, 2, 3]. +BSONCXX_ABI_EXPORT_CDECL_TESTING(std::vector) split_version(bsoncxx::stdx::string_view input); + +} // namespace test_util +} // namespace bsoncxx diff --git a/src/bsoncxx/test/CMakeLists.txt b/src/bsoncxx/test/CMakeLists.txt index 2a53864813..36662ca48b 100644 --- a/src/bsoncxx/test/CMakeLists.txt +++ b/src/bsoncxx/test/CMakeLists.txt @@ -41,6 +41,7 @@ add_executable(test_bson json.cpp oid.cpp vector.cpp + version.cpp optional.test.cpp view_or_value.cpp make_unique.test.cpp @@ -54,16 +55,11 @@ add_library(bsoncxx_test_properties INTERFACE) target_link_libraries(bsoncxx_test_properties INTERFACE Catch2::Catch2) target_compile_features(bsoncxx_test_properties INTERFACE cxx_std_14) -target_link_libraries(bsoncxx_test_properties INTERFACE bsoncxx_testing ${libbson_target}) +target_link_libraries(bsoncxx_test_properties INTERFACE bsoncxx_testing ${bson_target}) target_include_directories(bsoncxx_test_properties INTERFACE - - # Allow `#include ` - ${libbson_include_directories} - # Allow `#include `. ${CMAKE_CURRENT_SOURCE_DIR}/../.. ) -target_compile_definitions(bsoncxx_test_properties INTERFACE ${libbson_definitions}) # Use `__vectorcall` by default with MSVC to catch missing `__cdecl`. target_compile_options(bsoncxx_test_properties INTERFACE "$<$:/Gv>") add_library(bsoncxx::test_properties ALIAS bsoncxx_test_properties) @@ -164,6 +160,7 @@ set_dist_list(src_bsoncxx_test_DIST test_macro_guards.cpp.in to_string.hh view_or_value.cpp + version.cpp make_unique.test.cpp string_view.test.cpp type_traits.test.cpp diff --git a/src/bsoncxx/test/version.cpp b/src/bsoncxx/test/version.cpp new file mode 100644 index 0000000000..9ba1c73e8a --- /dev/null +++ b/src/bsoncxx/test/version.cpp @@ -0,0 +1,102 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +// + +#include +#include + +#include + +#include +#include + +#include + +namespace { + +using bsoncxx::test_util::split_version; + +TEST_CASE("split_version", "[bsoncxx][test][split_version]") { + CHECK(split_version("1") == std::vector({1})); + CHECK(split_version("1.2") == std::vector({1, 2})); + CHECK(split_version("1.2.3") == std::vector({1, 2, 3})); + CHECK(split_version("11.22.33") == std::vector({11, 22, 33})); + + CHECK(split_version("1-") == std::vector({1})); + CHECK(split_version("1.2-") == std::vector({1, 2})); + CHECK(split_version("1.2.3-") == std::vector({1, 2, 3})); + CHECK(split_version("1.2.3-suffix") == std::vector({1, 2, 3})); +} + +TEST_CASE("bson version numbers", "[bsoncxx][test]") { + auto expect_str = bsoncxx::stdx::string_view{BSON_REQUIRED_VERSION()}; + + CAPTURE(expect_str); + + auto const expect = split_version(expect_str); + + // The major version number should always be present. + REQUIRE(expect.size() > 0); + + CHECK(bson_get_major_version() == expect[0]); + + CHECKED_IF(expect.size() > 1) { + CHECK(bson_get_minor_version() >= expect[1]); + + // Only when minor version number compares equal. + CHECKED_IF(expect.size() > 2) { + CHECK(bson_get_micro_version() >= expect[2]); + } + } +} + +TEST_CASE( + "bson version string" + "[bsoncxx][test]") { + auto const expect_str = bsoncxx::stdx::string_view{BSON_REQUIRED_VERSION()}; + auto const actual_str = bsoncxx::stdx::string_view{BSON_VERSION_S}; + + CAPTURE(expect_str); + CAPTURE(actual_str); + + auto const expect = split_version(expect_str); + auto const actual = split_version(actual_str); + + // The major version number should always be present. + REQUIRE(expect.size() > 0); + REQUIRE(actual.size() > 0); + + // Major version number. + CHECK(actual[0] == expect[0]); + + CHECKED_IF(expect.size() > 1) { + REQUIRE(actual.size() > 1); + + // Minor version number. + CHECK(actual[1] >= expect[1]); + + // Only when minor version number compares equal. + CHECKED_IF(expect.size() > 2) { + REQUIRE(actual.size() > 2); + + // Patch version number. + CHECK(actual[2] >= expect[2]); + } + } +} + +} // namespace diff --git a/src/mongocxx/CMakeLists.txt b/src/mongocxx/CMakeLists.txt index e92198c7bb..8b40ce7b72 100644 --- a/src/mongocxx/CMakeLists.txt +++ b/src/mongocxx/CMakeLists.txt @@ -16,9 +16,9 @@ project(MONGOCXX) ParseVersion(${BUILD_VERSION} MONGOCXX) -set(MONGOCXX_VERSION_NO_EXTRA ${MONGOCXX_VERSION_MAJOR}.${MONGOCXX_VERSION_MINOR}.${MONGOCXX_VERSION_PATCH}) -set(MONGOCXX_VERSION ${MONGOCXX_VERSION_NO_EXTRA}${MONGOCXX_VERSION_EXTRA}) -message(STATUS "mongocxx version: ${MONGOCXX_VERSION}") +set(MONGOCXX_VERSION_NO_EXTRA ${MONGOCXX_VERSION_MAJOR}.${MONGOCXX_VERSION_MINOR}.${MONGOCXX_VERSION_PATCH} CACHE INTERNAL "") +set(MONGOCXX_VERSION ${MONGOCXX_VERSION_NO_EXTRA}${MONGOCXX_VERSION_EXTRA} CACHE INTERNAL "") +message(STATUS "mongocxx version: $CACHE{MONGOCXX_VERSION}") option(MONGOCXX_ENABLE_SSL "Enable SSL - if the underlying C driver offers it" ON) option(MONGOCXX_ENABLE_SLOW_TESTS "Run slow tests when invoking the the test target" OFF) @@ -31,49 +31,23 @@ endif () set(MONGOCXX_OUTPUT_BASENAME "mongocxx" CACHE STRING "Output mongocxx library base name") -set(MONGOCXX_PKG_DEP "") # Required by mongocxx-config.cmake.in. - if(TARGET mongoc_shared OR TARGET mongoc_static) - # If these targets exist, then libmongoc has already been included as a project - # sub-directory - message(STATUS "found libmongoc targets declared in current build scope; version not checked") + message(STATUS "Using mongoc targets imported via add_subdirectory() without version checks") - if(NOT MONGOCXX_LINK_WITH_STATIC_MONGOC) - set(libmongoc_target mongoc_shared) + if(MONGOCXX_LINK_WITH_STATIC_MONGOC) + set(mongoc_target mongoc::static) else() - set(libmongoc_target mongoc_static) + set(mongoc_target mongoc::shared) endif() - - set(MONGOCXX_PKG_DEP "find_dependency(mongoc-${LIBMONGOC_REQUIRED_ABI_VERSION} REQUIRED)") else() - # Attempt to find libmongoc by new package name (without lib). - find_package(mongoc-${LIBMONGOC_REQUIRED_ABI_VERSION} ${LIBMONGOC_REQUIRED_VERSION} QUIET) - - if(mongoc-${LIBMONGOC_REQUIRED_ABI_VERSION}_FOUND) - message(STATUS "found libmongoc version ${mongoc-${LIBMONGOC_REQUIRED_ABI_VERSION}_VERSION}") + find_package(mongoc ${MONGOC_REQUIRED_VERSION} REQUIRED) - if(NOT MONGOCXX_LINK_WITH_STATIC_MONGOC) - set(libmongoc_target mongo::mongoc_shared) - else() - set(libmongoc_target mongo::mongoc_static) - endif() + message(STATUS "Found mongoc: ${mongoc_DIR} (found version \"${mongoc_VERSION}\")") - set(MONGOCXX_PKG_DEP "find_dependency(mongoc-${LIBMONGOC_REQUIRED_ABI_VERSION} REQUIRED)") + if(MONGOCXX_LINK_WITH_STATIC_MONGOC) + set(mongoc_target mongoc::static) else() - # Require package of old libmongoc name (with lib). - if(NOT MONGOCXX_LINK_WITH_STATIC_MONGOC) - find_package(libmongoc-${LIBMONGOC_REQUIRED_ABI_VERSION} ${LIBMONGOC_REQUIRED_VERSION} REQUIRED) - message(STATUS "found libmongoc version ${MONGOC_VERSION}") - set(libmongoc_target ${MONGOC_LIBRARIES}) - set(libmongoc_definitions ${MONGOC_DEFINITIONS}) - set(MONGOCXX_PKG_DEP "find_dependency(libmongoc-${LIBMONGOC_REQUIRED_ABI_VERSION} REQUIRED)") - else() - find_package(libmongoc-static-${LIBMONGOC_REQUIRED_ABI_VERSION} ${LIBMONGOC_REQUIRED_VERSION} REQUIRED) - message(STATUS "found libmongoc version ${MONGOC_STATIC_VERSION}") - set(libmongoc_target ${MONGOC_STATIC_LIBRARIES}) - set(libmongoc_definitions ${MONGOC_STATIC_DEFINITIONS}) - set(MONGOCXX_PKG_DEP "find_dependency(libmongoc-static-${LIBMONGOC_REQUIRED_ABI_VERSION} REQUIRED)") - endif() + set(mongoc_target mongoc::shared) endif() endif() diff --git a/src/mongocxx/cmake/CMakeLists.txt b/src/mongocxx/cmake/CMakeLists.txt index 3b107f89b0..eb24dca92c 100644 --- a/src/mongocxx/cmake/CMakeLists.txt +++ b/src/mongocxx/cmake/CMakeLists.txt @@ -18,15 +18,19 @@ include(CMakePackageConfigHelpers) if(1) write_basic_package_version_file( mongocxx-config-version.cmake - VERSION ${MONGOCXX_VERSION} + VERSION $CACHE{MONGOCXX_VERSION} COMPATIBILITY SameMajorVersion ) - configure_file( - mongocxx-config.cmake.in - mongocxx-config.cmake - @ONLY - ) + function(configure_mongocxx_config_cmake) + set(BSONCXX_VERSION_NO_EXTRA $CACHE{BSONCXX_VERSION_NO_EXTRA}) + configure_file( + mongocxx-config.cmake.in + mongocxx-config.cmake + @ONLY + ) + endfunction() + configure_mongocxx_config_cmake() install(TARGETS ${mongocxx_target_list} @@ -42,14 +46,14 @@ if(1) install(EXPORT mongocxx_targets NAMESPACE mongo:: FILE mongocxx_targets.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mongocxx-${MONGOCXX_VERSION} + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mongocxx-$CACHE{MONGOCXX_VERSION} ) install( FILES ${CMAKE_CURRENT_BINARY_DIR}/mongocxx-config-version.cmake ${CMAKE_CURRENT_BINARY_DIR}/mongocxx-config.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mongocxx-${MONGOCXX_VERSION} + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mongocxx-$CACHE{MONGOCXX_VERSION} COMPONENT Devel ) endif() @@ -88,11 +92,10 @@ if(1) -D "includedir=${CMAKE_INSTALL_INCLUDEDIR}" -D "libdir=${CMAKE_INSTALL_LIBDIR}" -D "output_name=${output_name}" - -D "version=${MONGOCXX_VERSION_NO_EXTRA}" + -D "version=$CACHE{MONGOCXX_VERSION_NO_EXTRA}" -D "is_static=${is_static}" -D "bsoncxx_name=${bsoncxx_name}" - -D "libmongoc_req_abi_ver=${LIBMONGOC_REQUIRED_ABI_VERSION}" - -D "libmongoc_req_ver=${LIBMONGOC_REQUIRED_VERSION}" + -D "mongoc_req_ver=${MONGOC_REQUIRED_VERSION}" -P ${CMAKE_CURRENT_SOURCE_DIR}/generate-pc.cmake MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/libmongocxx.pc.in diff --git a/src/mongocxx/cmake/generate-pc.cmake b/src/mongocxx/cmake/generate-pc.cmake index af76aab029..9d19df453a 100644 --- a/src/mongocxx/cmake/generate-pc.cmake +++ b/src/mongocxx/cmake/generate-pc.cmake @@ -8,8 +8,7 @@ set(input_vars version is_static bsoncxx_name - libmongoc_req_abi_ver - libmongoc_req_ver + mongoc_req_ver ) foreach(var ${input_vars}) @@ -29,7 +28,7 @@ if(1) if(is_static) list(APPEND requires "lib${bsoncxx_name} >= ${version}") - list(APPEND requires "libmongoc-static-${libmongoc_req_abi_ver} >= ${libmongoc_req_ver}") + list(APPEND requires "mongoc2 >= ${mongoc_req_ver}") else() list(APPEND requires "lib${bsoncxx_name} >= ${version}") endif() diff --git a/src/mongocxx/cmake/mongocxx-config.cmake.in b/src/mongocxx/cmake/mongocxx-config.cmake.in index 402614349d..569c5ed000 100644 --- a/src/mongocxx/cmake/mongocxx-config.cmake.in +++ b/src/mongocxx/cmake/mongocxx-config.cmake.in @@ -1,4 +1,4 @@ include(CMakeFindDependencyMacro) -@MONGOCXX_PKG_DEP@ -find_dependency(bsoncxx REQUIRED) +find_dependency(mongoc @MONGOC_REQUIRED_VERSION@) +find_dependency(bsoncxx @BSONCXX_VERSION_NO_EXTRA@) include("${CMAKE_CURRENT_LIST_DIR}/mongocxx_targets.cmake") diff --git a/src/mongocxx/lib/mongocxx/private/config/config.hh.in b/src/mongocxx/lib/mongocxx/private/config/config.hh.in index 91790cd77b..50b7bb1466 100644 --- a/src/mongocxx/lib/mongocxx/private/config/config.hh.in +++ b/src/mongocxx/lib/mongocxx/private/config/config.hh.in @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#define MONGOC_REQUIRED_VERSION() "@MONGOC_REQUIRED_VERSION@" + #cmakedefine MONGOCXX_ENABLE_SSL #cmakedefine MONGOCXX_COMPILER_ID "${MONGOCXX_COMPILER_ID}" #cmakedefine MONGOCXX_COMPILER_VERSION "${MONGOCXX_COMPILER_VERSION}" diff --git a/src/mongocxx/test/CMakeLists.txt b/src/mongocxx/test/CMakeLists.txt index fffb820b78..bfb86d327a 100644 --- a/src/mongocxx/test/CMakeLists.txt +++ b/src/mongocxx/test/CMakeLists.txt @@ -103,6 +103,7 @@ add_executable(test_driver transactions.cpp uri.cpp validation_criteria.cpp + version.cpp write_concern.cpp ) target_link_libraries(test_driver PRIVATE client_helpers) @@ -147,12 +148,8 @@ add_library(mongocxx_test_properties INTERFACE) target_link_libraries(mongocxx_test_properties INTERFACE Catch2::Catch2) target_compile_features(mongocxx_test_properties INTERFACE cxx_std_14) -target_link_libraries(mongocxx_test_properties INTERFACE mongocxx_mocked ${libmongoc_target}) +target_link_libraries(mongocxx_test_properties INTERFACE mongocxx_mocked ${mongoc_target}) target_include_directories(mongocxx_test_properties INTERFACE - - # Allow `#include ` - ${libmongoc_include_directories} - # Allow `#include `. ${CMAKE_CURRENT_SOURCE_DIR}/../.. ) @@ -192,10 +189,6 @@ set_property( mongocxx::test_properties_with_main ) -target_compile_definitions(test_driver PRIVATE ${libmongoc_definitions}) -target_compile_definitions(test_logging PRIVATE ${libmongoc_definitions}) -target_compile_definitions(test_instance PRIVATE ${libmongoc_definitions}) - if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") target_compile_options(test_driver PRIVATE /bigobj) endif() @@ -413,6 +406,7 @@ set_dist_list(src_mongocxx_test_DIST transactions.cpp uri.cpp validation_criteria.cpp + version.cpp versioned_api.cpp write_concern.cpp ) diff --git a/src/mongocxx/test/version.cpp b/src/mongocxx/test/version.cpp new file mode 100644 index 0000000000..b902b4cede --- /dev/null +++ b/src/mongocxx/test/version.cpp @@ -0,0 +1,87 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +// + +#include + +#include +#include + +#include + +namespace { + +using bsoncxx::test_util::split_version; + +TEST_CASE("mongoc version numbers", "[mongocxx][test]") { + auto expect_str = bsoncxx::stdx::string_view{MONGOC_REQUIRED_VERSION()}; + + CAPTURE(expect_str); + + auto const expect = split_version(expect_str); + + // The major version number should always be present. + REQUIRE(expect.size() > 0); + + CHECK(mongoc_get_major_version() == expect[0]); + + CHECKED_IF(expect.size() > 1) { + CHECK(bson_get_minor_version() >= expect[1]); + + // Only when minor version number compares equal. + CHECKED_IF(expect.size() > 2) { + CHECK(bson_get_micro_version() >= expect[2]); + } + } +} + +TEST_CASE( + "mongoc version string" + "[mongocxx][test]") { + auto const expect_str = bsoncxx::stdx::string_view{MONGOC_REQUIRED_VERSION()}; + auto const actual_str = bsoncxx::stdx::string_view{MONGOC_VERSION_S}; + + CAPTURE(expect_str); + CAPTURE(actual_str); + + auto const expect = split_version(expect_str); + auto const actual = split_version(actual_str); + + // The major version number should always be present. + REQUIRE(expect.size() > 0); + REQUIRE(actual.size() > 0); + + // Major version number. + CHECK(actual[0] == expect[0]); + + CHECKED_IF(expect.size() > 1) { + REQUIRE(actual.size() > 1); + + // Minor version number. + CHECK(actual[1] >= expect[1]); + + // Only when minor version number compares equal. + CHECKED_IF(expect.size() > 2) { + REQUIRE(actual.size() > 2); + + // Patch version number. + CHECK(actual[2] >= expect[2]); + } + } +} + +} // namespace