1#!/bin/bash
2
3#
4# Arm SCP/MCP Software
5# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
6#
7# SPDX-License-Identifier: BSD-3-Clause
8#
9
10STEPS=4
11
12SCRIPT_NAME=$(basename $0)
13TMP_FOLDER=/tmp/${SCRIPT_NAME}
14LOG_OUTPUT_FILE=${TMP_FOLDER}/out.log
15
16# helper functions
17error() {
18    >&2 echo "error: $@"
19    echo "error: $@" >> $LOG_OUTPUT_FILE
20}
21log() { echo -n "$@" >> $LOG_OUTPUT_FILE; }
22logln() { echo "$@" >> $LOG_OUTPUT_FILE; }
23lindent() { log "  $@"; }
24lindentln() { logln "  $@"; }
25println() { logln "$@"; echo "$@"; }
26indentln() { lindentln "$@"; echo "  $@"; }
27indent() { lindent "$@"; echo -n "  $@"; }
28input() { read -p $1 $2; logln "$1$$2"; }
29step() {
30    STEP=$1;
31    shift;
32    println "[$STEP/$STEPS] $@"
33}
34eexit() {
35    >&2 echo
36    >&2 echo "error: please find the complete logs in ${LOG_OUTPUT_FILE}. Aborting..."
37    exit 1
38}
39cmd() {
40    logln "$ $@"
41    eval "$@" >> $LOG_OUTPUT_FILE 2>&1
42    [[ $? -ne 0 ]] && echo "error!" && eexit
43}
44cmdp() { logln $1; echo -n "  $1..."; shift; cmd $@; echo "ok!"; }
45
46echo "Setting up temporary directory"
47mkdir -p $TMP_FOLDER
48echo -n "" > $LOG_OUTPUT_FILE
49
50# Step 1 : obtaining the GNU Arm Embedded Toolchain sysroot for later...
51
52step 1 "Obtaining the sysroot of the GNU Arm Embedded Toolchain..."
53SYSROOT=""
54
55if [[ -n $GCC_TOOLCHAIN ]]; then
56    lindentln "\$GCC_TOOLCHAIN was found set, using it..."
57    SYSROOT=${GCC_TOOLCHAIN}/arm-none-eabi
58elif command -v arm-none-eabi-gcc &> /dev/null; then
59    lindentln "arm-none-eabi-gcc was found! Fetching the sysroot..."
60    SYSROOT=$(arm-none-eabi-gcc -print-sysroot)
61else
62    indentln "Could not find the GNU Arm Embedded Toolchain!"
63    indentln
64    indentln "Please enter the path to the GNU Arm Embedded Toolchain:"
65    input "> " GCC_TOOLCHAIN
66
67    SYSROOT=${GCC_TOOLCHAIN}/arm-none-eabi
68fi
69
70if [[ ! -d $SYSROOT ]]; then
71    error "${GCC_TOOLCHAIN} is not a valid GNU Arm Embedded Toolchain path!"
72    eexit
73fi
74
75step 1 "Toolchain sysroot found at: ${SYSROOT}"
76
77# Step 2: retrieve LLVM 13
78step 2 "Installing the LLVM 13 toolchain..."
79lindentln "Checking if clang-13 is present in the path..."
80if command -v clang-13 &> /dev/null; then
81    indentln "LLVM 13 is already installed! Skipping..."
82else
83    lindentln "clang-13 was not found in the PATH. Proceeding to installation..."
84    cmdp "Downloading the LLVM installation script" wget https://apt.llvm.org/llvm.sh -O $TMP_FOLDER/install-llvm.sh
85    cmd chmod +x $TMP_FOLDER/install-llvm.sh
86    cmdp "Downloading and installing LLVM 13 (this will take a while)" $TMP_FOLDER/install-llvm.sh 13
87fi
88
89# Step 3: Retrieving Compiler-RT source code
90step 3 "Installing the Compiler-RT Arm builtins..."
91cmdp "Downloading the source code" wget https://github.com/llvm/llvm-project/releases/download/llvmorg-13.0.1/compiler-rt-13.0.1.src.tar.xz -O ${TMP_FOLDER}/rt-src.tar.xz
92cmdp "Extracting the source code" tar -xf ${TMP_FOLDER}/rt-src.tar.xz -C ${TMP_FOLDER}
93cmd cd ${TMP_FOLDER}/compiler-rt-13.0.1.src
94
95BAREMETAL_PATH=$(clang-13 -print-resource-dir)/lib/baremetal
96cmd mkdir -p $BAREMETAL_PATH
97
98for ARCH in armv7m armv7em armv8m.main armv8.1m.main; do
99    cmdp "Preparing Compiler-RT builtins for target ${ARCH}" cmake -GNinja \
100        -DLLVM_CONFIG_PATH=$(command -v llvm-config-13) \
101        -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY \
102        -DCOMPILER_RT_OS_DIR="baremetal" \
103        -DCOMPILER_RT_BUILD_BUILTINS=ON \
104        -DCOMPILER_RT_BUILD_SANITIZERS=OFF \
105        -DCOMPILER_RT_BUILD_XRAY=OFF \
106        -DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
107        -DCOMPILER_RT_BUILD_PROFILE=OFF \
108        -DCMAKE_C_COMPILER=$(command -v clang-13) \
109        -DCMAKE_C_COMPILER_TARGET="${ARCH}-none-eabi" \
110        -DCMAKE_ASM_COMPILER_TARGET="${ARCH}-none-eabi" \
111        -DCMAKE_AR=$(command -v llvm-ar-13) \
112        -DCMAKE_NM=$(command -v llvm-nm-13) \
113        -DCMAKE_RANLIB=$(command -v llvm-ranlib-13) \
114        -DCOMPILER_RT_BAREMETAL_BUILD=ON \
115        -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \
116        -DCOMPILER_RT_INCLUDE_TESTS=OFF \
117        -DCMAKE_C_FLAGS=\"-march=${ARCH} -mthumb -mfpu=none -mfloat-abi=soft -I${SYSROOT}/include\" \
118        -DCMAKE_ASM_FLAGS=\"-march=${ARCH} -mthumb -mfpu=none -mfloat-abi=soft -I${SYSROOT}/include\"
119    cmdp "Building builtins for target ${ARCH}" ninja builtins
120    cmdp "Installing the builtins" cp ./lib/baremetal/libclang_rt.builtins-arm.a ${BAREMETAL_PATH}/libclang_rt.builtins-${ARCH}.a
121done
122
123echo
124rm -rf ${TMP_FOLDER}
125