Search code examples
cmaketargetglfw

CMake exclude submodule targets


I import glfw as submodule in my project, following the pattern described here

find_package(Git QUIET)

if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
    # Update submodules as needed
    option(GIT_SUBMODULE "Check submodules during build" ON)
    if(GIT_SUBMODULE)
        message(STATUS "Submodule update")
        execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
            WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
            RESULT_VARIABLE GIT_SUBMOD_RESULT)
        if(NOT GIT_SUBMOD_RESULT EQUAL "0")
            message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
        endif()
    endif()
endif()

## Check for GLFW
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/glfw/CMakeLists.txt")
    message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
else()
    add_subdirectory(glfw EXCLUDE_FROM_ALL)
endif()

As you can notice, I'm including the project with add_subdirectory(). This effectively adds the glfw target and I can link it with mine, but it also includes all the glfw targets (docs, examples, ...).

Surely, I'm including the project in the wrong way, I don't know.

Is there any way to include from submodule and exclude the other targets?

Apparently EXCLUDE_FROM_ALL doesn't work.


Solution

  • You can override the options defined in the GLFW CMakeLists.txt file from the command line. This will turn off the extra builds:

    cmake -DGLFW_BUILD_EXAMPLES=OFF -DGLFW_BUILD_TESTS=OFF -DGLFW_BUILD_DOCS=OFF ..
    

    If you want to override the default GLFW options by editing your top-level CMake file instead, you can do as @Cinder Biscuits suggested, but place the set() commands before the add_subdirectory() call:

    ## Check for GLFW
    if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/glfw/CMakeLists.txt")
        message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
    else()
        set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "Build GLFW without examples")
        set(GLFW_BUILD_TESTS OFF CACHE BOOL "Build GLFW without tests")
        set(GLFW_BUILD_DOCS OFF CACHE BOOL "Build GLFW without docs")
        add_subdirectory(glfw EXCLUDE_FROM_ALL)
    endif()