使用 CMake ExternalProject_Add 构建 mongo-cxx-driver
Building mongo-cxx-driver using CMake ExternalProject_Add
我正在尝试在基于 CMake 的项目中构建 mongo-cxx-driver
。这个项目应该建立在 Windows、macOS 和 ubuntu 容器中,我想确保我在所有这些平台上的软件都使用相同的驱动程序版本,所以我负担不起安装驱动程序及其通过 apt-get
、brew
等依赖项。所以我只剩下一个选择:ExternalProject_Add
。但鉴于 libmongoc
的设置方式,我很难完成这项工作。
下面是我目前拥有的 CMake 模块。
include(ExternalProject)
set(libmongoc_CMAKE_ARGS
"-DCMAKE_BUILD_TYPE:STRIING=${CMAKE_BUILD_TYPE}"
"-DENABLE_TESTS:BOOL=OFF"
"-DENABLE_STATIC:BOOL=OFF"
"-DENABLE_EXAMPLES:BOOL=OFF"
"-DENABLE_EXTRA_ALIGNMENT:BOOL=OFF"
)
set(mongocxx_CMAKE_ARGS
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DCMAKE_BUILD_TYPE:STRIING=${CMAKE_BUILD_TYPE}"
"-DBUILD_SHARED_LIBS:BOOL=ON"
"-DENABLE_TESTS:BOOL=OFF"
"-DENABLE_EXAMPLES:BOOL=OFF"
"-DBSONCXX_POLY_USE_BOOST:BOOL=ON"
"-DBSONCXX_POLY_USE_MNMLSTC:BOOL=OFF"
"-Dlibbson-1.0_DIR:PATH=${OTS_DEPDENDENCIES_DIR}/libmongoc/src/libbson"
)
if (NOT TARGET libmongoc)
ExternalProject_Add(
libmongoc
GIT_REPOSITORY "https://github.com/mongodb/mongo-c-driver.git"
GIT_TAG "1.12.0"
SOURCE_DIR "${OTS_DEPDENDENCIES_DIR}/libmongoc"
BINARY_DIR "${OTS_DEPDENDENCIES_DIR}/libmongoc"
CMAKE_ARGS "${libmongoc_CMAKE_ARGS}"
INSTALL_COMMAND ""
)
endif()
if (NOT TARGET mongocxx)
ExternalProject_Add(
mongocxx
GIT_REPOSITORY "https://github.com/mongodb/mongo-cxx-driver.git"
GIT_TAG "r3.3.1"
SOURCE_DIR "${OTS_DEPDENDENCIES_DIR}/mongocxx"
BINARY_DIR "${OTS_DEPDENDENCIES_DIR}/mongocxx"
CMAKE_ARGS "${mongocxx_CMAKE_ARGS}"
INSTALL_COMMAND ""
DEPENDS libmongoc
)
endif()
请注意作为 mongo-cxx-driver
的 CMAKE_ARGS
之一给出的 CMAKE 选项 libbson-1.0_DIR
。我对此持怀疑态度,我相信这可能是罪魁祸首。有了它,我得到以下错误:
CMake Error at ${OTS_DEPENDENCIES_DIR}/libmongoc/src/libbson/libbson-1.0-config.cmake:30 (message):
File or directory
${OTS_DEPENDENCIES_DIR}/include/libbson-1.0
referenced by variable BSON_INCLUDE_DIRS does not exist !
Call Stack (most recent call first):
${OTS_DEPENDENCIES_DIR}/libmongoc/src/libbson/libbson-1.0-config.cmake:46 (set_and_check)
src/bsoncxx/CMakeLists.txt:81 (find_package)
哪一种说得通因为 src/bsoncxx/CMakeLists.txt:81
读作:
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
set_and_check (BSON_INCLUDE_DIRS "${PACKAGE_PREFIX_DIR}/include/libbson-1.0")
这使得 CMake 最终在 ${OTS_DEPDENDENCIES_DIR}/include
中寻找不存在的 libbson-1.0。如果我能告诉 cmake,"hey don't run this find_package
" 我可以给你 INCLUDE_DIR
、LIBRARIES
和 DEFINITIONS
我自己的路径。
如果删除此选项,我会收到以下错误:
CMake Error at src/bsoncxx/CMakeLists.txt:81 (find_package):
By not providing "Findlibbson-1.0.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"libbson-1.0", but CMake did not find one.
Could not find a package configuration file provided by "libbson-1.0"
(requested version 1.10.0) with any of the following names:
libbson-1.0Config.cmake
libbson-1.0-config.cmake
Add the installation prefix of "libbson-1.0" to CMAKE_PREFIX_PATH or set
"libbson-1.0_DIR" to a directory containing one of the above files. If
"libbson-1.0" provides a separate development package or SDK, be sure it
has been installed.
这也不是很奇怪,因为 CMake 尝试 find_package libbson-1.0 但无法弄清楚它的安装位置。
初步评论
在查看详细信息时,这里有一些初步评论:
SOURCE_DIR
和 BINARY_DIR
使用不同的目录
- 而不是
CMAKE_ARG
,更喜欢CMAKE_CACHE_ARGS
libbson-1.0_DIR
不应设置为源目录,而应设置为包含 config-file
包的构建目录(下面的 link 提供了有关此概念的更多详细信息)
- 确保始终指定 CMake 参数的类型(
-DCMAKE_CXX_COMPILER:PATH=${CMAKE_CXX_COMPILER}
而不是 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
)
- 不为多配置生成器设置
CMAKE_BUILD_TYPE
关于最后一点,这意味着您应该执行以下操作:
set(EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS)
if(NOT DEFINED CMAKE_CONFIGURATION_TYPES)
list(APPEND EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
)
endif()
在此 post 中,您可以了解如何构建项目:
允许编译 mongocxx 的工作项目"test.cpp"
下面是 CMakeLists.txt
和 test.cpp
的内容,允许构建名为 <build-dir>/Test-build/test_mongocxx
的可执行文件:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD 11)
project(Test)
option(${PROJECT_NAME}_SUPERBUILD "Build ${PROJECT_NAME} and the projects it depends on." ON)
if(${PROJECT_NAME}_SUPERBUILD)
include(ExternalProject)
set(common_cmake_cache_args
-DCMAKE_CXX_COMPILER:PATH=${CMAKE_CXX_COMPILER}
)
if(NOT DEFINED CMAKE_CONFIGURATION_TYPES)
list(APPEND common_cmake_cache_args
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
)
endif()
ExternalProject_Add(libmongoc
GIT_REPOSITORY "https://github.com/mongodb/mongo-c-driver.git"
GIT_TAG "1.12.0"
GIT_PROGRESS 1
GIT_SHALLOW 1
SOURCE_DIR "${CMAKE_BINARY_DIR}/libmongoc"
BINARY_DIR "${CMAKE_BINARY_DIR}/libmongoc-build"
INSTALL_DIR "${CMAKE_BINARY_DIR}/libmongoc-install"
CMAKE_CACHE_ARGS
${common_cmake_cache_args}
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/libmongoc-install
-DENABLE_TESTS:BOOL=OFF
-DENABLE_STATIC:BOOL=OFF
-DENABLE_EXAMPLES:BOOL=OFF
-DENABLE_EXTRA_ALIGNMENT:BOOL=OFF
#INSTALL_COMMAND ""
)
set(libmongoc-1.0_DIR "${CMAKE_BINARY_DIR}/libmongoc-install/lib/cmake/libmongoc-1.0/")
set(libbson-1.0_DIR "${CMAKE_BINARY_DIR}/libmongoc-install/lib/cmake/libbson-1.0/")
ExternalProject_Add(libmongocxx
GIT_REPOSITORY "https://github.com/mongodb/mongo-cxx-driver.git"
GIT_TAG "r3.3.1"
GIT_PROGRESS 1
GIT_SHALLOW 1
SOURCE_DIR "${CMAKE_BINARY_DIR}/libmongocxx"
BINARY_DIR "${CMAKE_BINARY_DIR}/libmongocxx-build"
INSTALL_DIR "${CMAKE_BINARY_DIR}/libmongocxx-install"
CMAKE_CACHE_ARGS
${common_cmake_cache_args}
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/libmongocxx-install
-DBUILD_SHARED_LIBS:BOOL=ON
-DENABLE_TESTS:BOOL=OFF
-DENABLE_EXAMPLES:BOOL=OFF
-DBSONCXX_POLY_USE_BOOST:BOOL=OFF
-DBSONCXX_POLY_USE_MNMLSTC:BOOL=ON
-DBSONCXX_POLY_USE_STD:BOOL=OFF
-Dlibmongoc-1.0_DIR:PATH=${libmongoc-1.0_DIR}
-Dlibbson-1.0_DIR:PATH=${libbson-1.0_DIR}
DEPENDS
libmongoc
)
set(libmongocxx_DIR "${CMAKE_BINARY_DIR}/libmongocxx-install/lib/cmake/libmongocxx-3.3.1/")
set(libbsoncxx_DIR "${CMAKE_BINARY_DIR}/libmongocxx-install//lib/cmake/libbsoncxx-3.3.1/")
function(ExternalProject_AlwaysConfigure proj)
# This custom external project step forces the configure and later
# steps to run.
_ep_get_step_stampfile(${proj} "configure" stampfile)
ExternalProject_Add_Step(${proj} forceconfigure
COMMAND ${CMAKE_COMMAND} -E remove ${stampfile}
COMMENT "Forcing configure step for '${proj}'"
DEPENDEES build
ALWAYS 1
)
endfunction()
ExternalProject_Add(${PROJECT_NAME}
SOURCE_DIR "${CMAKE_SOURCE_DIR}"
BINARY_DIR "${CMAKE_BINARY_DIR}/${PROJECT_NAME}-build"
DOWNLOAD_COMMAND ""
UPDATE_COMMAND ""
CMAKE_CACHE_ARGS
${common_cmake_cache_args}
-D${PROJECT_NAME}_SUPERBUILD:BOOL=OFF
-Dlibbsoncxx_DIR:PATH=${libbsoncxx_DIR}
-Dlibmongocxx_DIR:PATH=${libmongocxx_DIR}
INSTALL_COMMAND ""
DEPENDS
libmongocxx
)
ExternalProject_AlwaysConfigure(${PROJECT_NAME})
return()
endif()
message(STATUS "Configuring inner-build")
find_package(libmongocxx REQUIRED)
add_executable(test_mongocxx test.cpp)
target_link_libraries(test_mongocxx PUBLIC ${LIBMONGOCXX_LIBRARIES})
target_include_directories(test_mongocxx PUBLIC ${LIBMONGOCXX_INCLUDE_DIRS})
target_compile_definitions(test_mongocxx PUBLIC ${LIBMONGOCXX_DEFINITIONS})
test.cpp(从https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/#step-6-test-your-installation复制):
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
bsoncxx::builder::stream::document document{};
auto collection = conn["testdb"]["testcollection"];
document << "hello" << "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
我正在尝试在基于 CMake 的项目中构建 mongo-cxx-driver
。这个项目应该建立在 Windows、macOS 和 ubuntu 容器中,我想确保我在所有这些平台上的软件都使用相同的驱动程序版本,所以我负担不起安装驱动程序及其通过 apt-get
、brew
等依赖项。所以我只剩下一个选择:ExternalProject_Add
。但鉴于 libmongoc
的设置方式,我很难完成这项工作。
下面是我目前拥有的 CMake 模块。
include(ExternalProject)
set(libmongoc_CMAKE_ARGS
"-DCMAKE_BUILD_TYPE:STRIING=${CMAKE_BUILD_TYPE}"
"-DENABLE_TESTS:BOOL=OFF"
"-DENABLE_STATIC:BOOL=OFF"
"-DENABLE_EXAMPLES:BOOL=OFF"
"-DENABLE_EXTRA_ALIGNMENT:BOOL=OFF"
)
set(mongocxx_CMAKE_ARGS
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DCMAKE_BUILD_TYPE:STRIING=${CMAKE_BUILD_TYPE}"
"-DBUILD_SHARED_LIBS:BOOL=ON"
"-DENABLE_TESTS:BOOL=OFF"
"-DENABLE_EXAMPLES:BOOL=OFF"
"-DBSONCXX_POLY_USE_BOOST:BOOL=ON"
"-DBSONCXX_POLY_USE_MNMLSTC:BOOL=OFF"
"-Dlibbson-1.0_DIR:PATH=${OTS_DEPDENDENCIES_DIR}/libmongoc/src/libbson"
)
if (NOT TARGET libmongoc)
ExternalProject_Add(
libmongoc
GIT_REPOSITORY "https://github.com/mongodb/mongo-c-driver.git"
GIT_TAG "1.12.0"
SOURCE_DIR "${OTS_DEPDENDENCIES_DIR}/libmongoc"
BINARY_DIR "${OTS_DEPDENDENCIES_DIR}/libmongoc"
CMAKE_ARGS "${libmongoc_CMAKE_ARGS}"
INSTALL_COMMAND ""
)
endif()
if (NOT TARGET mongocxx)
ExternalProject_Add(
mongocxx
GIT_REPOSITORY "https://github.com/mongodb/mongo-cxx-driver.git"
GIT_TAG "r3.3.1"
SOURCE_DIR "${OTS_DEPDENDENCIES_DIR}/mongocxx"
BINARY_DIR "${OTS_DEPDENDENCIES_DIR}/mongocxx"
CMAKE_ARGS "${mongocxx_CMAKE_ARGS}"
INSTALL_COMMAND ""
DEPENDS libmongoc
)
endif()
请注意作为 mongo-cxx-driver
的 CMAKE_ARGS
之一给出的 CMAKE 选项 libbson-1.0_DIR
。我对此持怀疑态度,我相信这可能是罪魁祸首。有了它,我得到以下错误:
CMake Error at ${OTS_DEPENDENCIES_DIR}/libmongoc/src/libbson/libbson-1.0-config.cmake:30 (message):
File or directory
${OTS_DEPENDENCIES_DIR}/include/libbson-1.0
referenced by variable BSON_INCLUDE_DIRS does not exist !
Call Stack (most recent call first):
${OTS_DEPENDENCIES_DIR}/libmongoc/src/libbson/libbson-1.0-config.cmake:46 (set_and_check)
src/bsoncxx/CMakeLists.txt:81 (find_package)
哪一种说得通因为 src/bsoncxx/CMakeLists.txt:81
读作:
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
set_and_check (BSON_INCLUDE_DIRS "${PACKAGE_PREFIX_DIR}/include/libbson-1.0")
这使得 CMake 最终在 ${OTS_DEPDENDENCIES_DIR}/include
中寻找不存在的 libbson-1.0。如果我能告诉 cmake,"hey don't run this find_package
" 我可以给你 INCLUDE_DIR
、LIBRARIES
和 DEFINITIONS
我自己的路径。
如果删除此选项,我会收到以下错误:
CMake Error at src/bsoncxx/CMakeLists.txt:81 (find_package):
By not providing "Findlibbson-1.0.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"libbson-1.0", but CMake did not find one.
Could not find a package configuration file provided by "libbson-1.0"
(requested version 1.10.0) with any of the following names:
libbson-1.0Config.cmake
libbson-1.0-config.cmake
Add the installation prefix of "libbson-1.0" to CMAKE_PREFIX_PATH or set
"libbson-1.0_DIR" to a directory containing one of the above files. If
"libbson-1.0" provides a separate development package or SDK, be sure it
has been installed.
这也不是很奇怪,因为 CMake 尝试 find_package libbson-1.0 但无法弄清楚它的安装位置。
初步评论
在查看详细信息时,这里有一些初步评论:
SOURCE_DIR
和BINARY_DIR
使用不同的目录
- 而不是
CMAKE_ARG
,更喜欢CMAKE_CACHE_ARGS
libbson-1.0_DIR
不应设置为源目录,而应设置为包含config-file
包的构建目录(下面的 link 提供了有关此概念的更多详细信息)- 确保始终指定 CMake 参数的类型(
-DCMAKE_CXX_COMPILER:PATH=${CMAKE_CXX_COMPILER}
而不是-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
) - 不为多配置生成器设置
CMAKE_BUILD_TYPE
关于最后一点,这意味着您应该执行以下操作:
set(EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS)
if(NOT DEFINED CMAKE_CONFIGURATION_TYPES)
list(APPEND EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
)
endif()
在此 post 中,您可以了解如何构建项目:
允许编译 mongocxx 的工作项目"test.cpp"
下面是 CMakeLists.txt
和 test.cpp
的内容,允许构建名为 <build-dir>/Test-build/test_mongocxx
的可执行文件:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.12) set(CMAKE_CXX_STANDARD 11) project(Test) option(${PROJECT_NAME}_SUPERBUILD "Build ${PROJECT_NAME} and the projects it depends on." ON) if(${PROJECT_NAME}_SUPERBUILD) include(ExternalProject) set(common_cmake_cache_args -DCMAKE_CXX_COMPILER:PATH=${CMAKE_CXX_COMPILER} ) if(NOT DEFINED CMAKE_CONFIGURATION_TYPES) list(APPEND common_cmake_cache_args -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} ) endif() ExternalProject_Add(libmongoc GIT_REPOSITORY "https://github.com/mongodb/mongo-c-driver.git" GIT_TAG "1.12.0" GIT_PROGRESS 1 GIT_SHALLOW 1 SOURCE_DIR "${CMAKE_BINARY_DIR}/libmongoc" BINARY_DIR "${CMAKE_BINARY_DIR}/libmongoc-build" INSTALL_DIR "${CMAKE_BINARY_DIR}/libmongoc-install" CMAKE_CACHE_ARGS ${common_cmake_cache_args} -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/libmongoc-install -DENABLE_TESTS:BOOL=OFF -DENABLE_STATIC:BOOL=OFF -DENABLE_EXAMPLES:BOOL=OFF -DENABLE_EXTRA_ALIGNMENT:BOOL=OFF #INSTALL_COMMAND "" ) set(libmongoc-1.0_DIR "${CMAKE_BINARY_DIR}/libmongoc-install/lib/cmake/libmongoc-1.0/") set(libbson-1.0_DIR "${CMAKE_BINARY_DIR}/libmongoc-install/lib/cmake/libbson-1.0/") ExternalProject_Add(libmongocxx GIT_REPOSITORY "https://github.com/mongodb/mongo-cxx-driver.git" GIT_TAG "r3.3.1" GIT_PROGRESS 1 GIT_SHALLOW 1 SOURCE_DIR "${CMAKE_BINARY_DIR}/libmongocxx" BINARY_DIR "${CMAKE_BINARY_DIR}/libmongocxx-build" INSTALL_DIR "${CMAKE_BINARY_DIR}/libmongocxx-install" CMAKE_CACHE_ARGS ${common_cmake_cache_args} -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/libmongocxx-install -DBUILD_SHARED_LIBS:BOOL=ON -DENABLE_TESTS:BOOL=OFF -DENABLE_EXAMPLES:BOOL=OFF -DBSONCXX_POLY_USE_BOOST:BOOL=OFF -DBSONCXX_POLY_USE_MNMLSTC:BOOL=ON -DBSONCXX_POLY_USE_STD:BOOL=OFF -Dlibmongoc-1.0_DIR:PATH=${libmongoc-1.0_DIR} -Dlibbson-1.0_DIR:PATH=${libbson-1.0_DIR} DEPENDS libmongoc ) set(libmongocxx_DIR "${CMAKE_BINARY_DIR}/libmongocxx-install/lib/cmake/libmongocxx-3.3.1/") set(libbsoncxx_DIR "${CMAKE_BINARY_DIR}/libmongocxx-install//lib/cmake/libbsoncxx-3.3.1/") function(ExternalProject_AlwaysConfigure proj) # This custom external project step forces the configure and later # steps to run. _ep_get_step_stampfile(${proj} "configure" stampfile) ExternalProject_Add_Step(${proj} forceconfigure COMMAND ${CMAKE_COMMAND} -E remove ${stampfile} COMMENT "Forcing configure step for '${proj}'" DEPENDEES build ALWAYS 1 ) endfunction() ExternalProject_Add(${PROJECT_NAME} SOURCE_DIR "${CMAKE_SOURCE_DIR}" BINARY_DIR "${CMAKE_BINARY_DIR}/${PROJECT_NAME}-build" DOWNLOAD_COMMAND "" UPDATE_COMMAND "" CMAKE_CACHE_ARGS ${common_cmake_cache_args} -D${PROJECT_NAME}_SUPERBUILD:BOOL=OFF -Dlibbsoncxx_DIR:PATH=${libbsoncxx_DIR} -Dlibmongocxx_DIR:PATH=${libmongocxx_DIR} INSTALL_COMMAND "" DEPENDS libmongocxx ) ExternalProject_AlwaysConfigure(${PROJECT_NAME}) return() endif() message(STATUS "Configuring inner-build") find_package(libmongocxx REQUIRED) add_executable(test_mongocxx test.cpp) target_link_libraries(test_mongocxx PUBLIC ${LIBMONGOCXX_LIBRARIES}) target_include_directories(test_mongocxx PUBLIC ${LIBMONGOCXX_INCLUDE_DIRS}) target_compile_definitions(test_mongocxx PUBLIC ${LIBMONGOCXX_DEFINITIONS})
test.cpp(从https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/#step-6-test-your-installation复制):
#include <iostream> #include <bsoncxx/builder/stream/document.hpp> #include <bsoncxx/json.hpp> #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> int main(int, char**) { mongocxx::instance inst{}; mongocxx::client conn{mongocxx::uri{}}; bsoncxx::builder::stream::document document{}; auto collection = conn["testdb"]["testcollection"]; document << "hello" << "world"; collection.insert_one(document.view()); auto cursor = collection.find({}); for (auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; } }