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 | sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python |
Using Bazel custom APT repository
unable to access
googleapis.com
1 | sudo apt-get install openjdk-8-jdk |
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 | cc_binary( |
build target
1 | cd stage1 |
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 | bazel query --nohost_deps --noimplicit_deps 'deps(//main:hello-world)' --output graph |
graph
stage2
Specify multiple build targets
cpp-tutorial/stage2/main/BUILD
1 | cc_library( |
build target
1 | cd stage2 |
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
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 | cc_library( |
This is because by default targets are only visible to other targets in the same BUILD file.
main/BUILD
1 | cc_library( |
build target
1 | cd stage3 |
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
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.