build: support submodule update during config
This patch adds support to update git submodules during config
phase of RMM build. Doing this within the RMM build phase means
that if there is any update of the dependency after rebase of
project, this will be catered for transparently during build.
Also, typical `git clone` command for projects with submodules
usually specify `recursive` option which then recursively
downloads the dependencies of submodules. This is a problem
when `libspdm` is included as a dependency for RMM as it has
several dependant submodules of its own and is not required
in the RMM context. So, having the RMM build system update the
submodule avoids this problem.
This also ties in with the `git patch` apply logic in RMM. The
patch application assumes a commit SHA for the dependency. With
this framework, the commit SHA can be ensured before the patch
is applied.
The submodule update is specified as shallow clone of the git
repositories to make the operation efficient.
The userguide is also updated for instructions to clone the
project.
Change-Id: Id73243e88df762eaee391d646731ec08a3e2aa4f
Signed-off-by: Soby Mathew <soby.mathew@arm.com>
diff --git a/cmake/CommonConfigs.cmake b/cmake/CommonConfigs.cmake
index d34f2b9..7a33e92 100644
--- a/cmake/CommonConfigs.cmake
+++ b/cmake/CommonConfigs.cmake
@@ -126,15 +126,7 @@
#
# Get git commit information
#
-find_package(Git)
-if(GIT_FOUND)
- execute_process(
- COMMAND ${GIT_EXECUTABLE} describe --always --dirty --tags
- WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
- OUTPUT_VARIABLE COMMIT_INFO
- OUTPUT_STRIP_TRAILING_WHITESPACE
- )
-endif()
+Git_Get_Commit_Info(COMMIT_INFO)
target_compile_definitions(rmm-common
INTERFACE "COMMIT_INFO=\"${COMMIT_INFO}\"")
diff --git a/cmake/Modules/GitUtils.cmake b/cmake/Modules/GitUtils.cmake
index bef620b..2e909bb 100644
--- a/cmake/Modules/GitUtils.cmake
+++ b/cmake/Modules/GitUtils.cmake
@@ -8,6 +8,9 @@
# @FileList_Out: All files in the Git repo in list format. Empty list
# on error
#
+
+find_package(Git)
+
function(Git_Get_All_Files FileList_Out)
if (GIT_NOT_FOUND OR NOT IS_DIRECTORY .git)
set(${FileList_Out} "" PARENT_SCOPE)
@@ -129,6 +132,8 @@
#
# Args In:
# @Git_Repo: Git repository
+#
+# Returns:
# @Patch_Files_List: List of .patch files to apply
#
function(Git_Apply_Patches Git_Repo Patch_Files_List)
@@ -167,3 +172,74 @@
endif()
endforeach()
endfunction()
+
+#
+# Retrieve the Commit Info
+#
+# Returns:
+# @CommitInfo_Out: The commit info
+#
+function(Git_Get_Commit_Info CommitInfo_Out)
+
+ if(GIT_NOT_FOUND OR NOT EXISTS "${CMAKE_SOURCE_DIR}/.git")
+ message(FATAL_ERROR "${CMAKE_SOURCE_DIR} not a git repository")
+ return()
+ endif()
+
+ execute_process(
+ COMMAND ${GIT_EXECUTABLE} describe --always --dirty --tags
+ WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+ OUTPUT_VARIABLE commit_info
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+ )
+
+ set(${CommitInfo_Out} ${commit_info} PARENT_SCOPE)
+endfunction()
+
+#
+# Check if the git repo is dirty
+#
+# Args In:
+# @Git_Repo: Git repository
+#
+# Returns:
+# @result: result of the check
+#
+function(is_git_repo_dirty Git_Repo result)
+ execute_process(
+ COMMAND git status --porcelain
+ WORKING_DIRECTORY ${Git_Repo}
+ OUTPUT_VARIABLE git_status_output
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+ )
+ if(git_status_output)
+ set(${result} TRUE PARENT_SCOPE)
+ else()
+ set(${result} FALSE PARENT_SCOPE)
+ endif()
+endfunction()
+
+#
+# Fetch the submodules mentioned in .gitmodules
+#
+function(Git_Update_Submodule)
+ if(GIT_NOT_FOUND OR NOT EXISTS "${CMAKE_SOURCE_DIR}/.git")
+ message(FATAL_ERROR "${CMAKE_SOURCE_DIR} not a git repository")
+ endif()
+
+ message(STATUS "Updating submodules")
+ execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --depth 1
+ WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+ RESULT_VARIABLE GIT_SUBMOD_RESULT)
+ if(NOT GIT_SUBMOD_RESULT EQUAL "0")
+ # TODO: can be enhanced to check all submodules which are patched.
+ if(EXISTS "${CMAKE_SOURCE_DIR}/ext/mbedtls/.git")
+ is_git_repo_dirty("${CMAKE_SOURCE_DIR}/ext/mbedtls" repo_dirty)
+ if(repo_dirty)
+ message(WARNING "The submodules are modified and cannot be updated, try deleting them.")
+ endif()
+
+ message(FATAL_ERROR "git submodule update failed with error: ${GIT_SUBMOD_RESULT}")
+ endif()
+ endif()
+endfunction()