Skip to content

Commit 841c7eb

Browse files
committed
update README with full guide
1 parent 67bad6d commit 841c7eb

File tree

1 file changed

+230
-2
lines changed

1 file changed

+230
-2
lines changed

README.md

Lines changed: 230 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
riscv-tools [![Build Status](https://travis-ci.org/ucb-bar/riscv-tools.svg?branch=master)](https://travis-ci.org/ucb-bar/riscv-tools)
2-
===========
1+
riscv-tools [![Build Status](https://travis-ci.org/ucb-bar/riscv-tools.svg?branch=master)](https://travis-ci.org/ucb-bar/riscv-tools)
2+
===========================================================================
3+
4+
[In depth instructions are located below](#in-depth). For experienced users, here's a summary:
35

46
Quick and dirty instructions:
57

@@ -15,3 +17,229 @@ Note: This requires GCC >= 4.8 for C++11 support (including thread_local).
1517
To use a compiler different than the default (for example on OS X), use:
1618

1719
$ CC=gcc-4.8 CXX=g++-4.8 ./build.sh
20+
21+
22+
<a id="in-depth"></a>The RISC-V GCC/Newlib Toolchain Installation Manual
23+
===========================================
24+
25+
This document was authored by [Quan Nguyen](http://ocf.berkeley.edu/~qmn) and is a mirrored version (with slight modifications) of the one found at [Quan's OCF
26+
website](http://ocf.berkeley.edu/~qmn/linux/install-newlib.html). Recent updates were made by Sagar Karandikar.
27+
28+
Last updated August 6, 2014
29+
30+
## Introduction
31+
32+
The purpose of this page is to document a procedure through
33+
which an interested user can build the RISC-V GCC/Newlib toolchain.
34+
35+
A project with a duration such as this requires adequate
36+
documentation to support future development and maintenance. This document is
37+
created with the hope of being useful; however, its accuracy is not
38+
guaranteed.
39+
40+
This work was completed at Andrew and Yunsup's request.
41+
42+
## Table of Contents
43+
44+
1. Introduction
45+
2. Table of Contents
46+
3. [Meta-installation Notes](#meta-installation-notes)
47+
4. [Installing the Toolchain](#installing-toolchain)
48+
5. [Testing Your Toolchain](#testing-toolchain)
49+
6. ["Help! It doesn't work!"](#help-it-doesnt-work)
50+
51+
## <a name="meta-installation-notes"></a>Meta-installation Notes
52+
53+
You may notice this document strikes you as similar to its
54+
bigger sibling, the <a href="install-linux.html">
55+
Linux/RISC-V Installation Manual</a>. That's because the instructions are rather
56+
similar. That said...
57+
58+
### Running Shell Commands
59+
60+
Instructive text will appear as this paragraph does. Any
61+
instruction to execute in your terminal will look like this:
62+
63+
$ echo "execute this"
64+
65+
_Optional_ shell commands that may be required for
66+
your particular system will have their prompt preceeded with an O:
67+
68+
O$ echo "call this, maybe"
69+
70+
If you will need to replace a bit of code that applies
71+
specifically to your situation, it will be surrounded by [square brackets].
72+
73+
### The Standard Build Unit
74+
75+
To instruct how long it will take someone to build the
76+
various components of the packages on this page, I have provided build times in
77+
terms of the Standard Build Unit (SBU), as coined by Gerard Beekmans in his
78+
immensely useful [Linux From Scratch](http://www.linuxfromscratch.org)
79+
website.
80+
81+
On an Intel Xeon Dual Quad-core server with 48 GiB RAM, I
82+
achieved the following build time for `binutils`: 38.64 seconds.
83+
Thus, **38.64 seconds = 1 SBU**. (EECS members at the University
84+
of California, Berkeley: I used the `s141.millennium` server.)
85+
86+
As a point of reference, my 2007 MacBook with an Intel Core 2
87+
Duo and 1 GiB RAM has 100.1 seconds to each SBU. Building
88+
`riscv-linux-gcc`, unsurprisingly, took about an hour.
89+
90+
Items marked as "optional" are not measured.
91+
92+
### Having Superuser Permissions
93+
94+
You will need root privileges to install
95+
the tools to directories like `/usr/bin`, but you may optionally
96+
specify a different installation directory. Otherwise, superuser privileges are
97+
not necessary.
98+
99+
### GCC Version
100+
101+
Note: Building `riscv-tools` requires GCC >= 4.8 for C++11 support (including thread_local). To use a compiler different than the default (for example on OS X), you'll need to do the following when the guide requires you to run `build.sh`:
102+
103+
$ CC=gcc-4.8 CXX=g++-4.8 ./build.sh
104+
105+
106+
## <a name="installing-toolchain"></a>Installing the Toolchain
107+
108+
Let's start with the directory in which we will install our
109+
tools. Find a nice, big expanse of hard drive space, and let's call that
110+
`$TOP`. Change to the directory you want to install in, and then set
111+
the `$TOP` environment variable accordingly:
112+
113+
$ export TOP=$(pwd)
114+
115+
For the sake of example, my `$TOP` directory is on
116+
`s141.millennium`, at `/scratch/quannguyen/noob`, named so
117+
because I believe even a newbie at the command prompt should be able to complete
118+
this tutorial. Here's to you, n00bs!
119+
120+
### Tour of the Sources
121+
122+
If we are starting from a relatively fresh install of
123+
GNU/Linux, it will be necessary to install the RISC-V toolchain. The toolchain
124+
consists of the following components:
125+
126+
* `riscv-gcc`, a RISC-V cross-compiler
127+
* `riscv-fesvr`, a "front-end" server that
128+
services calls between the host and target processors on the Host-Target
129+
InterFace (HTIF) (it also provides a virtualized console and disk device)
130+
* `riscv-isa-sim`, the ISA simulator and
131+
"golden standard" of execution
132+
* `riscv-opcodes`, the enumeration of all
133+
RISC-V opcodes executable by the simulator
134+
* `riscv-pk`, a proxy kernel that services
135+
system calls generated by code built and linked with the RISC-V Newlib port
136+
(this does not apply to Linux, as _it_ handles the system calls)
137+
* `riscv-tests`, a set of assembly tests
138+
and benchmarks
139+
140+
In the installation guide for Linux builds, we built only the
141+
simulator and the front-end server. Binaries built against Newlib with
142+
`riscv-gcc` will not have the luxury of being run on a full-blown
143+
operating system, but they will still demand to have access to some crucial
144+
system calls.
145+
146+
### What's Newlib?
147+
148+
[Newlib](http://www.sourceware.org/newlib/) is a
149+
"C library intended for use on embedded systems." It has the advantage of not
150+
having so much cruft as Glibc at the obvious cost of incomplete support (and
151+
idiosyncratic behavior) in the fringes. The porting process is much less complex
152+
than that of Glibc because you only have to fill in a few stubs of glue
153+
code.
154+
155+
These stubs of code include the system calls that are
156+
supposed to call into the operating system you're running on. Because there's no
157+
operating system proper, the simulator runs, on top of it, a proxy kernel
158+
(`riscv-pk`) to handle many system calls, like `open`,
159+
`close`, and `printf`.
160+
161+
### Obtaining and Compiling the Sources (7.87 SBU)
162+
163+
First, clone the tools from the `riscv-tools` GitHub
164+
repository:
165+
166+
$ git clone https://github.com/ucb-bar/riscv-tools.git
167+
168+
This command will bring in only references to the
169+
repositories that we will need. We rely on Git's submodule system to take care
170+
of resolving the references. Enter the newly-created riscv-tools directory and
171+
instruct Git to update its submodules.
172+
173+
$ cd $TOP/riscv-tools
174+
$ git submodule update --init --recursive
175+
176+
To build GCC, we will need several other packages, including
177+
flex, bison, autotools, libmpc, libmpfr, and libgmp. Ubuntu distribution
178+
installations will require this command to be run. If you have not installed
179+
these things yet, then run this:
180+
181+
O$ sudo apt-get install autoconf automake autotools-dev libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf
182+
183+
Before we start installation, we need to set the
184+
`$RISCV` environment variable. The variable is used throughout the
185+
build script process to identify where to install the new tools. (This value is
186+
used as the argument to the `--prefix` configuration switch.)
187+
188+
$ export RISCV=$TOP/riscv
189+
190+
If your `$PATH` variable does not contain the
191+
directory specified by `$RISCV`, add it to the `$PATH`
192+
environment variable now:
193+
194+
$ export PATH=$PATH:$RISCV/bin
195+
196+
One more thing: If your machine doesn't have the capacity to
197+
handle 16 make jobs (or conversely, it can handle more), edit
198+
`build.common` to change the number specified by
199+
`JOBS`.
200+
201+
O$ sed -i 's/JOBS=16/JOBS=[number]/' build.common
202+
203+
With everything else set up, run the build script. Recall that if you're using a new-version of gcc that isn't the default on your system, you'll need to precede the `./build.sh` with `CC=gcc-4.8 CXX=g++-4.8`:
204+
205+
$ ./build.sh
206+
207+
208+
## <a name="testing-toolchain"></a> Testing Your Toolchain
209+
210+
Now that you have a toolchain, it'd be a good idea to test it
211+
on the quintessential "Hello world!" program. Exit the `riscv-tools`
212+
directory and write your "Hello world!" program. I'll use a long-winded
213+
`echo` command.
214+
215+
$ cd $TOP
216+
$ echo -e '#include <stdio.h>\n int main(void) { printf("Hello world!\\n"); return 0; }' > hello.c
217+
218+
Then, build your program with `riscv-gcc`.
219+
220+
$ riscv-gcc -o hello hello.c
221+
222+
When you're done, you may think to do `./hello`,
223+
but not so fast. We can't even run `spike hello`, because our "Hello
224+
world!" program involves a system call, which couldn't be handled by our host
225+
x86 system. We'll have to run the program within the
226+
proxy kernel, which itself is run by `spike`, the RISC-V
227+
architectural simulator. Run this command to run your "Hello world!"
228+
program:
229+
230+
$ spike pk hello
231+
232+
The RISC-V architectural simulator, `spike`, takes
233+
as its argument the path of the binary to run. This binary is `pk`,
234+
and is located at `$RISCV/riscv-elf/bin/pk`.
235+
`spike` finds this automatically.
236+
Then, `riscv-pk` receives as _its_
237+
argument the name of the program you want to run.
238+
239+
Hopefully, if all's gone well, you'll have your program
240+
saying, "Hello world!". If not...
241+
242+
243+
## <a name="help-it-doesnt-work"></a>"Help! It doesn't work!"
244+
245+
I know, I've been there too. Good luck!

0 commit comments

Comments
 (0)