From d98ac1490ae398090c525bc4e0448c45578d4bbd Mon Sep 17 00:00:00 2001 From: Mathieu Gouttenoire Date: Thu, 4 Jul 2024 07:46:41 +0000 Subject: [PATCH] New toolchain builder script for GCC and LLVM (#2320) * Move build-toolchain.sh * New toolchain builder script for GCC and LLVM --- .gitignore | 4 +- util/gcc-toolchain-builder/build-toolchain.sh | 223 --------------- .../README.md | 36 +-- util/toolchain-builder/build-toolchain.sh | 253 ++++++++++++++++++ .../config/gcc-13.1.0-baremetal.sh | 30 +-- .../config/gcc-14.1.0-baremetal.sh | 37 +++ .../config/gcc-master-baremetal.sh | 30 +-- util/toolchain-builder/config/global.sh | 137 ++++++++++ .../config/llvm-18-baremetal.sh | 37 +++ .../config/llvm-master-baremetal.sh | 37 +++ .../get-toolchain.sh | 37 ++- 11 files changed, 555 insertions(+), 306 deletions(-) delete mode 100644 util/gcc-toolchain-builder/build-toolchain.sh rename util/{gcc-toolchain-builder => toolchain-builder}/README.md (85%) create mode 100755 util/toolchain-builder/build-toolchain.sh rename util/{gcc-toolchain-builder => toolchain-builder}/config/gcc-13.1.0-baremetal.sh (59%) create mode 100644 util/toolchain-builder/config/gcc-14.1.0-baremetal.sh rename util/{gcc-toolchain-builder => toolchain-builder}/config/gcc-master-baremetal.sh (59%) create mode 100755 util/toolchain-builder/config/global.sh create mode 100644 util/toolchain-builder/config/llvm-18-baremetal.sh create mode 100644 util/toolchain-builder/config/llvm-master-baremetal.sh rename util/{gcc-toolchain-builder => toolchain-builder}/get-toolchain.sh (83%) diff --git a/.gitignore b/.gitignore index 4ceeee9048..962e066632 100644 --- a/.gitignore +++ b/.gitignore @@ -45,8 +45,8 @@ __pycache__ .bender/ Bender.lock /tools/ -/util/gcc-toolchain-builder/src/ -/util/gcc-toolchain-builder/build/ +/util/toolchain-builder/src/ +/util/toolchain-builder/build/ # Both following lines are needed to list contents of ISA manual build dir. !/vendor/riscv/riscv-isa-manual/build/ !/vendor/riscv/riscv-isa-manual/build/* diff --git a/util/gcc-toolchain-builder/build-toolchain.sh b/util/gcc-toolchain-builder/build-toolchain.sh deleted file mode 100644 index 61fa5ea68b..0000000000 --- a/util/gcc-toolchain-builder/build-toolchain.sh +++ /dev/null @@ -1,223 +0,0 @@ -#!/bin/sh - -############################################################################# -# -# Copyright 2020-2023 Thales -# -# 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 -# -# http://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. -# -############################################################################# -# -# Original Author: Zbigniew CHAMSKI, Thales Silicon Security -# -############################################################################# - -# Paths in configuration files are relative to this directory. -ROOT_DIR=$(dirname $(readlink -f $0)) - -# Assumptions: -# - the required binutils source is in src/binutils-gdb -# - the required GCC source is in src/gcc -# - the required newlib is in src/newlib -# - the user invoking this script has sufficient permissions -# to create/populate the installation directory. -# - there are no restrictions on the parallelism of the build process -# ("make -j" without an explicit job limit causes no significant harm) -# -# Builds of individual tools are performed under the matching -# build/... subdirectories - -# Provide a means of throttling parallel 'make' executions. -# Use a conservative setting to avoid overloading the host machine. -if [ -z "$NUM_JOBS" ]; then - NUM_JOBS=1 -fi - -# Helper function to print usage information. -print_usage() -{ - echo Usage: - echo " $SHELL $0 [-h|--help]" - echo " $SHELL $0 [-f|--force] [CONFIG_NAME] INSTALL_PREFIX" - echo "" - echo " -h, --help Print this help message and exit." - echo " -f, --force Rebuild toolchain from scratch (remove build dirs," - echo " configure and build again.)" - echo " CONFIG_NAME Use configuration from file config/CONFIG_NAME.sh" - echo " (default: '$CONFIG_NAME')" - echo " INSTALL_PREFIX Path where the toolchain should be installed" - echo " (relative paths will be converted to absolute ones," - echo " missing parent directories will be created as needed.)" -} - -# Helper function to parse the cmdline args. -# Takes the complete list of positional args, drops them as they get parsed. -parse_cmdline() -{ - # Print help message and exit. - if [ $# -ge 1 ]; then - if [ $1 = "-h" -o $1 = "--help" ]; then - print_usage - exit 0 - fi - fi - - # "Force rebuild" mode: try before any file/directory names. - # Valid only with 2+ cmdline args. - if [ $# -ge 2 ]; then - if [ $1 = "-f" -o $1 = "--force" ]; then - FORCE_REBUILD=yes - shift - fi - fi - - # Check for the config name. Valid only with 2+ cmdline args left. - if [ $# -ge 2 ]; then - CONFIG_NAME=$1 - shift - fi - - # Check for the installation prefix. Must exist after dropping previous args. - if [ $# -eq 1 ]; then - # Resolve the path to an absolute one (it needs NOT to exist yet.) - PREFIX=`readlink -m "$1"` - shift - fi - - # Any remaining arg past the prefix means an error. - if [ $# -gt 0 ]; then - echo "*** Excess arguments past INSTALL_PREFIX: please correct the command line!" - echo "" - print_usage - exit 12 - fi -} - -# ======== Default settings: GCC 13.1.0 baremetal, no forced rebuild ======== -# - toolchain configuration. -# NOTE: config/$CONFIG_NAME.sh can be a symbolic link. -CONFIG_NAME="gcc-13.1.0-baremetal" - -# - rebuild mode -FORCE_REBUILD=no - -# The INSTALL_PREFIX argument is required -if [ $# -lt 1 ]; then - echo "*** Please specify the installation prefix of the toolchain!" - echo "" - print_usage; - exit 11 -fi - -# ======== Parse the command line. Drop each successfully parsed arg. ======== -echo "### Parsing the cmdline..." -parse_cmdline "$@" - -# ======== Check if config file exists, and load it if it does ======== -# Check for the presence of source code and build configuration. -CONFIG_FILE="$ROOT_DIR/config/$CONFIG_NAME.sh" - -if [ -f $CONFIG_FILE ]; then - # File present: read the settings. - . $CONFIG_FILE -else - echo "*** Configuration file '$CONFIG_FILE' missing!" - echo "" - print_usage - exit 13 -fi - -# ======== Actual build process ======== - -# Force rebuilding if asked to: remove all build directories. -[ $FORCE_REBUILD = "yes" ] && rm -rf $ROOT_DIR/build/{binutils-gdb,gcc,newlib} - -# Overall build policy: try to be as push-button as possible... -# - If a Makefile already exists, do not configure again - just build+install. -# - If there is no Makefile, run configure, then build and install. -# - If the first configure attempt failed try making 'clean' and 'distclean' -# targets. -# - In case of build error in GCC once configured, remove the target-specific -# build subdirectories and try making again. -# - binutils and GCC are built with CFLAGS and CXXFLAGS set to "-O2" -# ("production" mode: no debug, stripping disabled = 10x smaller binaries). -# - CFLAGS and CXXFLAGS are left unset for newlib. - -# Disable debug support to reduce size of executables and speed up their launching. -export CFLAGS="-O2" -export CXXFLAGS="-O2" - -# Configure and build binutils (required by GCC). -# Binutils 2.40 has an annoying bug caused by a missing 'gas/doc' -# directory ==> create it prior to launching 'make'. -[ -d $ROOT_DIR/build/binutils-gdb ] || mkdir -p $ROOT_DIR/build/binutils-gdb -cd $ROOT_DIR/build/binutils-gdb -[ -f Makefile ] || \ - ../../$BINUTILS_DIR/configure $BINUTILS_CONFIGURE_OPTS || \ - { [ -f Makefile ] && make clean && make distclean && \ - ../../$BINUTILS_DIR/configure $BINUTILS_CONFIGURE_OPTS || \ - { echo "Could not configure binutils-gdb, bailing out!" ; \ - exit 2 ; } ; } && \ - { [ -d gas/doc ] || mkdir -p gas/doc; } && \ - make -j"$NUM_JOBS" all && make install || \ - { echo "*** Could not build binutils, bailing out!" ; exit 2; } -cd - - -# Configure and build GCC (required by newlib). -# If an initial configure failed (e.g., due to a change in PREFIX), -# try making 'distclean' target and configuring again. -# The target-specific subdirectories configured during *build* -# are simply removed in case of build error, and 'make all' is -# then restarted. -[ -d $ROOT_DIR/build/gcc ] || mkdir -p $ROOT_DIR/build/gcc -cd $ROOT_DIR/build/gcc -[ -f Makefile ] || \ - ../../$GCC_DIR/configure $GCC_CONFIGURE_OPTS || \ - { [ -f Makefile ] && make clean && make distclean && \ - make -C libcc1 distclean && \ - ../../$GCC_DIR/configure $GCC_CONFIGURE_OPTS || \ - { echo "Could not configure GCC, bailing out!" ; \ - exit 2 ; } ; } && \ - make -j"$NUM_JOBS" all || { rm -rf $TARGET && \ - make -j"$NUM_JOBS" all ; } && make install || \ - { echo "*** Could not build GCC (even after removing target dirs), bailing out!" ; exit 2; } -cd - - -# Unset the variables forced for binutils and GCC builds. -unset CFLAGS CXXFLAGS - -# Configure and build newlib. - -# We need the path to the newly installed tools -# when running 'configure' and building newlib. -[ -d $ROOT_DIR/build/newlib ] || mkdir -p $ROOT_DIR/build/newlib -cd $ROOT_DIR/build/newlib -export PATH="$PREFIX/bin:$PATH" - -# Assume a fully capable code model (medium) -export CFLAGS="-mcmodel=medium" - -# If an initial configure failed, try making 'distclean' target -# and configuring again. -[ -f Makefile ] || \ - ../../$NEWLIB_DIR/configure $NEWLIB_CONFIGURE_OPTS || \ - { [ -f Makefile ] && make clean && make distclean && \ - ../../$NEWLIB_DIR/configure $NEWLIB_CONFIGURE_OPTS || \ - { echo "Could not configure newlib, bailing out!" ; \ - exit 2 ; } ; } && \ - make -j"$NUM_JOBS" all && make install || \ - { echo "*** Could not build newlib, bailing out!" ; exit 2; } -cd - - -# Exit happily. -exit 0 diff --git a/util/gcc-toolchain-builder/README.md b/util/toolchain-builder/README.md similarity index 85% rename from util/gcc-toolchain-builder/README.md rename to util/toolchain-builder/README.md index 3da6b1903d..2a1bf35d00 100644 --- a/util/gcc-toolchain-builder/README.md +++ b/util/toolchain-builder/README.md @@ -1,8 +1,8 @@ -# `gcc-toolchain-builder`: Basic scripts for building a RISC-V GCC compiler toolchain +# `toolchain-builder`: Basic scripts for building a RISC-V GCC or LLVM compiler toolchain ## Overview -This directory contains basic scripts for building local instances of CORE-V GCC toolchains. +This directory contains basic scripts for building local instances of CORE-V GCC or LLVM toolchains. The scripts provide the means of fetching the source code and building the executables and libraries for well-defined toolchain configurations. The intention is to simplify the processs of building such toolchains and make it as "push-button" @@ -14,6 +14,7 @@ These configurations are deliberately lightweight and consist of: * `binutils-gdb`: assembler, linker, GDB debugger, and object file utilities * `GCC`: the GNU GCC compiler configured for C only +* `LLVM`: the LLVM compiler infrastructure * `newlib`: an open-source C library suitable for embedded applications. ## Prerequisites @@ -25,7 +26,7 @@ from source code: * 1.1 GB is needed for the build space; * 0.5 GB is needed for the installed toolchain. -Several **standard packages** are needed to build the GCC-based compiler +Several **standard packages** are needed to build the compiler toolchains. On Debian/Ubuntu, executing the following command should suffice: $ sudo apt-get install autoconf automake autotools-dev curl git libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool bc zlib1g-dev @@ -41,7 +42,7 @@ On macOS, you can use [Homebrew](http://brew.sh) to install the dependencies: ## Getting started Once the prerequisites (see [above](#prerequisites)) are satisfied, you can fetch and build the -upstream GCC toolchain (default: 13.1.0) for bare-metal 32-bit and 64-bit applications in just three steps. +upstream toolchain (default: GCC 13.1.0) for bare-metal 32-bit and 64-bit applications in just three steps. # 1. Select an installation location for the toolchain (here: the default RISC-V tooling directory $RISCV). INSTALL_DIR=$RISCV @@ -66,11 +67,11 @@ different toolchain components in suitable order. In the process of building the toolchain, two new directory trees are created under the current working directory: - * `src/`: Source code is fetched and checked out into subdirectories of `src` in - the current working directory. + * `SRC_DIR`: Source code is fetched and checked out into subdirectories of `$SRC_DIR`, which + defaults to `src/` in the current working directory when it is not set. - * `build/`: The building of the various components of the toolchain occurs in - subdirectories of `build` in the current working directory. + * `BUILD_DIR`: The building of the various components of the toolchain occurs in subdirectories + of `$BUILD_DIR`, which defaults to `build/` in the current working directory when it is not set. This directory structure was chosen to keep the source and build directories local to the user's workspace while supporting systematic out-of-source-tree @@ -89,12 +90,12 @@ yet: any missing directories will be created during the building process. _The u `build-toolchain.sh` script must have sufficient permissions to create the missing directories of the installation location._ -Once a configuration name `CONFIG_NAME` and an installation location -`INSTALL_LOCATION` are chosen, use +Once a configuration name `CONFIG_NAME` and an installation +location `INSTALL_DIR` are chosen, use sh get-toolchain.sh CONFIG_NAME # E.g., - # sh get-toolchain.sh gcc-10.2.0-baremetal + # sh get-toolchain.sh gcc-13.1.0-baremetal to fetch/update the source code and to check out the matching baseline of code. @@ -112,6 +113,13 @@ To build the toolchain from the retrieved source baseline, use # E.g., # sh build-toolchain.sh gcc-13.1.0-baremetal $RISCV +To speedup the building it is recommended to set the number of threads to use + + # Use all available threads + export NUM_JOBS=$(nproc) + # Use 8 threads + export NUM_JOBS=8 + The `build-toolchain.sh` script incorporates fallbacks for several commonly encountered configuration and build issues. However, it is not meant to auto-detect major reconfigurations of source code such as a change of baseline configuration. _Whenever the source @@ -144,15 +152,13 @@ adjusting the values of per-component variables. Taking `GCC` as an example: `get-toolchain.sh` will update the local repository to the tip of the remote branch at every invocation._ - * `GCC_CONFIGURE_OPTS` is the list of options to pass to the configure script. \ - _**NOTE:** Since `GCC_CONFIGURE_OPTS` is a Bourne shell variable, any double-quotes in - the option list must be duly escaped to be correctly handled by the shell._ + * `GCC_CONFIGURE_OPTS` is the list of options to pass to the configure script. + It is located in `config/global.sh`. ## Potential additions Several extensions are envisioned: * Explicit selection of GDB version -* Addition of LLVM/Clang compilers * Support for Linux-based target environments * Addition of full-featured C library implementations diff --git a/util/toolchain-builder/build-toolchain.sh b/util/toolchain-builder/build-toolchain.sh new file mode 100755 index 0000000000..59d027ac06 --- /dev/null +++ b/util/toolchain-builder/build-toolchain.sh @@ -0,0 +1,253 @@ +#!/bin/bash + +############################################################################# +# +# Copyright 2024 Thales DIS France SAS +# +# 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 +# +# http://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. +# +############################################################################# +# +# Original Author: Zbigniew CHAMSKI, Thales Silicon Security +# +# Adapted by Mathieu Gouttenoire, Thales DIS France SAS +# +############################################################################# + +# Assumptions: +# - the required binutils source is in src/binutils-gdb +# - the required GCC source is in src/gcc +# - the required LLVM source is in src/llvm-project +# - the required newlib is in src/newlib +# - the user invoking this script has sufficient permissions +# to create/populate the installation directory. +# - there are no restrictions on the parallelism of the build process +# ("make -j" without an explicit job limit causes no significant harm) + + +# Helper function to print usage information. +print_usage() { + echo Usage: + echo " $SHELL $0 [-h|--help]" + echo " $SHELL $0 [-f|--force] [CONFIG_NAME] INSTALL_DIR" + echo "" + echo " -h, --help Print this help message and exit." + echo " -f, --force Rebuild toolchain from scratch (remove build dirs," + echo " configure and build again.)" + echo " CONFIG_NAME Use configuration from file config/CONFIG_NAME.sh" + echo " (default: '$CONFIG_NAME')" + echo " INSTALL_DIR Path where the toolchain should be installed" + echo " (relative paths will be converted to absolute ones," + echo " missing parent directories will be created as needed.)" +} + +# Helper function to parse the cmdline args. +# Takes the complete list of cmdline args, drops them as they get parsed. +parse_cmdline() { + + POSITIONAL_ARGS=() + + while [ $# -gt 0 ]; do + case $1 in + -h|--help) + print_usage + exit 0 + ;; + -f|--force) + FORCE_REBUILD=yes + shift + ;; + -*|--*) + echo "*** Unknown option $1" + print_usage + exit 1 + ;; + *) + POSITIONAL_ARGS+=("$1") + shift + ;; + esac + done + + case ${#POSITIONAL_ARGS[@]} in + 0) + echo "*** Argument INSTALL_DIR is missing!" + print_usage + exit 1 + ;; + 1) + export INSTALL_DIR="${POSITIONAL_ARGS[0]}" + ;; + 2) + CONFIG_NAME="${POSITIONAL_ARGS[0]}" + export INSTALL_DIR="${POSITIONAL_ARGS[1]}" + ;; + *) + echo "*** Too may arguments were given!" + print_usage + exit 1 + ;; + esac +} + + + +build_binutils() { + # Create build directory + mkdir -p "$BUILD_DIR/binutils-$1" + cd "$BUILD_DIR/binutils-$1" + [ -d gas/doc ] || mkdir -p gas/doc # Binutils 2.40 has a bug caused by a missing 'gas/doc' + + [ -f Makefile ] || CFLAGS="-O2" CXXFLAGS="-O2" \ + $SRC_DIR/$BINUTILS_DIR/configure $(BINUTILS_CONFIGURE_OPTS $1) + make -j$NUM_JOBS + make install + + # Add symlinks for riscv64 triple + shopt -s nullglob + cd "$INSTALL_DIR/bin" + for TOOL in riscv32-unknown-elf-*; do + ln -sv ${TOOL} riscv64-unknown-elf-${TOOL#riscv32-unknown-elf-} + done +} + + +build_gcc() { + # Create build directory + mkdir -p "$BUILD_DIR/gcc" + cd "$BUILD_DIR/gcc" + + [ -f Makefile ] || CFLAGS="-O2" CXXFLAGS="-O2" \ + $SRC_DIR/$GCC_DIR/configure $(GCC_CONFIGURE_OPTS) + make -j$NUM_JOBS + make install +} + + +build_llvm() { + # Create build directory + mkdir -p "$BUILD_DIR/llvm" + cd "$BUILD_DIR/llvm" + + [ -f Makefile ] || cmake $SRC_DIR/$LLVM_DIR/llvm $(LLVM_CONFIGURE_OPTS) + make -j$NUM_JOBS + make install-distribution + + # Add symlinks to LLVM tools + cd "$INSTALL_DIR/bin" + for TOOL in clang clang++ cc c++; do + ln -sv clang riscv32-unknown-elf-$TOOL + ln -sv clang riscv64-unknown-elf-$TOOL + done +} + + +build_newlib() { + # Create build directory + PATH=${INSTALL_DIR}/bin:${PATH} + mkdir -p "$BUILD_DIR/newlib-$1" + cd "$BUILD_DIR/newlib-$1" + + [ -f Makefile ] || CFLAGS_FOR_TARGET="-O2 -mcmodel=medany -Wno-unused-command-line-argument -Wno-implicit-function-declaration -Wno-int-conversion" \ + $SRC_DIR/$NEWLIB_DIR/configure $(NEWLIB_CONFIGURE_OPTS $1) + make -j$NUM_JOBS + make install +} + + +build_compiler_rt() { + # Create build directory + mkdir -p "$BUILD_DIR/compiler-rt-$1" + cd "$BUILD_DIR/compiler-rt-$1" + + COMPILER_RT_CONFIGURE_OPTS $1 + [ -f Makefile ] || cmake $SRC_DIR/$LLVM_DIR/compiler-rt $(COMPILER_RT_CONFIGURE_OPTS $1) + make -j$NUM_JOBS + make install +} + + +build_gcc_toolchain() { + [ $FORCE_REBUILD = "yes" ] && rm -rf $BUILD_DIR/{gcc,*-none-elf} + + echo "### Building Binutils ..." + build_binutils riscv-none-elf + + echo "### Building GCC ..." + build_gcc + + echo "### Building Newlib ..." + build_newlib riscv-none-elf +} + + +build_llvm_toolchain() { + [ $FORCE_REBUILD = "yes" ] && rm -rf $BUILD_DIR/{llvm,*-unknown-elf} + + echo "### Building Binutils ..." + build_binutils riscv32-unknown-elf + + echo "### Building LLVM ..." + build_llvm + + echo "### Building Newlib 32 bits ..." + build_newlib riscv32-unknown-elf + + echo "### Building Newlib 64 bits ..." + build_newlib riscv64-unknown-elf + + echo "### Building Compiler-RT 32 bits ..." + build_compiler_rt riscv32-unknown-elf + + echo "### Building Compiler-RT 64 bits ..." + build_compiler_rt riscv64-unknown-elf +} + + + +# Absolute path of the toolchain-builder directory +export ROOT_DIR=$(dirname $(readlink -f $0)) + +# ======== Default settings: GCC 13.1.0 baremetal, no forced rebuild ======== +# - toolchain configuration. +# NOTE: config/$CONFIG_NAME.sh can be a symbolic link. +CONFIG_NAME="gcc-13.1.0-baremetal" + +# - rebuild mode +FORCE_REBUILD=no + + +echo "### Parsing the cmdline..." +parse_cmdline "$@" + +# Load global config +. $ROOT_DIR/config/global.sh + +# Make sure the install directory exists and is empty +if [ -n "$(ls -A $INSTALL_DIR 2>/dev/null)" ]; then + echo "Install directory $INSTALL_DIR is not empty!" + exit 1 +else + mkdir -p "$INSTALL_DIR" +fi + +if [[ $CONFIG_NAME == "gcc"* ]]; then + build_gcc_toolchain +else + build_llvm_toolchain +fi + + +# Exit happily. +exit 0 + diff --git a/util/gcc-toolchain-builder/config/gcc-13.1.0-baremetal.sh b/util/toolchain-builder/config/gcc-13.1.0-baremetal.sh similarity index 59% rename from util/gcc-toolchain-builder/config/gcc-13.1.0-baremetal.sh rename to util/toolchain-builder/config/gcc-13.1.0-baremetal.sh index d48eb3cbe0..3ee75b06d4 100644 --- a/util/gcc-toolchain-builder/config/gcc-13.1.0-baremetal.sh +++ b/util/toolchain-builder/config/gcc-13.1.0-baremetal.sh @@ -1,6 +1,6 @@ ############################################################################# # -# Copyright 2020-2023 Thales +# Copyright 2024 Thales DIS France SAS # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -18,44 +18,20 @@ # # Original Author: Zbigniew CHAMSKI, Thales Silicon Security # +# Adapted by Mathieu Gouttenoire, Thales DIS France SAS +# ############################################################################# -# Name of the target to use for the toolchain. -export TARGET=riscv-none-elf - -# ======= Source code setup: path, repo, commit, configure options ======== # Each *_COMMIT variable can designate any valid 'commit-ish' entity: # typically a tag, a commit or the output of "git describe" of a Git tree. # Binutils -BINUTILS_DIR=src/binutils-gdb -BINUTILS_REPO=https://sourceware.org/git/binutils-gdb.git BINUTILS_COMMIT=binutils-2_40 -BINUTILS_CONFIGURE_OPTS="\ - --prefix=$PREFIX \ - --target=$TARGET \ - --disable-nls \ - --disable-werror" # GCC -GCC_DIR=src/gcc -GCC_REPO=https://github.com/gcc-mirror/gcc.git GCC_COMMIT=releases/gcc-13.1.0 -GCC_CONFIGURE_OPTS="\ - --prefix=$PREFIX \ - --target=$TARGET \ - --enable-languages=c \ - --disable-libssp \ - --disable-libgomp \ - --disable-libmudflap" # newlib -NEWLIB_DIR=src/newlib -NEWLIB_REPO=https://sourceware.org/git/newlib-cygwin.git NEWLIB_COMMIT=newlib-4.3.0 -NEWLIB_CONFIGURE_OPTS="\ - --prefix=$PREFIX \ - --target=$TARGET \ - --enable-multilib" diff --git a/util/toolchain-builder/config/gcc-14.1.0-baremetal.sh b/util/toolchain-builder/config/gcc-14.1.0-baremetal.sh new file mode 100644 index 0000000000..08b7d7601a --- /dev/null +++ b/util/toolchain-builder/config/gcc-14.1.0-baremetal.sh @@ -0,0 +1,37 @@ +############################################################################# +# +# Copyright 2024 Thales DIS France SAS +# +# 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 +# +# http://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. +# +############################################################################# +# +# Original Author: Zbigniew CHAMSKI, Thales Silicon Security +# +# Adapted by Mathieu Gouttenoire, Thales DIS France SAS +# +############################################################################# + + +# Each *_COMMIT variable can designate any valid 'commit-ish' entity: +# typically a tag, a commit or the output of "git describe" of a Git tree. + +# Binutils +BINUTILS_COMMIT=binutils-2_41 + +# GCC +GCC_COMMIT=releases/gcc-14.1.0 + +# newlib +NEWLIB_COMMIT=newlib-4.4.0 + diff --git a/util/gcc-toolchain-builder/config/gcc-master-baremetal.sh b/util/toolchain-builder/config/gcc-master-baremetal.sh similarity index 59% rename from util/gcc-toolchain-builder/config/gcc-master-baremetal.sh rename to util/toolchain-builder/config/gcc-master-baremetal.sh index 9ddf7af49a..bd722ec168 100644 --- a/util/gcc-toolchain-builder/config/gcc-master-baremetal.sh +++ b/util/toolchain-builder/config/gcc-master-baremetal.sh @@ -1,6 +1,6 @@ ############################################################################# # -# Copyright 2020-2023 Thales +# Copyright 2024 Thales DIS France SAS # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -18,44 +18,20 @@ # # Original Author: Zbigniew CHAMSKI, Thales Silicon Security # +# Adapted by Mathieu Gouttenoire, Thales DIS France SAS +# ############################################################################# -# Name of the target to use for the toolchain. -export TARGET=riscv-none-elf - -# ======= Source code setup: path, repo, commit, configure options ======== # Each *_COMMIT variable can designate any valid 'commit-ish' entity: # typically a tag, a commit or the output of "git describe" of a Git tree. # Binutils -BINUTILS_DIR=src/binutils-gdb -BINUTILS_REPO=https://sourceware.org/git/binutils-gdb.git BINUTILS_COMMIT=binutils-2_41 -BINUTILS_CONFIGURE_OPTS="\ - --prefix=$PREFIX \ - --target=$TARGET \ - --disable-nls \ - --disable-werror" # GCC -GCC_DIR=src/gcc -GCC_REPO=https://github.com/gcc-mirror/gcc.git GCC_COMMIT=master -GCC_CONFIGURE_OPTS="\ - --prefix=$PREFIX \ - --target=$TARGET \ - --enable-languages=c \ - --disable-libssp \ - --disable-libgomp \ - --disable-libmudflap" # newlib -NEWLIB_DIR=src/newlib -NEWLIB_REPO=https://sourceware.org/git/newlib-cygwin.git NEWLIB_COMMIT=newlib-4.3.0 -NEWLIB_CONFIGURE_OPTS="\ - --prefix=$PREFIX \ - --target=$TARGET \ - --enable-multilib" diff --git a/util/toolchain-builder/config/global.sh b/util/toolchain-builder/config/global.sh new file mode 100755 index 0000000000..dab7b0cba3 --- /dev/null +++ b/util/toolchain-builder/config/global.sh @@ -0,0 +1,137 @@ +#!/bin/bash + +############################################################################# +# +# Copyright 2024 Thales DIS France SAS +# +# 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 +# +# http://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. +# +############################################################################# +# +# Original Author: Zbigniew CHAMSKI, Thales Silicon Security +# +# Adapted by Mathieu Gouttenoire, Thales DIS France SAS +# +############################################################################# + + +# Set the default build directory to build/ if it isn't already set +if [ -z "$BUILD_DIR" ]; then + export BUILD_DIR="$ROOT_DIR/build" +fi + +# Set the default source directory to src/ if it isn't already set +if [ -z "$SRC_DIR" ]; then + export SRC_DIR="$ROOT_DIR/src" +fi + +# Provide a means of throttling parallel 'make' executions. +# Use a conservative setting to avoid overloading the host machine. +if [ -z "$NUM_JOBS" ]; then + NUM_JOBS=1 +fi + + +# Name of the source directories +BINUTILS_DIR=binutils-gdb +GCC_DIR=gcc +LLVM_DIR=llvm-project +NEWLIB_DIR=newlib + +# Address of the Git repositories +BINUTILS_REPO=https://sourceware.org/git/binutils-gdb.git +GCC_REPO=https://github.com/gcc-mirror/gcc.git +LLVM_REPO=https://github.com/llvm/llvm-project.git +NEWLIB_REPO=https://sourceware.org/git/newlib-cygwin.git + + +BINUTILS_CONFIGURE_OPTS() { + OPTS=( + --target=$1 + --prefix=${INSTALL_DIR} + --disable-werror + --disable-gdb + --disable-nls + --disable-sim + --disable-libdecnumber + --disable-readline + ) + echo "${OPTS[@]}" +} + +GCC_CONFIGURE_OPTS() { + OPTS=( + --prefix=${INSTALL_DIR} + --target=riscv-none-elf + --enable-languages=c + --disable-libssp + --disable-libgomp + --disable-libmudflap + ) + echo "${OPTS[@]}" +} + +LLVM_CONFIGURE_OPTS() { + OPTS=( + -DCMAKE_BUILD_TYPE=Release + -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} + -DLLVM_ENABLE_PROJECTS='clang;lld' + -DLLVM_BINUTILS_INCDIR=${SRC_DIR}/${BINUTILS_DIR}/include + -DLLVM_DISTRIBUTION_COMPONENTS='clang;clang-resource-headers;lld;llvm-ar;llvm-cov;llvm-cxxfilt;llvm-dwp;llvm-nm;llvm-objcopy;llvm-objdump;llvm-ranlib;llvm-readobj;llvm-size;llvm-strings;llvm-strip;llvm-profdata;llvm-symbolizer' + -DLLVM_TARGETS_TO_BUILD='RISCV' + -DLLVM_OPTIMIZED_TABLEGEN=ON + -DLLVM_INSTALL_TOOLCHAIN_ONLY=ON + -DLLVM_INSTALL_BINUTILS_SYMLINKS=ON + ) + echo "${OPTS[@]}" +} + +NEWLIB_CONFIGURE_OPTS() { + OPTS=( + --target=$1 + --prefix=${INSTALL_DIR} + --enable-multilib + --enable-newlib-io-long-double + --enable-newlib-io-long-long + --enable-newlib-io-c99-formats + --enable-newlib-register-fini + ) + echo "${OPTS[@]}" +} + +COMPILER_RT_CONFIGURE_OPTS() { + OPTS=( + -DCMAKE_INSTALL_PREFIX=$(${INSTALL_DIR}/bin/clang -print-resource-dir) + -DCMAKE_C_COMPILER=${INSTALL_DIR}/bin/clang + -DCMAKE_CXX_COMPILER=${INSTALL_DIR}/bin/clang + -DCMAKE_AR=${INSTALL_DIR}/bin/llvm-ar + -DCMAKE_NM=${INSTALL_DIR}/bin/llvm-nm + -DCMAKE_RANLIB=${INSTALL_DIR}/bin/llvm-ranlib + -DCMAKE_C_COMPILER_TARGET=$1 + -DCMAKE_CXX_COMPILER_TARGET=$1 + -DCMAKE_ASM_COMPILER_TARGET=$1 + -DCMAKE_EXE_LINKER_FLAGS=-nostdlib + -DLLVM_CONFIG_PATH=${BUILD_DIR}/llvm/bin/llvm-config + -DCOMPILER_RT_BAREMETAL_BUILD=ON + -DCOMPILER_RT_BUILD_BUILTINS=ON + -DCOMPILER_RT_BUILD_LIBFUZZER=OFF + -DCOMPILER_RT_BUILD_MEMPROF=OFF + -DCOMPILER_RT_BUILD_PROFILE=OFF + -DCOMPILER_RT_BUILD_SANITIZERS=OFF + -DCOMPILER_RT_BUILD_XRAY=OFF + -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON + -DCOMPILER_RT_OS_DIR= + ) + echo "${OPTS[@]}" +} + diff --git a/util/toolchain-builder/config/llvm-18-baremetal.sh b/util/toolchain-builder/config/llvm-18-baremetal.sh new file mode 100644 index 0000000000..0ba81c56a1 --- /dev/null +++ b/util/toolchain-builder/config/llvm-18-baremetal.sh @@ -0,0 +1,37 @@ +############################################################################# +# +# Copyright 2024 Thales DIS France SAS +# +# 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 +# +# http://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. +# +############################################################################# +# +# Original Author: Zbigniew CHAMSKI, Thales Silicon Security +# +# Adapted by Mathieu Gouttenoire, Thales DIS France SAS +# +############################################################################# + + +# Each *_COMMIT variable can designate any valid 'commit-ish' entity: +# typically a tag, a commit or the output of "git describe" of a Git tree. + +# Binutils +BINUTILS_COMMIT=binutils-2_42 + +# LLVM +LLVM_COMMIT=release/18.x + +# newlib +NEWLIB_COMMIT=newlib-4.4.0 + diff --git a/util/toolchain-builder/config/llvm-master-baremetal.sh b/util/toolchain-builder/config/llvm-master-baremetal.sh new file mode 100644 index 0000000000..c1efc26eb4 --- /dev/null +++ b/util/toolchain-builder/config/llvm-master-baremetal.sh @@ -0,0 +1,37 @@ +############################################################################# +# +# Copyright 2024 Thales DIS France SAS +# +# 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 +# +# http://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. +# +############################################################################# +# +# Original Author: Zbigniew CHAMSKI, Thales Silicon Security +# +# Adapted by Mathieu Gouttenoire, Thales DIS France SAS +# +############################################################################# + + +# Each *_COMMIT variable can designate any valid 'commit-ish' entity: +# typically a tag, a commit or the output of "git describe" of a Git tree. + +# Binutils +BINUTILS_COMMIT=master + +# LLVM +LLVM_COMMIT=main + +# newlib +NEWLIB_COMMIT=master + diff --git a/util/gcc-toolchain-builder/get-toolchain.sh b/util/toolchain-builder/get-toolchain.sh similarity index 83% rename from util/gcc-toolchain-builder/get-toolchain.sh rename to util/toolchain-builder/get-toolchain.sh index 884eef3e81..4a438061ce 100644 --- a/util/gcc-toolchain-builder/get-toolchain.sh +++ b/util/toolchain-builder/get-toolchain.sh @@ -2,7 +2,7 @@ ############################################################################# # -# Copyright 2020-2023 Thales +# Copyright 2024 Thales DIS France SAS # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -20,6 +20,8 @@ # # Original Author: Zbigniew CHAMSKI, Thales Silicon Security # +# Adapted by Mathieu Gouttenoire, Thales DIS France SAS +# ############################################################################# # Prerequisites: @@ -38,6 +40,10 @@ # Paths in config files are relative to this directory. ROOT_DIR=$(dirname $(readlink -f $0)) +# Load global config +. $ROOT_DIR/config/global.sh + + # Helper function to print usage information. print_usage() { @@ -98,7 +104,7 @@ fi # fi # Overall directory infrastructure: make sure `pwd`/src exists. -[ ! -d $ROOT_DIR/src ] && mkdir $ROOT_DIR/src +[ ! -d "$SRC_DIR" ] && mkdir "$SRC_DIR" # All Git-based source trees are handled in the same way. # Positional args: @@ -110,13 +116,13 @@ setup_sources_from_git() # make sure the source directory exists and is populated # with Git information. If a stale non-Git directory exits, # remove it unless it is write-protected. - [ -d $ROOT_DIR/$2 -a -d $ROOT_DIR/$2/.git ] || \ - { rm -rf $ROOT_DIR/$2 && \ - git clone --depth=1 --branch="$3" $1 $ROOT_DIR/$2 ; } + [ -d $SRC_DIR/$2 -a -d $SRC_DIR/$2/.git ] || \ + { rm -rf $SRC_DIR/$2 && \ + git clone --depth=1 --branch="$3" $1 $SRC_DIR/$2 ; } # Check out the required revision as local branch (the shallow clone # leaves a "detached HEAD" state.) - cd $ROOT_DIR/$2 + cd $SRC_DIR/$2 LOCAL_BRANCH="${3}-local" { git branch | grep -q "$LOCAL_BRANCH" ; } || git checkout -b "$LOCAL_BRANCH" git checkout "$LOCAL_BRANCH" @@ -125,17 +131,24 @@ setup_sources_from_git() cd - } -# Binutils -echo "# Step 1: Obtaining sources of binutils-gdb..." +# Get Binutils sources +echo "# Step 1: Obtaining sources of binutils..." setup_sources_from_git $BINUTILS_REPO $BINUTILS_DIR $BINUTILS_COMMIT -# GCC -echo "# Step 2: Obtaining sources of GCC..." -setup_sources_from_git $GCC_REPO $GCC_DIR $GCC_COMMIT +if [[ $CONFIG_NAME == "gcc"* ]]; then + # Get GCC sources + echo "# Step 2: Obtaining sources of GCC..." + setup_sources_from_git $GCC_REPO $GCC_DIR $GCC_COMMIT +else + # Get LLVM sources + echo "# Step 2: Obtaining sources of LLVM..." + setup_sources_from_git $LLVM_REPO $LLVM_DIR $LLVM_COMMIT +fi -# Newlib +# Get Newlib sources echo "# Step 3: Obtaining sources of newlib..." setup_sources_from_git $NEWLIB_REPO $NEWLIB_DIR $NEWLIB_COMMIT # Exit happily. exit 0 +