Skip to content

Commit 8456567

Browse files
authored
Arm backend: Refactor compiler choices in setup.sh (pytorch#13995)
Introduce helper scripts for toolchains to not clutter up setup.sh too much with all the combinations of platform, os and compiler options. Signed-off-by: [email protected]
1 parent 8607c89 commit 8456567

File tree

2 files changed

+85
-59
lines changed

2 files changed

+85
-59
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# Copyright 2025 Arm Limited and/or its affiliates.
6+
#
7+
# This source code is licensed under the BSD-style license found in the
8+
# LICENSE file in the root directory of this source tree.
9+
10+
# Important to check for unset variables since this script is always sourced from setup.sh
11+
set -u
12+
13+
# Check if the script is being sourced
14+
(return 0 2>/dev/null)
15+
if [[ $? -ne 0 ]]; then
16+
echo "Error: This script must be sourced."
17+
exit 1
18+
fi
19+
20+
function gcc_select_toolchain() {
21+
if [[ "${ARCH}" == "x86_64" ]] ; then
22+
toolchain_url="https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu/13.3.rel1/binrel/arm-gnu-toolchain-13.3.rel1-x86_64-arm-none-eabi.tar.xz"
23+
toolchain_dir="arm-gnu-toolchain-13.3.rel1-x86_64-arm-none-eabi"
24+
toolchain_md5_checksum="0601a9588bc5b9c99ad2b56133b7f118"
25+
elif [[ "${ARCH}" == "aarch64" ]] || [[ "${ARCH}" == "arm64" ]] ; then
26+
if [[ "${OS}" == "Darwin" ]]; then
27+
toolchain_url="https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu/13.3.rel1/binrel/arm-gnu-toolchain-13.3.rel1-darwin-arm64-arm-none-eabi.tar.xz"
28+
toolchain_dir="arm-gnu-toolchain-13.3.rel1-darwin-arm64-arm-none-eabi"
29+
toolchain_md5_checksum="f1c18320bb3121fa89dca11399273f4e"
30+
elif [[ "${OS}" == "Linux" ]]; then
31+
toolchain_url="https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu/13.3.rel1/binrel/arm-gnu-toolchain-13.3.rel1-aarch64-arm-none-eabi.tar.xz"
32+
toolchain_dir="arm-gnu-toolchain-13.3.rel1-aarch64-arm-none-eabi"
33+
toolchain_md5_checksum="303102d97b877ebbeb36b3158994b218"
34+
fi
35+
else
36+
# This should never happen, it should be covered by setup.sh but catch it anyway
37+
echo "[gcc_select_toolchain]: Unsupported arch!"; exit 1
38+
fi
39+
}
40+
41+
function zephyr_select_toolchain() {
42+
if [[ "${OS}" != "Linux" ]] ; then
43+
echo "[zephyr_select_toolchain] Error: Linux is only supported for zephyr!"; exit 1;
44+
fi
45+
46+
if [[ "${ARCH}" == "x86_64" ]] ; then
47+
toolchain_url="https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.17.2/toolchain_linux-x86_64_arm-zephyr-eabi.tar.xz"
48+
toolchain_dir="arm-zephyr-eabi"
49+
toolchain_md5_checksum="93128be0235cf5cf5f1ee561aa6eac5f"
50+
elif [[ "${ARCH}" == "aarch64" ]] || [[ "${ARCH}" == "arm64" ]] ; then
51+
toolchain_url="https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.17.2/toolchain_linux-aarch64_arm-zephyr-eabi.tar.xz"
52+
toolchain_dir="arm-zephyr-eabi"
53+
toolchain_md5_checksum="ef4ca56786204439a75270ba800cc64b"
54+
else
55+
# This should never happen, it should be covered by setup.sh but catch it anyway
56+
echo "[zephyr_select_toolchain]: Unsupported arch!"; exit 1
57+
fi
58+
}
59+
60+
function select_toolchain() {
61+
if [[ "${target_toolchain}" == "zephyr" ]]; then
62+
zephyr_select_toolchain
63+
else
64+
gcc_select_toolchain
65+
fi
66+
echo "[main] Info selected ${toolchain_dir} for ${ARCH} - ${OS} platform"
67+
}
68+
69+
function setup_toolchain() {
70+
# Download and install the arm toolchain (default is arm-none-eabi)
71+
# setting --target-toolchain to zephyr sets this to arm-zephyr-eabi
72+
cd "${root_dir}"
73+
if [[ ! -e "${toolchain_dir}.tar.xz" ]]; then
74+
echo "[${FUNCNAME[0]}] Downloading ${toolchain_dir} toolchain ..."
75+
curl --output "${toolchain_dir}.tar.xz" -L "${toolchain_url}"
76+
verify_md5 ${toolchain_md5_checksum} "${toolchain_dir}.tar.xz" || exit 1
77+
fi
78+
79+
echo "[${FUNCNAME[0]}] Installing ${toolchain_dir} toolchain ..."
80+
rm -rf "${toolchain_dir}"
81+
tar xf "${toolchain_dir}.tar.xz"
82+
}

examples/arm/setup.sh

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -228,63 +228,6 @@ function setup_vulkan_sdk() {
228228
fi
229229
}
230230

231-
function select_toolchain() {
232-
if [[ "${ARCH}" == "x86_64" ]]; then
233-
if [[ "${OS}" == "Linux" ]]; then
234-
if [[ "${target_toolchain}" == "zephyr" ]]; then
235-
# TODO can include support for zephyr toolchain for other host platforms later
236-
toolchain_url="https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.17.2/toolchain_linux-x86_64_arm-zephyr-eabi.tar.xz"
237-
toolchain_dir="arm-zephyr-eabi"
238-
toolchain_md5_checksum="93128be0235cf5cf5f1ee561aa6eac5f"
239-
else
240-
toolchain_url="https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu/13.3.rel1/binrel/arm-gnu-toolchain-13.3.rel1-x86_64-arm-none-eabi.tar.xz"
241-
toolchain_dir="arm-gnu-toolchain-13.3.rel1-x86_64-arm-none-eabi"
242-
toolchain_md5_checksum="0601a9588bc5b9c99ad2b56133b7f118"
243-
fi
244-
else
245-
echo "[main] Error: only Linux is currently supported for x86-64 architecture now!"; exit 1;
246-
fi
247-
elif [[ "${ARCH}" == "aarch64" ]] || [[ "${ARCH}" == "arm64" ]]; then
248-
if [[ "${OS}" == "Darwin" ]]; then
249-
if [[ "${target_toolchain}" == "zephyr" ]]; then
250-
echo "[main] Error: only Linux OS is currently supported for aarch64 architecture targeting Zephyr now!"; exit 1;
251-
else
252-
toolchain_url="https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu/13.3.rel1/binrel/arm-gnu-toolchain-13.3.rel1-darwin-arm64-arm-none-eabi.tar.xz"
253-
toolchain_dir="arm-gnu-toolchain-13.3.rel1-darwin-arm64-arm-none-eabi"
254-
toolchain_md5_checksum="f1c18320bb3121fa89dca11399273f4e"
255-
fi
256-
elif [[ "${OS}" == "Linux" ]]; then
257-
if [[ "${target_toolchain}" == "zephyr" ]]; then
258-
toolchain_url="https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.17.2/toolchain_linux-aarch64_arm-zephyr-eabi.tar.xz"
259-
toolchain_dir="arm-zephyr-eabi"
260-
toolchain_md5_checksum="ef4ca56786204439a75270ba800cc64b"
261-
else
262-
toolchain_url="https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu/13.3.rel1/binrel/arm-gnu-toolchain-13.3.rel1-aarch64-arm-none-eabi.tar.xz"
263-
toolchain_dir="arm-gnu-toolchain-13.3.rel1-aarch64-arm-none-eabi"
264-
toolchain_md5_checksum="303102d97b877ebbeb36b3158994b218"
265-
fi
266-
fi
267-
else
268-
echo "[main] Error: only x86-64 & aarch64/arm64 architecture is supported for now!"; exit 1;
269-
fi
270-
echo "[main] Info selected ${toolchain_dir} for ${ARCH} - ${OS} platform"
271-
}
272-
273-
function setup_toolchain() {
274-
# Download and install the arm toolchain (default is arm-none-eabi)
275-
# setting --target-toolchain to zephyr sets this to arm-zephyr-eabi
276-
cd "${root_dir}"
277-
if [[ ! -e "${toolchain_dir}.tar.xz" ]]; then
278-
echo "[${FUNCNAME[0]}] Downloading ${toolchain_dir} toolchain ..."
279-
curl --output "${toolchain_dir}.tar.xz" -L "${toolchain_url}"
280-
verify_md5 ${toolchain_md5_checksum} "${toolchain_dir}.tar.xz" || exit 1
281-
fi
282-
283-
echo "[${FUNCNAME[0]}] Installing ${toolchain_dir} toolchain ..."
284-
rm -rf "${toolchain_dir}"
285-
tar xf "${toolchain_dir}.tar.xz"
286-
}
287-
288231
function setup_ethos_u_tools() {
289232
CMAKE_POLICY_VERSION_MINIMUM=3.5 BUILD_PYBIND=1 pip install --no-dependencies -r $et_dir/backends/arm/requirements-arm-ethos-u.txt
290233
}
@@ -361,6 +304,7 @@ if [[ $is_script_sourced -eq 0 ]]; then
361304
# Import utils
362305
source $et_dir/backends/arm/scripts/utils.sh
363306
source $et_dir/backends/arm/scripts/fvp_utils.sh
307+
source $et_dir/backends/arm/scripts/toolchain_utils.sh
364308

365309
echo "[main]: Checking platform and os"
366310
check_platform_support
@@ -382,11 +326,11 @@ if [[ $is_script_sourced -eq 0 ]]; then
382326
echo "enable-vela=${enable_vela}"
383327
echo "mlsdk-manifest-url=${mlsdk_manifest_url}"
384328

385-
# Select appropriate toolchain
386-
select_toolchain
387329

388330
# Setup toolchain
389331
if [[ "${enable_baremetal_toolchain}" -eq 1 ]]; then
332+
# Select appropriate toolchain
333+
select_toolchain
390334
setup_toolchain
391335
fi
392336

0 commit comments

Comments
 (0)