Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Improve feature gate and x.py docs #1701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Improve feature gate and x.py docs #1701
Changes from all commits
51fe17f
dd6a850
8c6a337
cfe2046
19c7c86
8e11652
5f5394f
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
How to build and run the compiler
The compiler is built using a tool called
x.py
. You will need to have Python installed to run it.For instructions on how to install Python and other prerequisites, see the
rust-lang/rust
README.Get the source code
The main repository is
rust-lang/rust
. This contains the compiler, the standard library (includingcore
,alloc
,test
,proc_macro
, etc), and a bunch of tools (e.g.rustdoc
, the bootstrapping infrastructure, etc).The very first step to work on
rustc
is to clone the repository:git clone https://github.com/rust-lang/rust.git cd rust
Shallow clone the repository
Due to the size of the repository, cloning on a slower internet connection can take a long time. To sidestep this, you can use the
--depth N
option with thegit clone
command. This instructsgit
to perform a "shallow clone", cloning the repository but truncating it to the lastN
commits.Passing
--depth 1
tellsgit
to clone the repository but truncate the history to the latest commit that is on themaster
branch, which is usually fine for browsing the source code or building the compiler.git clone --depth 1 https://github.com/rust-lang/rust.git cd rust
What is
x.py
?x.py
is the build tool for therust
repository. It can build docs, run tests, and compile the compiler and standard library.This chapter focuses on the basics to be productive, but if you want to learn more about
x.py
, read this chapter.Running
x.py
The
x.py
command can be run directly on most Unix systems in the following format:This is how the documentation and examples assume you are running
x.py
. Some alternative ways are:On Windows, the Powershell commands may give you an error that looks like this:
You can avoid this error by allowing powershell to run local scripts:
Running
x.py
slightly more convenientlyThere is a binary that wraps
x.py
calledx
insrc/tools/x
. All it does is runx.py
, but it can be installed system-wide and run from any subdirectory of a checkout. It also looks up the appropriate version ofpython
to use.You can install it with
cargo install --path src/tools/x
.Create a
config.toml
To start, run
./x.py setup
and select thecompiler
defaults. This will do some initialization and create aconfig.toml
for you with reasonable defaults. If you use a different default (which you'll likely want to do if you want to contribute to an area of rust other than the compiler, such as rustdoc), make sure to read information about that default (located insrc/bootstrap/defaults
) as the build process may be different for other defaults.Alternatively, you can write
config.toml
by hand. Seeconfig.example.toml
for all the available settings and explanations of them. Seesrc/bootstrap/defaults
for common settings to change.If you have already built
rustc
and you change settings related to LLVM, then you may have to executerm -rf build
for subsequent configuration changes to take effect. Note that./x.py clean
will not cause a rebuild of LLVM.Common
x.py
commandsHere are the basic invocations of the
x.py
commands most commonly used when working onrustc
,std
,rustdoc
, and other tools../x.py check
./x.py build
rustc
,std
, andrustdoc
./x.py test
./x.py fmt
As written, these commands are reasonable starting points. However, there are additional options and arguments for each of them that are worth learning for serious development work. In particular,
./x.py build
and./x.py test
provide many ways to compile or test a subset of the code, which can save a lot of time.Also, note that
x.py
supports all kinds of path suffixes forcompiler
,library
, andsrc/tools
directories. So, you can simply runx.py test tidy
instead ofx.py test src/tools/tidy
. Or,x.py build std
instead ofx.py build library/std
.See the chapters on building, testing, and rustdoc for more details.
Building the compiler
Note that building will require a relatively large amount of storage space. You may want to have upwards of 10 or 15 gigabytes available to build the compiler.
Once you've created a
config.toml
, you are now ready to runx.py
. There are a lot of options here, but let's start with what is probably the best "go to" command for building a local compiler:This may look like it only builds the standard library, but that is not the case. What this command does is the following:
std
using the stage0 compilerrustc
using the stage0 compilerstd
using the stage1 compilerThis final product (stage1 compiler + libs built using that compiler) is what you need to build other Rust programs (unless you use
#![no_std]
or#![no_core]
).You will probably find that building the stage1
std
is a bottleneck for you, but fear not, there is a (hacky) workaround... see the section on avoiding rebuilds for std.Sometimes you don't need a full build. When doing some kind of "type-based refactoring", like renaming a method, or changing the signature of some function, you can use
./x.py check
instead for a much faster build.Note that this whole command just gives you a subset of the full
rustc
build. The fullrustc
build (what you get with./x.py build --stage 2 compiler/rustc
) has quite a few more steps:rustc
with the stage1 compiler.std
with stage2 compiler.librustdoc
and a bunch of other things with the stage2 compiler.You almost never need to do this.
Build specific components
If you are working on the standard library, you probably don't need to build the compiler unless you are planning to use a recently added nightly feature. Instead, you can just build using the bootstrap compiler.
If you choose the
library
profile when runningx.py setup
, you can omit--stage 0
(it's the default).Creating a rustup toolchain
Once you have successfully built
rustc
, you will have created a bunch of files in yourbuild
directory. In order to actually run the resultingrustc
, we recommend creating rustup toolchains. The first one will run the stage1 compiler (which we built above). The second will execute the stage2 compiler (which we did not build, but which you will likely need to build at some point; for example, if you want to run the entire test suite).rustup toolchain link stage0 build/host/stage0-sysroot # beta compiler + stage0 std rustup toolchain link stage1 build/host/stage1 rustup toolchain link stage2 build/host/stage2
Now you can run the
rustc
you built with. If you run with-vV
, you should see a version number ending in-dev
, indicating a build from your local environment:The rustup toolchain points to the specified toolchain compiled in your
build
directory, so the rustup toolchain will be updated wheneverx.py build
orx.py test
are run for that toolchain/stage.Note: the toolchain we've built does not include
cargo
. In this case,rustup
will fall back to usingcargo
from the installednightly
,beta
, orstable
toolchain (in that order). If you need to use unstablecargo
flags, be sure to runrustup install nightly
if you haven't already. See the rustup documentation on custom toolchains.Note: rust-analyzer and IntelliJ Rust plugin use a component called
rust-analyzer-proc-macro-srv
to work with proc macros. If you intend to use a custom toolchain for a project (e.g. viarustup override set stage1
) you may want to build this component:Building targets for cross-compilation
To produce a compiler that can cross-compile for other targets, pass any number of
target
flags tox.py build
. For example, if your host platform isx86_64-unknown-linux-gnu
and your cross-compilation target iswasm32-wasi
, you can build with:Note that if you want the resulting compiler to be able to build crates that involve proc macros or build scripts, you must be sure to explicitly build target support for the host platform (in this case,
x86_64-unknown-linux-gnu
).If you want to always build for other targets without needing to pass flags to
x.py build
, you can configure this in the[build]
section of yourconfig.toml
like so:Note that building for some targets requires having external dependencies installed (e.g. building musl targets requires a local copy of musl). Any target-specific configuration (e.g. the path to a local copy of musl) will need to be provided by your
config.toml
. Please seeconfig.example.toml
for information on target-specific configuration keys.For examples of the complete configuration necessary to build a target, please visit the rustc book, select any target under the "Platform Support" heading on the left, and see the section related to building a compiler for that target. For targets without a corresponding page in the rustc book, it may be useful to inspect the Dockerfiles that the Rust infrastructure itself uses to set up and configure cross-compilation.
If you have followed the directions from the prior section on creating a rustup toolchain, then once you have built your compiler you will be able to use it to cross-compile like so:
Other
x.py
commandsHere are a few other useful
x.py
commands. We'll cover some of them in detail in other sections:./x.py build
– builds everything using the stage 1 compiler, not just up tostd
./x.py build --stage 2
– builds everything with the stage 2 compiler includingrustdoc
./x.py test library/std
– runs the unit tests and integration tests fromstd
./x.py test tests/ui
– runs theui
test suite./x.py test tests/ui/const-generics
- runs all the tests in theconst-generics/
subdirectory of theui
test suite./x.py test tests/ui/const-generics/const-types.rs
- runs the single testconst-types.rs
from theui
test suiteCleaning out build directories
Sometimes you need to start fresh, but this is normally not the case. If you need to run this then
rustbuild
is most likely not acting right and you should file a bug as to what is going wrong. If you do need to clean everything up then you only need to run one command!rm -rf build
works too, but then you have to rebuild LLVM, which can take a long time even on fast computers.