fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 1 | Project: /_project.yaml |
| 2 | Book: /_book.yaml |
| 3 | |
| 4 | # Compiling Bazel from Source |
| 5 | |
| 6 | |
| 7 | This page describes how to install Bazel from source and provides |
| 8 | troubleshooting tips for common issues. |
| 9 | |
| 10 | To build Bazel from source, you can do one of the following: |
| 11 | |
| 12 | * Build it [using an existing Bazel binary](#build-bazel-using-bazel) |
| 13 | |
| 14 | * Build it [without an existing Bazel binary](#bootstrap-bazel) which is known |
| 15 | as _bootstrapping_. |
| 16 | |
| 17 | ## Build Bazel using Bazel {:#build-bazel-using-bazel} |
| 18 | |
| 19 | ### Summary {:#summary} |
| 20 | |
| 21 | 1. Get the latest Bazel release from the |
| 22 | [GitHub release page](https://github.com/bazelbuild/bazel/releases){: .external} or with |
| 23 | [Bazelisk](https://github.com/bazelbuild/bazelisk){: .external}. |
| 24 | |
| 25 | 2. [Download Bazel's sources from GitHub](https://github.com/bazelbuild/bazel/archive/master.zip){: .external} |
| 26 | and extract somewhere. |
| 27 | Alternatively you can git clone the source tree from https://github.com/bazelbuild/bazel |
| 28 | |
| 29 | 3. Install the same prerequisites as for bootstrapping (see |
| 30 | [for Unix-like systems](#bootstrap-unix-prereq) or |
| 31 | [for Windows](#bootstrap-windows-prereq)) |
| 32 | |
| 33 | 4. Build a development build of Bazel using Bazel: |
| 34 | `bazel build //src:bazel-dev` (or `bazel build //src:bazel-dev.exe` on |
| 35 | Windows) |
| 36 | |
| 37 | 5. The resulting binary is at `bazel-bin/src/bazel-dev` |
| 38 | (or `bazel-bin\src\bazel-dev.exe` on Windows). You can copy it wherever you |
| 39 | like and use immediately without further installation. |
| 40 | |
| 41 | Detailed instructions follow below. |
| 42 | |
| 43 | ### Step 1: Get the latest Bazel release {:#build-bazel-install-bazel} |
| 44 | |
| 45 | **Goal**: Install or download a release version of Bazel. Make sure you can run |
| 46 | it by typing `bazel` in a terminal. |
| 47 | |
| 48 | **Reason**: To build Bazel from a GitHub source tree, you need a pre-existing |
| 49 | Bazel binary. You can install one from a package manager or download one from |
fwe | 6c2bd4a | 2022-02-18 10:47:46 -0800 | [diff] [blame] | 50 | GitHub. See [Installing Bazel](/install). (Or you can [build from |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 51 | scratch (bootstrap)](#bootstrap-bazel).) |
| 52 | |
| 53 | **Troubleshooting**: |
| 54 | |
| 55 | * If you cannot run Bazel by typing `bazel` in a terminal: |
| 56 | |
| 57 | * Maybe your Bazel binary's directory is not on the PATH. |
| 58 | |
| 59 | This is not a big problem. Instead of typing `bazel`, you will need to |
| 60 | type the full path. |
| 61 | |
| 62 | * Maybe the Bazel binary itself is not called `bazel` (on Unixes) or |
| 63 | `bazel.exe` (on Windows). |
| 64 | |
| 65 | This is not a big problem. You can either rename the binary, or type the |
| 66 | binary's name instead of `bazel`. |
| 67 | |
| 68 | * Maybe the binary is not executable (on Unixes). |
| 69 | |
| 70 | You must make the binary executable by running `chmod +x /path/to/bazel`. |
| 71 | |
| 72 | ### Step 2: Download Bazel's sources from GitHub {:#build-bazel-git} |
| 73 | |
| 74 | If you are familiar with Git, then just git clone https://github.com/bazelbuild/bazel |
| 75 | |
| 76 | Otherwise: |
| 77 | |
| 78 | 1. Download the |
| 79 | [latest sources as a zip file](https://github.com/bazelbuild/bazel/archive/master.zip){: .external}. |
| 80 | |
| 81 | 2. Extract the contents somewhere. |
| 82 | |
| 83 | For example create a `bazel-src` directory under your home directory and |
| 84 | extract there. |
| 85 | |
| 86 | ### Step 3: Install prerequisites {:#build-bazel-prerequisites} |
| 87 | |
| 88 | Install the same prerequisites as for bootstrapping (see below) -- JDK, C++ |
| 89 | compiler, MSYS2 (if you are building on Windows), etc. |
| 90 | |
| 91 | ### Step 4a: Build Bazel on Ubuntu Linux, macOS, and other Unix-like systems {:#build-bazel-on-unixes} |
| 92 | |
| 93 | For instructions for Windows, see [Build Bazel on Windows](#build-bazel-on-windows). |
| 94 | |
| 95 | **Goal**: Run Bazel to build a custom Bazel binary (`bazel-bin/src/bazel-dev`). |
| 96 | |
| 97 | **Instructions**: |
| 98 | |
| 99 | 1. Start a Bash terminal |
| 100 | |
| 101 | 2. `cd` into the directory where you extracted (or cloned) Bazel's sources. |
| 102 | |
| 103 | For example if you extracted the sources under your home directory, run: |
| 104 | |
| 105 | cd ~/bazel-src |
| 106 | |
| 107 | 3. Build Bazel from source: |
| 108 | |
| 109 | bazel build //src:bazel-dev |
| 110 | |
| 111 | Alternatively you can run `bazel build //src:bazel --compilation_mode=opt` |
| 112 | to yield a smaller binary but it's slower to build. |
| 113 | |
| 114 | 4. The output will be at `bazel-bin/src/bazel-dev` (or `bazel-bin/src/bazel`). |
| 115 | |
| 116 | ### Step 4b: Build Bazel on Windows {:#build-bazel-on-windows} |
| 117 | |
| 118 | For instructions for Unix-like systems, see |
| 119 | [Ubuntu Linux, macOS, and other Unix-like systems](#build-bazel-on-unixes). |
| 120 | |
| 121 | **Goal**: Run Bazel to build a custom Bazel binary |
| 122 | (`bazel-bin\src\bazel-dev.exe`). |
| 123 | |
| 124 | **Instructions**: |
| 125 | |
| 126 | 1. Start Command Prompt (Start Menu > Run > "cmd.exe") |
| 127 | |
| 128 | 2. `cd` into the directory where you extracted (or cloned) Bazel's sources. |
| 129 | |
| 130 | For example if you extracted the sources under your home directory, run: |
| 131 | |
| 132 | cd %USERPROFILE%\bazel-src |
| 133 | |
| 134 | 3. Build Bazel from source: |
| 135 | |
| 136 | bazel build //src:bazel-dev.exe |
| 137 | |
| 138 | Alternatively you can run `bazel build //src:bazel.exe |
| 139 | --compilation_mode=opt` to yield a smaller binary but it's slower to build. |
| 140 | |
| 141 | 4. The output will be at `bazel-bin\src\bazel-dev.exe` (or |
| 142 | `bazel-bin\src\bazel.exe`). |
| 143 | |
| 144 | ### Step 5: Install the built binary {:#build-bazel-install} |
| 145 | |
| 146 | Actually, there's nothing to install. |
| 147 | |
| 148 | The output of the previous step is a self-contained Bazel binary. You can copy |
| 149 | it to any directory and use immediately. (It's useful if that directory is on |
| 150 | your PATH so that you can run "bazel" everywhere.) |
| 151 | |
| 152 | --- |
| 153 | |
| 154 | ## Build Bazel from scratch (bootstrapping) {:#bootstrap-bazel} |
| 155 | |
| 156 | You can also build Bazel from scratch, without using an existing Bazel binary. |
| 157 | |
| 158 | ### Step 1: Download Bazel's sources (distribution archive) {:#download-distfile} |
| 159 | |
| 160 | (This step is the same for all platforms.) |
| 161 | |
| 162 | 1. Download `bazel-<version>-dist.zip` from |
| 163 | [GitHub](https://github.com/bazelbuild/bazel/releases){: .external}, for example |
| 164 | `bazel-0.28.1-dist.zip`. |
| 165 | |
| 166 | **Attention**: |
| 167 | |
| 168 | - There is a **single, architecture-independent** distribution archive. |
| 169 | There are no architecture-specific or OS-specific distribution archives. |
| 170 | - These sources are **not the same as the GitHub source tree**. You |
| 171 | have to use the distribution archive to bootstrap Bazel. You cannot |
| 172 | use a source tree cloned from GitHub. (The distribution archive contains |
| 173 | generated source files that are required for bootstrapping and are not part |
| 174 | of the normal Git source tree.) |
| 175 | |
| 176 | 2. Unpack the distribution archive somewhere on disk. |
| 177 | |
| 178 | You should verify the signature made by Bazel's |
| 179 | [release key](https://bazel.build/bazel-release.pub.gpg) 3D5919B448457EE0. |
| 180 | |
| 181 | ### Step 2a: Bootstrap Bazel on Ubuntu Linux, macOS, and other Unix-like systems {:#bootstrap-unix-overview} |
| 182 | |
| 183 | For instructions for Windows, see [Bootstrap Bazel on Windows](#bootstrap-windows). |
| 184 | |
| 185 | #### 2.1. Install the prerequisites {:#bootstrap-unix-prereq} |
| 186 | |
| 187 | * **Bash** |
| 188 | |
| 189 | * **zip, unzip** |
| 190 | |
| 191 | * **C++ build toolchain** |
| 192 | |
| 193 | * **JDK.** Version 11 is required. |
| 194 | |
| 195 | * **Python**. Versions 2 and 3 are supported, installing one of them is |
| 196 | enough. |
| 197 | |
| 198 | For example on Ubuntu Linux you can install these requirements using the |
| 199 | following command: |
| 200 | |
| 201 | ```sh |
| 202 | sudo apt-get install build-essential openjdk-11-jdk python zip unzip |
| 203 | ``` |
| 204 | |
| 205 | #### 2.2. Bootstrap Bazel on Unix {:#bootstrap-unix} |
| 206 | |
| 207 | 1. Open a shell or Terminal window. |
| 208 | |
| 209 | 3. `cd` to the directory where you unpacked the distribution archive. |
| 210 | |
Ivo List | 2f9ed1c | 2022-03-16 04:41:53 -0700 | [diff] [blame^] | 211 | 3. Run the compilation script: `env EXTRA_BAZEL_ARGS="--tool_java_runtime_version=local_jdk" bash ./compile.sh`. |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 212 | |
| 213 | The compiled output is placed into `output/bazel`. This is a self-contained |
| 214 | Bazel binary, without an embedded JDK. You can copy it anywhere or use it |
| 215 | in-place. For convenience, copy this binary to a directory that's on your |
| 216 | `PATH` (such as `/usr/local/bin` on Linux). |
| 217 | |
| 218 | To build the `bazel` binary in a reproducible way, also set |
| 219 | [`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/specs/source-date-epoch/) |
| 220 | in the "Run the compilation script" step. |
| 221 | |
| 222 | ### Step 2b: Bootstrap Bazel on Windows {:#bootstrap-windows-overview} |
| 223 | |
| 224 | For instructions for Unix-like systems, see |
| 225 | [Bootstrap Bazel on Ubuntu Linux, macOS, and other Unix-like systems](#bootstrap-unix). |
| 226 | |
| 227 | #### 2.1. Install the prerequisites {:#bootstrap-windows-prereq} |
| 228 | |
| 229 | * [MSYS2 shell](https://msys2.github.io/) |
| 230 | |
| 231 | * **The MSYS2 packages for zip and unzip.** Run the following command in the MSYS2 shell: |
| 232 | |
| 233 | ``` |
| 234 | pacman -S zip unzip patch |
| 235 | ``` |
| 236 | |
| 237 | * **The Visual C++ compiler.** Install the Visual C++ compiler either as part |
| 238 | of Visual Studio 2015 or newer, or by installing the latest [Build Tools |
| 239 | for Visual Studio 2017](https://aka.ms/BuildTools). |
| 240 | |
| 241 | * **JDK 8.** You must install version 8 of the JDK. Versions other than 8 are |
| 242 | *not* supported. |
| 243 | |
| 244 | * **Python**. Versions 2 and 3 are supported, installing one of them is |
| 245 | enough. You need the Windows-native version (downloadable from |
| 246 | [https://www.python.org](https://www.python.org)). Versions installed via |
| 247 | pacman in MSYS2 will not work. |
| 248 | |
| 249 | #### 2.2. Bootstrap Bazel on Windows {:#bootstrap-windows} |
| 250 | |
| 251 | 1. Open the MSYS2 shell. |
| 252 | |
| 253 | 2. Set the following environment variables: |
| 254 | * Either `BAZEL_VS` or `BAZEL_VC` (they are *not* the same): Set to the |
| 255 | path to the Visual Studio directory (BAZEL\_V<b>S</b>) or to the Visual |
| 256 | C++ directory (BAZEL\_V<b>C</b>). Setting one of them is enough. |
| 257 | * `BAZEL_SH`: Path of the MSYS2 `bash.exe`. See the command in the |
| 258 | examples below. |
| 259 | |
| 260 | Do not set this to `C:\Windows\System32\bash.exe`. (You have that file |
| 261 | if you installed Windows Subsystem for Linux.) Bazel does not support |
| 262 | this version of `bash.exe`. |
| 263 | * `PATH`: Add the Python directory. |
| 264 | * `JAVA_HOME`: Set to the JDK directory. |
| 265 | |
| 266 | **Example** (using BAZEL\_V<b>S</b>): |
| 267 | |
| 268 | export BAZEL_VS="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools" |
| 269 | export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))" |
| 270 | export PATH="/c/python27:$PATH" |
| 271 | export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112" |
| 272 | |
| 273 | or (using BAZEL\_V<b>C</b>): |
| 274 | |
| 275 | export BAZEL_VC="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC" |
| 276 | export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))" |
| 277 | export PATH="/c/python27:$PATH" |
| 278 | export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112" |
| 279 | |
| 280 | 3. `cd` to the directory where you unpacked the distribution archive. |
| 281 | |
Ivo List | 2f9ed1c | 2022-03-16 04:41:53 -0700 | [diff] [blame^] | 282 | 4. Run the compilation script: `env EXTRA_BAZEL_ARGS="--tool_java_runtime_version=local_jdk" ./compile.sh` |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 283 | |
| 284 | The compiled output is placed into `output/bazel.exe`. This is a self-contained |
| 285 | Bazel binary, without an embedded JDK. You can copy it anywhere or use it |
| 286 | in-place. For convenience, copy this binary to a directory that's on |
| 287 | your `PATH`. |
| 288 | |
| 289 | To build the `bazel.exe` binary in a reproducible way, also set |
| 290 | [`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/specs/source-date-epoch/) |
| 291 | in the "Run the compilation script" step. |
| 292 | |
| 293 | You don't need to run Bazel from the MSYS2 shell. You can run Bazel from the |
| 294 | Command Prompt (`cmd.exe`) or PowerShell. |