Bazel BUILD Files: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 25: Line 25:


Functions can be declared in <code>.bzl</code> files.
Functions can be declared in <code>.bzl</code> files.
=<span id='Extension'></span>Extensions=
Bazel extensions are files ending in <code>.bzl</code>. Symbols from an extension are imported with the <code>load()</code> statement:
<syntaxhighlight lang='go'>
load("//foo/bar:file.bzl", "some_library")
load("//meta/rules:go.bzl", "go_library")
load("//somewhere/gomock:defs.bzl", "gomock")
some_library(
  ...
)
go_library(
  ...
)
gomock(
  ...
)
</syntaxhighlight>

Revision as of 21:14, 16 July 2024

External

Internal

Overview

A BUILD is a short program, an interpretable list of Starlark statements. It can be named BUILD or BUILD.bazel and where both exist, BUILD.bazel takes precedence.

General Syntax

Variables must be defined before they are used.

The relative order in which build rules are declared is unimportant.

The file cannot contain function definitions, for or if control statements. List comprehensions and if expressions are allowed.

Starlark programs can't perform arbitrary I/O, which makes the interpretation of the BUILD files hermetic.

BUILD files need to be updated whenever the dependencies of the underlying code change. IntelliJ does that via the "Sync" functionality.

Build Rules

When a build rule function is executed, it creates a new target in the graph, which can later referred using a label.

Functions

Functions can be declared in .bzl files.

Extensions

Bazel extensions are files ending in .bzl. Symbols from an extension are imported with the load() statement:

load("//foo/bar:file.bzl", "some_library")
load("//meta/rules:go.bzl", "go_library")
load("//somewhere/gomock:defs.bzl", "gomock")

some_library(
  ...
)

go_library(
  ...
)

gomock(
  ...
)