RISC-V LLVM setup in Ubuntu

mucrolores
3 min readJul 10, 2021

This is a note for teaching how to setup your RISC-V LLVM compile environment, I’ve reference the set up on sifive/riscv-llvm github pages

System environment list

Running on a virtual machine: VMWare Player
Ubuntu Version: 21.04

Before you start

please update your apt or apt-get to avoid some error while setting up.

sudo apt-get update
# or
sudo apt update

Setup all RISC-V and LLVM environments

In this part, you can use either apt or apt-get, apt-get seems to be an older tool for package management, in my experience, using apt is good for go.

This part of code will help you out to install packages you’ll need for later use.

sudo apt-get -y install \
binutils build-essential libtool texinfo \
gzip zip unzip patchutils curl git \
make cmake ninja-build automake bison flex gperf \
grep sed gawk python bc \
zlib1g-dev libexpat1-dev libmpc-dev \
libglib2.0-dev libfdt-dev libpixman-1-dev

After installing the requirements packages, you’ll need to make a directory to put all of your riscv files, I’ve create my own in the /bin directory as I’m installing a new program in system.
It’s up to yourself decide where to install and setup your risc-v environment :).

shell command hash -r is to clean the command hash.

mkdir riscv
cd riscv
mkdir _install
export PATH=`pwd`/_install/bin:$PATH
hash -r

git cloning the whole riscv toolchain source code, and start to build it up.

# gcc, binutils, newlib
git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
pushd riscv-gnu-toolchain
./configure --prefix=`pwd`/../_install --enable-multilib
make -j`nproc`

build up qeum, command popd is help you to move back to the /riscv dicectory.

# qemu
make -j`nproc` build-qemu
popd

git cloning the whole riscv in llvm compiler source code, and start to build it up.
Command ln help to link to clang and llvm tools.
camke to build up all the llvm source in to executable.

# LLVM
git clone https://github.com/llvm/llvm-project.git riscv-llvm
#git clone https://github.com/sifive/riscv-llvm
pushd riscv-llvm
ln -s ../../clang llvm/tools || true
mkdir _build
cd _build
cmake -G Ninja -DCMAKE_BUILD_TYPE="Release" \
-DBUILD_SHARED_LIBS=True -DLLVM_USE_SPLIT_DWARF=True \
-DCMAKE_INSTALL_PREFIX="../../_install" \
-DLLVM_OPTIMIZED_TABLEGEN=True -DLLVM_BUILD_TESTS=False \
-DDEFAULT_SYSROOT="../../_install/riscv64-unknown-elf" \
-DLLVM_DEFAULT_TARGET_TRIPLE="riscv64-unknown-elf" \
-DLLVM_TARGETS_TO_BUILD="RISCV" \
../llvm
cmake --build . --target install
popd

Now, you can change your directory to the place you want to start playing with risc-v environment.

Write the hello.c file vim hello.c
Add the code in to files and save quit.

#include <stdio.h>
int main(){
printf("Hello RISCV!\n");
return 0;
}

Now compile the code by riscv64-unknown-elf-gcc, and execute by qemu-riscv64.

# 32 bit
clang -O -c hello.c --target=riscv32
riscv64-unknown-elf-gcc hello.o -o hello -march=rv32imac -mabi=ilp32
qemu-riscv32 hello

# 64 bit
clang -O -c hello.c
riscv64-unknown-elf-gcc hello.o -o hello -march=rv64imac -mabi=lp64
qemu-riscv64 hello

Some more information about setup the path

Command export PATH=`pwd`/_install/bin:$PATH sets the PATH of the system, be careful, this kind of command can only set the PATH “temporary”, when ever you restart the system, you’ll have to set PATH again and again. To set PATH permanent, please follow instruction bellow.

  1. get into sudo su and type your su password sudo su
  2. edit the file by command vim ~/.profile
  3. add the line into file and save and quit
    export PATH=/the/path/of/your/riscv/bin:$PATH
    You should use your own path of the riscv files binary directory.
  4. call command source ~/.profile to refresh to PATH variable
  5. while you’ve done for the work you should be able to see the command in terminal.

Start to Enjoy your RISC-V LLVM compile environments :)

--

--