Skip to content

Commit e56be3e

Browse files
authored
Arm backend: Fix Ethos-U setup patch application (pytorch#20020)
Make fetched Ethos-U patching independent of the caller's global git identity by using a temporary identity for git am. Return patch_repo failures and make CMake stop when fetched-source patches fail, so setup cannot continue with partially patched sources. cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell @rascani Signed-off-by: Usamah Zaheer <usamah.zaheer@arm.com>
1 parent 9400da1 commit e56be3e

2 files changed

Lines changed: 50 additions & 22 deletions

File tree

backends/arm/scripts/corstone_utils.cmake

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
55

6+
function(patch_ethos_u_repo REPO_PATH BASE_REV PATCH_DIR ET_DIR_PATH)
7+
execute_process(
8+
COMMAND
9+
bash -c
10+
"source backends/arm/scripts/utils.sh && patch_repo \"$1\" \"$2\" \"$3\""
11+
patch_ethos_u_repo "${REPO_PATH}" "${BASE_REV}" "${PATCH_DIR}"
12+
WORKING_DIRECTORY "${ET_DIR_PATH}"
13+
RESULT_VARIABLE patch_result
14+
)
15+
if(patch_result)
16+
message(
17+
FATAL_ERROR "Failed to apply Ethos-U setup patches to ${REPO_PATH}."
18+
)
19+
endif()
20+
endfunction()
21+
622
function(fetch_ethos_u_content ETHOS_SDK_PATH ET_DIR_PATH)
723
message(STATUS "Fetching Ethos-U content into ${ETHOS_SDK_PATH}")
824

@@ -28,11 +44,8 @@ function(fetch_ethos_u_content ETHOS_SDK_PATH ET_DIR_PATH)
2844
# Patch manifest to remove unused projects.
2945
set(patch_dir "${ET_DIR_PATH}/examples/arm/ethos-u-setup")
3046
set(ethos_u_base_rev "26.02")
31-
execute_process(
32-
COMMAND
33-
bash -c
34-
"source backends/arm/scripts/utils.sh && patch_repo ${ETHOS_SDK_PATH} ${ethos_u_base_rev} ${patch_dir}"
35-
WORKING_DIRECTORY ${ET_DIR_PATH}
47+
patch_ethos_u_repo(
48+
"${ETHOS_SDK_PATH}" "${ethos_u_base_rev}" "${patch_dir}" "${ET_DIR_PATH}"
3649
)
3750

3851
# Get ethos_u externals only if core driver headers do not already exist.
@@ -47,11 +60,9 @@ function(fetch_ethos_u_content ETHOS_SDK_PATH ET_DIR_PATH)
4760
endif()
4861
# Patch core_software to remove unused projects.
4962
set(core_software_base_rev "26.02")
50-
execute_process(
51-
COMMAND
52-
bash -c
53-
"source backends/arm/scripts/utils.sh && patch_repo ${ETHOS_SDK_PATH}/core_software ${core_software_base_rev} ${patch_dir}"
54-
WORKING_DIRECTORY ${ET_DIR_PATH}
63+
patch_ethos_u_repo(
64+
"${ETHOS_SDK_PATH}/core_software" "${core_software_base_rev}"
65+
"${patch_dir}" "${ET_DIR_PATH}"
5566
)
5667
# Always patch the core_platform repo since this is fast enough. TODO:
5768
# examples/arm/ethos-u-setup/core_platform/0002-*.patch and 0003-*.patch are
@@ -61,11 +72,9 @@ function(fetch_ethos_u_content ETHOS_SDK_PATH ET_DIR_PATH)
6172
# ethos-u/core_platform and ${core_platform_base_rev} is bumped past those
6273
# commits, delete the 0002 and 0003 patches.
6374
set(core_platform_base_rev "26.02")
64-
execute_process(
65-
COMMAND
66-
bash -c
67-
"source backends/arm/scripts/utils.sh && patch_repo ${ETHOS_SDK_PATH}/core_platform ${core_platform_base_rev} ${patch_dir}"
68-
WORKING_DIRECTORY ${ET_DIR_PATH}
75+
patch_ethos_u_repo(
76+
"${ETHOS_SDK_PATH}/core_platform" "${core_platform_base_rev}"
77+
"${patch_dir}" "${ET_DIR_PATH}"
6978
)
7079
endfunction()
7180

backends/arm/scripts/utils.sh

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,42 @@ function patch_repo() {
114114
# Arg 2: Rev to start patching at
115115
# Arg 3: Directory 'setup-dir' containing patches in 'setup-dir/$name'
116116
# Exits with return code 1 if the number of arguments is incorrect.
117-
# Does not do any error handling if the base_rev or patch_dir is not found etc.
117+
# Returns non-zero if the repo cannot be reset or patched.
118118

119119
[[ $# -ne 3 ]] \
120120
&& { echo "[${FUNCNAME[0]}] Invalid number of args, expecting 3, but got $#"; exit 1; }
121121

122122
local repo_dir="${1}"
123123
local base_rev="${2}"
124-
local name="$(basename $repo_dir)"
124+
local name="$(basename "${repo_dir}")"
125125
local patch_dir="${3}/$name"
126+
local rc=0
126127

127128
echo -e "[${FUNCNAME[0]}] Patching ${name}. repo_dir:${repo_dir}\t base_rev:${base_rev}\t patch_dir:${patch_dir}"
128-
pushd $repo_dir > /dev/null
129-
git fetch --quiet
130-
git reset --hard ${base_rev} --quiet
129+
pushd "${repo_dir}" > /dev/null || return 1
130+
git fetch --quiet || rc=$?
131+
if [[ ${rc} -eq 0 ]]; then
132+
git reset --hard "${base_rev}" --quiet || rc=$?
133+
fi
131134

132-
[[ -e ${patch_dir} && $(ls -A ${patch_dir}) ]] && \
133-
git am -3 ${patch_dir}/*.patch
135+
if [[ ${rc} -eq 0 && -d "${patch_dir}" ]]; then
136+
local patches=("${patch_dir}"/*.patch)
137+
if [[ -e "${patches[0]}" ]]; then
138+
# git am needs an identity even though these commits stay local.
139+
git -c user.name="ExecuTorch Arm Setup" \
140+
-c user.email="executorch-arm-setup@example.invalid" \
141+
am -3 "${patches[@]}" || {
142+
rc=$?
143+
git am --abort > /dev/null 2>&1 || true
144+
}
145+
fi
146+
fi
147+
148+
if [[ ${rc} -ne 0 ]]; then
149+
echo -e "[${FUNCNAME[0]}] Failed to patch ${name} in ${repo_dir}."
150+
popd > /dev/null
151+
return "${rc}"
152+
fi
134153

135154
echo -e "[${FUNCNAME[0]}] Patched ${name} @ $(git describe --all --long 2> /dev/null) in ${repo_dir} dir."
136155
popd > /dev/null

0 commit comments

Comments
 (0)