Skip to content

Instantly share code, notes, and snippets.

@GOROman
Forked from scivision/AddGitSubmodule.cmake
Created May 5, 2022 01:48
Show Gist options
  • Save GOROman/72cb87228e636682935683e94d264f7b to your computer and use it in GitHub Desktop.
Save GOROman/72cb87228e636682935683e94d264f7b to your computer and use it in GitHub Desktop.
CMake: init/update a Git submodule and add_subdirectory()
cmake_minimum_required(VERSION 3.19)
function(add_git_submodule dir)
# add a Git submodule directory to CMake, assuming the
# Git submodule directory is a CMake project.
#
# Usage: in CMakeLists.txt
#
# include(AddGitSubmodule.cmake)
# add_git_submodule(mysubmod_dir)
find_package(Git REQUIRED)
if(NOT EXISTS ${dir}/CMakeLists.txt)
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive -- ${dir}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
endif()
add_subdirectory(${dir})
endfunction(add_git_submodule)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment