How do I run cmake from cmake?

My project depends on mariadb-connector-c and I am trying to automate the download, build and linking process with cmake.

I am currently uploading a project to a directory, then I try to execute generate ninja files and run them, but I cannot run cmake at all:

execute_process(COMMAND "cmake -GNinja ." WORKING_DIRECTORY ${mariadb-connector-c_SOURCE_DIR})

      

I know this does not work because the next step that does the ninja fails:

execute_process(COMMAND "ninja" WORKING_DIRECTORY ${mariadb-connector-c_SOURCE_DIR})

      

cmake works fine in the CLI, I've tried using the full path of the cmake executable and replace the dot with the variable with the full directory (which is also a valid variable, if you're interested).

How can I tell cmake to run cmake on this external project?

+3


source to share


1 answer


You can organize a project at the top level CMakeLists.txt

to build your subprojects as ExternalProject

.

This approach requires more work and maintenance of more CMake modules, but it has its advantages. I download Google Test like this:

# Create download URL derived from version number.
set(GTEST_HOME https://github.com/google/googletest/archive)
set(GTEST_DOWNLOAD_URL ${GTEST_HOME}/release-${GTEST_VERSION}.tar.gz)
unset(GTEST_HOME)

# Download and build the Google Test library and add its properties to the third party arguments.
set(GTEST_ROOT ${THIRDPARTY_INSTALL_PATH}/gtest CACHE INTERNAL "")
ExternalProject_Add(gtest
    URL ${GTEST_DOWNLOAD_URL}
    CMAKE_ARGS -DBUILD_GTEST=ON -DBUILD_GMOCK=ON -DCMAKE_INSTALL_PREFIX=${GTEST_ROOT}
    INSTALL_COMMAND make install
)

list(APPEND GLOBAL_THIRDPARTY_LIB_ARGS "-DGTEST_ROOT:PATH=${GTEST_ROOT}")
unset(GTEST_DOWNLOAD_URL)
unset(GTEST_ROOT)

      

The abowe code is inside my module ExternalGoogleTest.cmake

, which is included by CMakeLists.txt

third party libraries:



set_directory_properties(PROPERTIES EP_BASE ${CMAKE_BINARY_DIR}/ThirdParty)
get_directory_property(THIRDPARTY_BASE_PATH EP_BASE)

set(THIRDPARTY_INSTALL_PATH ${THIRDPARTY_BASE_PATH}/Install)
set(GTEST_VERSION 1.8.0)

include(ExternalProject)
include(ExternalGoogleTest)

      

Your own project, which depends on an external library, will need the CMake module to build it as ExternalProject

. It might look like this:

ExternalProject_Add(my_project
    DEPENDS gtest whatever
    SOURCE_DIR ${CMAKE_SOURCE_DIR}/lib
    CMAKE_ARGS
        ${GLOBAL_DEFAULT_ARGS}
        ${GLOBAL_THIRDPARTY_LIB_ARGS}
        -DCMAKE_INSTALL_PREFIX=${DESIRED_INSTALL_PATH}/my_project
    BUILD_COMMAND make
)

      

You can find more tips for this pattern here .

+2


source







All Articles