Bazel is an open-source build and test tool developed by Google. It is
designed to handle large-scale software projects with complex dependencies and
is used to automate the building and testing of code.
Here are some key features and characteristics of Bazel:
1. High Performance: Bazel is optimized
for speed and can efficiently handle large codebases with many dependencies. It
supports parallel execution and caching to minimize build times.
2. Language Support: Bazel supports
multiple programming languages, including Java, C++, Python, Go, and more. It
allows for polyglot builds, where projects can have components written in
different languages.
3. Reproducible Builds: Bazel ensures that
builds are reproducible by maintaining a consistent build environment. This
means that given the same source code, dependencies, and environment, Bazel
will produce the same build outputs.
4. Scalability: Bazel is designed
to scale with your project. It can handle projects with millions of lines of
code and complex dependency graphs.
5. Remote Execution and
Caching: Bazel can distribute build and test tasks across multiple machines,
leveraging remote execution and caching to further speed up the build process.
6. Extensibility: Bazel is highly
extensible, allowing users to define custom build rules and macros to fit their
specific needs.
7. Dependency
Management: Bazel provides a robust mechanism for managing dependencies, ensuring
that builds are correct and isolated from changes in the environment or other
parts of the codebase.
Bazel is used by many large organizations, including Google, to manage
their extensive codebases and complex build processes.
Bazel
terms and basics explained
Workspace
The workspace is typically the
directory where Bazel constructs your project's build files from your source
code. It contains various source files in a nested hierarchical fashion. At the
highest or root level of your workspace, your project might also have a dedicated WORKSPACE text file. It contains
references to all the external dependencies your project requires to generate
your builds. Precisely, the workspace is where your inputs are extracted and
converted to outputs to generate the desired build file.
Packages
Packages are simply
directories located below the top-level directory in a workspace. They contain
your build files that may be named BUILD or BUILD.bazel , along with other
related files and specified dependencies. One package may be nested in another
since your source code is organized in a hierarchical fashion.
Targets
Everything inside your packages can be
considered targets. It includes source
files that developers of your team wrote and added to your project or generated
files that Bazel constructs based on your build configurations. Other than
files, targets often involve rules that govern the relationship between your input
files and output files. In other words, target rules specify how Bazel is going
to construct your build files, the intermediary steps it's going to take along
with every executable operation it will need to perform. Your input files for
target rules could be either source files or generated files. Although, your
output files are always generated files, since it's an outcome of the build
tool itself. You may specify your target rules to chain your inputs and outputs
for consecutive build operations. For instance, you could use a generated file
from a previous step as an input file for another step in the future.
Labels
The nomenclature of a target is known
as a label. It's just a way to
recognize different targets falling under a package and distinguishing them
from other targets in the same or different packages.
Dependencies
When you build your specified targets,
one or more target may depend on another for the build
process. The latter is a dependency. To explain
further, consider two targets Target1 and Target2. Let's say Target1 needs Target2 at build or execution time. We
can express this relationship as a Directed Acyclic Graph that
demonstrates how Target1 is
dependent on `Target12 in this process. Bazel calls this a dependency graph and is used to
differentiate between actual and declared dependencies.
Build
Files
Earlier, we said that under a WORKSPACE every package contains its
own BUILD file. Build files contain the
program that enables Bazel to generate the desired builds for your project.
It's evaluated using Starlark and contains a list of sequential statements that
are executed by Bazel. The build files contain your declared rules, functions
that execute these rules, and variables required in a simple syntax form.
We know that Bazel caches previous builds for speeding up the build process.
Whenever your source files change, it is the BUILD file that Bazel references for understanding the
underlying changes.
Commands
Bazel offers a set of commands that
allow you to execute certain operations. The simplest command you can run is to
check if Bazel is successfully installed in your system: $
bazel version