You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
You may notice this document strikes you as similar to its
54
+
bigger sibling, the <ahref="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
+
## <aname="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
`$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
+
## <aname="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
0 commit comments