Googler | 1fbef39 | 2017-03-22 09:05:09 +0000 | [diff] [blame] | 1 | --- |
| 2 | layout: documentation |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 3 | title: Compiling Bazel from source |
Googler | 1fbef39 | 2017-03-22 09:05:09 +0000 | [diff] [blame] | 4 | --- |
| 5 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 6 | <h1 id="compiling-from-source">Compiling Bazel from source</h1> |
Googler | 1fbef39 | 2017-03-22 09:05:09 +0000 | [diff] [blame] | 7 | |
laszlocsomor | 810d236 | 2018-09-17 09:35:09 -0700 | [diff] [blame] | 8 | To build Bazel from source, you can do one of the following: |
| 9 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 10 | * Build it [using an existing Bazel binary](#build-bazel-using-bazel) |
laszlocsomor | 810d236 | 2018-09-17 09:35:09 -0700 | [diff] [blame] | 11 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 12 | * Build it [without an existing Bazel binary](#bootstrap-bazel) which is known |
| 13 | as _bootstraping_. |
laszlocsomor | 810d236 | 2018-09-17 09:35:09 -0700 | [diff] [blame] | 14 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 15 | <h2 id="build-bazel-using-bazel">Build Bazel using Bazel</h2> |
steren | 30c35a2 | 2017-06-30 15:49:47 +0200 | [diff] [blame] | 16 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 17 | If you already have a Bazel binary, you can build Bazel from a GitHub checkout. |
Googler | 1fbef39 | 2017-03-22 09:05:09 +0000 | [diff] [blame] | 18 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 19 | You will need: |
Laszlo Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 20 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 21 | * 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 | |
| 31 | Once you have a Bazel binary to build with and the source tree of Bazel, `cd` |
| 32 | into the directory and run `bazel build //src:bazel`. |
| 33 | |
| 34 | <h2 id="bootstrap-bazel">Build Bazel from scratch (bootstrapping)</h2> |
| 35 | |
| 36 | You 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 | |
| 42 | 1. 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 | |
| 54 | 2. 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 |
| 60 | working Bazel binary. [Scroll up](#build-bazel-using-bazel) to see how to build |
| 61 | Bazel 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 Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 68 | |
Laszlo Csomor | 4691ad9 | 2018-05-14 01:58:00 -0700 | [diff] [blame] | 69 | * **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 Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 78 | * **Python**. Versions 2 and 3 are supported, installing one of them is |
| 79 | enough. |
Laszlo Csomor | 4691ad9 | 2018-05-14 01:58:00 -0700 | [diff] [blame] | 80 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 81 | For example on Ubuntu Linux you can install these requirements using the |
| 82 | following command: |
Googler | 1fbef39 | 2017-03-22 09:05:09 +0000 | [diff] [blame] | 83 | |
Serge | 70e84f8 | 2018-04-18 10:04:22 -0700 | [diff] [blame] | 84 | ```sh |
Laszlo Csomor | 4691ad9 | 2018-05-14 01:58:00 -0700 | [diff] [blame] | 85 | sudo apt-get install build-essential openjdk-8-jdk python zip unzip |
Serge | 70e84f8 | 2018-04-18 10:04:22 -0700 | [diff] [blame] | 86 | ``` |
Laszlo Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 87 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 88 | <h4 id="bootstrap-unix-bootstrap">2.2. Bootstrap Bazel</h4> |
Laszlo Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 89 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 90 | 1. Open a shell or Terminal window. |
| 91 | |
| 92 | 3. `cd` to the directory where you unpacked the distribution archive. |
| 93 | |
Klaus Aehlig | 004bcb4 | 2019-01-24 03:53:32 -0800 | [diff] [blame] | 94 | 3. Run the compilation script: `env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./compile.sh`. |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 95 | |
| 96 | The compiled output is placed into `output/bazel`. This is a self-contained |
| 97 | Bazel binary, without an embedded JDK. You can copy it anywhere or use it |
| 98 | in-place. For convenience we recommend copying this binary to a directory that's |
| 99 | on your `PATH` (such as `/usr/local/bin` on Linux). |
| 100 | |
Klaus Aehlig | fc996af | 2019-02-19 07:21:28 -0800 | [diff] [blame] | 101 | To build the `bazel` binary in a reproducible way, also set |
| 102 | [`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/specs/source-date-epoch/) |
| 103 | in the `compile.sh` step. |
| 104 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 105 | <h3 id="bootstrap-windows">2. Bootstrap Bazel on Windows</h3> |
| 106 | |
| 107 | ([Scroll up](#bootstrap-unix) for instructions for Linux, macOS, and other |
| 108 | Unix-like systems.) |
| 109 | |
| 110 | <h4 id="bootstrap-windows-prereq">2.1. Install the prerequisites</h4> |
Laszlo Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 111 | |
| 112 | * [MSYS2 shell](https://msys2.github.io/) |
| 113 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 114 | * **The MSYS2 packages for zip and unzip.** Run the following command in the MSYS2 shell: |
Laszlo Csomor | ab2431e | 2018-05-29 05:15:42 -0700 | [diff] [blame] | 115 | |
| 116 | ``` |
Laszlo Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 117 | 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 Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 127 | * **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. |
Googler | 1fbef39 | 2017-03-22 09:05:09 +0000 | [diff] [blame] | 131 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 132 | <h4 id="bootstrap-windows-bootstrap">2.2. Bootstrap Bazel</h4> |
Laszlo Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 133 | |
| 134 | 1. Open the MSYS2 shell. |
| 135 | |
| 136 | 2. Set the following environment variables: |
Laszlo Csomor | a7e0c75 | 2018-06-10 23:24:26 -0700 | [diff] [blame] | 137 | * 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 Csomor | 7574865 | 2018-07-26 07:38:14 -0700 | [diff] [blame] | 140 | * `BAZEL_SH`: Path of the MSYS2 `bash.exe`. See the command in the |
| 141 | examples below. |
Laszlo Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 142 | |
| 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 Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 146 | * `PATH`: Add the Python directory. |
Laszlo Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 147 | * `JAVA_HOME`: Set to the JDK directory. |
| 148 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 149 | **Example** (using BAZEL\_V<b>S</b>): |
Laszlo Csomor | 4691ad9 | 2018-05-14 01:58:00 -0700 | [diff] [blame] | 150 | |
| 151 | export BAZEL_VS="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools" |
Laszlo Csomor | 7574865 | 2018-07-26 07:38:14 -0700 | [diff] [blame] | 152 | export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))" |
Laszlo Csomor | 4691ad9 | 2018-05-14 01:58:00 -0700 | [diff] [blame] | 153 | export PATH="/c/python27:$PATH" |
| 154 | export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112" |
| 155 | |
Laszlo Csomor | a7e0c75 | 2018-06-10 23:24:26 -0700 | [diff] [blame] | 156 | or (using BAZEL\_V<b>C</b>): |
Laszlo Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 157 | |
Laszlo Csomor | 4691ad9 | 2018-05-14 01:58:00 -0700 | [diff] [blame] | 158 | export BAZEL_VC="C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC" |
Laszlo Csomor | 7574865 | 2018-07-26 07:38:14 -0700 | [diff] [blame] | 159 | export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))" |
Laszlo Csomor | 4691ad9 | 2018-05-14 01:58:00 -0700 | [diff] [blame] | 160 | export PATH="/c/python27:$PATH" |
| 161 | export JAVA_HOME="C:/Program Files/Java/jdk1.8.0_112" |
Laszlo Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 162 | |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 163 | 3. `cd` to the directory where you unpacked the distribution archive. |
Laszlo Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 164 | |
| 165 | 4. Run the compilation script: `./compile.sh` |
| 166 | |
| 167 | The compiled output is placed into `output/bazel.exe`. This is a self-contained |
Laszlo Csomor | ebb902e | 2018-10-22 00:56:43 -0700 | [diff] [blame] | 168 | Bazel binary, without an embedded JDK. You can copy it anywhere or use it |
| 169 | in-place. For convenience we recommend copying this binary to a directory that's |
| 170 | on your `PATH`. |
Laszlo Csomor | 6532ecc | 2018-05-04 01:21:54 -0700 | [diff] [blame] | 171 | |
| 172 | You don't need to run Bazel from the MSYS2 shell. You can run Bazel from the |
| 173 | Command Prompt (`cmd.exe`) or PowerShell. |