1 # rustbuild - Bootstrapping Rust
3 This is an in-progress README which is targeted at helping to explain how Rust
4 is bootstrapped and in general some of the technical details of the build
9 The rustbuild build system has a primary entry point, a top level `x.py` script:
15 Note that if you're on Unix you should be able to execute the script directly:
21 The script accepts commands, flags, and arguments to determine what to do:
23 * `build` - a general purpose command for compiling code. Alone `build` will
24 bootstrap the entire compiler, and otherwise arguments passed indicate what to
28 # build the whole compiler
31 # build the stage1 compiler
32 ./x.py build --stage 1
35 ./x.py build --stage 0 library/std
37 # build a particular crate in stage0
38 ./x.py build --stage 0 library/test
41 If files are dirty that would normally be rebuilt from stage 0, that can be
42 overridden using `--keep-stage 0`. Using `--keep-stage n` will skip all steps
43 that belong to stage n or earlier:
46 # keep old build products for stage 0 and build stage 1
47 ./x.py build --keep-stage 0 --stage 1
50 * `test` - a command for executing unit tests. Like the `build` command this
51 will execute the entire test suite by default, and otherwise it can be used to
52 select which test suite is run:
61 # execute the UI test suite
62 ./x.py test src/test/ui
64 # execute only some tests in the UI test suite
65 ./x.py test src/test/ui --test-args substring-of-test-name
67 # execute tests in the standard library in stage0
68 ./x.py test --stage 0 library/std
70 # execute tests in the core and standard library in stage0,
71 # without running doc tests (thus avoid depending on building the compiler)
72 ./x.py test --stage 0 --no-doc library/core library/std
74 # execute all doc tests
78 * `doc` - a command for building documentation. Like above can take arguments
81 ## Configuring rustbuild
83 There are currently two methods for configuring the rustbuild build system.
85 First, rustbuild offers a TOML-based configuration system with a `config.toml`
86 file. An example of this configuration can be found at `config.toml.example`,
87 and the configuration file can also be passed as `--config path/to/config.toml`
88 if the build system is being invoked manually (via the python script).
90 Next, the `./configure` options serialized in `config.mk` will be
91 parsed and read. That is, if any `./configure` options are passed, they'll be
92 handled naturally. `./configure` should almost never be used for local
93 installations, and is primarily useful for CI. Prefer to customize behavior
96 Finally, rustbuild makes use of the [gcc-rs crate] which has [its own
97 method][env-vars] of configuring C compilers and C flags via environment
100 [gcc-rs crate]: https://github.com/alexcrichton/gcc-rs
101 [env-vars]: https://github.com/alexcrichton/gcc-rs#external-configuration-via-environment-variables
105 The rustbuild build system goes through a few phases to actually build the
106 compiler. What actually happens when you invoke rustbuild is:
108 1. The entry point script, `x.py` is run. This script is
109 responsible for downloading the stage0 compiler/Cargo binaries, and it then
110 compiles the build system itself (this folder). Finally, it then invokes the
111 actual `bootstrap` binary build system.
112 2. In Rust, `bootstrap` will slurp up all configuration, perform a number of
113 sanity checks (compilers exist for example), and then start building the
115 3. The stage0 `cargo` downloaded earlier is used to build the standard library
116 and the compiler, and then these binaries are then copied to the `stage1`
117 directory. That compiler is then used to generate the stage1 artifacts which
118 are then copied to the stage2 directory, and then finally the stage2
119 artifacts are generated using that compiler.
121 The goal of each stage is to (a) leverage Cargo as much as possible and failing
122 that (b) leverage Rust as much as possible!
124 ## Incremental builds
126 You can configure rustbuild to use incremental compilation. Because
127 incremental is new and evolving rapidly, if you want to use it, it is
128 recommended that you replace the snapshot with a locally installed
129 nightly build of rustc. You will want to keep this up to date.
131 To follow this course of action, first thing you will want to do is to
132 install a nightly, presumably using `rustup`. You will then want to
133 configure your directory to use this build, like so:
136 # configure to use local rust instead of downloading a beta.
137 # `--local-rust-root` is optional here. If elided, we will
138 # use whatever rustc we find on your PATH.
139 $ ./configure --local-rust-root=~/.cargo/ --enable-local-rebuild
142 After that, you can use the `--incremental` flag to actually do
146 $ ./x.py build --incremental
149 The `--incremental` flag will store incremental compilation artifacts
150 in `build/<host>/stage0-incremental`. Note that we only use incremental
151 compilation for the stage0 -> stage1 compilation -- this is because
152 the stage1 compiler is changing, and we don't try to cache and reuse
153 incremental artifacts across different versions of the compiler. For
154 this reason, `--incremental` defaults to `--stage 1` (though you can
155 manually select a higher stage, if you prefer).
157 You can always drop the `--incremental` to build as normal (but you
158 will still be using the local nightly as your bootstrap).
162 This build system houses all output under the `build` directory, which looks
166 # Root folder of all output. Everything is scoped underneath here
169 # Location where the stage0 compiler downloads are all cached. This directory
170 # only contains the tarballs themselves as they're extracted elsewhere.
177 # Output directory for building this build system itself. The stage0
178 # cargo/rustc are used to build the build system into this location.
183 # Output of the dist-related steps like dist-std, dist-rustc, and dist-docs
186 # Temporary directory used for various input/output as part of various stages
189 # Each remaining directory is scoped by the "host" triple of compilation at
191 x86_64-unknown-linux-gnu/
193 # The build artifacts for the `compiler-rt` library for the target this
194 # folder is under. The exact layout here will likely depend on the platform,
195 # and this is also built with CMake so the build system is also likely
200 # Output folder for LLVM if it is compiled for this target
203 # build folder (e.g. the platform-specific build system). Like with
204 # compiler-rt this is compiled with CMake
207 # Installation of LLVM. Note that we run the equivalent of 'make install'
208 # for LLVM to setup these folders.
215 # Output folder for all documentation of this target. This is what's filled
216 # in whenever the `doc` step is run.
219 # Output for all compiletest-based test suites
226 # Location where the stage0 Cargo and Rust compiler are unpacked. This
227 # directory is purely an extracted and overlaid tarball of these two (done
228 # by the bootstrapy python script). In theory the build system does not
229 # modify anything under this directory afterwards.
232 # These to build directories are the cargo output directories for builds of
233 # the standard library and compiler, respectively. Internally these may also
234 # have other target directories, which represent artifacts being compiled
235 # from the host to the specified target.
237 # Essentially, each of these directories is filled in by one `cargo`
238 # invocation. The build system instruments calling Cargo in the right order
239 # with the right variables to ensure these are filled in correctly.
245 # This is a special case of the above directories, **not** filled in via
246 # Cargo but rather the build system itself. The stage0 compiler already has
247 # a set of target libraries for its own host triple (in its own sysroot)
248 # inside of stage0/. When we run the stage0 compiler to bootstrap more
249 # things, however, we don't want to use any of these libraries (as those are
250 # the ones that we're building). So essentially, when the stage1 compiler is
251 # being compiled (e.g. after libstd has been built), *this* is used as the
252 # sysroot for the stage0 compiler being run.
254 # Basically this directory is just a temporary artifact use to configure the
255 # stage0 compiler to ensure that the libstd we just built is used to
256 # compile the stage1 compiler.
259 # These output directories are intended to be standalone working
260 # implementations of the compiler (corresponding to each stage). The build
261 # system will link (using hard links) output from stageN-{std,rustc} into
262 # each of these directories.
264 # In theory there is no extra build output in these directories.
272 The current build is unfortunately not quite as simple as `cargo build` in a
273 directory, but rather the compiler is split into three different Cargo projects:
275 * `library/std` - the standard library
276 * `library/test` - testing support, depends on libstd
277 * `compiler/rustc` - the actual compiler itself
279 Each "project" has a corresponding Cargo.lock file with all dependencies, and
280 this means that building the compiler involves running Cargo three times. The
281 structure here serves two goals:
283 1. Facilitating dependencies coming from crates.io. These dependencies don't
284 depend on `std`, so libstd is a separate project compiled ahead of time
285 before the actual compiler builds.
286 2. Splitting "host artifacts" from "target artifacts". That is, when building
287 code for an arbitrary target you don't need the entire compiler, but you'll
288 end up needing libraries like libtest that depend on std but also want to use
289 crates.io dependencies. Hence, libtest is split out as its own project that
290 is sequenced after `std` but before `rustc`. This project is built for all
293 There is some loss in build parallelism here because libtest can be compiled in
294 parallel with a number of rustc artifacts, but in theory the loss isn't too bad!
298 We've actually got quite a few tools that we use in the compiler's build system
299 and for testing. To organize these, each tool is a project in `src/tools` with a
300 corresponding `Cargo.toml`. All tools are compiled with Cargo (currently having
301 independent `Cargo.lock` files) and do not currently explicitly depend on the
302 compiler or standard library. Compiling each tool is sequenced after the
303 appropriate libstd/libtest/librustc compile above.
305 ## Extending rustbuild
307 So you'd like to add a feature to the rustbuild build system or just fix a bug.
308 Great! One of the major motivational factors for moving away from `make` is that
309 Rust is in theory much easier to read, modify, and write. If you find anything
310 excessively confusing, please open an issue on this and we'll try to get it
311 documented or simplified pronto.
313 First up, you'll probably want to read over the documentation above as that'll
314 give you a high level overview of what rustbuild is doing. You also probably
315 want to play around a bit yourself by just getting it up and running before you
316 dive too much into the actual build system itself.
318 After that, each module in rustbuild should have enough documentation to keep
319 you up and running. Some general areas that you may be interested in modifying
322 * Adding a new build tool? Take a look at `bootstrap/tool.rs` for examples of
324 * Adding a new compiler crate? Look no further! Adding crates can be done by
325 adding a new directory with `Cargo.toml` followed by configuring all
326 `Cargo.toml` files accordingly.
327 * Adding a new dependency from crates.io? This should just work inside the
328 compiler artifacts stage (everything other than libtest and libstd).
329 * Adding a new configuration option? You'll want to modify `bootstrap/flags.rs`
330 for command line flags and then `bootstrap/config.rs` to copy the flags to the
332 * Adding a sanity check? Take a look at `bootstrap/sanity.rs`.
334 If you have any questions feel free to reach out on `#infra` channel in the
335 [Rust Discord server][rust-discord] or ask on internals.rust-lang.org. When
336 you encounter bugs, please file issues on the rust-lang/rust issue tracker.
338 [rust-discord]: https://discord.gg/rust-lang