|  | #!/bin/bash | 
|  | # | 
|  | # Copyright 2021 Google LLC | 
|  | # | 
|  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | # you may not use this file except in compliance with the License. | 
|  | # You may obtain a copy of the License at | 
|  | # | 
|  | #     https://www.apache.org/licenses/LICENSE-2.0 | 
|  | # | 
|  | # Unless required by applicable law or agreed to in writing, software | 
|  | # distributed under the License is distributed on an "AS IS" BASIS, | 
|  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | # See the License for the specific language governing permissions and | 
|  | # limitations under the License. | 
|  |  | 
|  | # Download toolchain source. | 
|  |  | 
|  |  | 
|  | if [[ -z "${ROOTDIR}" ]]; then | 
|  | echo "Source build/setup.sh first" | 
|  | exit 1 | 
|  | fi | 
|  | if [[ -z "$1" ]]; then | 
|  | echo "Usage: download-toolchain.sh <gcc dir> [<TARGET> | GCC | LLVM | KELVIN | CHERIOT]" | 
|  | exit 1 | 
|  | fi | 
|  |  | 
|  | TOOLCHAIN_TARGET=${2:-GCC} | 
|  |  | 
|  | TOOLCHAIN_GCC_SRC="$1" | 
|  | TOOLCHAIN_SRC="${OUT}/tmp/toolchain" | 
|  | LLVM_SRC="${TOOLCHAIN_SRC}/llvm-project" | 
|  |  | 
|  | # Download from the http://github.com/riscv/riscv-gnu-toolchain. For proper | 
|  | # support of GDB symbol rendering, it requires a tag points to gcc 10.2. | 
|  | # NB: the riscv-binutils has to point to upstream binutil-gdb regardless of | 
|  | # what it is in .gitmodules | 
|  | TOOLCHAIN_TAG="2021.06.18" | 
|  | TOOLCHAIN_GCC_URL="https://github.com/riscv/riscv-gnu-toolchain" | 
|  | TOOLCHAINLLVM_BINUTILS_URL="git://sourceware.org/git/binutils-gdb.git" | 
|  |  | 
|  | TOOLCHAIN_CHERIOT_TAG= | 
|  | TOOLCHAIN_CHERIOT_URL="https://github.com/CHERIoT-Platform/llvm-project" | 
|  | TOOLCHAIN_CHERIOT_SRC="${TOOLCHAIN_SRC}/cheriot-llvm-project" | 
|  |  | 
|  | if [[ ! "${TOOLCHAIN_TARGET}" == "GCC" ]] && | 
|  | [[ ! "${TOOLCHAIN_TARGET}" == "LLVM" ]] && | 
|  | [[ ! "${TOOLCHAIN_TARGET}" == "KELVIN" ]] && | 
|  | [[ ! "${TOOLCHAIN_TARGET}" == "CHERIOT" ]]; then | 
|  | echo "Unsupported toochain target: ${TOOLCHAIN_TARGET}" | 
|  | exit 1 | 
|  | fi | 
|  |  | 
|  | function git_clone() { | 
|  | local url="$1" | 
|  | local src_dir="$2" | 
|  | local opt_tag="$3" | 
|  |  | 
|  | if [[ -d "${src_dir}" ]]; then | 
|  | echo "Remove existing ${src_dir}..." | 
|  | rm -rf "${src_dir}" | 
|  | fi | 
|  |  | 
|  | if [[ -n "${opt_tag}" ]]; then | 
|  | echo "Downloading the toolchain source code from tag ${opt_tag}" | 
|  | git clone "${url}" -b "${opt_tag}" "${src_dir}" | 
|  | else | 
|  | echo "Downloading the toolchain source code" | 
|  | git clone "${url}" "${src_dir}" | 
|  | fi | 
|  | } | 
|  |  | 
|  | echo "Download toolchain for target ${TOOLCHAIN_TARGET}" | 
|  |  | 
|  | case "${TOOLCHAIN_TARGET}" in | 
|  | GCC) | 
|  | git_clone "${TOOLCHAIN_GCC_URL}" "${TOOLCHAIN_GCC_SRC}" "${TOOLCHAIN_TAG}" | 
|  |  | 
|  | # Update submodules. | 
|  | pushd "${TOOLCHAIN_GCC_SRC}" > /dev/null | 
|  | git submodule update --init --jobs=8 riscv-* | 
|  | popd > /dev/null | 
|  | ;; | 
|  | LLVM) | 
|  | git_clone "${TOOLCHAIN_GCC_URL}" "${TOOLCHAIN_GCC_SRC}" "${TOOLCHAIN_TAG}" | 
|  |  | 
|  | # Update submodules. | 
|  | pushd "${TOOLCHAIN_GCC_SRC}" > /dev/null | 
|  | git submodule update --init --jobs=8 riscv-* | 
|  |  | 
|  | cd "riscv-binutils" | 
|  | git remote set-url origin "${TOOLCHAINLLVM_BINUTILS_URL}" | 
|  | git pull -f origin master --jobs=8 --depth=1 | 
|  | git checkout FETCH_HEAD | 
|  | popd > /dev/null | 
|  |  | 
|  | # Download LLVM project if necessary. Always pull from main ToT. | 
|  | if [[ -d "${LLVM_SRC}" ]]; then | 
|  | echo "Removing existing ${LLVM_SRC}..." | 
|  | rm -rf "${LLVM_SRC}" | 
|  | fi | 
|  | mkdir -p "${LLVM_SRC}" | 
|  | pushd "${LLVM_SRC}" > /dev/null | 
|  | git init | 
|  | git remote add origin https://github.com/llvm/llvm-project | 
|  | git pull origin main --jobs=8 --depth=1 | 
|  | popd > /dev/null | 
|  | ;; | 
|  | KELVIN) | 
|  | git_clone "${TOOLCHAIN_GCC_URL}" "${TOOLCHAIN_GCC_SRC}" "${TOOLCHAIN_TAG}" | 
|  |  | 
|  | # Update submodules. | 
|  | pushd "${TOOLCHAIN_GCC_SRC}" > /dev/null | 
|  | # CentOS7 git does not support parellel jobs. | 
|  | git submodule update --init riscv-* | 
|  |  | 
|  | # Update riscv-binutils for kelvin to binutils 2.40 | 
|  | cd "riscv-binutils" | 
|  | git remote set-url origin "${TOOLCHAINLLVM_BINUTILS_URL}" | 
|  | git fetch -f origin binutils-2_40 --depth=1 | 
|  | git checkout FETCH_HEAD | 
|  | popd > /dev/null | 
|  |  | 
|  | # Patch Kelvin custom ops | 
|  | pushd "${TOOLCHAIN_GCC_SRC}/riscv-binutils" > /dev/null | 
|  | git am "${ROOTDIR}/build/patches/kelvin/0001-Kelvin-riscv-binutils-patch.patch" | 
|  | cp "${ROOTDIR}/build/patches/kelvin/kelvin-opc.h" "include/opcode/kelvin-opc.h" | 
|  | cp "${ROOTDIR}/build/patches/kelvin/kelvin-opc.c" "opcodes/kelvin-opc.c" | 
|  | popd > /dev/null | 
|  |  | 
|  | pushd "${TOOLCHAIN_GCC_SRC}/riscv-gcc" > /dev/null | 
|  | git am "${ROOTDIR}/build/patches/riscv-gcc/0001-Define-__KELVIN__.patch" | 
|  | popd > /dev/null | 
|  |  | 
|  | pushd "${TOOLCHAIN_GCC_SRC}/riscv-newlib" > /dev/null | 
|  | git am "${ROOTDIR}/build/patches/riscv-newlib/0001-Add-memcpy-kelvin.c.patch" | 
|  | popd > /dev/null | 
|  | ;; | 
|  | CHERIOT) | 
|  | git_clone "${TOOLCHAIN_CHERIOT_URL}" "${TOOLCHAIN_CHERIOT_SRC}" "${TOOLCHAIN_CHERIOT_TAG}" | 
|  | ;; | 
|  | *) | 
|  | echo "Unexpected target ${TARGET}!" | 
|  | exit -1 | 
|  | ;; | 
|  | esac |