Search code examples
cmakedependency-managementexternal-project

How Do I Flatten A CMake Superbuild?


While arduously learning how to make CMake do what I need in Visual Studio for a cross-platform project, I learned about ExternalProject_Add and it solved all my needs beautifully. Excerpt:

ExternalProject_Add(googletest
    PREFIX "${CMAKE_BINARY_DIR}/Downloads/googletest"
    GIT_REPOSITORY "https://github.com/google/googletest.git"
    GIT_TAG 718fd88d8f145c63b8cc134cf8fed92743cc112f 
    BINARY_DIR "${CMAKE_BINARY_DIR}/Downloads/googletest/${CMAKE_CFG_INTDIR}/build"
    CMAKE_ARGS
        "-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/Dependencies/googletest"
        "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
        "-DCMAKE_DEBUG_POSTFIX=''"
        "-Dgtest_force_shared_crt=ON"
)

It pulls in several dependencies like this, and it works well in Linux, and on Windows where you can't expect to have things like LLVM installed. The problem is that I end up with a Makefile/Solution for the "superbuild", and another one for my actual project. It works well, but it's messy. I've been considering trying git modules to pull in the dependencies but I'm not sure if that'll work well. How can I reduce the complexity so there's only one build system?

The base CMakeLists.txt is at https://github.com/coder0xff/Plange/blob/master/CMakeLists.txt


Solution

  • I've updated my project to not use ExternalProject_Add at all, and instead use add_subdirectory. The trick for me was to set any relevant variables before add_subdirectory.

    function(add_googletest)
        set(BUILD_GMOCK OFF CACHE BOOL "")
        set(BUILD_GTEST ON CACHE BOOL "")
        set(INSTALL_GMOCK OFF CACHE BOOL "")
        set(INSTALL_GTEST ON CACHE BOOL "")
        set(gtest_force_shared_crt ON CACHE BOOL "Force gtest to used shared VC++ CRT")
        add_subdirectory(source/googletest)
        include_directories("${gtest_SOURCE_DIR}/include")
    endfunction(add_googletest)
    add_googletest()