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