Files
GitMap/cmake/AddGitSubmodule.cmake
2024-07-08 16:56:54 +05:30

39 lines
1.3 KiB
CMake

function(add_git_submodule relative_dir lib link)
find_package(Git REQUIRED)
set(FULL_DIR ${CMAKE_SOURCE_DIR}/${relative_dir})
#For intializing git if not already initialized.
if(NOT EXISTS "${CMAKE_SOURCE_DIR}/.git")
# If .git directory does not exist, execute git init
execute_process(
COMMAND git init
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
RESULT_VARIABLE result
)
if(result)
message(FATAL_ERROR "Failed to initialize git repository.")
endif()
endif()
#For adding the external deps if not added yet
if (NOT EXISTS ${FULL_DIR}/${lib})
execute_process(
COMMAND ${GIT_EXECUTABLE}
submodule add -- ${link}
WORKING_DIRECTORY ${FULL_DIR}
)
elseif (NOT EXISTS ${FULL_DIR}/CMakeLists.txt)
execute_process(COMMAND ${GIT_EXECUTABLE}
submodule update --init -- ${relative_dir}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
endif()
if (EXISTS ${FULL_DIR}/CMakeLists.txt)
message("Submodule is CMake Project: ${FULL_DIR}/CMakeLists.txt")
add_subdirectory(${FULL_DIR})
else()
message("Submodule is NO CMake Project: ${FULL_DIR}")
endif()
endfunction(add_git_submodule)