Gazelle is the tool used across the Bazel ecosystem to create and maintain BUILD files automatically.
It implements a tree walk over the source tree, essentially doing these steps:
- List the source files, to create or update a
cc_library
target with those in thesrcs
- Read the source files and find the
include
statements. - Each included symbol comes from a header file. Locate the
cc_library
target which provides the symbol and add it todeps
. - Merge the results with the original BUILD file
Thanks to that final step, Gazelle is non-destructive. Sections of the file which Gazelle doesn’t manage are left alone. You can also opt-out of Gazelle management of any syntactic element (a rule declaration, attribute list, or individual attribute) with a # keep
comment.
Gazelle extension written in Go
Gazelle has a Language
interface allowing extensions to add support for more languages. EngFlow have open-sourced an extension, which expects import statements to be relative to the repository root:
Gazelle extension written in Starlark
Aspect CLI includes a bridge that allows writing extensions in Starlark, Bazel’s configuration language. See https://docs.aspect.build/cli/starlark for details.
Here’s an example: Embed GitHub
Demo
We can modify source files in the bazel-examples repo and run bazel configure
.