Skip to content

Instantly share code, notes, and snippets.

@xobs
Created February 10, 2026 08:44
Show Gist options
  • Select an option

  • Save xobs/bb76037c50aaf5ecc0a9d36137ae060f to your computer and use it in GitHub Desktop.

Select an option

Save xobs/bb76037c50aaf5ecc0a9d36137ae060f to your computer and use it in GitHub Desktop.
Build the Rust toolchain for riscv32emc-unknown-none-elf
#!/usr/bin/env bash
# Build the riscv32emc-unknown-none-elf target and install it.
if [ -z $RUST_TOOLCHAIN ]
then
RUST_TOOLCHAIN=""
fi
set -e
set -o pipefail
rust_sysroot=$(rustc $RUST_TOOLCHAIN --print sysroot)
export RUST_COMPILER_RT_ROOT="$(pwd)/src/llvm-project/compiler-rt"
export CARGO_PROFILE_RELEASE_DEBUG=0
export CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
export CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="true"
export RUSTC_BOOTSTRAP=1
export RUSTFLAGS="-Cforce-unwind-tables=yes -Cembed-bitcode=yes -Zforce-unstable-if-unmarked"
export __CARGO_DEFAULT_LIB_METADATA="stablestd"
command_exists() {
which $1 &> /dev/null && $1 --version 2>&1 > /dev/null
}
# Set up the C compiler. We need to explicitly specify these variables
# because the `cc` package obviously doesn't recognize our target triple.
if [ ! -z $CC ]
then
echo "Using compiler $CC"
elif command_exists riscv32-unknown-elf-gcc
then
export CC="riscv32-unknown-elf-gcc"
export AR="riscv32-unknown-elf-ar"
elif command_exists riscv-none-embed-gcc
then
export CC ="riscv-none-embed-gcc"
export AR ="riscv-none-embed-ar"
elif command_exists riscv64-unknown-elf-gcc
then
export CC="riscv64-unknown-elf-gcc"
export AR="riscv64-unknown-elf-ar"
elif command_exists riscv64-elf-gcc
then
export CC="riscv64-elf-gcc"
export AR="riscv64-elf-ar"
else
echo "No C compiler found for riscv" 1>&2
exit 1
fi
export CFLAGS_riscv32emc_unknown_none_elf="-mabi=ilp32e"
target=riscv32emc-unknown-none-elf
src_path="./library/target/$target/release/deps"
dest_path="$rust_sysroot/lib/rustlib/$target"
dest_lib_path="$dest_path/lib"
mkdir -p $dest_lib_path
rustc $RUST_TOOLCHAIN --version | awk '{print $2}' > "$dest_path/RUST_VERSION"
# Remove stale objects
rm -rf $src_path
rm -f $dest_lib_path/*.rlib
cargo $RUST_TOOLCHAIN build \
--target $target \
-Zbinary-dep-depinfo \
--release \
--features "compiler-builtins-c compiler-builtins-mem" \
--manifest-path "library/alloc/Cargo.toml" || exit 1
# TODO: Remove duplicates here by comparing it with $previous_libraries
for new_item in $(ls -1 $src_path/*.rlib)
do
file=$(basename $new_item)
base_string=$(echo $file | rev | cut -d- -f2- | rev)
done
cp $src_path/*.rlib "$dest_lib_path"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment