Skip to content

Commit 5f7a985

Browse files
committed
extend bootstrap related documentations
Signed-off-by: ozkanonur <[email protected]>
1 parent 7352353 commit 5f7a985

File tree

3 files changed

+195
-5
lines changed

3 files changed

+195
-5
lines changed

src/building/bootstrapping.md

+80-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,23 @@ rustc, then uses it to compile the new compiler.
1818

1919
## Stages of bootstrapping
2020

21-
Compiling `rustc` is done in stages. Here's a diagram, adapted from Joshua Nelson's
22-
[talk on bootstrapping][rustconf22-talk] at RustConf 2022, with detailed explanations below.
21+
The build system defers most of the complicated logic managing invocations
22+
of rustc and rustdoc to Cargo itself. However, moving through various stages
23+
and copying artifacts is still necessary for it to do. Each time rustbuild
24+
is invoked, it will iterate through the list of predefined steps and execute
25+
each serially in turn if it matches the paths passed or is a default rule.
26+
For each step rustbuild relies on the step internally being incremental and
27+
parallel. Note, though, that the `-j` parameter to rustbuild gets forwarded
28+
to appropriate test harnesses and such.
29+
30+
Most of the "meaty" steps that matter are backed by Cargo, which does indeed
31+
have its own parallelism and incremental management. Later steps, like
32+
tests, aren't incremental and simply run the entire suite currently.
33+
However, compiletest itself tries to avoid running tests when the artifacts
34+
that are involved (mainly the compiler) haven't changed.
35+
36+
Here's a diagram, adapted from Joshua Nelson's [talk on bootstrapping][rustconf22-talk]
37+
at RustConf 2022, with detailed explanations below.
2338

2439
The `A`, `B`, `C`, and `D` show the ordering of the stages of bootstrapping.
2540
<span style="background-color: lightblue; color: black">Blue</span> nodes are downloaded,
@@ -448,3 +463,66 @@ This is an incomplete reference for the outputs generated by bootstrap:
448463
| copy `rustdoc` | `build/HOST/stage2/bin` |
449464

450465
`--stage=2` stops here.
466+
467+
### Clarification of build command's stdout
468+
469+
In this part, we will investigate the build command's stdout in an action
470+
(similar, but more detailed and complete documentation compare to topic above).
471+
When you execute `x.py build` command, the build output will be something
472+
like the following:
473+
474+
```text
475+
Building stage0 std artifacts
476+
Copying stage0 std
477+
Building stage0 test artifacts
478+
Copying stage0 test
479+
Building stage0 compiler artifacts
480+
Copying stage0 rustc
481+
Assembling stage1 compiler
482+
Building stage1 std artifacts
483+
Copying stage1 std
484+
Building stage1 test artifacts
485+
Copying stage1 test
486+
Building stage1 compiler artifacts
487+
Copying stage1 rustc
488+
Assembling stage2 compiler
489+
Uplifting stage1 std
490+
Uplifting stage1 test
491+
Uplifting stage1 rustc
492+
```
493+
494+
#### Building stage0 {std,test,compiler} artifacts
495+
496+
These steps use the provided (downloaded, usually) compiler to compile the
497+
local Rust source into libraries we can use.
498+
499+
#### Copying stage0 {std,test,rustc}
500+
501+
This copies the build output from Cargo into
502+
`build/$HOST/stage0-sysroot/lib/rustlib/$ARCH/lib`.
503+
[comment]: FIXME: this step's documentation should be expanded -- the information already here may be incorrect.
504+
505+
#### Assembling stage1 compiler
506+
507+
This copies the libraries we built in "building stage0 ... artifacts" into
508+
the stage1 compiler's lib directory. These are the host libraries that the
509+
compiler itself uses to run. These aren't actually used by artifacts the new
510+
compiler generates. This step also copies the rustc and rustdoc binaries we
511+
generated into `build/$HOST/stage/bin`.
512+
513+
The stage1/bin/rustc is a fully functional compiler, but it doesn't yet have
514+
any libraries to link built binaries or libraries to. The next 3 steps will
515+
provide those libraries for it; they are mostly equivalent to constructing
516+
the stage1/bin compiler so we don't go through them individually.
517+
518+
#### Uplifting stage1 {std,test,rustc}
519+
520+
This step copies the libraries from the stage1 compiler sysroot into the
521+
stage2 compiler. This is done to avoid rebuilding the compiler; libraries
522+
we'd build in this step should be identical (in function, if not necessarily
523+
identical on disk) so there's no need to recompile the compiler again. Note
524+
that if you want to, you can enable the full-bootstrap option to change this
525+
behavior.
526+
527+
Each step is driven by a separate Cargo project and rustbuild orchestrates
528+
copying files between steps and otherwise preparing for Cargo to run.

src/building/how-to-build-and-run.md

+111-3
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,43 @@ clean` will not cause a rebuild of LLVM.
5454

5555
## Building the Compiler
5656

57+
Building compiler is unfortunately not quite as simple as building other
58+
rust projects using`cargo build`. Instead, we use `x.py` script for building
59+
the compiler. The main reason behind it is the bootstrap stages where you use
60+
the output of one cargo invocation as the input compiler for another. Which
61+
is not possible with `cargo` itself.
62+
5763
Note that building will require a relatively large amount of storage space.
5864
You may want to have upwards of 10 or 15 gigabytes available to build the compiler.
5965

60-
Once you've created a `config.toml`, you are now ready to run
61-
`x.py`. There are a lot of options here, but let's start with what is
62-
probably the best "go to" command for building a local compiler:
66+
The script accepts commands, flags, and arguments to determine what to do.
67+
To see capabilities of x in an action, check the following examples:
68+
69+
```bash
70+
# build the whole compiler
71+
./x.py build --stage 2
72+
73+
# build the stage1 compiler
74+
./x.py build
75+
76+
# build stage0 libstd
77+
./x.py build --stage 0 library/std
78+
79+
# build a particular crate in stage0
80+
./x.py build --stage 0 library/test
81+
```
82+
83+
If files that would normally be rebuilt from stage 0 are dirty, the rebuild can be
84+
overridden using `--keep-stage 0`. Using `--keep-stage n` will skip all steps
85+
that belong to stage n or earlier:
86+
87+
```bash
88+
# build stage 1, keeping old build products for stage 0
89+
./x.py build --keep-stage 0
90+
```
91+
92+
Let's dive into details with probably the best "go to"
93+
command for building a local compiler:
6394

6495
```bash
6596
./x.py build library
@@ -98,6 +129,24 @@ build. The **full** `rustc` build (what you get with `./x.py build
98129

99130
You almost never need to do this.
100131

132+
## Incremental builds
133+
134+
You can configure rustbuild to use incremental compilation with the
135+
`--incremental` flag:
136+
137+
```sh
138+
$ ./x.py build --incremental
139+
```
140+
141+
The `--incremental` flag will store incremental compilation artifacts
142+
in `build/<host>/stage0-incremental`. Note that we only use incremental
143+
compilation for the stage0 -> stage1 compilation -- this is because
144+
the stage1 compiler is changing, and we don't try to cache and reuse
145+
incremental artifacts across different versions of the compiler.
146+
147+
You can always drop the `--incremental` to build as normal (note that
148+
you will still be using the downloaded beta as your bootstrap).
149+
101150
## Build specific components
102151

103152
If you are working on the standard library, you probably don't need to build
@@ -242,3 +291,62 @@ everything up then you only need to run one command!
242291

243292
`rm -rf build` works too, but then you have to rebuild LLVM, which can take
244293
a long time even on fast computers.
294+
295+
## Build tools
296+
297+
We've actually got quite a few tools that we use in the compiler's build system
298+
and for testing. To organize these, each tool is a project in `src/tools` with a
299+
corresponding `Cargo.toml`. All tools are compiled with Cargo (currently having
300+
independent `Cargo.lock` files) and do not currently explicitly depend on the
301+
compiler or standard library. Compiling each tool is sequenced after the
302+
appropriate libstd/libtest/librustc compile above.
303+
304+
## Extending rustbuild
305+
306+
So, you'd like to add a feature to the rustbuild build system or just fix a bug.
307+
Great! One of the major motivational factors for moving away from `make` is that
308+
Rust is in theory much easier to read, modify, and write. If you find anything
309+
excessively confusing, please open an issue on this, and we'll try to get it
310+
documented or simplified, pronto.
311+
312+
First up, you'll probably want to read over the documentation above, as that'll
313+
give you a high level overview of what rustbuild is doing. You also probably
314+
want to play around a bit yourself by just getting it up and running before you
315+
dive too much into the actual build system itself.
316+
317+
After that, each module in rustbuild should have enough documentation to keep
318+
you up and running. Some general areas that you may be interested in modifying
319+
are:
320+
321+
* Adding a new build tool? Take a look at `bootstrap/tool.rs` for examples of
322+
other tools.
323+
* Adding a new compiler crate? Look no further! Adding crates can be done by
324+
adding a new directory with `Cargo.toml` followed by configuring all
325+
`Cargo.toml` files accordingly.
326+
* Adding a new dependency from crates.io? This should just work inside the
327+
compiler artifacts stage (everything other than libtest and libstd).
328+
* Adding a new configuration option? You'll want to modify `bootstrap/flags.rs`
329+
for command line flags and then `bootstrap/config.rs` to copy the flags to the
330+
`Config` struct.
331+
* Adding a sanity check? Take a look at `bootstrap/sanity.rs`.
332+
333+
If you make a major change, please remember to:
334+
335+
+ Update `VERSION` in `src/bootstrap/main.rs`.
336+
* Update `changelog-seen = N` in `config.toml.example`.
337+
* Add an entry in `src/bootstrap/CHANGELOG.md`.
338+
339+
A 'major change' includes
340+
341+
* A new option or
342+
* A change in the default options.
343+
344+
Changes that do not affect contributors to the compiler or users
345+
building rustc from source don't need an update to `VERSION`.
346+
347+
If you have any questions, feel free to reach out on the `#t-infra` channel in
348+
the [Rust Zulip server][rust-zulip] or ask on internals.rust-lang.org. When
349+
you encounter bugs, please file issues on [Rust issue tracker][rust-issue-tracker].
350+
351+
[rust-zulip]: https://rust-lang.zulipchat.com/#narrow/stream/242791-t-infra
352+
[rust-issue-tracker]: https://github.com/rust-lang/rust/issues

src/getting-started.md

+4
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ serious development work. In particular, `./x.py build` and `./x.py test`
114114
provide many ways to compile or test a subset of the code, which can save a lot
115115
of time.
116116

117+
Also, note that `x.py` supports all kinds of path suffixes for `compiler`, `library`,
118+
and `src/tools` directories. So, you can simply run `x.py test tidy` instead of
119+
`x.py test src/tools/tidy`. Or, `x.py build std` instead of `x.py build library/std`.
120+
117121
[rust-analyzer]: ./building/suggested.html#configuring-rust-analyzer-for-rustc
118122

119123
See the chapters on [building](./building/how-to-build-and-run.md),

0 commit comments

Comments
 (0)