1#!/bin/bash
2set -u -e
3
4BASENAME=arm-gnu-toolchain
5VER=${VER:-12.2.rel1}
6HOST_ARCH=${HOST_ARCH:-$(uname -m)}
7
8DOWNLOAD_DIR=$1
9TOOLCHAIN_DIR=$2
10TOOLCHAIN_LINK_DIR=$3
11
12# These should be already created by .gitlab-ci.yml, but do here if run outside of that env
13mkdir -p $DOWNLOAD_DIR $TOOLCHAIN_DIR $TOOLCHAIN_LINK_DIR
14
15download() {
16	TRIPLE=$1
17	URL=https://developer.arm.com/-/media/Files/downloads/gnu/$VER/binrel/$BASENAME-$VER-$HOST_ARCH-$TRIPLE.tar.xz
18	wget -P $DOWNLOAD_DIR -nc $URL
19}
20
21if [ $HOST_ARCH = "aarch64" ]; then
22	# AArch64 Linux hosted cross compilers
23
24	# AArch32 target with hard float
25	download arm-none-linux-gnueabihf
26elif [ $HOST_ARCH = "x86_64" ]; then
27	# x86_64 Linux hosted cross compilers
28
29	# AArch32 target with hard float
30	download arm-none-linux-gnueabihf
31
32	# AArch64 GNU/Linux target
33	download aarch64-none-linux-gnu
34else
35	echo "ERROR - Unknown build arch of $HOST_ARCH"
36	exit 1
37fi
38
39for i in arm aarch64; do
40	if [ ! -d $TOOLCHAIN_DIR/$BASENAME-$VER-$HOST_ARCH-$i-none-linux-gnu*/ ]; then
41		if [ ! -f $DOWNLOAD_DIR/$BASENAME-$VER-$HOST_ARCH-$i-none-linux-gnu*.tar.xz ]; then
42			continue
43		fi
44
45		tar -C $TOOLCHAIN_DIR -axvf $DOWNLOAD_DIR/$BASENAME-$VER-$HOST_ARCH-$i-none-linux-gnu*.tar.xz
46	fi
47
48	# Setup a link for the toolchain to use local to the building machine (e.g., not in a shared location)
49	ln -s $TOOLCHAIN_DIR/$BASENAME-$VER-$HOST_ARCH-$i-none-linux-gnu* $TOOLCHAIN_LINK_DIR/$i
50done
51