cmake: git check submodules updates

This commit is contained in:
Daniel Agar 2021-10-01 14:51:23 -04:00
parent 2adf3f959b
commit a0a0a3e983
No known key found for this signature in database
GPG Key ID: FD3CBA98017A69DE
3 changed files with 123 additions and 110 deletions

View File

@ -110,14 +110,22 @@ include(px4_parse_function_args)
#=============================================================================
# git
#
find_package(Git QUIET)
include(px4_git)
# sync submodules
execute_process(
COMMAND git describe --exclude ext/* --always --tags
OUTPUT_VARIABLE PX4_GIT_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
WORKING_DIRECTORY ${PX4_SOURCE_DIR}
)
COMMAND ${GIT_EXECUTABLE} submodule sync --recursive --quiet
OUTPUT_VARIABLE GIT_SUBMODULE_SYNC
WORKING_DIRECTORY ${PX4_SOURCE_DIR}
)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --exclude ext/* --always --tags
OUTPUT_VARIABLE PX4_GIT_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
WORKING_DIRECTORY ${PX4_SOURCE_DIR}
)
message(STATUS "PX4 version: ${PX4_GIT_TAG}")
define_property(GLOBAL PROPERTY PX4_MODULE_LIBRARIES

View File

@ -1,85 +0,0 @@
#!/usr/bin/env bash
function check_git_submodule {
# The .git exists in a submodule if init and update have been done.
if [[ -f $1"/.git" || -d $1"/.git" ]]; then
# always update within CI environment or configuring withing VSCode CMake where you can't interact
if [ "$CI" == "true" ] || [ -n "${VSCODE_PID+set}" ]; then
git submodule --quiet sync --recursive -- $1
git submodule --quiet update --init --recursive --jobs=8 -- $1 || true
git submodule --quiet update --init --recursive --jobs=8 -- $1
exit 0
fi
SUBMODULE_STATUS=$(git submodule summary "$1")
STATUSRETVAL=$(echo $SUBMODULE_STATUS | grep -A20 -i "$1")
if ! [[ -z "$STATUSRETVAL" ]]; then
echo -e "\033[31mChecked $1 submodule, ACTION REQUIRED:\033[0m"
echo ""
echo -e "Different commits:"
echo -e "$SUBMODULE_STATUS"
echo ""
echo ""
echo -e " *******************************************************************************"
echo -e " * \033[31mIF YOU DID NOT CHANGE THIS FILE (OR YOU DON'T KNOW WHAT A SUBMODULE IS):\033[0m *"
echo -e " * \033[31mHit 'u' and <ENTER> to update ALL submodules and resolve this.\033[0m *"
echo -e " * (performs \033[94mgit submodule sync --recursive\033[0m *"
echo -e " * and \033[94mgit submodule update --init --recursive\033[0m ) *"
echo -e " *******************************************************************************"
echo ""
echo ""
echo -e " Only for EXPERTS:"
echo -e " $1 submodule is not in the recommended version."
echo -e " Hit 'y' and <ENTER> to continue the build with this version. Hit <ENTER> to resolve manually."
echo -e " Use \033[94mgit add $1 && git commit -m 'Updated $1'\033[0m to choose this version (careful!)"
echo ""
read user_cmd
if [ "$user_cmd" == "y" ]; then
echo "Continuing build with manually overridden submodule.."
elif [ "$user_cmd" == "u" ]; then
git submodule sync --recursive -- $1
git submodule update --init --recursive -- $1 || true
git submodule update --init --recursive --force -- $1
echo "Submodule fixed, continuing build.."
else
echo "Build aborted."
exit 1
fi
fi
else
git submodule --quiet sync --recursive --quiet -- $1
git submodule --quiet update --init --recursive -- $1 || true
git submodule --quiet update --init --recursive -- $1
fi
}
# If called with a path then respect $GIT_SUBMODULES_ARE_EVIL but do normal processing
if [ "$#" != "0" ]; then
# called with a path then process only that path but respect $GIT_SUBMODULES_ARE_EVIL
[ -n "$GIT_SUBMODULES_ARE_EVIL" ] && {
# GIT_SUBMODULES_ARE_EVIL is set, meaning user doesn't want submodules updated
exit 0
}
check_git_submodule $1
else
[ -n "$GIT_SUBMODULES_ARE_EVIL" ] && {
# GIT_SUBMODULES_ARE_EVIL is set, meaning user doesn't want submodules updated
echo "GIT_SUBMODULES_ARE_EVIL is defined - Skipping All submodule checking!"
exit 0
}
submodules=$(git submodule status | awk '{ print $2 }')
for i in $submodules;
do
check_git_submodule $i
done
fi
exit 0

View File

@ -62,30 +62,120 @@ function(px4_add_git_submodule)
REQUIRED TARGET PATH
ARGN ${ARGN})
set(REL_PATH)
option(GIT_SUBMODULE "Check submodules during build" ON)
if(IS_ABSOLUTE ${PATH})
file(RELATIVE_PATH REL_PATH ${PX4_SOURCE_DIR} ${PATH})
else()
file(RELATIVE_PATH REL_PATH ${PX4_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/${PATH})
# ENV GIT_SUBMODULES_ARE_EVIL
# ENV TERM_PROGRAM="vscode"
# ENV VSCODE_GIT_ASKPASS_MAIN
# ENV VSCODE_GIT_ASKPASS_NODE
# ENV VSCODE_GIT_IPC_HANDLE
# ENV VSCODE_IPC_HOOK_CLI
# if CI or vscode then force submodule update?
if(DEFINED ENV{VSCODE_IPC_HOOK_CLI})
#message(STATUS "VSCODE_IPC_HOOK_CLI: $ENV{VSCODE_IPC_HOOK_CLI}")
#message(STATUS "TERM_PROGRAM: $ENV{TERM_PROGRAM}")
#message(STATUS "VSCODE_GIT_ASKPASS_MAIN: $ENV{VSCODE_GIT_ASKPASS_MAIN}")
#message(STATUS "VSCODE_GIT_ASKPASS_NODE: $ENV{VSCODE_GIT_ASKPASS_NODE}")
#message(STATUS "VSCODE_GIT_IPC_HANDLE: $ENV{VSCODE_GIT_IPC_HANDLE}")
endif()
execute_process(
COMMAND Tools/check_submodules.sh ${REL_PATH}
WORKING_DIRECTORY ${PX4_SOURCE_DIR}
)
if(GIT_FOUND AND GIT_SUBMODULE AND NOT DEFINED ENV{GIT_SUBMODULES_ARE_EVIL})
string(REPLACE "/" "_" NAME ${PATH})
string(REPLACE "." "_" NAME ${NAME})
set(REL_PATH)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/git_init_${NAME}.stamp
COMMAND Tools/check_submodules.sh ${REL_PATH}
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/git_init_${NAME}.stamp
DEPENDS ${PX4_SOURCE_DIR}/.gitmodules ${PATH}/.git
COMMENT "git submodule ${REL_PATH}"
WORKING_DIRECTORY ${PX4_SOURCE_DIR}
USES_TERMINAL
)
if(IS_ABSOLUTE ${PATH})
file(RELATIVE_PATH REL_PATH ${PX4_SOURCE_DIR} ${PATH})
else()
file(RELATIVE_PATH REL_PATH ${PX4_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/${PATH})
endif()
add_custom_target(${TARGET} DEPENDS git_init_${NAME}.stamp)
if(NOT EXISTS ${PATH}/.git)
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive --jobs=4 -- ${REL_PATH}
OUTPUT_VARIABLE GIT_SUBMODULE_UPDATE
RESULT_VARIABLE GIT_SUBMODULE_UPDATE_RESULT
WORKING_DIRECTORY ${PX4_SOURCE_DIR}
)
if(NOT GIT_SUBMODULE_UPDATE_RESULT EQUAL "0")
message(FATAL_ERROR "git submodule update --init --recursive -- ${REL_PATH} failed: ${GIT_SUBMODULE_UPDATE}")
endif()
else()
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule status -- ${REL_PATH}
OUTPUT_VARIABLE GIT_SUBMODULE_STATUS
RESULT_VARIABLE GIT_SUBMODULE_STATUS_RESULT
WORKING_DIRECTORY ${PX4_SOURCE_DIR}
)
#message(STATUS "git submodule status ${REL_PATH} result ${GIT_SUBMODULE_STATUS_RESULT} : ${GIT_SUBMODULE_STATUS}")
if(NOT GIT_SUBMODULE_STATUS_RESULT EQUAL "0")
message(FATAL_ERROR "git submodule status -- ${REL_PATH} failed: ${GIT_SUBMODULE_STATUS}")
else()
# submodule status characters
string(FIND "${GIT_SUBMODULE_STATUS}" "+" submodule_plus)
string(FIND "${GIT_SUBMODULE_STATUS}" "-" submodule_minus)
string(FIND "${GIT_SUBMODULE_STATUS}" "U" submodule_merge_conflicts)
# + commit does not match the SHA-1 found in the index
if("${submodule_plus}" EQUAL 0)
# GIT_SHA_HEAD: submodule git SHA on current HEAD
execute_process(
COMMAND ${GIT_EXECUTABLE} ls-tree -d HEAD ${REL_PATH} | awk '{print $3;}
OUTPUT_VARIABLE GIT_SHA_HEAD
WORKING_DIRECTORY ${PX4_SOURCE_DIR}
)
# GIT_SHA_PREV_HEAD: submodule git SHA on previous HEAD
execute_process(
COMMAND ${GIT_EXECUTABLE} ls-tree -d HEAD@{1} ${REL_PATH} | awk '{print $3;}
OUTPUT_VARIABLE GIT_SHA_PREV_HEAD
WORKING_DIRECTORY ${PX4_SOURCE_DIR}
)
#message(STATUS "GIT_SHA_HEAD: ${GIT_SHA_HEAD}")
#message(STATUS "GIT_SHA_PREV_HEAD: ${GIT_SHA_PREV_HEAD}")
# TODO: check if submodule is dirty?
if(${GIT_SHA_HEAD} MATCHES ${GIT_SHA_PREV_HEAD})
#message(STATUS "${rel_path} GIT SHA matched previous")
# submodule hasn't changed, assume it's safe to update
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive --jobs=4 -- ${REL_PATH}
OUTPUT_VARIABLE GIT_SUBMODULE_UPDATE
WORKING_DIRECTORY ${PX4_SOURCE_DIR}
)
else()
message(NOTICE "submodule ${REL_PATH} commit does not match the SHA-1 found in the index")
endif()
endif()
# - uninitialized
if("${submodule_minus}" EQUAL 0)
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive --jobs=4 -- ${REL_PATH}
OUTPUT_VARIABLE GIT_SUBMODULE_UPDATE
WORKING_DIRECTORY ${PX4_SOURCE_DIR}
)
endif()
# U merge conflicts
if("${submodule_merge_conflicts}" EQUAL 0)
message(NOTICE "submodule ${REL_PATH} has merge conflicts")
endif()
endif()
endif()
add_custom_target(${TARGET})
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PATH}/.git)
endif()
endfunction()