Compiling Bazel from source on Raspberry Pi

Building Bazel without an existing Bazel binary is known as bootstraping.

INSTALL THE PREREQUISITES

First, update apt-get to make sure it knows where to download everything.

sudo apt-get update
Next, install some base dependencies.
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip autoconf automake libtool
sudo apt-get install gcc-4.8
sudo apt-get install gcc++-4.8

PROCEDURE FOR RASPBIAN BUSTER

Check the version of the JDK:
dpkg --list | grep -i jdk
You must install version 8 of the JDK. Versions other than 8 are not supported.

REMOVING THE JDK

To uninstall Oracle JDK, remove the alternatives by executing the following commands:
sudo update-alternatives --remove "java" "/usr/lib/jvm/jdk[version]/bin/java"
sudo update-alternatives --remove "javac" "/usr/lib/jvm/jdk[version]/bin/javac"
sudo update-alternatives --remove "javaws" "/usr/lib/jvm/jdk[version]/jre/bin/javaws"
After removing link, remove the package inside /usr/lib/jvm/jdk[version] by executing following command:
sudo rm -r /usr/lib/jvm/jdk[version]

INSTALLING THE JDK

Raspbian Stretch came with Java 8 while Raspbian Buster with Java 11. We shall use Java 8.
sudo apt-get install openjdk-8-jdk

PROCEDURE FOR RASPBIAN STRETCH AND BUSTER

ADJUSTING A SWAP FILE

The Raspbian distribution comes with a 100mb swapfile. This is actually a bit on the small side. A general rule of thumb is swapfile size should be about twice as much as the available RAM on the machine. We will change the configuration in the file */etc/dphys-swapfile *:
sudo nano /etc/dphys-swapfile
The default value in Raspbian is:
CONF_SWAPSIZE=100
We will need to change this to:
CONF_SWAPSIZE=2048
Then you will need to stop and start the service that manages the swapfile on Rasbian:
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
You can then verify the amount of memory + swap by issuing the following command:
free -m
The output should look like:
              total        used        free      shared  buff/cache   available
Mem:           3854         446        2697         162         710        3120
Swap:          2047           0        2047

BUILDING BAZEL

Getting Bazel:
wget https://github.com/bazelbuild/bazel/releases/download/0.21.0/bazel-0.21.0-dist.zip
unzip -d bazel bazel-0.21.0-dist.zip
cd bazel
Change the permissions of every file in the Bazel project with:
sudo chmod u+w ./* -R
Compile Bazel with options:
env BAZEL_JAVAC_OPTS="-J-Xms384m -J-Xmx800m" \
JAVA_TOOL_OPTS="-Xmx800m" \
EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" \
bash ./compile.sh
When the build finishes, you end up with a new binary, output/bazel. Copy that to your /usr/local/bin directory:
sudo cp output/bazel /usr/local/bin/bazel
To make sure it's working properly, run bazel on the command line and verify it prints help text:
bazel

Usage: bazel <command> <options> ...

Available commands:
  analyze-profile     Analyzes build profile data.
  build               Builds the specified targets.
  canonicalize-flags  Canonicalizes a list of bazel options.
  clean               Removes output files and optionally stops the server.
  dump                Dumps the internal state of the bazel server process.
  fetch               Fetches external repositories that are prerequisites to the targets.
  help                Prints help for commands, or the index.
  info                Displays runtime info about the bazel server.
  mobile-install      Installs targets to mobile devices.
  query               Executes a dependency graph query.
  run                 Runs the specified target.
  shutdown            Stops the bazel server.
  test                Builds and runs the specified test targets.
  version             Prints version information for bazel.

Getting more help:
  bazel help <command>
                   Prints help and options for <command>.
  bazel help startup_options
                   Options for the JVM hosting bazel.
  bazel help target-syntax
                   Explains the syntax for specifying targets.
  bazel help info-keys
                   Displays a list of keys used by the info command.

REFERENCES:

0. Compiling Bazel from source