Skip to content

Instantly share code, notes, and snippets.

@Caellian
Created April 26, 2024 22:42
Show Gist options
  • Save Caellian/9f92506fa24df111729589d392e84553 to your computer and use it in GitHub Desktop.
Save Caellian/9f92506fa24df111729589d392e84553 to your computer and use it in GitHub Desktop.
A CMake script that automatically fetches git submodules if they're not initalized
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
option(INIT_SUBMODULES "Check submodules during build" ON)
if(INIT_SUBMODULES)
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(GIT_SUBMOD_RESULT EQUAL "0")
message(STATUS "Submodules initialized")
else()
message(FATAL_ERROR "Unable to initalize git submodules; error: ${GIT_SUBMOD_RESULT}")
endif()
endif()
endif()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment