1

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.

Kevin
  • 16,549
  • 8
  • 60
  • 74
quetzalfir
  • 528
  • 7
  • 22

2 Answers2

2

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()
Kevin
  • 16,549
  • 8
  • 60
  • 74
1

From the looks of it, you just need to tell CMake not to build those by setting those options in the CACHE. add this to your CMakeLists.txt

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")

You need to specify the variable type and a description so CMake knows how to display this entry in the GUI.

Cinder Biscuits
  • 4,880
  • 31
  • 51
  • I added the lines right after `add_subdirectory(glfw EXCLUDE_FROM_ALL)`, and didn't work, still appears in project builds and cmake reserve the name "docs" for that target – quetzalfir Sep 11 '19 at 21:06
  • 1
    You have to place them before `add_subdirectory()`, otherwise they won't be read until after the subdirectory has been processed. – Cinder Biscuits Sep 12 '19 at 00:08