|
| 1 | +# `rustc_codegen_gcc` |
| 2 | + |
| 3 | +[`rustc_codegen_gcc`](https://github.com/rust-lang/rustc_codegen_gcc) is a GCC ahead-of-time codegen for rustc, meaning that it can be loaded by the existing rustc frontend, but benefits from GCC by having more architectures supported and having access to GCC’s optimizations. |
| 4 | + |
| 5 | +Rust for Linux can be compiled with `rustc_codegen_gcc` which allows to have a GCC-compiled Linux kernel with Rust kernel modules compiled with GCC as well. |
| 6 | + |
| 7 | +[Blog with updates about the progress of the GCC codegen.](https://blog.antoyo.xyz/) |
| 8 | + |
| 9 | +## Building `rustc_codegen_gcc` and the sysroot |
| 10 | + |
| 11 | +The following have been tested with GCC commit [b334f15ed21c95868059a16484a1948be08e26a3](https://github.com/antoyo/gcc/commit/b334f15ed21c95868059a16484a1948be08e26a3) and `rustc_codegen_gcc` commit [c6bc7ecd65046ee502118664f42637ca318cdfb5](https://github.com/rust-lang/rustc_codegen_gcc/commit/c6bc7ecd65046ee502118664f42637ca318cdfb5). |
| 12 | + |
| 13 | +Follow the build instructions [here](https://github.com/rust-lang/rustc_codegen_gcc#building). |
| 14 | + |
| 15 | +## Building Rust for Linux |
| 16 | + |
| 17 | +Follow the [Rust for Linux Quick Start instructions](https://docs.kernel.org/rust/quick-start.html) with a few changes as explained below. |
| 18 | + |
| 19 | +Since the GCC codegen might not work on every nightly version (that should soon be fixed now that we run some tests in the Rust CI), we're going to use [the same nightly version as the GCC codegen](https://github.com/rust-lang/rustc_codegen_gcc/blob/master/rust-toolchain) instead of using the version recommended by Rust for Linux: |
| 20 | + |
| 21 | +```sh |
| 22 | +rustup override set nightly-2023-10-21 # Adjust to the version used by the GCC codegen. |
| 23 | +``` |
| 24 | + |
| 25 | +Now, you need to set some variables to build Rust for Linux with the GCC codegen (do not forget to adjust your path to `rustc_codegen_gcc`): |
| 26 | + |
| 27 | +```sh |
| 28 | +make -j20 KRUSTFLAGS="-Zcodegen-backend=/path/to/rustc_codegen_gcc/target/debug/librustc_codegen_gcc.so --sysroot /path/to/rustc_codegen_gcc/build_sysroot/sysroot" HOSTRUSTFLAGS="-Zcodegen-backend=/path/to/rustc_codegen_gcc/target/debug/librustc_codegen_gcc.so --sysroot /path/to/rustc_codegen_gcc/build_sysroot/sysroot -Clto=no" |
| 29 | +``` |
| 30 | + |
| 31 | +Since we use a differently nightly version, you might need to adjust the code of the `alloc` crate built by Rust for Linux. |
| 32 | +You'll see some errors when running the above command in this case. |
| 33 | + |
| 34 | +## Troubleshooting |
| 35 | + |
| 36 | +If that didn't build the Rust object files, run `make menuconfig` again and check if the "Rust support" is available. |
| 37 | +It could be that you have `RUST_IS_AVAILABLE [=n]`. |
| 38 | +In that case, run `make rustavailable` with the `KRUSTFLAGS` you used above. |
| 39 | +That should give you the correct error, which could be one of those: |
| 40 | + |
| 41 | + * `libgccjit.so.0: cannot open shared object file: No such file or directory` |
| 42 | + - In this case, make sure you set `LD_LIBRARY_PATH` and `LIBRARY_PATH`. |
| 43 | + * `Source code for the 'core' standard library could not be found` |
| 44 | + - In this case, make sure you used a recent enough version of `rustc_codegen_gcc` (c6bc7ecd65046ee502118664f42637ca318cdfb5 or more recent should be good) that copies the source of the sysroot at the correct location. |
0 commit comments