Build Bazel on Raspberry Pi

In this post you will learn the followings:

  • What is bazel
  • How to build it on Raspberry PI 3 B+

What is Bazel?

The answer to this question is simple! Bazel is a build tool like Make, Maven, etc.! It will build your code like other build tools but from my point of view the advantages that bazel gives are:

  • it provides a simple workspace to build codes in different languages

  • it is multi-platform, that’s why we want to use it in Raspberry Pi.

You can find more information about bazel on its official website.

How to build it on Raspberry PI 3B+

Sometimes you want to build a very simple application (Let say, object detection and tracking using Tensorflow ;)) in C/C++ on your edge device like Raspberry Pi (or whatever) but it uses huge libraries and you first need to build those libraries for your edge device and there you will need to use bazel. The following steps are necessary to build bazel version 3.7.2 from the source which has been tested on Raspberry PI 3B+ 1.4GHz Cortex-A53 Rev. 1.3 with 1GB RAM and 16GB SD card which runs Raspberry Pi OS based on Debian Buster (Legacy). I guess these steps also work for other versions (comment below if it didn’t work – we will make it work).

  • If you have a fresh SD card with Raspbian on it, first make sure to expand the filesystem, so it ensures the availability of all SD card. You can do it in different ways like:

    sudo raspi-config # Advanced Options -> Expand Filesystem
    
  • (Optional): increase swap size:

    sudo dphys-swapfile swapoff # Disable Swap 
    sudo nano /etc/dphys-swapfile # Set CONF_SWAPSIZE to 512 or more 
    sudo dphys-swapfile swapon # Enable Swap
    
  • Install the requirements for compiling bazel:

    sudo apt update sudo apt install build-essential libatomic1 zip unzip
    
  • Install openjdk 11 (you may compile it with openjdk 8 – see this for more information) set it:

    sudo apt install openjdk-11-jdk-headless sudo update-java-alternatives -s java-1.11.0-openjdk-armhf
    
  • Download the proper source version from Github releases, I’ve used Release 3.7.2 (2020-12-17):

    mkdir ~/bazel11 && cd $_ 
    wget https://github.com/bazelbuild/bazel/releases/download/[YOUR_VERSION]/bazel-[YOUR_VERSION]-dist.zip unzip bazel-[YOUR_VERSION]-dist.zip
    
  • Open ~/bazel/tools/cpp/unix_cc_configure.bzl and add -latomic link flag to bazel_linkopts around line 395. Otherwise, some undefined reference to … will pop up during compilation.

  • Open ~/bazel/tools/jdk/BUILD and add ":jni_md_header-linux" to "//conditions:default" around line 145 for cc_library-bazel-rule in hdrs-block . And "include/linux" to "//conditions:default" in includes-block.

  • Start compiling (it will take time):

    env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./compile.sh
    
  • You can add output/bazel to PATH or copy everything in /usr/local/bin.

  • Run bazel in the terminal and you should see the following result:

    [bazel release 3.7.2- (@non-git)]
    Usage: bazel <command> <options> ...
    
    Available commands:
      analyze-profile     Analyzes build profile data.
      aquery              Analyzes the given targets and queries the action graph.
      build               Builds the specified targets.
      canonicalize-flags  Canonicalizes a list of bazel options.
      clean               Removes output files and optionally stops the server.
      coverage            Generates code coverage report for specified test targets.
      cquery              Loads, analyzes, and queries the specified targets w/ configurations.
      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.
      license             Prints the license of this software.
      mobile-install      Installs targets to mobile devices.
      print_action        Prints the command line args for compiling a file.
      query               Executes a dependency graph query.
      run                 Runs the specified target.
      shutdown            Stops the bazel server.
      sync                Syncs all repositories specified in the workspace file
      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.
    

That’s it!

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *