Skip to content

Commit

Permalink
Improve initialize macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
spahrenk committed Jul 2, 2024
1 parent 4eeb7ed commit ae45aae
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "vendor/googletest"]
[submodule "googletest"]
path = vendor/googletest
url = https://github.com/google/googletest.git
[submodule "vendor/fmt"]
[submodule "fmt"]
path = vendor/fmt
url = https://github.com/fmtlib/fmt.git
95 changes: 74 additions & 21 deletions cmake/initialize-submodules.cmake
Original file line number Diff line number Diff line change
@@ -1,36 +1,89 @@
#update the named submodule
macro(update_submodule module_name module_path)
if(NOT EXISTS "${module_path}")
message(FATAL_ERROR "Submodule directory \"${module_path}\" does not exist")
endif()

# Initialize submodule if it hasn't already been cloned
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${module_name}/.git")
message(STATUS "Initialize ${module_name} submodule")
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init "${module_path}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
message(FATAL_ERROR "git submodule update --init ${module_path} failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
endif()
endif()
endmacro()

#update the named submodule iff it is listed in .gitmodules
#attempt to add the submodule
#optional arguments:
# 1 - path
# 2 - alternate reference name
macro(add_submodule module_name)

message(STATUS "Attempting to add \"${module_name}\"")
#check of specific path is provided
set(MacroArgs ${ARGN})
list(LENGTH MacroArgs NumArgs)

set(have_submodule FALSE)
set(have_path FALSE)

# first optional argument is module path
if(NumArgs GREATER 0)
set(module_path ${ARGV1})
else()
set(module_path ${CMAKE_CURRENT_SOURCE_DIR}/${module_name})
endif()
message(${module_path})

if (NOT TARGET ${module_name})
update_submodule(${module_name} ${module_path})
add_subdirectory(${module_path})
# second optional argument is module reference
if(NumArgs GREATER 1)
set(module_ref_name ${ARGV2})
else()
set(module_ref_name ${module_name})
endif()

set(is_submodule FALSE)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
set(git_modules_file "${PROJECT_SOURCE_DIR}/.gitmodules")
if (EXISTS ${git_modules_file})
file(STRINGS ${git_modules_file} file_lines)
set(have_path FALSE)

foreach(line ${file_lines})
if (NOT is_submodule)
string(COMPARE EQUAL ${line} "[submodule \"${module_ref_name}\"]" is_submodule)
if (is_submodule)
message(STATUS "\"${module_name}\" is a submodule of this project.")
continue()
endif()
endif()

if (is_submodule AND (NOT have_path))
string(FIND ${line} "path =" pos)
set(have_path NOT(pos EQUALS -1))
if (have_path)
string(REPLACE "path =" "" submodule_path ${line})
string(STRIP "${submodule_path}" submodule_path)
set(submodule_path "${PROJECT_SOURCE_DIR}/${submodule_path}")
continue()
endif()
endif()
endforeach()

if(is_submodule)
if(have_path)
message(STATUS "Updating submodule \"${module_name}\" at ${submodule_path}")
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive "${submodule_path}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(GIT_SUBMOD_RESULT EQUAL "0")
message(STATUS "Successfully updated submodule \"${module_name}\"")
else()
message(FATAL_ERROR "Unable to update submodule \"${module_name}\"")
endif()
endif()
else()
message(STATUS "\"${module_name}\" is not a submodule of this project")
endif()

endif()
endif()

if (TARGET ${module_name})
message(STATUS "Submodule \"${module_name}\" is a target.")
else()
if(EXISTS "${module_path}")
message(STATUS "Adding subdirectory ${module_path} to this project")
add_subdirectory(${module_path})
endif()
endif()

message("")

endmacro()

0 comments on commit ae45aae

Please sign in to comment.