Skip to content

Instantly share code, notes, and snippets.

@JonathanCline
Created July 7, 2022 23:50
Show Gist options
  • Save JonathanCline/2d2a6d9f29ce507f69af7cc1abda6a43 to your computer and use it in GitHub Desktop.
Save JonathanCline/2d2a6d9f29ce507f69af7cc1abda6a43 to your computer and use it in GitHub Desktop.
# Enables ADD_GIT_DEPENDENCY functionality
option(ENABLE_GIT_DEPENDENCIES "Enables automatic cloning of dependencies that do not already exit" ON)
find_package(Git QUIET)
#
# Adds a new dependency and automatically clones it if the target does not already exist.
#
# @param depPath Where to clone the repo into, must be an absolute path!
# @param depTarget Name of the target, this is used to check if the dependency already exists
# @param depRepo Path or URL to clone the repo from
# @param branchName? Name of the branch to clone, defaults to HEAD
#
function(ADD_GIT_DEPENDENCY_FN depPath depTarget depRepo)
# Ignore if target already exists
if (NOT TARGET ${depTarget})
# Add the subdir if it already exists and has a CMake support
if (EXISTS "${depPath}/CMakeLists.txt")
add_subdirectory("${depPath}")
# Check that dependency target is now defined
if (NOT TARGET ${depTarget})
message(FATAL "Cloned dependency has a CMakeLists but the dependency target was not defined!")
endif()
else()
# Only preform branch if git dependencies are allowed
if (ENABLE_GIT_DEPENDENCIES)
set(gitResult )
# Use branch optional parameter if it was provided
if (ARGC GREATER 3)
execute_process(COMMAND
${GIT_EXECUTABLE} clone -b "${ARGV3}" ${depRepo} ${depPath}
RESULTS_VARIABLE gitResult)
else()
execute_process(COMMAND
${GIT_EXECUTABLE} clone ${depRepo} ${depPath}
RESULTS_VARIABLE gitResult)
endif()
# Add the cloned repo as a subdir if it has CMake support
if (EXISTS "${depPath}/CMakeLists.txt")
add_subdirectory("${depPath}")
# Check that dependency target is now defined
if (NOT TARGET ${depTarget})
message(FATAL "Cloned dependency has a CMakeLists but the dependency target was not defined!")
endif()
endif()
endif()
endif()
endif()
endfunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment