Skip to content

Commit b71b25b

Browse files
authored
Arm backend: Make setup.sh sourceable to run single functions (#7934)
If setup is run like `./examples/arm/setup.sh` ..., it works like before. If it is run like `source ./examples/arm/setup.sh`, the functions in the script can be run afterwards, for example you can just run `setup_toolchain` To increase modularity, I broke out creating setup_path.sh to its own function. This avoids breaking setup_path.sh if setting up the toolchain and FVP fails/ is aborted. Signed-off-by: Erik Lundell <[email protected]>
1 parent 0dd4333 commit b71b25b

File tree

1 file changed

+82
-75
lines changed

1 file changed

+82
-75
lines changed

examples/arm/setup.sh

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,15 @@
77
# This source code is licensed under the BSD-style license found in the
88
# LICENSE file in the root directory of this source tree.
99

10-
set -eu
11-
12-
if [[ "${1:-'.'}" == "-h" || "${#}" -gt 2 ]]; then
13-
echo "Usage: $(basename $0) <--i-agree-to-the-contained-eula> [path-to-a-scratch-dir]"
14-
echo "Supplied args: $*"
15-
exit 1
16-
fi
17-
18-
19-
########
20-
### Helper functions
21-
########
22-
ARCH="$(uname -m)"
23-
OS="$(uname -s)"
24-
25-
10+
set -u
2611

2712
########
2813
### Hardcoded constants
2914
########
3015
script_dir=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)
3116
et_dir=$(realpath $script_dir/../..)
17+
ARCH="$(uname -m)"
18+
OS="$(uname -s)"
3219

3320
if [[ "${ARCH}" == "x86_64" ]]; then
3421
# FVPs
@@ -80,37 +67,38 @@ tosa_reference_model_rev="v0.80.1"
8067
vela_repo_url="https://gitlab.arm.com/artificial-intelligence/ethos-u/ethos-u-vela"
8168
vela_rev="fc970e3da72e5f6930b840b357684126602b3126"
8269

83-
########
84-
### Mandatory user args
85-
########
86-
eula_acceptance="${1:-'.'}"
87-
if [[ "${eula_acceptance}" != "--i-agree-to-the-contained-eula" ]]; then
88-
if [[ ${ARM_FVP_INSTALL_I_AGREE_TO_THE_CONTAINED_EULA} != "True" ]]; then
89-
echo "Must pass first positional argument '--i-agree-to-the-contained-eula' to agree to EULA associated with downloading the FVP. Exiting!"
90-
exit 1
91-
else
92-
echo "Arm EULA for FVP agreed to with ARM_FVP_INSTALL_I_AGREE_TO_THE_CONTAINED_EULA=True environment variable"
93-
fi
94-
else
95-
shift; # drop this arg
96-
fi
9770

9871
########
9972
### Optional user args
10073
########
101-
root_dir=${1:-"${script_dir}/ethos-u-scratch"}
74+
root_dir=${2:-"${script_dir}/ethos-u-scratch"}
10275
mkdir -p ${root_dir}
10376
root_dir=$(realpath ${root_dir})
77+
setup_path_script="${root_dir}/setup_path.sh"
78+
10479

10580
########
10681
### Functions
10782
########
10883

10984
function setup_fvp() {
85+
86+
# Mandatory user arg --i-agree-to-the-contained-eula
87+
eula_acceptance="${1:-'.'}"
88+
if [[ "${eula_acceptance}" != "--i-agree-to-the-contained-eula" ]]; then
89+
if [[ ${ARM_FVP_INSTALL_I_AGREE_TO_THE_CONTAINED_EULA} != "True" ]]; then
90+
echo "Must pass first positional argument '--i-agree-to-the-contained-eula' to agree to EULA associated with downloading the FVP. Exiting!"
91+
exit 1
92+
else
93+
echo "Arm EULA for FVP agreed to with ARM_FVP_INSTALL_I_AGREE_TO_THE_CONTAINED_EULA=True environment variable"
94+
fi
95+
else
96+
shift; # drop this arg
97+
fi
11098
if [[ "${OS}" != "Linux" ]]; then
11199
echo "[${FUNCNAME[0]}] Warning: FVP only supported with Linux OS, skipping FVP setup..."
112100
echo "[${FUNCNAME[0]}] Warning: For MacOS, using https://github.com/Arm-Examples/FVPs-on-Mac is recommended."
113-
echo "[${FUNCNAME[0]}] Warning: Follow the instructions and make sure the path is set correctly."
101+
echo "[${FUNCNAME[0]}] Warning: Follow the instructions and make sure the path is set correctly."
114102
return 1
115103
fi
116104

@@ -148,17 +136,7 @@ function setup_fvp() {
148136
exit 1
149137
;;
150138
esac
151-
152-
model_dir_variable=${fvp}_model_dir
153-
fvp_model_dir=${!model_dir_variable}
154-
fvp_bin_path="$(cd models/${fvp_model_dir} && pwd)"
155-
export PATH=${PATH}:${fvp_bin_path}
156-
157-
echo "export PATH=\${PATH}:${fvp_bin_path}" >> ${setup_path_script}
158139
done
159-
160-
# Fixup for Corstone-320 python dependency
161-
echo "export LD_LIBRARY_PATH=${root_dir}/FVP-corstone320/python/lib/" >> ${setup_path_script}
162140
}
163141

164142
function setup_toolchain() {
@@ -173,10 +151,6 @@ function setup_toolchain() {
173151
echo "[${FUNCNAME[0]}] Installing toolchain ..."
174152
rm -rf "${toolchain_dir}"
175153
tar xf "${toolchain_dir}.tar.xz"
176-
toolchain_bin_path="$(cd ${toolchain_dir}/bin && pwd)"
177-
export PATH=${PATH}:${toolchain_bin_path}
178-
hash arm-none-eabi-gcc
179-
echo "export PATH=\${PATH}:${toolchain_bin_path}" >> ${setup_path_script}
180154
}
181155

182156
function setup_tosa_reference_model() {
@@ -188,48 +162,81 @@ function setup_tosa_reference_model() {
188162
}
189163

190164
function setup_vela() {
191-
#
192-
# Prepare the Vela compiler for AoT to Ethos-U compilation
193-
#
194165
pip install ethos-u-vela@git+${vela_repo_url}@${vela_rev}
195166
}
196167

168+
function setup_path() {
169+
echo $setup_path_script
170+
}
171+
172+
function create_setup_path(){
173+
echo "" > "${setup_path_script}"
174+
fvps=("corstone300" "corstone320")
175+
for fvp in "${fvps[@]}"; do
176+
model_dir_variable=${fvp}_model_dir
177+
fvp_model_dir=${!model_dir_variable}
178+
fvp_bin_path="${root_dir}/FVP-${fvp}/models/${fvp_model_dir}"
179+
echo "export PATH=\${PATH}:${fvp_bin_path}" >> ${setup_path_script}
180+
done
181+
182+
# Fixup for Corstone-320 python dependency
183+
echo "export LD_LIBRARY_PATH=${root_dir}/FVP-corstone320/python/lib/" >> ${setup_path_script}
184+
185+
toolchain_bin_path="$(cd ${toolchain_dir}/bin && pwd)"
186+
echo "export PATH=\${PATH}:${toolchain_bin_path}" >> ${setup_path_script}
187+
188+
echo "hash FVP_Corstone_SSE-300_Ethos-U55" >> ${setup_path_script}
189+
echo "hash FVP_Corstone_SSE-300_Ethos-U65" >> ${setup_path_script}
190+
echo "hash FVP_Corstone_SSE-320" >> ${setup_path_script}
191+
}
192+
197193
########
198194
### main
199195
########
200-
# do basic checks
201-
# Make sure we are on a supported platform
202-
if [[ "${ARCH}" != "x86_64" ]] && [[ "${ARCH}" != "aarch64" ]] \
203-
&& [[ "${ARCH}" != "arm64" ]]; then
204-
echo "[main] Error: only x86-64 & aarch64 architecture is supported for now!"
205-
exit 1
206-
fi
196+
# Only run this if script is executed, not if it is sourced
197+
(return 0 2>/dev/null) && is_script_sourced=1 || is_script_sourced=0
198+
if [[ $is_script_sourced -eq 0 ]]
199+
then
200+
set -e
201+
if [[ "${ARCH}" != "x86_64" ]] && [[ "${ARCH}" != "aarch64" ]] \
202+
&& [[ "${ARCH}" != "arm64" ]]; then
203+
echo "[main] Error: only x86-64 & aarch64 architecture is supported for now!"
204+
exit 1
205+
fi
207206

208-
cd "${script_dir}"
207+
# Make sure we are on a supported platform
208+
if [[ "${1:-'.'}" == "-h" || "${#}" -gt 2 ]]; then
209+
echo "Usage: $(basename $0) <--i-agree-to-the-contained-eula> [path-to-a-scratch-dir]"
210+
echo "Supplied args: $*"
211+
exit 1
212+
fi
209213

210-
# Setup the root dir
211-
cd "${root_dir}"
212-
echo "[main] Using root dir ${root_dir}"
214+
cd "${script_dir}"
213215

214-
setup_path_script="${root_dir}/setup_path.sh"
215-
echo "" > "${setup_path_script}"
216+
# Setup the root dir
217+
cd "${root_dir}"
218+
echo "[main] Using root dir ${root_dir}"
219+
220+
# Import utils
221+
source $et_dir/backends/arm/scripts/utils.sh
216222

217-
# Import utils
218-
source $et_dir/backends/arm/scripts/utils.sh
223+
# Setup FVP
224+
setup_fvp ${1:-'.'}
219225

220-
# Setup toolchain
221-
setup_toolchain
226+
# Setup toolchain
227+
setup_toolchain
222228

223-
# Setup the tosa_reference_model
224-
setup_tosa_reference_model
229+
# Create new setup_path script only if fvp and toolchain setup went well.
230+
create_setup_path
225231

226-
# Setup vela and patch in codegen fixes
227-
setup_vela
232+
# Setup the tosa_reference_model
233+
setup_tosa_reference_model
228234

229-
# Setup FVP
230-
setup_fvp
235+
# Setup vela and patch in codegen fixes
236+
setup_vela
231237

232-
echo "[main] update path by doing 'source ${setup_path_script}'"
238+
echo "[main] update path by doing 'source ${setup_path_script}'"
233239

234-
echo "[main] success!"
235-
exit 0
240+
echo "[main] success!"
241+
exit 0
242+
fi

0 commit comments

Comments
 (0)