Last active
March 18, 2024 14:02
-
-
Save scivision/bb1d47a9529e153617414e91ff5390af to your computer and use it in GitHub Desktop.
CMake: init/update a Git submodule and add_subdirectory()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
find_package(Git REQUIRED) | |
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) | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.19) | |
project(Demo LANGUAGES NONE) | |
include(AddGitSubmodule.cmake) | |
add_git_submodule(my_submod_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the gist 🙂🙏
I was searching for how to do this in cmakelists if user missed recursively cloning.