1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# `riscv64gc-unknown-linux-gnu`
**Tier: 2 (with Host Tools)**
RISC-V targets using the *RV64I* base instruction set with the *G* collection of extensions, as well as the *C* extension.
## Target maintainers
- Kito Cheng, <kito.cheng@gmail.com>, [@kito-cheng](https://github.com/kito-cheng)
- Michael Maitland, <michaeltmaitland@gmail.com>, [@michaelmaitland](https://github.com/michaelmaitland)
- Robin Randhawa, <robin.randhawa@sifive.com>, [@robin-randhawa-sifive](https://github.com/robin-randhawa-sifive)
- Craig Topper, <craig.topper@sifive.com>, [@topperc](https://github.com/topperc)
## Requirements
This target requires:
* Linux Kernel version 4.20 or later
* glibc 2.17 or later
## Building the target
These targets are distributed through `rustup`, and otherwise require no
special configuration.
If you need to build your own Rust for some reason though, the targets can be
enabled in `config.toml`. For example:
```toml
[build]
target = ["riscv64gc-unknown-linux-gnu"]
```
## Building Rust programs
On a RISC-V host, the `riscv64gc-unknown-linux-gnu` target should be automatically
installed and used by default.
On a non-RISC-V host, add the target:
```bash
rustup target add riscv64gc-unknown-linux-gnu
```
Then cross compile crates with:
```bash
cargo build --target riscv64gc-unknown-linux-gnu
```
## Testing
There are no special requirements for testing and running the targets.
For testing cross builds on the host, please refer to the "Cross-compilation
toolchains and C code"
section below.
## Cross-compilation toolchains and C code
A RISC-V toolchain can be obtained for Windows/Mac/Linux from the
[`riscv-gnu-toolchain`](https://github.com/riscv-collab/riscv-gnu-toolchain)
repostory. Binaries are available via
[embecosm](https://www.embecosm.com/resources/tool-chain-downloads/#riscv-linux),
and may also be available from your OS's package manager.
On Ubuntu, a RISC-V toolchain can be installed with:
```bash
apt install gcc-riscv64-linux-gnu g++-riscv64-linux-gnu libc6-dev-riscv64-cross
```
Depending on your system, you may need to configure the target to use the GNU
GCC linker. To use it, add the following to your `.cargo/config.toml`:
```toml
[target.riscv64gc-unknown-linux-gnu]
linker = "riscv64-linux-gnu-gcc"
```
If your `riscv64-linux-gnu-*` toolchain is not in your `PATH` you may need to
configure additional settings:
```toml
[target.riscv64gc-unknown-linux-gnu]
# Adjust the paths to point at your toolchain
cc = "/TOOLCHAIN_PATH/bin/riscv64-linux-gnu-gcc"
cxx = "/TOOLCHAIN_PATH/bin/riscv64-linux-gnu-g++"
ar = "/TOOLCHAIN_PATH/bin/riscv64-linux-gnu-ar"
ranlib = "/TOOLCHAIN_PATH/bin/riscv64-linux-gnu-ranlib"
linker = "/TOOLCHAIN_PATH/bin/riscv64-linux-gnu-gcc"
```
To test cross compiled binaries on a non-RISCV-V host, you can use
[`qemu`](https://www.qemu.org/docs/master/system/target-riscv.html).
On Ubuntu, a RISC-V emulator can be obtained with:
```bash
apt install qemu-system-riscv64
```
Then, in `.cargo/config.toml` set the `runner`:
```toml
[target.riscv64gc-unknown-linux-gnu]
runner = "qemu-riscv64-static -L /usr/riscv64-linux-gnu -cpu rv64"
```
On Mac and Linux, it's also possible to use
[`lima`](https://github.com/lima-vm/lima) to emulate RISC-V in a similar way to
how WSL2 works on Windows:
```bash
limactl start template://riscv
limactl shell riscv
```
Using [Docker (with BuildKit)](https://docs.docker.com/build/buildkit/) the
[`riscv64/ubuntu`](https://hub.docker.com/r/riscv64/ubuntu) image can be used
to build or run `riscv64gc-unknown-linux-gnu` binaries.
```bash
docker run --platform linux/riscv64 -ti --rm --mount "type=bind,src=$(pwd),dst=/checkout" riscv64/ubuntu bash
```
|