Skip to content

Instantly share code, notes, and snippets.

@bwoods
Last active August 29, 2015 14:15
Show Gist options
  • Save bwoods/8adf1d5e77522c7c539c to your computer and use it in GitHub Desktop.
Save bwoods/8adf1d5e77522c7c539c to your computer and use it in GitHub Desktop.
CMake git submodule macro
function(add_submodule path url)
find_program(GIT git CACHE)
message(STATUS "git submodule ${path}")
execute_process(ERROR_QUIET WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${GIT} submodule add ${url} ${path}
COMMAND ${GIT} submodule update --init --recursive ${path})
if (NOT ARGV2 STREQUAL "") # a tag or a branch
get_filename_component(submodule_directory "${path}" ABSOLUTE)
execute_process(COMMAND ${GIT} checkout ${ARGV2} OUTPUT_QUIET WORKING_DIRECTORY ${submodule_directory} ERROR_QUIET)
endif()
get_filename_component(dirname ${path} DIRECTORY)
if (dirname STREQUAL "")
add_subdirectory(${path}) # submodule is not being nested; it should already have a CMakeLists.txt
else()
get_filename_component(submodule_cmake_file "${dirname}/CMakeLists.txt" ABSOLUTE)
if(NOT EXISTS ${submodule_cmake_file})
message(STATUS "Creating empty ${dirname}/CMakeLists.txt")
file(APPEND ${submodule_cmake_file} "cmake_minimum_required(VERSION 2.8.4)\nproject(${dirname})\n")
endif()
add_subdirectory(${dirname})
endif()
endfunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment