Skip to content

Commit c28d061

Browse files
committed
how to build and run compiler, first shot
1 parent 67da39e commit c28d061

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

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

+117
Original file line numberDiff line numberDiff line change
@@ -1 +1,118 @@
11
# How to build the compiler and run what you built
2+
3+
The compiler is built using a tool called `x.py`. You will need to
4+
have Python installed to run it. But before we get to that, if you're going to
5+
be hacking on rustc, you'll want to tweak the configuration of the compiler. The default
6+
configuration is oriented towards running the compiler as a user, not a developer.
7+
8+
### Create a config.toml
9+
10+
To start, copy [`config.toml.example`] to `config.toml`:
11+
12+
[`config.toml.example`]: https://github.com/rust-lang/rust/blob/master/config.toml.example
13+
14+
```bash
15+
> cd $RUST_CHECKOUT
16+
> cp config.toml.example config.toml
17+
```
18+
19+
Then you will want to open up the file and change the following
20+
settings (and possibly others, such as `llvm.ccache`):
21+
22+
```
23+
[llvm]
24+
# Enables LLVM assertions, which will check that the LLVM bitcode generated
25+
# by the compiler is internally consistent. These are particularly helpful
26+
# if you edit `trans`.
27+
assertions = true
28+
29+
[rust]
30+
# This enables some assertions, but more importantly it enables the `debug!` logging
31+
# macros that are essential for debugging rustc.
32+
debug-assertions = true
33+
34+
# This will make your build more parallel; it costs a bit of runtime
35+
# performance perhaps (less inlining) but it's worth it.
36+
codegen-units = 0
37+
38+
# I always enable full debuginfo, though debuginfo-lines is more important.
39+
debuginfo = true
40+
41+
# Gives you line numbers for backtraces.
42+
debuginfo-lines = true
43+
44+
# Using the system allocator (instead of jemalloc) means that tools
45+
# like valgrind and memcache work better.
46+
use-jemalloc = false
47+
```
48+
49+
### Running x.py and building a stage1 compiler
50+
51+
Once you've created a config.toml, you are now ready to run
52+
`x.py`. There are a lot of options here, but let's start with what is
53+
probably the best "go to" command for building a local rust:
54+
55+
```
56+
./x.py build --incremental --stage 1 src/libstd
57+
```
58+
59+
What this command will do is the following:
60+
61+
- Using the beta compiler (also called stage 0), it will build the
62+
standard library and rustc from the `src` directory. The resulting
63+
compiler is called the "stage 1" compiler.
64+
- During this build, the `--incremental` switch enables incremental
65+
compilation, so that if you later rebuild after editing things in
66+
`src`, you can save a bit of time.
67+
- Using this stage 1 compiler, it will build the standard library.
68+
(this is what the `src/libstd`) means.
69+
70+
This is just a subset of the full rustc build. The **full** rustc build (what you
71+
get if you just say `./x.py build`) has quite a few more steps:
72+
73+
- Build stage1 rustc with stage0 compiler
74+
- Build libstd with stage1 compiler (up to here is the same)
75+
- Build stage2 rustc with stage1 compiler (this part is new)
76+
- Build libstd with stage2 compiler
77+
- Build librustdoc and a bunch of other things
78+
79+
### Creating a rustup toolchain
80+
81+
Once you have successfully built rustc, you will have created a bunch
82+
of files in your `build` directory. In order to actually run the
83+
resulting rustc, we recommend creating rustup toolchains. The first
84+
one will run the stage1 compiler (which we built above). The second
85+
will execute the stage2 compiler (which we did not build, but which
86+
you will likely build at some point).
87+
88+
```
89+
> rustup toolchain link stage1 build/<host-triple>/stage1
90+
> rustup toolchain link stage2 build/<host-triple>/stage2
91+
```
92+
93+
Now you can run the rustc you built with. If you run with `-vV`, you
94+
should see a version number ending in `-dev`, indicating a build from
95+
your local environment:
96+
97+
```
98+
> rustc +stage1 -vV
99+
rustc 1.25.0-dev
100+
binary: rustc
101+
commit-hash: unknown
102+
commit-date: unknown
103+
host: x86_64-unknown-linux-gnu
104+
release: 1.25.0-dev
105+
LLVM version: 4.0
106+
```
107+
108+
### Other x.py commands
109+
110+
Here are a few other useful x.py commands. We'll cover some of them in detail in other sections:
111+
112+
- Building things:
113+
- `./x.py clean` -- clean up the build directory (`rm -rf build` works too, but then you have to rebuild LLVM)
114+
- `./x.py build --stage 1` -- builds everything using the stage 1 compiler, not just up to libstd
115+
- `./x.py build` -- builds the stage2 compiler
116+
- Running tests (see the section [running tests](./running-tests.md) for more details):
117+
- `./x.py test --stage 1 src/libstd` -- runs the `#[test]` tests from libstd
118+
- `./x.py test --stage 1 src/test/run-pass` -- runs the `run-pass` test suite

0 commit comments

Comments
 (0)