blob: 9fd1a3b6b1458789afd27443faa23774de441cee [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
13 as _bootstraping_.
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 Csomorebb902e2018-10-22 00:56:43 -070017If you already have a Bazel binary, you can build Bazel from a GitHub checkout.
Googler1fbef392017-03-22 09:05:09 +000018
Laszlo Csomorebb902e2018-10-22 00:56:43 -070019You will need:
Laszlo Csomor6532ecc2018-05-04 01:21:54 -070020
Laszlo Csomorebb902e2018-10-22 00:56:43 -070021* A GitHub checkout of Bazel's sources at the desired commit.
22
23* The Bazel version that was the latest when the commit was merged. (Other
24 Bazel versions may work too, but are not guaranteed to.) You can either
25 download this version from
26 [GitHub](https://github.com/bazelbuild/bazel/releases), or build it from
27 source, or bootstrap it as described below.
28
29* The same prerequisites as for bootstrapping (JDK, C++ compiler, etc.)
30
31Once you have a Bazel binary to build with and the source tree of Bazel, `cd`
32into the directory and run `bazel build //src:bazel`.
33
34<h2 id="bootstrap-bazel">Build Bazel from scratch (bootstrapping)</h2>
35
36You can also build Bazel from scratch, without using an existing Bazel binary.
37
38<h3 id="download-distfile">1. Download Bazel's sources (distribution archive)</h3>
39
40(This step is the same for all platforms.)
41
421. Download `bazel-<version>-dist.zip` from
43 [GitHub](https://github.com/bazelbuild/bazel/releases), for example
44 `bazel-0.18.0-dist.zip`.
45
46 There is a **single, architecture-independent** distribution archive. There
47 are no architecture-specific or OS-specific distribution archives.
48
49 You **have to use the distribution archive** to bootstrap Bazel. You cannot
50 use a source tree cloned from GitHub. (The distribution archive contains
51 generated source files that are required for bootstrapping and are not part
52 of the normal Git source tree.)
53
542. Unpack the zip file somewhere on disk.
55
56 We recommend to also verify the signature made by our
57 [release key](https://bazel.build/bazel-release.pub.gpg) 48457EE0.
58
59**To build a development version** of Bazel from a GitHub checkout, you need a
60working Bazel binary. [Scroll up](#build-bazel-using-bazel) to see how to build
61Bazel using Bazel.
62
63<h3 id="bootstrap-unix">2. Bootstrap Bazel on Ubuntu Linux, macOS, and other Unix-like systems</h3>
64
65([Scroll down](#bootstrap-windows) for instructions for Windows.)
66
67<h4 id="bootstrap-unix-prereq">2.1. Install the prerequisites</h4>
Laszlo Csomor6532ecc2018-05-04 01:21:54 -070068
Laszlo Csomor4691ad92018-05-14 01:58:00 -070069* **Bash**
70
71* **zip, unzip**
72
73* **C++ build toolchain**
74
75* **JDK 8.** You must install version 8 of the JDK. Versions other than 8 are
76 *not* supported.
77
Laszlo Csomorebb902e2018-10-22 00:56:43 -070078* **Python**. Versions 2 and 3 are supported, installing one of them is
79 enough.
Laszlo Csomor4691ad92018-05-14 01:58:00 -070080
Laszlo Csomorebb902e2018-10-22 00:56:43 -070081For example on Ubuntu Linux you can install these requirements using the
82following command:
Googler1fbef392017-03-22 09:05:09 +000083
Serge70e84f82018-04-18 10:04:22 -070084```sh
Laszlo Csomor4691ad92018-05-14 01:58:00 -070085sudo apt-get install build-essential openjdk-8-jdk python zip unzip
Serge70e84f82018-04-18 10:04:22 -070086```
Laszlo Csomor6532ecc2018-05-04 01:21:54 -070087
Laszlo Csomorebb902e2018-10-22 00:56:43 -070088<h4 id="bootstrap-unix-bootstrap">2.2. Bootstrap Bazel</h4>
Laszlo Csomor6532ecc2018-05-04 01:21:54 -070089
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700901. Open a shell or Terminal window.
91
923. `cd` to the directory where you unpacked the distribution archive.
93
Klaus Aehlig004bcb42019-01-24 03:53:32 -0800943. Run the compilation script: `env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./compile.sh`.
Laszlo Csomorebb902e2018-10-22 00:56:43 -070095
96The compiled output is placed into `output/bazel`. This is a self-contained
97Bazel binary, without an embedded JDK. You can copy it anywhere or use it
98in-place. For convenience we recommend copying this binary to a directory that's
99on your `PATH` (such as `/usr/local/bin` on Linux).
100
Klaus Aehligfc996af2019-02-19 07:21:28 -0800101To build the `bazel` binary in a reproducible way, also set
102[`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/specs/source-date-epoch/)
103in the `compile.sh` step.
104
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700105<h3 id="bootstrap-windows">2. Bootstrap Bazel on Windows</h3>
106
107([Scroll up](#bootstrap-unix) for instructions for Linux, macOS, and other
108Unix-like systems.)
109
110<h4 id="bootstrap-windows-prereq">2.1. Install the prerequisites</h4>
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700111
112* [MSYS2 shell](https://msys2.github.io/)
113
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700114* **The MSYS2 packages for zip and unzip.** Run the following command in the MSYS2 shell:
Laszlo Csomorab2431e2018-05-29 05:15:42 -0700115
116 ```
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700117 pacman -Syu zip unzip
118 ```
119
120* **The Visual C++ compiler.** Install the Visual C++ compiler either as part
121 of Visual Studio 2015 or newer, or by installing the latest [Build Tools
122 for Visual Studio 2017](https://aka.ms/BuildTools).
123
124* **JDK 8.** You must install version 8 of the JDK. Versions other than 8 are
125 *not* supported.
126
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700127* **Python**. Versions 2 and 3 are supported, installing one of them is
128 enough. You need the Windows-native version (downloadable from
129 [https://www.python.org](https://www.python.org)). Versions installed via
130 pacman in MSYS2 will not work.
Googler1fbef392017-03-22 09:05:09 +0000131
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700132<h4 id="bootstrap-windows-bootstrap">2.2. Bootstrap Bazel</h4>
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700133
1341. Open the MSYS2 shell.
135
1362. Set the following environment variables:
Laszlo Csomora7e0c752018-06-10 23:24:26 -0700137 * Either `BAZEL_VS` or `BAZEL_VC` (they are *not* the same): Set to the
138 path to the Visual Studio directory (BAZEL\_V<b>S</b>) or to the Visual
139 C++ directory (BAZEL\_V<b>C</b>). Setting one of them is enough.
Laszlo Csomor75748652018-07-26 07:38:14 -0700140 * `BAZEL_SH`: Path of the MSYS2 `bash.exe`. See the command in the
141 examples below.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700142
143 Do not set this to `C:\Windows\System32\bash.exe`. (You have that file
144 if you installed Windows Subsystem for Linux.) Bazel does not support
145 this version of `bash.exe`.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700146 * `PATH`: Add the Python directory.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700147 * `JAVA_HOME`: Set to the JDK directory.
148
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700149 **Example** (using BAZEL\_V<b>S</b>):
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700150
151 export BAZEL_VS="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools"
Laszlo Csomor75748652018-07-26 07:38:14 -0700152 export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))"
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700153 export PATH="/c/python27:$PATH"
154 export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112"
155
Laszlo Csomora7e0c752018-06-10 23:24:26 -0700156 or (using BAZEL\_V<b>C</b>):
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700157
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700158 export BAZEL_VC="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC"
Laszlo Csomor75748652018-07-26 07:38:14 -0700159 export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))"
Laszlo Csomor4691ad92018-05-14 01:58:00 -0700160 export PATH="/c/python27:$PATH"
161 export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112"
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700162
Laszlo Csomorebb902e2018-10-22 00:56:43 -07001633. `cd` to the directory where you unpacked the distribution archive.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700164
1654. Run the compilation script: `./compile.sh`
166
167The compiled output is placed into `output/bazel.exe`. This is a self-contained
Laszlo Csomorebb902e2018-10-22 00:56:43 -0700168Bazel binary, without an embedded JDK. You can copy it anywhere or use it
169in-place. For convenience we recommend copying this binary to a directory that's
170on your `PATH`.
Laszlo Csomor6532ecc2018-05-04 01:21:54 -0700171
172You don't need to run Bazel from the MSYS2 shell. You can run Bazel from the
173Command Prompt (`cmd.exe`) or PowerShell.