|
| 1 | +# RISC-V (Linux) |
| 2 | + |
| 3 | +Julia has experimental support for 64-bit RISC-V (RV64) processors running |
| 4 | +Linux. This file provides general guidelines for compilation, in addition to |
| 5 | +instructions for specific devices. |
| 6 | + |
| 7 | +A list of [known issues](https://github.com/JuliaLang/julia/labels/system:riscv) |
| 8 | +for RISC-V is available. If you encounter difficulties, please create an issue |
| 9 | +including the output from `cat /proc/cpuinfo`. |
| 10 | + |
| 11 | + |
| 12 | +## Compiling Julia |
| 13 | + |
| 14 | +For now, Julia will need to be compiled entirely from source, i.e., including |
| 15 | +all of its dependencies. This can be accomplished with the following |
| 16 | +`Make.user`: |
| 17 | + |
| 18 | +```make |
| 19 | +USE_BINARYBUILDER := 0 |
| 20 | +``` |
| 21 | + |
| 22 | +Additionally, it is required to indicate what architecture, and optionally which |
| 23 | +CPU to build for. This can be done by setting the `MARCH` and `MCPU` variables |
| 24 | +in `Make.user` |
| 25 | + |
| 26 | +The `MARCH` variable needs to be set to a RISC-V ISA string, which can be found by |
| 27 | +looking at the documentation of your device, or by inspecting `/proc/cpuinfo`. Only |
| 28 | +use flags that your compiler supports, e.g., run `gcc -march=help` to see a list of |
| 29 | +supported flags. A common value is `rv64gc`, which is a good starting point. |
| 30 | + |
| 31 | +The `MCPU` variable is optional, and can be used to further optimize the |
| 32 | +generated code for a specific CPU. If you are unsure, it is recommended to leave |
| 33 | +it unset. You can find a list of supported values by running `gcc --target-help`. |
| 34 | + |
| 35 | +For example, if you are using a StarFive VisionFive2, which contains a JH7110 |
| 36 | +processor based on the SiFive U74, you can set these flags as follows: |
| 37 | + |
| 38 | +```make |
| 39 | +MARCH := rv64gc_zba_zbb |
| 40 | +MCPU := sifive-u74 |
| 41 | +``` |
| 42 | + |
| 43 | +If you prefer a portable build, you could use: |
| 44 | + |
| 45 | +```make |
| 46 | +MARCH := rv64gc |
| 47 | + |
| 48 | +# also set JULIA_CPU_TARGET to the expanded form of rv64gc |
| 49 | +# (it normally copies the value of MCPU, which we don't set) |
| 50 | +JULIA_CPU_TARGET := generic-rv64,i,m,a,f,d,zicsr,zifencei,c |
| 51 | +``` |
| 52 | + |
| 53 | +### Cross-compilation |
| 54 | + |
| 55 | +A native build on a RISC-V device may take a very long time, so it's also |
| 56 | +possible to cross-compile Julia on a faster machine. |
| 57 | + |
| 58 | +First, get a hold of a RISC-V cross-compilation toolchain that provides |
| 59 | +support for C, C++ and Fortran. This can be done by checking-out the |
| 60 | +[riscv-gnu-toolchain](https://github.com/riscv-collab/riscv-gnu-toolchain) |
| 61 | +repository and building it as follows: |
| 62 | + |
| 63 | +```sh |
| 64 | +sudo mkdir /opt/riscv && sudo chown $USER /opt/riscv |
| 65 | +./configure --prefix=/opt/riscv --with-languages=c,c++,fortran |
| 66 | +make linux -j$(nproc) |
| 67 | +``` |
| 68 | + |
| 69 | +Then, install the QEMU user-mode emulator for RISC-V, along with `binfmt` |
| 70 | +support to enable execution of RISC-V binaries on the host machine. The |
| 71 | +exact steps depend on your distribution, e.g., on Arch Linux it involves |
| 72 | +installing the `qemu-user-static` and `qemu-user-static-binfmt` packages. |
| 73 | +Note that to actually execute RISC-V binaries, QEMU will need to be able to |
| 74 | +find the RISC-V system root, which can be achieved by setting the |
| 75 | +`QEMU_LD_PREFIX` environment variable to the path of the root filesystem. |
| 76 | + |
| 77 | +Finally, compile Julia with the following `Make.user` variables (in addition to |
| 78 | +the ones from the previous section): |
| 79 | + |
| 80 | +```make |
| 81 | +XC_HOST=riscv64-unknown-linux-gnu |
| 82 | +OS=Linux |
| 83 | +export QEMU_LD_PREFIX=/opt/riscv/sysroot |
| 84 | +``` |
| 85 | + |
| 86 | +Note that you will have to execute `make` with `PATH` set to include the |
| 87 | +cross-compilation toolchain, e.g., by running: |
| 88 | + |
| 89 | +```sh |
| 90 | +PATH=/opt/riscv/bin:$PATH make -j$(nproc) |
| 91 | +``` |
| 92 | + |
| 93 | +Because of the RISC-V sysroot we use being very barren, you may need to |
| 94 | +add additional libraries that the Julia build system currently expects |
| 95 | +to be available system-wide. For example, the build currently relies on |
| 96 | +a system-provided `libz`, so you may need to copy this library from the |
| 97 | +Julia build into the system root: |
| 98 | + |
| 99 | +```sh |
| 100 | +make -C deps install-zlib |
| 101 | +cp -v usr/lib/libz.* /opt/riscv/sysroot/usr/lib |
| 102 | +cp -v usr/include/z*.h /opt/riscv/sysroot/usr/include |
| 103 | +``` |
0 commit comments