32bitmicro

RISC-V Programming Notes

Building and installing RISC-V GNU GCC/G++ compilers:

Code and scripts assosciated with these notes can be found on Github https://github.com/32bitmicro/riscv-programming-notes

Development tools for the RISC-V ISA are being activelly developed (as March 2016) and pre-built installation packages are not yet available. Currently it is necessary to build compiler toollchain from source to be able to compile code for the RISC-V. There are instructions on how to build the tools on the RISC-V website http://riscv.org/software-tools and the Github repsitory https://github.com/riscv/riscv-gnu-toolchain.

Since GNU tools can be build in a number of configurations these notes will focus only on building a generic ELF/Newlib toolchain.

Here are the step by step instructions on how to build and install RISC-V GNU toolchain in your home directory on Ubuntu Linux:

Installing dependecies

First open terminal window with either combination of ‘CTRL+ALT+T’ keys or by clicking on the Terminal icon.
Before compiling the toolchain it is necessary to install all of the required dependencies.
On Ubuntu Linux it can be done with the following command typed or pasted in the terminal window:
‘sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc’
On other systems follow the instructions here: https://github.com/riscv/riscv-gnu-toolchain
Once all of the dependecies are installed you can check by issuing the above command again.
On my system it outputs these messages:

pawel@dell-pw:~$ sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc
[sudo] password for pawel: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
autoconf is already the newest version.
automake is already the newest version.
autotools-dev is already the newest version.
bc is already the newest version.
bison is already the newest version.
build-essential is already the newest version.
flex is already the newest version.
gawk is already the newest version.
gperf is already the newest version.
libgmp-dev is already the newest version.
libmpc-dev is already the newest version.
libmpfr-dev is already the newest version.
libtool is already the newest version.
patchutils is already the newest version.
texinfo is already the newest version.
curl is already the newest version.

Building and installing the toolchain

Step by step instructions:

  • go to your home directory command ‘cd ~’
  • create riscv directory command ‘mkdir dir riscv’
  • change working directory to riscv commmand cd riscv/
  • clone github riscv-gnu-toolchain repository command ‘git clone https://github.com/riscv/riscv-gnu-toolchain.git
  • create riscv directory command ‘mkdir dir riscv’
  • change working directory to riscv-gnu-toolchain command ‘cd riscv-gnu-toolchain/‘
  • configure the tools to install under your home directory command ‘./configure –prefix=/home/YourUserName/riscv/‘
  • wait for a while when build is downloading sources for the GNU tools, patching and building them

Here is the log from my session:

pawel@dell-pw:~$ cd ~
pawel@dell-pw:~$ mkdir dir riscv
pawel@dell-pw:~$ cd riscv/
pawel@dell-pw:~/riscv$ git clone https://github.com/riscv/riscv-gnu-toolchain.git
Cloning into 'riscv-gnu-toolchain'...
remote: Counting objects: 4272, done.
remote: Total 4272 (delta 0), reused 0 (delta 0), pack-reused 4272
Receiving objects: 100% (4272/4272), 2.59 MiB | 1.47 MiB/s, done.
Resolving deltas: 100% (1752/1752), done.
Checking connectivity... done.
pawel@dell-pw:~/riscv$
pawel@dell-pw:~/riscv$ cd riscv-gnu-toolchain/
pawel@dell-pw:~/riscv/riscv-gnu-toolchain$
pawel@dell-pw:~/riscv/riscv-gnu-toolchain$ ./configure --prefix=/home/pawel/riscv/
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for grep that handles long lines and -e... /bin/grep
checking for fgrep... /bin/grep -F
checking for bash... /bin/bash
checking for __gmpz_init in -lgmp... yes
checking for mpfr_init in -lmpfr... yes
checking for mpc_init2 in -lmpc... yes
checking for curl... /usr/bin/curl
checking for wget... /usr/bin/wget
checking for ftp... /usr/bin/ftp
configure: creating ./config.status
config.status: creating Makefile
config.status: creating scripts/wrapper/awk/awk
config.status: creating scripts/wrapper/sed/sed
pawel@dell-pw:~/riscv/riscv-gnu-toolchain$ make 
mkdir -p src
rm -rf src/original-gcc src/gcc-*
cd src && (cat /var/cache/distfiles/gcc-5.3.0.tar.gz || /usr/bin/curl -o - --ftp-pasv http://mirrors.kernel.org/gnu/gcc/gcc-5.3.0/gcc-5.3.0.tar.gz) | tar zxf -
cat: /var/cache/distfiles/gcc-5.3.0.tar.gz: No such file or directory
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  117M  100  117M    0     0  1267k      0  0:01:34  0:01:34 --:--:-- 1445k
mv src/gcc-5.3.0 src/original-gcc
rm -rf src/gcc src/gcc.tmp
cp -a src/original-gcc src/gcc.tmp
...
mkdir -p -- /home/pawel/riscv/riscv64-unknown-elf/include/machine
mkdir -p -- /home/pawel/riscv/riscv64-unknown-elf/include/rpc
mkdir -p -- /home/pawel/riscv/riscv64-unknown-elf/include/sys
mkdir -p -- /home/pawel/riscv/riscv64-unknown-elf/include/bits
 /bin/mkdir -p '/home/pawel/riscv/riscv64-unknown-elf/lib'
make[5]: Leaving directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib/riscv64-unknown-elf/newlib'
make[4]: Leaving directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib/riscv64-unknown-elf/newlib'
make[3]: Leaving directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib/riscv64-unknown-elf/newlib'
make[3]: Entering directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib/riscv64-unknown-elf/libgloss'
make[4]: Entering directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib/riscv64-unknown-elf/libgloss/doc'
make[4]: Nothing to be done for 'install'.
make[4]: Leaving directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib/riscv64-unknown-elf/libgloss/doc'
make[4]: Entering directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib/riscv64-unknown-elf/libgloss/riscv'
/home/pawel/riscv/riscv-gnu-toolchain/src/newlib-gcc/libgloss/riscv/mk-install-dirs.sh /home/pawel/riscv/riscv64-unknown-elf/include/machine
for file in /home/pawel/riscv/riscv-gnu-toolchain/src/newlib-gcc/libgloss/riscv/machine/syscfg.h /home/pawel/riscv/riscv-gnu-toolchain/src/newlib-gcc/libgloss/riscv/machine/syscall.h /home/pawel/riscv/riscv-gnu-toolchain/src/newlib-gcc/libgloss/riscv/memory.h; do \
    /usr/bin/install -c -m 444 $file /home/pawel/riscv/riscv64-unknown-elf/include/machine; \
  done
/home/pawel/riscv/riscv-gnu-toolchain/src/newlib-gcc/libgloss/riscv/mk-install-dirs.sh /home/pawel/riscv/riscv64-unknown-elf/lib
for file in libgloss.a crt0.o /home/pawel/riscv/riscv-gnu-toolchain/src/newlib-gcc/libgloss/riscv/riscv.ld; do \
    /usr/bin/install -c -m 644 $file /home/pawel/riscv/riscv64-unknown-elf/lib; \
  done
make[4]: Leaving directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib/riscv64-unknown-elf/libgloss/riscv'
make[4]: Entering directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib/riscv64-unknown-elf/libgloss/libnosys'
make[4]: Leaving directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib/riscv64-unknown-elf/libgloss/libnosys'
make[3]: Leaving directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib/riscv64-unknown-elf/libgloss'
make[2]: Leaving directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib'
make[1]: Leaving directory '/home/pawel/riscv/riscv-gnu-toolchain/build-gcc-newlib'
mkdir -p stamps/ && touch stamps/build-gcc-newlib

Verifying the toolchain

At this point RISC-V GNU toolchain has been built and installed in ‘/home/YourUserName/riscv/‘ directory.
To check the installation use command ‘ls -1 ~/riscv/‘
On my computer it shows following output:

pawel@dell-pw:~/riscv/riscv-gnu-toolchain$ ls -1 ~/riscv/
bin
include
lib
libexec
riscv64-unknown-elf
riscv-gnu-toolchain
share

You can also do a quick smoke test by verifying gcc and g++ version issueing following commads:
‘~/riscv/bin/riscv64-unknown-elf-gcc –version’
‘~/riscv/bin/riscv64-unknown-elf-g++ –version’

pawel@dell-pw:~/riscv/riscv-gnu-toolchain$ ~/riscv/bin/riscv64-unknown-elf-gcc --version
riscv64-unknown-elf-gcc (GCC) 5.3.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

pawel@dell-pw:~/riscv/riscv-gnu-toolchain$ ~/riscv/bin/riscv64-unknown-elf-g++ --version
riscv64-unknown-elf-g++ (GCC) 5.3.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.