2

I need to use mangrove (mongo ODM lib over mongo-c-driver and mongo-cxx-driver) and included this into my project as CMake ExternalProject_Add command, with a dependencies as mongo-c-driver/mongo-cxx-driver

# mongo-c-driver
ExternalProject_Add(mongo-c-driver
        GIT_REPOSITORY https://github.com/mongodb/mongo-c-driver.git
        GIT_TAG r1.12
        CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF
#        CMAKE_ARGS -DINCLUDE_DIRECTORIES=${EXTERNAL_INSTALL_LOCATION}/include/libbson-1.0 ${EXTERNAL_INSTALL_LOCATION}/include/libmongoc-1.0
        )

#include_directories(${EXTERNAL_INSTALL_LOCATION}/include/libbson-1.0
#        ${EXTERNAL_INSTALL_LOCATION}/include/libmongoc-1.0)
#link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)

include_directories command here is not really required because cmake configuration file of mongoc makes this work. However, for sure, I also checked with uncommented too.

So includes from mongo-c-driver are required by next-included external project - mangrove.

Which fails due the c++ error not found some include files, which are exist in include paths of the current project. So it seems that included external project doesn't use current CMake includes, which are previously added by another external project

# mangrove
ExternalProject_Add(mangrove
        GIT_REPOSITORY https://github.com/aospan/mangrove.git
        CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} -DCMAKE_INCLUDE_DIRECTORIES_BEFORE=${EXTERNAL_INSTALL_LOCATION}/include/libbson-1.0
        )

ExternalProject_Add_StepDependencies(mangrove build mongo-c-driver mongo-cxx-driver)

include_directories(${EXTERNAL_INSTALL_LOCATION}/include/libbson-1.0
        ${EXTERNAL_INSTALL_LOCATION}/include/libmongoc-1.0)
link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)

It seems that include_directories inside the script doesn't affect CMake-based project included by ExternalProject_Add.

Is there a technique to pass those include paths to external project?

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
amigo421
  • 2,429
  • 4
  • 26
  • 55
  • I did what I could, but I still cannot figure out some of your phrases. If the bson library is not found, set bson variables so that they get populated properly. – Matthieu Brucher Dec 29 '18 at 17:27
  • yes. bson.h is not found (error message), I tried even specify the path manually and even with absolute path to that bson.h - issue is still there, for me , it means that inclde paths in project cmakelists.txt doesn't affect cmakelists.txt added to main project by ExternalProject_Add – amigo421 Dec 29 '18 at 17:30
  • They wouldn't, it's another project. But this other project should have a proper way of populating bson include paths. That's where you should look for the proper variable. – Matthieu Brucher Dec 29 '18 at 17:31
  • so yes, the question is how to set proper variable for external project (mangove here) – amigo421 Dec 29 '18 at 17:32
  • this is my attempt to do this - line in ExternalProject_Add . but seems something wrong in this , it doesn't work : CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} -DCMAKE_INCLUDE_DIRECTORIES_BEFORE=${EXTERNAL_INSTALL_LOCATION}/include/libbson-1.0 – amigo421 Dec 29 '18 at 17:35

3 Answers3

2

You are using the wrong macro. CMAKE_INCLUDE_DIRECTORIES_BEFORE only indicates how include-directories work and is a flag.

There is no other option as to set the proper variables so that the external CMakeLists.txt uses find_path to populate the path to bson.h. If it doesn't, then I suggest you patch CMakeLists.txt to use find_path and make a pull request.

Edit:

It seems that the mangrove project has a way of setting all this with ENABLE_BSON. You can then set BSON_VERSION, BSON_INCLUDE_DIRS and BSON_LIBRARIES manually if the default AUTO doesn't work.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • you are right, seems the authors of mangrove did n't look into mongo-c cmake config - they use incorrect variables in attempts to set includes in their project. – amigo421 Dec 29 '18 at 17:52
  • Well, there is a `ENABLE_BSON=SYSTEM` that should set the variables automatically and for which you can set `CMAKE_INCLUDE_PATH`. – Matthieu Brucher Dec 29 '18 at 17:56
  • thanks for advice however I have to supply a project which should be built without pre-requisites , so this is a reason why I'm using externalproject which are downloaded and built automatically from github – amigo421 Dec 29 '18 at 18:02
0

As stated in the other answers you can't pass in the includes to the mangrove project and that the external projects and current project are all independent of each other.

That seems to be half the problem. It looks like you are trying to use mongo-c-driver to provide libbson. But mangrove is using:

set(LIBBSON_REQUIRED_VERSION 1.7.0)
set(LIBBSON_REQUIRED_ABI_VERSION 1.0)
find_package(LibBSON ${LIBBSON_REQUIRED_VERSION} REQUIRED)

What's not exactly clear is if find_package is running in module or config mode and what it's results were. But for it work successfully libbson needs to have been built when mongo-c-driver is built so that find_package can find the library and setup the include paths and the link line.

It seems that mangrove found something that allowed the find_package to not fail but didn't populate the ${LIBBSON_INCLUDE_DIRS} with the correct include path which caused the compilation error you mentioned.

Without specifics it's hard to give you a detailed answer but you need to make sure that the find_package commands in mangrove are working correctly with a built and not installed libbson or install libbson in a location that is compatible with the find_package command used by mangrove.

fdk1342
  • 3,274
  • 1
  • 16
  • 17
  • thanks for the reply. if we look into libbson-1.0-config.cmake , we can see this: set_and_check (BSON_INCLUDE_DIRS "${PACKAGE_PREFIX_DIR}/include/libbson-1.0") however mangrove cmakelists.txt uses LIBBSON_INCLUDE_DIRS for some reason (suppose the mistake), so the simple fix with a name of this variable makes mangove built successfully – amigo421 Dec 29 '18 at 21:02
  • by the way, unfortunatelly mangrove looks as still unstable and unsupported on the moment, my colleagues fixed a few mistakes in the code, you see that cmakelists is also incorrect, i found three errors there – amigo421 Dec 29 '18 at 21:05
  • @amigo421 I've always seen it as `_INCLUDE_DIRS`. In this case the package name you are looking for is `libbson` not `bson`. Not a big deal but hence the mismatch. – fdk1342 Dec 29 '18 at 22:00
0

Same issue with trying to build mongo-cxx-driver at the same time as mongo-c-driver, both as external projects in a superbuild

Fix feels a bit gnarly but it's a case of passing the includes as CPLUS_INCLUDES_PATH on the next step:

Include(ExternalProject)
ExternalProject_Add(libmongoc
        GIT_REPOSITORY  https://github.com/mongodb/mongo-c-driver.git
        GIT_TAG         [tag]
        STEP_TARGETS    build
        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}
            -DENABLE_AUTOMATIC_INIT_AND_CLEANUP:BOOL=OFF
            -DCMAKE_BUILD_TYPE:STRING=Release
            -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/libmongoc-install
            -DCMAKE_INSTALL_RPATH:PATH=${CMAKE_BINARY_DIR}/libmongoc-install
            -DENABLE_TESTS:BOOL=OFF
            -DENABLE_STATIC:BOOL=OFF
            -DENABLE_EXAMPLES:BOOL=OFF
            -DENABLE_EXTRA_ALIGNMENT:BOOL=OFF
            -DBUILD_SHARED_LIBS:BOOL=ON
)
set(libmongoc-1.0_DIR "${CMAKE_BINARY_DIR}/libmongoc-install/lib64/cmake/libmongoc-1.0")
set(libbson-1.0_DIR "${CMAKE_BINARY_DIR}/libmongoc-install/lib64/cmake/libbson-1.0")
set(libmongoc_hdrs "${CMAKE_BINARY_DIR}/libmongoc-install/include/libmongoc-1.0")
set(libbson_hdrs "${CMAKE_BINARY_DIR}/libmongoc-install/include/libbson-1.0")
set(all-includes "${libmongoc_hdrs}:${libbson_hdrs}")

ExternalProject_Add(libmongocxx
        GIT_REPOSITORY  https://github.com/mongodb/mongo-cxx-driver.git
        GIT_TAG         [tag]
        SOURCE_DIR      "${CMAKE_BINARY_DIR}/libmongocxx"
        BINARY_DIR      "${CMAKE_BINARY_DIR}/libmongocxx-build"
        INSTALL_DIR     "${CMAKE_BINARY_DIR}/libmongocxx-install"
        BUILD_COMMAND   CPLUS_INCLUDE_PATH=${all-includes} make
        CMAKE_CACHE_ARGS
            ${common_cmake_cache_args}
            -DCMAKE_BUILD_TYPE:STRING=Release
            -DMONGOC_INCLUDE_DIRS:PATH=${libmongoc_hdrs}
            -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/mongocxx-install
            -DCMAKE_INSTALL_RPATH:PATH=${CMAKE_BINARY_DIR}/mongocxx-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
)
Ron
  • 254
  • 2
  • 17