src/bootstrap/) manages the complex process of building a self-hosting compiler. Since Rust is written in Rust, a sophisticated bootstrapping mechanism is required.
The Bootstrap Problem
The fundamental challenge: How do you compile a Rust compiler when you need a Rust compiler to compile Rust code?The solution: Use a previously-built compiler (stage 0) to compile the current source code (stage 1), then use that to recompile itself (stage 2) for verification.
Three-Stage Bootstrap Process
Fromsrc/bootstrap/README.md:
Bootstrap build system goes through a few phases to actually build the compiler.
Stage 0: Download Prebuilt Compiler
The entry point script (These scripts:
x, x.ps1, or x.py) downloads prebuilt compiler binaries:- Download the stage 0 compiler/Cargo binaries from CI
- Extract them to
build/<host-triple>/stage0/ - Compile the bootstrap system itself
- Invoke the compiled bootstrap binary
Stage 1: Build with Stage 0
Use the prebuilt stage 0 compiler to build the current source:
- Build the stage 1 standard library using stage 0
- Build the stage 1 compiler (links against stage 0 std)
- Use stage 1 compiler to rebuild the stage 1 standard library
build/<host-triple>/stage1/Stage 2: Rebuild with Stage 1
Use the stage 1 compiler to rebuild everything:
- Build the stage 2 standard library using stage 1
- Build the stage 2 compiler (links against stage 1 std)
build/<host-triple>/stage2/Stage 2 ensures the compiler can compile itself correctly. This is the compiler distributed to users.
Bootstrap System Architecture
Entry Points
The bootstrap system has multiple entry points:- bootstrap.py
- x / x.ps1 / x.py
- Bootstrap Binary
bootstrap.py is the Python entry point:Location:
src/bootstrap/bootstrap.pyThis is the “outer” bootstrap - it bootstraps the bootstrap!Bootstrap Crate Structure
Bootstrap includes wrapper binaries for rustc and rustdoc to intercept and instrument compilation during the build process.
Source Organization
The bootstrap source is organized insrc/bootstrap/src/:
Build Steps
Bootstrap organizes compilation into discrete build steps insrc/core/build_steps/:
compile.rs
Compiling rustc and the standard library
- Std compilation
- Rustc compilation
- Tool compilation
check.rs
Fast checking without codegen
cargo checkintegration- Faster feedback
test.rs
Test suite execution
- Compiletest harness
- Unit tests
- Integration tests
- UI tests
doc.rs
Documentation generation
- Rustdoc invocation
- Book building
- API documentation
dist.rs
Creating distribution artifacts
- Tarball creation
- Installation packages
- Cross-compilation
install.rs
Installing to system
- System installation
- Sysroot setup
llvm.rs
Building LLVM
- LLVM compilation
- CMake integration
- Caching
clippy.rs
Running Clippy linter
tool.rs
Building tools
- rustfmt
- cargo
- Other tools
Build Directory Structure
Fromsrc/bootstrap/README.md, all output goes under build/:
The build system uses hard links when possible to avoid copying large artifacts between stage directories.
Configuration
Bootstrap is configured through multiple mechanisms:- config.toml
- Command Line
- Environment Variables
Main configuration file
config.toml (or bootstrap.toml):src/core/config/:
- config.rs: Main configuration struct
- flags.rs: Command-line flag parsing
Dependency Management
Bootstrap has pinned dependencies for reliability:Why Pinned Dependencies?
Why Pinned Dependencies?
From
Cargo.toml comments:Most of the time updating these dependencies requires modifications to the bootstrap codebase (e.g., https://github.com/rust-lang/rust/issues/124565); otherwise, some targets will fail. That’s why these dependencies are explicitly pinned.
Build Performance Optimizations
Compilation Speed
No Debug Info for Deps
Incremental Bootstrap
Bootstrap leverages Cargo’s incremental compilation:
- Only recompile changed crates
- Cache build artifacts
- Parallel compilation
Downloading Pre-built Artifacts
- LLVM
- Stage 0
Pre-built LLVM can be downloaded instead of building:Managed by
src/core/download.rs:- Downloads from CI artifacts
- Verifies checksums (SHA-256)
- Caches in
build/cache/
Sanity Checks
src/core/sanity.rs performs extensive validation:
Verify Toolchains
- Check for C/C++ compiler
- Verify linker is available
- Confirm CMake for LLVM builds
- Check Python version
Validate Paths
- Ensure source directories exist
- Verify build directory is writable
- Check stage0 compiler
Parallel Execution
Bootstrap orchestrates parallel builds:The
-j parameter to bootstrap gets forwarded to Cargo and test harnesses for parallel execution.src/core/builder/:
- Dependency graph construction
- Parallel step execution
- Resource management
Testing Infrastructure
test.rs implements the test infrastructure:
- Test Suites
- Compiletest
- Running Tests
Multiple test suites:
- UI tests: Compiler diagnostic tests
- Codegen tests: Code generation verification
- Assembly tests: Assembly output tests
- Debuginfo tests: Debug information tests
- Incremental tests: Incremental compilation tests
- Rustdoc tests: Documentation tests
- Unit tests: Compiler unit tests
Cross-Compilation Support
Bootstrap supports building for multiple targets:build/<host-triple>/.
Distribution Creation
dist.rs creates distribution artifacts:
Component Packaging
- Package rustc binary
- Package standard library
- Package documentation
- Package tools (cargo, clippy, rustfmt)
build/dist/.
Extending Bootstrap
From the README:Some general areas that you may be interested in modifying:
Adding a New Tool
Adding a New Tool
See
bootstrap/src/core/build_steps/tool.rs for examples.Steps:- Define a new step struct
- Implement the
Steptrait - Register in the builder
Adding a Compiler Crate
Adding a Compiler Crate
- Create directory with
Cargo.toml - Configure all
Cargo.tomlfiles - Add to workspace members
Adding Configuration Option
Adding Configuration Option
- Modify
bootstrap/src/core/config/flags.rsfor CLI flags - Modify
bootstrap/src/core/config/config.rsfor config struct - Update
CONFIG_CHANGE_HISTORYinsrc/utils/change_tracker.rs
Adding Sanity Check
Adding Sanity Check
Add to
bootstrap/src/core/sanity.rsTwo Codebases in Sync
Synchronization mechanisms:- Environment variables set by
bootstrap.py - Explicitly ignored arguments
- Shared configuration format
Platform-Specific Handling
Bootstrap handles platform differences:- Windows
- Unix
- Different file extensions (.exe)
- Path separators
- Junction support
Debugging Bootstrap
Change Tracking
Bootstrap tracks configuration changes insrc/utils/change_tracker.rs:
Add entries to
CONFIG_CHANGE_HISTORY when making major changes:- New configuration options
- Changes to default behavior
- Breaking configuration changes
Design Philosophy
Leverage Cargo
Defer most compilation logic to Cargo itself
Incremental by Default
Each build step is incremental and parallelizable
Hard Links
Use hard links instead of copying to save space
Fail Fast
Sanity checks catch problems early
Common Build Commands
Further Reading
Compiler Architecture
What bootstrap compiles: the compiler crates
Standard Library
The standard library built by bootstrap
rustc dev guide
Building rustc: https://rustc-dev-guide.rust-lang.org/building/bootstrapping/
Contributing
CONTRIBUTING.md in the repository