[Gentoo] Bootstrapping Rust compiler from source code

← Back

Since the official Rust compiler is written in Rust, there’s an unpleasant chicken-egg problem as we need a Rust compiler in order to compile a Rust compiler. Also, given the security implications of compilers distributed solely as binaries (including compilers compiled from source code but using those distributed binaries), that’s an absolute no go for me.

Still, there is a way to bootstrap the Rust compiler from source code and here is how to do it (in Gentoo Linux).

How-to

  1. Fetch mrustc (ideally the latest master from git).
  2. Edit TestRustcBootstrap.sh like this (1.54.0 is the latest version mrustc can compile as of writing this note):
     diff --git a/TestRustcBootstrap.sh b/TestRustcBootstrap.sh
     index d34dca1f..7283fd54 100755
     --- a/TestRustcBootstrap.sh
     +++ b/TestRustcBootstrap.sh
     @@ -6,7 +6,7 @@ set -u  # Error on unset variables
      WORKDIR=${WORKDIR:-rustc_bootstrap}/
        
      RUSTC_TARGET=${RUSTC_TARGET:-x86_64-unknown-linux-gnu}
     -RUSTC_VERSION=${*-1.29.0}
     +RUSTC_VERSION=${*-1.54.0}
      RUN_RUSTC_SUF=""
      if [[ "$RUSTC_VERSION" == "1.29.0" ]]; then
          RUSTC_VERSION_NEXT=1.30.0
     @@ -67,6 +67,7 @@ rm -rf ${WORKDIR}mrustc-output
      cp -r ${WORKDIR}mrustc/rustc-${RUSTC_VERSION_NEXT}-src/build/${RUSTC_TARGET}/stage2 ${WORKDIR}mrustc-output
      tar -czf ${WORKDIR}mrustc.tar.gz -C ${WORKDIR} mrustc-output
        
     +exit
      #
      # Build rustc by downloading the previous version of rustc (and its matching cargo)
      #
    

    (Optionally, also change -j8 to a number better fitting your CPU.)

  3. Run the script.
  4. find cargo in rustc_bootstrap/ and copy/link it to rustc_bootstrap/mrustc-output/bin/.
  5. Fetch old ebuilds from https://packages.gentoo.org/packages/dev-lang/rust. (Hint: “Git log (short)”, look for commits saying “Drop …”)
  6. Don’t forget to fetch patches as well (tip: some are used for newer versions too, so you might need to also go through newer commits – you’ll probably need all versions up to the latest, anyway.)
  7. Edit the ebuilds:
  8. Also fetch llvm ebuilds the Rust ebuilds depend on. I have tried, if it says 13, 14 will fail, unfortunately. Not surprising from Rust, though.
  9. Copy all ebuilds either into /usr/portage/ (corresponding directories) or into your local overlay (in this case, make sure you have all needed patch files) and rebuild manifests.
  10. Start emerging. One… by… one… –_–

Personally, I have only these USE flags enabled for dev-lang/rust: system-bootstrap system-llvm verify-sig.

I would like to express huge kudos to thepowersgang, the author of mrustc. Without his immeasurable work, the bootstrap of Rust simply wouldn’t be possible. It’s a shame that the Rust community and Foundation don’t help him or toss a coin in his direction (it’s not surprising, though :·\ ).


Tagged with: Gentoo Linux, Rust

Written: 2023-03-22