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