]> git.proxmox.com Git - rustc.git/blob - src/doc/rustc-guide/src/building/how-to-build-and-run.md
New upstream version 1.41.1+dfsg1
[rustc.git] / src / doc / rustc-guide / src / building / how-to-build-and-run.md
1 # How to Build and Run the Compiler
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.
6 The default configuration is oriented towards running the compiler as a user,
7 not a developer.
8
9 ## Create a config.toml
10
11 To start, copy [`config.toml.example`] to `config.toml`:
12
13 [`config.toml.example`]: https://github.com/rust-lang/rust/blob/master/config.toml.example
14
15 ```bash
16 > cd $RUST_CHECKOUT
17 > cp config.toml.example config.toml
18 ```
19
20 Then you will want to open up the file and change the following
21 settings (and possibly others, such as `llvm.ccache`):
22
23 ```toml
24 [llvm]
25 # Enables LLVM assertions, which will check that the LLVM bitcode generated
26 # by the compiler is internally consistent. These are particularly helpful
27 # if you edit `codegen`.
28 assertions = true
29
30 [rust]
31 # This will make your build more parallel; it costs a bit of runtime
32 # performance perhaps (less inlining) but it's worth it.
33 codegen-units = 0
34
35 # This enables full debuginfo and debug assertions. The line debuginfo is also
36 # enabled by `debuginfo-level = 1`. Full debuginfo is also enabled by
37 # `debuginfo-level = 2`. Debug assertions can also be enabled with
38 # `debug-assertions = true`. Note that `debug = true` will make your build
39 # slower, so you may want to try individually enabling debuginfo and assertions
40 # or enable only line debuginfo which is basically free.
41 debug = true
42 ```
43
44 If you have already built `rustc`, then you may have to execute `rm -rf build` for subsequent
45 configuration changes to take effect. Note that `./x.py clean` will not cause a
46 rebuild of LLVM, so if your configuration change affects LLVM, you will need to
47 manually `rm -rf build/` before rebuilding.
48
49 ## What is `x.py`?
50
51 `x.py` is the script used to orchestrate the tooling in the `rustc` repository.
52 It is the script that can build docs, run tests, and compile `rustc`.
53 It is the now preferred way to build `rustc` and it replaces the old makefiles
54 from before. Below are the different ways to utilize `x.py` in order to
55 effectively deal with the repo for various common tasks.
56
57 This chapter focuses on the basics to be productive, but
58 if you want to learn more about `x.py`, read its README.md
59 [here](https://github.com/rust-lang/rust/blob/master/src/bootstrap/README.md).
60
61 ## Bootstrapping
62
63 One thing to keep in mind is that `rustc` is a _bootstrapping_
64 compiler. That is, since `rustc` is written in Rust, we need to use an
65 older version of the compiler to compile the newer version. In
66 particular, the newer version of the compiler and some of the artifacts needed
67 to build it, such as `libstd` and other tooling, may use some unstable features
68 internally, requiring a specific version which understands these unstable
69 features.
70
71 The result is that compiling `rustc` is done in stages:
72
73 - **Stage 0:** the stage0 compiler is usually (you can configure `x.py` to use
74 something else) the current _beta_ `rustc` compiler and its associated dynamic
75 libraries (which `x.py` will download for you). This stage0 compiler is then
76 used only to compile `rustbuild`, `std`, and `rustc`. When compiling
77 `rustc`, this stage0 compiler uses the freshly compiled `std`.
78 There are two concepts at play here: a compiler (with its set of dependencies)
79 and its 'target' or 'object' libraries (`std` and `rustc`).
80 Both are staged, but in a staggered manner.
81 - **Stage 1:** the code in your clone (for new version) is then
82 compiled with the stage0 compiler to produce the stage1 compiler.
83 However, it was built with an older compiler (stage0), so to
84 optimize the stage1 compiler we go to next the stage.
85 - In theory, the stage1 compiler is functionally identical to the
86 stage2 compiler, but in practice there are subtle differences. In
87 particular, the stage1 compiler itself was built by stage0 and
88 hence not by the source in your working directory: this means that
89 the symbol names used in the compiler source may not match the
90 symbol names that would have been made by the stage1 compiler.
91 This can be important when using dynamic linking (e.g., with
92 derives. Sometimes this means that some tests don't work when run
93 with stage1.
94 - **Stage 2:** we rebuild our stage1 compiler with itself to produce
95 the stage2 compiler (i.e. it builds itself) to have all the _latest
96 optimizations_. (By default, we copy the stage1 libraries for use by
97 the stage2 compiler, since they ought to be identical.)
98 - _(Optional)_ **Stage 3**: to sanity check our new compiler, we
99 can build the libraries with the stage2 compiler. The result ought
100 to be identical to before, unless something has broken.
101
102 To read more about the bootstrap process, [read this chapter][bootstrap].
103
104 [bootstrap]: ./bootstrapping.md
105
106 ## Building the Compiler
107
108 To build a compiler, run `./x.py build`. This will do the whole bootstrapping
109 process described above, producing a usable compiler toolchain from the source
110 code you have checked out. This takes a long time, so it is not usually what
111 you want to actually run (more on this later).
112
113 There are many flags you can pass to the build command of `x.py` that can be
114 beneficial to cutting down compile times or fitting other things you might
115 need to change. They are:
116
117 ```txt
118 Options:
119 -v, --verbose use verbose output (-vv for very verbose)
120 -i, --incremental use incremental compilation
121 --config FILE TOML configuration file for build
122 --build BUILD build target of the stage0 compiler
123 --host HOST host targets to build
124 --target TARGET target targets to build
125 --on-fail CMD command to run on failure
126 --stage N stage to build
127 --keep-stage N stage to keep without recompiling
128 --src DIR path to the root of the rust checkout
129 -j, --jobs JOBS number of jobs to run in parallel
130 -h, --help print this help message
131 ```
132
133 For hacking, often building the stage 1 compiler is enough, but for
134 final testing and release, the stage 2 compiler is used.
135
136 `./x.py check` is really fast to build the rust compiler.
137 It is, in particular, very useful when you're doing some kind of
138 "type-based refactoring", like renaming a method, or changing the
139 signature of some function.
140
141 <a name=command></a>
142
143 Once you've created a config.toml, you are now ready to run
144 `x.py`. There are a lot of options here, but let's start with what is
145 probably the best "go to" command for building a local rust:
146
147 ```bash
148 ./x.py build -i --stage 1 src/libstd
149 ```
150
151 This may *look* like it only builds libstd, but that is not the case.
152 What this command does is the following:
153
154 - Build `libstd` using the stage0 compiler (using incremental)
155 - Build `librustc` using the stage0 compiler (using incremental)
156 - This produces the stage1 compiler
157 - Build libstd using the stage1 compiler (cannot use incremental)
158
159 This final product (stage1 compiler + libs built using that compiler)
160 is what you need to build other rust programs (unless you use `#![no_std]` or
161 `#![no_core]`).
162
163 The command includes the `-i` switch which enables incremental compilation.
164 This will be used to speed up the first two steps of the process:
165 in particular, if you make a small change, we ought to be able to use your old
166 results to make producing the stage1 **compiler** faster.
167
168 Unfortunately, incremental cannot be used to speed up making the
169 stage1 libraries. This is because incremental only works when you run
170 the *same compiler* twice in a row. In this case, we are building a
171 *new stage1 compiler* every time. Therefore, the old incremental
172 results may not apply. **As a result, you will probably find that
173 building the stage1 `libstd` is a bottleneck for you** -- but fear not,
174 there is a (hacky) workaround. See [the section on "recommended
175 workflows"](./suggested.md) below.
176
177 Note that this whole command just gives you a subset of the full `rustc`
178 build. The **full** `rustc` build (what you get if you just say `./x.py
179 build`) has quite a few more steps:
180
181 - Build `librustc` and `rustc` with the stage1 compiler.
182 - The resulting compiler here is called the "stage2" compiler.
183 - Build libstd with stage2 compiler.
184 - Build librustdoc and a bunch of other things with the stage2 compiler.
185
186 <a name=toolchain></a>
187
188 ## Build specific components
189
190 Build only the libcore library
191
192 ```bash
193 ./x.py build src/libcore
194 ```
195
196 Build the libcore and libproc_macro library only
197
198 ```bash
199 ./x.py build src/libcore src/libproc_macro
200 ```
201
202 Build only libcore up to Stage 1
203
204 ```bash
205 ./x.py build src/libcore --stage 1
206 ```
207
208 Sometimes you might just want to test if the part you’re working on can
209 compile. Using these commands you can test that it compiles before doing
210 a bigger build to make sure it works with the compiler. As shown before
211 you can also pass flags at the end such as --stage.
212
213 ## Creating a rustup toolchain
214
215 Once you have successfully built `rustc`, you will have created a bunch
216 of files in your `build` directory. In order to actually run the
217 resulting `rustc`, we recommend creating rustup toolchains. The first
218 one will run the stage1 compiler (which we built above). The second
219 will execute the stage2 compiler (which we did not build, but which
220 you will likely need to build at some point; for example, if you want
221 to run the entire test suite).
222
223 ```bash
224 rustup toolchain link stage1 build/<host-triple>/stage1
225 rustup toolchain link stage2 build/<host-triple>/stage2
226 ```
227
228 The `<host-triple>` would typically be one of the following:
229
230 - Linux: `x86_64-unknown-linux-gnu`
231 - Mac: `x86_64-apple-darwin`
232 - Windows: `x86_64-pc-windows-msvc`
233
234 Now you can run the `rustc` you built with. If you run with `-vV`, you
235 should see a version number ending in `-dev`, indicating a build from
236 your local environment:
237
238 ```bash
239 $ rustc +stage1 -vV
240 rustc 1.25.0-dev
241 binary: rustc
242 commit-hash: unknown
243 commit-date: unknown
244 host: x86_64-unknown-linux-gnu
245 release: 1.25.0-dev
246 LLVM version: 4.0
247 ```
248 ## Other `x.py` commands
249
250 Here are a few other useful `x.py` commands. We'll cover some of them in detail
251 in other sections:
252
253 - Building things:
254 - `./x.py clean` – clean up the build directory (`rm -rf build` works too,
255 but then you have to rebuild LLVM)
256 - `./x.py build --stage 1` – builds everything using the stage 1 compiler,
257 not just up to libstd
258 - `./x.py build` – builds the stage2 compiler
259 - Running tests (see the [section on running tests](../tests/running.html) for
260 more details):
261 - `./x.py test --stage 1 src/libstd` – runs the `#[test]` tests from libstd
262 - `./x.py test --stage 1 src/test/ui` – runs the `ui` test suite
263 - `./x.py test --stage 1 src/test/ui/const-generics` - runs all the tests in
264 the `const-generics/` subdirectory of the `ui` test suite
265 - `./x.py test --stage 1 src/test/ui/const-generics/const-types.rs` - runs
266 the single test `const-types.rs` from the `ui` test suite
267
268 ### Cleaning out build directories
269
270 Sometimes you need to start fresh, but this is normally not the case.
271 If you need to run this then rustbuild is most likely not acting right and
272 you should file a bug as to what is going wrong. If you do need to clean
273 everything up then you only need to run one command!
274
275 ```bash
276 ./x.py clean
277 ```