mudlet/cmake/InitGitSubmodule.cmake
Stephen Lyons 67261edf9a
Infrastructure: allow CMake initGitSubmodule.cmake to act recursively (#8717)
#### Brief overview of PR changes/additions
Add a `RECURSIVE` option to this CMake function (`initGitSubmodule()`)
to recursively clone all the submodules of a submodule.

#### Motivation for adding to Mudlet
To allow CMake builds of Mudlet to pull in a submodule that itself
contains its own submodules. This is intended to aid a revision to the
meta-build system so that end-users will not have to clone every git
submodule themselves but instead to restore the original design that
would do it. This also means it would no longer be needed to use the
`submodules: recursive` option in the GH `actions/checkout@v6` Action -
thereby speeding things up and reducing the disk space used a bit.

#### Other info (issues closed, discussion etc)
I need this to fixup developer builds - particular on Windows - where
and when the Sentry crash-reporting functionality is NOT required given
that it uses a number of submodules and subsubmodules of its own.

As presented in this PR the new option is not actually used so should
not alter the build in any way (other than to also add the "normal" text
output from running any `git submodule update --init` action which were
previously discarded0 but it is a section of code that can be done as a
separate item.

Signed-off-by: Stephen Lyons <slysven@virginmedia.com>
2025-12-29 15:16:28 +00:00

89 lines
3.7 KiB
CMake

###########################################################################
# Copyright (C) 2019 Florian Scheel - keneanung@gmail.com #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
###########################################################################
###########################################################################
#
# Exports a function to initialize a git
# submodule if it is not there yet.
#
# Usage:
# git_submodule_init(
# CHECK_FILE "path/to/file/to/check"
# SUBMODULE_PATH "path/to/submodule"
# READABLE_NAME "submodule name"
# )
###########################################################################
find_package(Git REQUIRED QUIET)
function(git_submodule_init)
# parse readable arguments
set(OPTIONS RECURSIVE)
set(ONE_VALUE_ARGS CHECK_FILE SUBMODULE_PATH READABLE_NAME)
set(MULTI_VALUE_ARGS "") # not used
cmake_parse_arguments(GIT_SM "${OPTIONS}" "${ONE_VALUE_ARGS}"
"${MULTI_VALUE_ARGS}" ${ARGN})
# check arguments for existence
if(NOT GIT_SM_CHECK_FILE)
message(
FATAL_ERROR
"Function git_submodule_init(): Required argument 'CHECK_FILE' missing."
)
endif()
if(NOT GIT_SM_SUBMODULE_PATH)
message(
FATAL_ERROR
"Function git_submodule_init(): Required argument 'SUBMODULE_PATH' missing."
)
endif()
if(NOT GIT_SM_READABLE_NAME)
message(
FATAL_ERROR
"Function git_submodule_init(): Required argument 'READABLE_NAME' missing."
)
endif()
# actual code
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${GIT_SM_CHECK_FILE}")
set(GIT_SM_RECURSIVE_COMMAND "")
set(GIT_SM_TIMEOUT 30)
if(GIT_SM_RECURSIVE)
set(GIT_SM_RECURSIVE_COMMAND "--recursive")
# Use a longer timeout when also dealing with submodules of this submodule
set(GIT_SM_TIMEOUT 60)
message(STATUS "git submodule for ${GIT_SM_READABLE_NAME} missing from source code, will attempt to get it and, recursively, any submodules...")
else()
message(STATUS "git submodule for ${GIT_SM_READABLE_NAME} missing from source code, will attempt to get it...")
endif(GIT_SM_RECURSIVE)
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init ${GIT_SM_RECURSIVE_COMMAND} "${GIT_SM_SUBMODULE_PATH}"
TIMEOUT ${GIT_SM_TIMEOUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE result
OUTPUT_VARIABLE output_text
ERROR_VARIABLE error_text)
if(NOT result EQUAL "0")
message(FATAL_ERROR ${output_text} ${error_text})
else()
message(STATUS ${output_text})
endif()
endif()
endfunction(git_submodule_init)