0%

how to install and use bazel on ubuntu 16.04

How to Install

Bazel is an open-source build and test tool similar to Make, Maven, and Gradle. It uses a human-readable, high-level build language. Bazel supports projects in multiple languages and builds outputs for multiple platforms. Bazel supports large codebases across multiple repositories, and large numbers of users.

support language and platform:

  • c++
  • java
  • android
  • ios

Using binary installer

1
2
3
4
5
6
7
8
9
10
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python

#download `bazel-0.16.1-installer-linux-x86_64.sh` from `https://github.com/bazelbuild/bazel/releases`

chmod +x bazel-0.16.1-installer-linux-x86_64.sh
./bazel-0.16.1-installer-linux-x86_64.sh --user
# The --user flag installs Bazel to the $HOME/bin directory on your system and sets the .bazelrc path to $HOME/.bazelrc.

vim .bashrc
export PATH="$PATH:$HOME/bin"

Using Bazel custom APT repository

unable to access googleapis.com

1
2
3
4
5
6
7
8
sudo apt-get install openjdk-8-jdk

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -


sudo apt-get update && sudo apt-get install bazel
sudo apt-get upgrade bazel

Tutorial

get examples

1
git clone https://github.com/bazelbuild/examples/

folder structure

cpp-tutorial/
├── README.md
├── stage1
│   ├── main
│   │   ├── BUILD
│   │   └── hello-world.cc
│   ├── README.md
│   └── WORKSPACE
├── stage2
│   ├── main
│   │   ├── BUILD
│   │   ├── hello-greet.cc
│   │   ├── hello-greet.h
│   │   └── hello-world.cc
│   ├── README.md
│   └── WORKSPACE
└── stage3
    ├── lib
    │   ├── BUILD
    │   ├── hello-time.cc
    │   └── hello-time.h
    ├── main
    │   ├── BUILD
    │   ├── hello-greet.cc
    │   ├── hello-greet.h
    │   └── hello-world.cc
    ├── README.md
    └── WORKSPACE

7 directories, 20 files

stage1

Understand the BUILD file

cpp-tutorial/stage1/main/BUILD

1
2
3
4
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
)

build target

1
2
cd stage1
bazel build //main:hello-world

output

INFO: Found 1 target...
Target //main:hello-world up-to-date:
  bazel-bin/main/hello-world
INFO: Elapsed time: 2.267s, Critical Path: 0.25s

test binary

1
bazel-bin/main/hello-world

Review the dependency graph

install graphviz

1
sudo apt install graphviz xdot

vizualize

1
2
bazel query --nohost_deps --noimplicit_deps 'deps(//main:hello-world)' --output graph
xdot <(bazel query --nohost_deps --noimplicit_deps 'deps(//main:hello-world)' --output graph)

graph

png

stage2

Specify multiple build targets

cpp-tutorial/stage2/main/BUILD

1
2
3
4
5
6
7
8
9
10
11
12
13
cc_library(
name = "hello-greet",
srcs = ["hello-greet.cc"],
hdrs = ["hello-greet.h"],
)

cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
deps = [
":hello-greet",
],
)

build target

1
2
cd stage2
bazel build //main:hello-world

output

INFO: Found 1 target...
Target //main:hello-world up-to-date:
  bazel-bin/main/hello-world
INFO: Elapsed time: 2.267s, Critical Path: 0.25s

test binary

1
bazel-bin/main/hello-world

graph

png

stage3

Use multiple packages

folder structure

└──stage3
   ├── main
   │   ├── BUILD
   │   ├── hello-world.cc
   │   ├── hello-greet.cc
   │   └── hello-greet.h
   ├── lib
   │   ├── BUILD
   │   ├── hello-time.cc
   │   └── hello-time.h
   └── WORKSPACE

lib/BUILD

1
2
3
4
5
6
cc_library(
name = "hello-time",
srcs = ["hello-time.cc"],
hdrs = ["hello-time.h"],
visibility = ["//main:__pkg__"],
)

This is because by default targets are only visible to other targets in the same BUILD file.

main/BUILD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cc_library(
name = "hello-greet",
srcs = ["hello-greet.cc"],
hdrs = ["hello-greet.h"],
)

cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
deps = [
":hello-greet",
"//lib:hello-time",
],
)

build target

1
2
cd stage3
bazel build //main:hello-world

output

INFO: Found 1 target...
Target //main:hello-world up-to-date:
  bazel-bin/main/hello-world
INFO: Elapsed time: 2.267s, Critical Path: 0.25s

test binary

1
bazel-bin/main/hello-world

graph

png

Use labels to reference targets

//path/to/package:target-name
  • When referencing targets within the same package, you can skip the package path and just use //:target-name.
  • When referencing targets within the same BUILD file, you can even skip the // workspace root identifier and just use :target-name.

Reference

History

  • 20180821: created.