|
1 | 1 | # 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 | +```toml |
| 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 -i --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 `-i` (or `--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 rustc from `src` again, this time with the stage1 compiler (this part is new) |
| 76 | + - The resulting compiler here is called the "stage2" compiler |
| 77 | +- Build libstd with stage2 compiler |
| 78 | +- Build librustdoc and a bunch of other things |
| 79 | + |
| 80 | +### Creating a rustup toolchain |
| 81 | + |
| 82 | +Once you have successfully built rustc, you will have created a bunch |
| 83 | +of files in your `build` directory. In order to actually run the |
| 84 | +resulting rustc, we recommend creating rustup toolchains. The first |
| 85 | +one will run the stage1 compiler (which we built above). The second |
| 86 | +will execute the stage2 compiler (which we did not build, but which |
| 87 | +you will likely need to build at some point; for example, if you want |
| 88 | +to run the entire test suite). |
| 89 | + |
| 90 | +``` |
| 91 | +> rustup toolchain link stage1 build/<host-triple>/stage1 |
| 92 | +> rustup toolchain link stage2 build/<host-triple>/stage2 |
| 93 | +``` |
| 94 | + |
| 95 | +Now you can run the rustc you built with. If you run with `-vV`, you |
| 96 | +should see a version number ending in `-dev`, indicating a build from |
| 97 | +your local environment: |
| 98 | + |
| 99 | +``` |
| 100 | +> rustc +stage1 -vV |
| 101 | +rustc 1.25.0-dev |
| 102 | +binary: rustc |
| 103 | +commit-hash: unknown |
| 104 | +commit-date: unknown |
| 105 | +host: x86_64-unknown-linux-gnu |
| 106 | +release: 1.25.0-dev |
| 107 | +LLVM version: 4.0 |
| 108 | +``` |
| 109 | + |
| 110 | +### Other x.py commands |
| 111 | + |
| 112 | +Here are a few other useful x.py commands. We'll cover some of them in detail in other sections: |
| 113 | + |
| 114 | +- Building things: |
| 115 | + - `./x.py clean` -- clean up the build directory (`rm -rf build` works too, but then you have to rebuild LLVM) |
| 116 | + - `./x.py build --stage 1` -- builds everything using the stage 1 compiler, not just up to libstd |
| 117 | + - `./x.py build` -- builds the stage2 compiler |
| 118 | +- Running tests (see the section [running tests](./running-tests.html) for more details): |
| 119 | + - `./x.py test --stage 1 src/libstd` -- runs the `#[test]` tests from libstd |
| 120 | + - `./x.py test --stage 1 src/test/run-pass` -- runs the `run-pass` test suite |
0 commit comments