Skip to content

gcc: locate codegen-backends via host_tuple() - #920

Merged
antoyo merged 1 commit into
rust-lang:masterfrom
xobs:gcc-backend-via-host-tuple
Jul 23, 2026
Merged

antoyo merged 1 commit into
rust-lang:masterfrom
xobs:gcc-backend-via-host-tuple

Conversation

@xobs

@xobs xobs commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

file_path builds the lib/rustlib/<host>/codegen-backends path used to load the codegen backend. It passed &sess.host.llvm_target, but that is the host's LLVM target string, not its rustc target tuple. The two can differ (e.g. arm64-apple-macosx vs aarch64-apple-darwin), so the lookup can miss the directory that actually holds the backend. Use rustc_session::config::host_tuple() instead.

This fixes an issue when building custom toolchains. In particular, I'm using the gcc backend to build a custom SH4 compiler. Without this patch, this command fails to find the backend:

$ BOOTSTRAP_SKIP_TARGET_SANITY=1 python3 x.py install library --target sh4-dreamcast-none-elf

`file_path` builds the `lib/rustlib/<host>/codegen-backends` path used to
load the codegen backend. It passed `&sess.host.llvm_target`, but that is
the host's LLVM target string, not its rustc target tuple.  The two can
differ (e.g. `arm64-apple-macosx` vs `aarch64-apple-darwin`), so the
lookup can miss the directory that actually holds the backend. Use
`rustc_session::config::host_tuple()` instead.
@GuillaumeGomez

Copy link
Copy Markdown
Member

Looks good to me, thanks! Let's see if @antoyo agrees as well.

@antoyo

antoyo commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

I'll double-check something for the review, but in the meantime, could you please reopen rust-lang/rust#159753 (possibly marking it as draft is needed)?
I would like the see the CI pass there to make sure this works well.

@antoyo

antoyo commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

I think this is not needed, but just to double-check, do you need to adjust this host as well?

This seems to already use the Rust host, so I believe this is correct.

@xobs

xobs commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

I haven't run into that issue, and it's been working well for me so far.

For reference, I've settled on this script for building the GCC backend. It's been working really well, and I've accumulated several patches which I'm working to upstream. This is the first of the set, and I see I'll be able to drop my #[used] patch soon!

build-rust-sh4.sh
#!/bin/sh
set -x
set -e
cd "$(dirname "$0")"
cd ..

export PATH=$PWD/install/gcc/bin:$PATH

# Apply Rust patches
cd src/rust
git reset --hard HEAD
git am --abort || true
cd src/gcc
git am --abort || true
cd ../..
git checkout df6ee909ef35c75aa58aa45af6ac071a9b8285c2
git am ../../patches/rust/*.patch
cd ../..

# On macOS, fix libgccjit symbol visibility
if [ "$(uname -s)" = "Darwin" ]
then
    # Create .so symlinks for .dylib files (build system expects .so)
    for d in install/*/aarch64-apple-darwin install/*/sh4-dreamcast-none-elf
    do
        [ -f "$d/libgccjit.dylib" ] && ln -sf libgccjit.dylib "$d/libgccjit.so"
        [ -f "$d/libgccjit.0.dylib" ] && ln -sf libgccjit.0.dylib "$d/libgccjit.0.so"
    done
    ln -sf libgccjit.dylib install/gcc/lib/libgccjit.so
    ln -sf libgccjit.0.dylib install/gcc/lib/libgccjit.0.so
fi

cat > src/rust/bootstrap.toml <<EOF
change-id = 154587
[install]
prefix = "$PWD/install/rust"
sysconfdir = "etc"

[build]
build-dir = "$PWD/build/rust"
extended = true
submodules = false
tools = ["cargo","src"]
docs = false

[rust]
codegen-backends = ["llvm", "gcc"]

[gcc]
libgccjit-libs-dir = "$PWD/install/gcc"

[target.sh4-dreamcast-none-elf]
codegen-backends = ["gcc"]
cc = "$PWD/install/gcc/bin/shle-elf-gcc"
ar = "$PWD/install/gcc/bin/shle-elf-ar"
rustflags = ["-Zcodegen-backend=gcc", "-Crelocation-model=static", "-Ccodegen-units=1", "-Copt-level=1"]
EOF

cd src/rust
python3 x.py install
BOOTSTRAP_SKIP_TARGET_SANITY=1 python3 x.py install library --target sh4-dreamcast-none-elf
BOOTSTRAP_SKIP_TARGET_SANITY=1 python3 x.py build rustc_codegen_gcc --target sh4-dreamcast-none-elf
rustup toolchain link sh4 ../../install/rust

# Copy librustc_codegen_gcc.so to the codegen-backends directory
SYSROOT=$(rustc +sh4 --print sysroot)
HOST=$(rustc --print host-tuple)
CG_DIR="$SYSROOT/lib/rustlib/$HOST/codegen-backends"
BUILD_DIR="../../build/rust/$HOST/stage2-codegen/$HOST/release"
mkdir -p "$CG_DIR"
for EXT in so dylib dll
do
   [ -f "$BUILD_DIR/librustc_codegen_gcc.$EXT" ] && cp "$BUILD_DIR/librustc_codegen_gcc.$EXT" "$CG_DIR/librustc_codegen_gcc.$EXT"
done


# Copy libgccjit entries to their destination paths
mkdir -p $(rustc +sh4 --print sysroot)/lib/rustlib/$(rustc --print host-tuple)/codegen-backends/lib/sh4-dreamcast-none-elf
cp ../../install/gcc/$(rustc --print host-tuple)/sh4-dreamcast-none-elf/libgccjit.so $(rustc +sh4 --print sysroot)/lib/rustlib/$(rustc --print host-tuple)/codegen-backends/lib/sh4-dreamcast-none-elf

cd ../..

@antoyo

antoyo commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

For reference, I've settled on this script for building the GCC backend.

The script you use does not seem to use the build system used in this repo, but I think no changes are needed in it anyway.

I'll wait for the CI to pass in the Rust repo, then we can merge this.

@xobs

xobs commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Correct, it doesn't use the build system used in this repo. I'm working from the rust tree and am applying patches there, particularly since my usecase is pinned to a particular nightly. I'm pulling patches out of my local tree and upstreaming them here so I'll be able to drop them once things are synced.

@antoyo
antoyo merged commit d948981 into rust-lang:master Jul 23, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants