|
| 1 | +# Installing from Source |
| 2 | + |
| 3 | +**Note: This document describes _building_ Rust _from source_. |
| 4 | +This is _not recommended_ if you don't know what you're doing. |
| 5 | +If you just want to install Rust, check out the [README.md](README.md) instead.** |
| 6 | + |
| 7 | +The Rust build system uses a Python script called `x.py` to build the compiler, |
| 8 | +which manages the bootstrapping process. It lives at the root of the project. |
| 9 | +It also uses a file named `config.toml` to determine various configuration |
| 10 | +settings for the build. You can see a full list of options in |
| 11 | +`config.example.toml`. |
| 12 | + |
| 13 | +The `x.py` command can be run directly on most Unix systems in the following |
| 14 | +format: |
| 15 | + |
| 16 | +```sh |
| 17 | +./x.py <subcommand> [flags] |
| 18 | +``` |
| 19 | + |
| 20 | +This is how the documentation and examples assume you are running `x.py`. |
| 21 | +See the [rustc dev guide][rustcguidebuild] if this does not work on your |
| 22 | +platform. |
| 23 | + |
| 24 | +More information about `x.py` can be found by running it with the `--help` flag |
| 25 | +or reading the [rustc dev guide][rustcguidebuild]. |
| 26 | + |
| 27 | +[gettingstarted]: https://rustc-dev-guide.rust-lang.org/getting-started.html |
| 28 | +[rustcguidebuild]: https://rustc-dev-guide.rust-lang.org/building/how-to-build-and-run.html#what-is-xpy |
| 29 | + |
| 30 | +## Dependencies |
| 31 | + |
| 32 | +Make sure you have installed the dependencies: |
| 33 | + |
| 34 | +* `python` 3 or 2.7 |
| 35 | +* `git` |
| 36 | +* A C compiler (when building for the host, `cc` is enough; cross-compiling may |
| 37 | + need additional compilers) |
| 38 | +* `curl` (not needed on Windows) |
| 39 | +* `pkg-config` if you are compiling on Linux and targeting Linux |
| 40 | +* `libiconv` (already included with glibc on Debian-based distros) |
| 41 | + |
| 42 | +To build Cargo, you'll also need OpenSSL (`libssl-dev` or `openssl-devel` on |
| 43 | +most Unix distros). |
| 44 | + |
| 45 | +If building LLVM from source, you'll need additional tools: |
| 46 | + |
| 47 | +* `g++`, `clang++`, or MSVC with versions listed on |
| 48 | + [LLVM's documentation](https://llvm.org/docs/GettingStarted.html#host-c-toolchain-both-compiler-and-standard-library) |
| 49 | +* `ninja`, or GNU `make` 3.81 or later (Ninja is recommended, especially on |
| 50 | + Windows) |
| 51 | +* `cmake` 3.13.4 or later |
| 52 | +* `libstdc++-static` may be required on some Linux distributions such as Fedora |
| 53 | + and Ubuntu |
| 54 | + |
| 55 | +On tier 1 or tier 2 with host tools platforms, you can also choose to download |
| 56 | +LLVM by setting `llvm.download-ci-llvm = true`. |
| 57 | +Otherwise, you'll need LLVM installed and `llvm-config` in your path. |
| 58 | +See [the rustc-dev-guide for more info][sysllvm]. |
| 59 | + |
| 60 | +[sysllvm]: https://rustc-dev-guide.rust-lang.org/building/new-target.html#using-pre-built-llvm |
| 61 | + |
| 62 | + |
| 63 | +## Building on a Unix-like system |
| 64 | + |
| 65 | +### Build steps |
| 66 | + |
| 67 | +1. Clone the [source] with `git`: |
| 68 | + |
| 69 | + ```sh |
| 70 | + git clone https://github.com/rust-lang/rust.git |
| 71 | + cd rust |
| 72 | + ``` |
| 73 | + |
| 74 | +[source]: https://github.com/rust-lang/rust |
| 75 | + |
| 76 | +2. Configure the build settings: |
| 77 | + |
| 78 | + ```sh |
| 79 | + ./configure |
| 80 | + ``` |
| 81 | + |
| 82 | + If you plan to use `x.py install` to create an installation, it is |
| 83 | + recommended that you set the `prefix` value in the `[install]` section to a |
| 84 | + directory: `./configure --set install.prefix=<path>` |
| 85 | + |
| 86 | +3. Build and install: |
| 87 | + |
| 88 | + ```sh |
| 89 | + ./x.py build && ./x.py install |
| 90 | + ``` |
| 91 | + |
| 92 | + When complete, `./x.py install` will place several programs into |
| 93 | + `$PREFIX/bin`: `rustc`, the Rust compiler, and `rustdoc`, the |
| 94 | + API-documentation tool. By default, it will also include [Cargo], Rust's |
| 95 | + package manager. You can disable this behavior by passing |
| 96 | + `--set build.extended=false` to `./configure`. |
| 97 | + |
| 98 | +[Cargo]: https://github.com/rust-lang/cargo |
| 99 | + |
| 100 | +### Configure and Make |
| 101 | + |
| 102 | +This project provides a configure script and makefile (the latter of which just |
| 103 | +invokes `x.py`). `./configure` is the recommended way to programmatically |
| 104 | +generate a `config.toml`. `make` is not recommended (we suggest using `x.py` |
| 105 | +directly), but it is supported and we try not to break it unnecessarily. |
| 106 | + |
| 107 | +```sh |
| 108 | +./configure |
| 109 | +make && sudo make install |
| 110 | +``` |
| 111 | + |
| 112 | +`configure` generates a `config.toml` which can also be used with normal `x.py` |
| 113 | +invocations. |
| 114 | + |
| 115 | +## Building on Windows |
| 116 | + |
| 117 | +On Windows, we suggest using [winget] to install dependencies by running the |
| 118 | +following in a terminal: |
| 119 | + |
| 120 | +```powershell |
| 121 | +winget install -e Python.Python.3 |
| 122 | +winget install -e Kitware.CMake |
| 123 | +winget install -e Git.Git |
| 124 | +``` |
| 125 | + |
| 126 | +Then edit your system's `PATH` variable and add: `C:\Program Files\CMake\bin`. |
| 127 | +See |
| 128 | +[this guide on editing the system `PATH`](https://www.java.com/en/download/help/path.html) |
| 129 | +from the Java documentation. |
| 130 | + |
| 131 | +[winget]: https://github.com/microsoft/winget-cli |
| 132 | + |
| 133 | +There are two prominent ABIs in use on Windows: the native (MSVC) ABI used by |
| 134 | +Visual Studio and the GNU ABI used by the GCC toolchain. Which version of Rust |
| 135 | +you need depends largely on what C/C++ libraries you want to interoperate with. |
| 136 | +Use the MSVC build of Rust to interop with software produced by Visual Studio |
| 137 | +and the GNU build to interop with GNU software built using the MinGW/MSYS2 |
| 138 | +toolchain. |
| 139 | + |
| 140 | +### MinGW |
| 141 | + |
| 142 | +[MSYS2][msys2] can be used to easily build Rust on Windows: |
| 143 | + |
| 144 | +[msys2]: https://www.msys2.org/ |
| 145 | + |
| 146 | +1. Download the latest [MSYS2 installer][msys2] and go through the installer. |
| 147 | + |
| 148 | +2. Run `mingw32_shell.bat` or `mingw64_shell.bat` from the MSYS2 installation |
| 149 | + directory (e.g. `C:\msys64`), depending on whether you want 32-bit or 64-bit |
| 150 | + Rust. (As of the latest version of MSYS2 you have to run `msys2_shell.cmd |
| 151 | + -mingw32` or `msys2_shell.cmd -mingw64` from the command line instead.) |
| 152 | + |
| 153 | +3. From this terminal, install the required tools: |
| 154 | + |
| 155 | + ```sh |
| 156 | + # Update package mirrors (may be needed if you have a fresh install of MSYS2) |
| 157 | + pacman -Sy pacman-mirrors |
| 158 | + |
| 159 | + # Install build tools needed for Rust. If you're building a 32-bit compiler, |
| 160 | + # then replace "x86_64" below with "i686". If you've already got Git, Python, |
| 161 | + # or CMake installed and in PATH you can remove them from this list. |
| 162 | + # Note that it is important that you do **not** use the 'python2', 'cmake', |
| 163 | + # and 'ninja' packages from the 'msys2' subsystem. |
| 164 | + # The build has historically been known to fail with these packages. |
| 165 | + pacman -S git \ |
| 166 | + make \ |
| 167 | + diffutils \ |
| 168 | + tar \ |
| 169 | + mingw-w64-x86_64-python \ |
| 170 | + mingw-w64-x86_64-cmake \ |
| 171 | + mingw-w64-x86_64-gcc \ |
| 172 | + mingw-w64-x86_64-ninja |
| 173 | + ``` |
| 174 | + |
| 175 | +4. Navigate to Rust's source code (or clone it), then build it: |
| 176 | + |
| 177 | + ```sh |
| 178 | + python x.py setup user && python x.py build && python x.py install |
| 179 | + ``` |
| 180 | + |
| 181 | +### MSVC |
| 182 | + |
| 183 | +MSVC builds of Rust additionally require an installation of Visual Studio 2017 |
| 184 | +(or later) so `rustc` can use its linker. The simplest way is to get |
| 185 | +[Visual Studio], check the "C++ build tools" and "Windows 10 SDK" workload. |
| 186 | + |
| 187 | +[Visual Studio]: https://visualstudio.microsoft.com/downloads/ |
| 188 | + |
| 189 | +(If you're installing CMake yourself, be careful that "C++ CMake tools for |
| 190 | +Windows" doesn't get included under "Individual components".) |
| 191 | + |
| 192 | +With these dependencies installed, you can build the compiler in a `cmd.exe` |
| 193 | +shell with: |
| 194 | + |
| 195 | +```sh |
| 196 | +python x.py setup user |
| 197 | +python x.py build |
| 198 | +``` |
| 199 | + |
| 200 | +Right now, building Rust only works with some known versions of Visual Studio. |
| 201 | +If you have a more recent version installed and the build system doesn't |
| 202 | +understand, you may need to force rustbuild to use an older version. |
| 203 | +This can be done by manually calling the appropriate vcvars file before running |
| 204 | +the bootstrap. |
| 205 | + |
| 206 | +```batch |
| 207 | +CALL "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" |
| 208 | +python x.py build |
| 209 | +``` |
| 210 | + |
| 211 | +### Specifying an ABI |
| 212 | + |
| 213 | +Each specific ABI can also be used from either environment (for example, using |
| 214 | +the GNU ABI in PowerShell) by using an explicit build triple. The available |
| 215 | +Windows build triples are: |
| 216 | +- GNU ABI (using GCC) |
| 217 | + - `i686-pc-windows-gnu` |
| 218 | + - `x86_64-pc-windows-gnu` |
| 219 | +- The MSVC ABI |
| 220 | + - `i686-pc-windows-msvc` |
| 221 | + - `x86_64-pc-windows-msvc` |
| 222 | + |
| 223 | +The build triple can be specified by either specifying `--build=<triple>` when |
| 224 | +invoking `x.py` commands, or by creating a `config.toml` file (as described in |
| 225 | +[Building on a Unix-like system](#building-on-a-unix-like-system)), and passing |
| 226 | +`--set build.build=<triple>` to `./configure`. |
| 227 | + |
| 228 | +## Building Documentation |
| 229 | + |
| 230 | +If you'd like to build the documentation, it's almost the same: |
| 231 | + |
| 232 | +```sh |
| 233 | +./x.py doc |
| 234 | +``` |
| 235 | + |
| 236 | +The generated documentation will appear under `doc` in the `build` directory for |
| 237 | +the ABI used. That is, if the ABI was `x86_64-pc-windows-msvc`, the directory |
| 238 | +will be `build\x86_64-pc-windows-msvc\doc`. |
| 239 | + |
| 240 | +## Notes |
| 241 | + |
| 242 | +Since the Rust compiler is written in Rust, it must be built by a precompiled |
| 243 | +"snapshot" version of itself (made in an earlier stage of development). |
| 244 | +As such, source builds require an Internet connection to fetch snapshots, and an |
| 245 | +OS that can execute the available snapshot binaries. |
| 246 | + |
| 247 | +See https://doc.rust-lang.org/nightly/rustc/platform-support.html for a list of |
| 248 | +supported platforms. |
| 249 | +Only "host tools" platforms have a pre-compiled snapshot binary available; to |
| 250 | +compile for a platform without host tools you must cross-compile. |
| 251 | + |
| 252 | +You may find that other platforms work, but these are our officially supported |
| 253 | +build environments that are most likely to work. |
0 commit comments