Skip to content

Commit

Permalink
Add "rustc_codegen_gcc" page
Browse files Browse the repository at this point in the history
Signed-off-by: Antoni Boucher <[email protected]>
Signed-off-by: Miguel Ojeda <[email protected]>
  • Loading branch information
antoyo authored and ojeda committed Nov 9, 2023
1 parent dfae529 commit 5e20dd0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
## Tools

- [Coccinelle for Rust](Coccinelle-for-Rust.md)
- [`rustc_codegen_gcc`](rustc_codegen_gcc.md)

## Users

Expand Down
44 changes: 44 additions & 0 deletions src/rustc_codegen_gcc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# `rustc_codegen_gcc`

[`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.

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.

[Blog with updates about the progress of the GCC codegen.](https://blog.antoyo.xyz/)

## Building `rustc_codegen_gcc` and the sysroot

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).

Follow the build instructions [here](https://github.com/rust-lang/rustc_codegen_gcc#building).

## Building Rust for Linux

Follow the [Rust for Linux Quick Start instructions](https://docs.kernel.org/rust/quick-start.html) with a few changes as explained below.

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:

```sh
rustup override set nightly-2023-10-21 # Adjust to the version used by the GCC codegen.
```

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`):

```sh
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"
```

Since we use a differently nightly version, you might need to adjust the code of the `alloc` crate built by Rust for Linux.
You'll see some errors when running the above command in this case.

## Troubleshooting

If that didn't build the Rust object files, run `make menuconfig` again and check if the "Rust support" is available.
It could be that you have `RUST_IS_AVAILABLE [=n]`.
In that case, run `make rustavailable` with the `KRUSTFLAGS` you used above.
That should give you the correct error, which could be one of those:

* `libgccjit.so.0: cannot open shared object file: No such file or directory`
- In this case, make sure you set `LD_LIBRARY_PATH` and `LIBRARY_PATH`.
* `Source code for the 'core' standard library could not be found`
- 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 comments on commit 5e20dd0

Please sign in to comment.