blob: 8e1db4baae2bd5a7f50e82123d891827603d36fc [file] [log] [blame] [view]
Googler1fbef392017-03-22 09:05:09 +00001---
2layout: documentation
Laszlo Csomorebb902e2018-10-22 00:56:43 -07003title: Compiling Bazel from source
Googler1fbef392017-03-22 09:05:09 +00004---
5
Laszlo Csomorebb902e2018-10-22 00:56:43 -07006<h1 id="compiling-from-source">Compiling Bazel from source</h1>
Googler1fbef392017-03-22 09:05:09 +00007
laszlocsomor810d2362018-09-17 09:35:09 -07008To build Bazel from source, you can do one of the following:
9
Laszlo Csomorebb902e2018-10-22 00:56:43 -070010* Build it [using an existing Bazel binary](#build-bazel-using-bazel)
laszlocsomor810d2362018-09-17 09:35:09 -070011
Laszlo Csomorebb902e2018-10-22 00:56:43 -070012* Build it [without an existing Bazel binary](#bootstrap-bazel) which is known
Zackary Lowery81183892019-10-30 08:14:39 -070013 as _bootstrapping_.
laszlocsomor810d2362018-09-17 09:35:09 -070014
Laszlo Csomorebb902e2018-10-22 00:56:43 -070015<h2 id="build-bazel-using-bazel">Build Bazel using Bazel</h2>
steren30c35a22017-06-30 15:49:47 +020016
Laszlo Csomor7d479872019-08-16 07:00:04 -070017TL;DR:
Googler1fbef392017-03-22 09:05:09 +000018
Jingwen Chen7c15c032020-01-16 11:54:44 -0800191. Get the latest Bazel release from the
20 [GitHub release page](https://github.com/bazelbuild/bazel/releases) or with
21 [Bazelisk](https://github.com/bazelbuild/bazelisk).
Laszlo Csomor6532ecc2018-05-04 01:21:54 -070022
Laszlo Csomor7d479872019-08-16 07:00:04 -0700232. [Download Bazel's sources from GitHub](https://github.com/bazelbuild/bazel/archive/master.zip)
24 and extract somewhere.
25 Alternatively you can git clone the source tree from https://github.com/bazelbuild/bazel
Laszlo Csomorebb902e2018-10-22 00:56:43 -070026
Laszlo Csomor7d479872019-08-16 07:00:04 -0700273. Install the same prerequisites as for bootstrapping (see
28 [for Unix-like systems](#bootstrap-unix-prereq) or
29 [for Windows](#bootstrap-windows-prereq))
Laszlo Csomorebb902e2018-10-22 00:56:43 -070030
Jingwen Chen7c15c032020-01-16 11:54:44 -0800314. Build a development build of Bazel using Bazel:
32 `bazel build //src:bazel-dev` (or `bazel build //src:bazel-dev.exe` on
33 Windows)
Laszlo Csomorebb902e2018-10-22 00:56:43 -070034
Jingwen Chen7c15c032020-01-16 11:54:44 -0800355. The resulting binary is at `bazel-bin/src/bazel-dev`
Laszlo Csomor7d479872019-08-16 07:00:04 -070036 (or `bazel-bin\src\bazel-dev.exe` on Windows). You can copy it wherever you
37 like and use immediately without further installation.
38
39Detailed instructions follow below.
40
Greg Estrend2b063e2020-02-20 12:33:22 -080041<h3 id="build-bazel-install-bazel">Step 1: Get the latest Bazel release</h3>
Laszlo Csomor7d479872019-08-16 07:00:04 -070042
43**Goal**: Install or download a release version of Bazel. Make sure you can run
44it by typing `bazel` in a terminal.
45
46**Reason**: To build Bazel from a GitHub source tree, you need a pre-existing
47Bazel binary. You can install one from a package manager or download one from
48GitHub. See <a href="install.html">Installing Bazel</a>. (Or you can [build from
49scratch (bootstrap)](#bootstrap-bazel).)
50
51**Troubleshooting**:
52
53* If you cannot run Bazel by typing `bazel` in a terminal:
54
55 * Maybe your Bazel binary's directory is not on the PATH.
56
57 This is not a big problem. Instead of typing `bazel`, you will need to
58 type the full path.
59
60 * Maybe the Bazel binary itself is not called `bazel` (on Unixes) or
61 `bazel.exe` (on Windows).
62
63 This is not a big problem. You can either rename the binary, or type the
64 binary's name instead of `bazel`.
65
66 * Maybe the binary is not executable (on Unixes).
67
68 You must make the binary executable by running `chmod +x /path/to/bazel`.
69
Greg Estrend2b063e2020-02-20 12:33:22 -080070<h3 id="build-bazel-git">Step 2: Download Bazel's sources from GitHub</h3>
Laszlo Csomor7d479872019-08-16 07:00:04 -070071
72If you are familiar with Git, then just git clone https://github.com/bazelbuild/bazel
73
74Otherwise:
75
761. Download the
77 [latest sources as a zip file](https://github.com/bazelbuild/bazel/archive/master.zip).
78
792. Extract the contents somewhere.
80
81 For example create a `bazel-src` directory under your home directory and
82 extract there.
83
Greg Estrend2b063e2020-02-20 12:33:22 -080084<h3 id="build-bazel-prerequisites">Step 3: Install prerequisites</h3>
Laszlo Csomor7d479872019-08-16 07:00:04 -070085
86Install the same prerequisites as for bootstrapping (see below) -- JDK, C++
87compiler, MSYS2 (if you are building on Windows), etc.
88
Greg Estrend2b063e2020-02-20 12:33:22 -080089<h3 id="build-bazel-on-unixes">Step 4: Build Bazel</h3>
90
91<h4 id="build-bazel-on-unixes">Ubuntu Linux, macOS, and other Unix-like systems</h4>
Laszlo Csomor7d479872019-08-16 07:00:04 -070092
93([Scroll down](#build-bazel-on-windows) for instructions for Windows.)
94
Jingwen Chen7c15c032020-01-16 11:54:44 -080095**Goal**: Run Bazel to build a custom Bazel binary (`bazel-bin/src/bazel-dev`).
Laszlo Csomor7d479872019-08-16 07:00:04 -070096
97**Instructions**:
98
991. Start a Bash terminal
100
1012. `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
1073. Build Bazel from source:
108
Jingwen Chen7c15c032020-01-16 11:54:44 -0800109 bazel build //src:bazel-dev
Laszlo Csomor7d479872019-08-16 07:00:04 -0700110
Jingwen Chen7c15c032020-01-16 11:54:44 -0800111 Alternatively you can run `bazel build //src:bazel --compilation_mode=opt`
112 to yield a smaller binary but it's slower to build.
113
1144. The output will be at `bazel-bin/src/bazel-dev` (or `bazel-bin/src/bazel`).
Laszlo Csomor7d479872019-08-16 07:00:04 -0700115
Greg Estrend2b063e2020-02-20 12:33:22 -0800116<h4 id="build-bazel-on-windows">Windows</h4>
Laszlo Csomor7d479872019-08-16 07:00:04 -0700117
118([Scroll up](#build-bazel-on-unixes) for instructions for Linux, macOS, and other
119Unix-like systems.)
120
121**Goal**: Run Bazel to build a custom Bazel binary
122(`bazel-bin\src\bazel-dev.exe`).
123
124**Instructions**:
125
1261. Start Command Prompt (Start Menu &gt; Run &gt; "cmd.exe")
127
1282. `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
1343. Build Bazel from source:
135
136 bazel build //src:bazel-dev.exe
137
Jingwen Chen7c15c032020-01-16 11:54:44 -0800138 Alternatively you can run `bazel build //src:bazel.exe
139 --compilation_mode=opt` to yield a smaller binary but it's slower to build.
Laszlo Csomor7d479872019-08-16 07:00:04 -0700140
Jingwen Chen7c15c032020-01-16 11:54:44 -08001414. The output will be at `bazel-bin\src\bazel-dev.exe` (or
142 `bazel-bin\src\bazel.exe`).
Laszlo Csomor7d479872019-08-16 07:00:04 -0700143
Greg Estrend2b063e2020-02-20 12:33:22 -0800144<h3 id="build-bazel-install">Step 5: Install the built binary</h3>
Laszlo Csomor7d479872019-08-16 07:00:04 -0700145
146Actually, there's nothing to install.
147
148The output of the previous step is a self-contained Bazel binary. You can copy
149it to any directory and use immediately. (It's useful if that directory is on
150your PATH so that you can run "bazel" everywhere.)
151
152---
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700153
154<h2 id="bootstrap-bazel">Build Bazel from scratch (bootstrapping)</h2>
155
156You can also build Bazel from scratch, without using an existing Bazel binary.
157
Greg Estrend2b063e2020-02-20 12:33:22 -0800158<h3 id="download-distfile">Step 1: Download Bazel's sources (distribution archive)</h3>
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700159
160(This step is the same for all platforms.)
161
1621. Download `bazel-<version>-dist.zip` from
163 [GitHub](https://github.com/bazelbuild/bazel/releases), for example
Laszlo Csomor7d479872019-08-16 07:00:04 -0700164 `bazel-0.28.1-dist.zip`.
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700165
Laszlo Csomor7d479872019-08-16 07:00:04 -0700166 **Attention**:
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700167
Laszlo Csomor7d479872019-08-16 07:00:04 -0700168 - 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.)
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700175
Laszlo Csomor7d479872019-08-16 07:00:04 -07001762. Unpack the distribution archive somewhere on disk.
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700177
178 We recommend to also verify the signature made by our
Klaus Aehlig552cf562019-10-11 04:20:41 -0700179 [release key](https://bazel.build/bazel-release.pub.gpg) 3D5919B448457EE0.
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700180
Greg Estrend2b063e2020-02-20 12:33:22 -0800181<h3 id="bootstrap-unix">Step 2a: Bootstrap Bazel on Ubuntu Linux, macOS, and other Unix-like systems</h3>
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700182
183([Scroll down](#bootstrap-windows) for instructions for Windows.)
184
185<h4 id="bootstrap-unix-prereq">2.1. Install the prerequisites</h4>
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700186
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700187* **Bash**
188
189* **zip, unzip**
190
191* **C++ build toolchain**
192
wyva9de41c2020-05-11 07:23:47 -0700193* **JDK.** Versions 8 and 11 are supported.
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700194
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700195* **Python**. Versions 2 and 3 are supported, installing one of them is
196 enough.
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700197
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700198For example on Ubuntu Linux you can install these requirements using the
199following command:
Googler1fbef392017-03-22 09:05:09 +0000200
Serge70e84f82018-04-18 10:04:22 -0700201```sh
wyva9de41c2020-05-11 07:23:47 -0700202sudo apt-get install build-essential openjdk-11-jdk python zip unzip
Serge70e84f82018-04-18 10:04:22 -0700203```
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700204
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700205<h4 id="bootstrap-unix-bootstrap">2.2. Bootstrap Bazel</h4>
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700206
Laszlo Csomorebb902e2018-10-22 00:56:43 -07002071. Open a shell or Terminal window.
208
2093. `cd` to the directory where you unpacked the distribution archive.
210
Klaus Aehlig004bcb42019-01-24 03:53:32 -08002113. Run the compilation script: `env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./compile.sh`.
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700212
213The compiled output is placed into `output/bazel`. This is a self-contained
214Bazel binary, without an embedded JDK. You can copy it anywhere or use it
215in-place. For convenience we recommend copying this binary to a directory that's
216on your `PATH` (such as `/usr/local/bin` on Linux).
217
Klaus Aehligfc996af2019-02-19 07:21:28 -0800218To build the `bazel` binary in a reproducible way, also set
219[`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/specs/source-date-epoch/)
Laszlo Csomor7d479872019-08-16 07:00:04 -0700220in the "Run the compilation script" step.
Klaus Aehligfc996af2019-02-19 07:21:28 -0800221
Greg Estrend2b063e2020-02-20 12:33:22 -0800222<h3 id="bootstrap-windows">Step 2b: Bootstrap Bazel on Windows</h3>
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700223
224([Scroll up](#bootstrap-unix) for instructions for Linux, macOS, and other
225Unix-like systems.)
226
227<h4 id="bootstrap-windows-prereq">2.1. Install the prerequisites</h4>
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700228
229* [MSYS2 shell](https://msys2.github.io/)
230
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700231* **The MSYS2 packages for zip and unzip.** Run the following command in the MSYS2 shell:
Laszlo Csomorab2431e2018-05-29 05:15:42 -0700232
233 ```
laszlocsomorf6696b82019-04-29 01:02:51 -0700234 pacman -S zip unzip patch
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700235 ```
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
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700244* **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.
Googler1fbef392017-03-22 09:05:09 +0000248
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700249<h4 id="bootstrap-windows-bootstrap">2.2. Bootstrap Bazel</h4>
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700250
2511. Open the MSYS2 shell.
252
2532. Set the following environment variables:
Laszlo Csomora7e0c752018-06-10 23:24:26 -0700254 * 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.
Laszlo Csomor75748652018-07-26 07:38:14 -0700257 * `BAZEL_SH`: Path of the MSYS2 `bash.exe`. See the command in the
258 examples below.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700259
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`.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700263 * `PATH`: Add the Python directory.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700264 * `JAVA_HOME`: Set to the JDK directory.
265
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700266 **Example** (using BAZEL\_V<b>S</b>):
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700267
268 export BAZEL_VS="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools"
Laszlo Csomor75748652018-07-26 07:38:14 -0700269 export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))"
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700270 export PATH="/c/python27:$PATH"
271 export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112"
272
Laszlo Csomora7e0c752018-06-10 23:24:26 -0700273 or (using BAZEL\_V<b>C</b>):
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700274
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700275 export BAZEL_VC="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC"
Laszlo Csomor75748652018-07-26 07:38:14 -0700276 export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))"
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700277 export PATH="/c/python27:$PATH"
278 export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112"
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700279
Laszlo Csomorebb902e2018-10-22 00:56:43 -07002803. `cd` to the directory where you unpacked the distribution archive.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700281
Laszlo Csomor7d479872019-08-16 07:00:04 -07002824. Run the compilation script: `env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" ./compile.sh`
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700283
284The compiled output is placed into `output/bazel.exe`. This is a self-contained
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700285Bazel binary, without an embedded JDK. You can copy it anywhere or use it
286in-place. For convenience we recommend copying this binary to a directory that's
287on your `PATH`.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700288
Laszlo Csomor7d479872019-08-16 07:00:04 -0700289To build the `bazel.exe` binary in a reproducible way, also set
290[`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/specs/source-date-epoch/)
291in the "Run the compilation script" step.
292
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700293You don't need to run Bazel from the MSYS2 shell. You can run Bazel from the
294Command Prompt (`cmd.exe`) or PowerShell.