Bazel BUILD Files: Difference between revisions
(8 intermediate revisions by the same user not shown) | |||
Line 16: | Line 16: | ||
The BUILD file can be named <code>BUILD</code> or <code>BUILD.bazel</code> and where both exist, <code>BUILD.bazel</code> takes precedence. | The BUILD file can be named <code>BUILD</code> or <code>BUILD.bazel</code> and where both exist, <code>BUILD.bazel</code> takes precedence. | ||
= | =Example= | ||
A simple BUILD file that produces an executable whose source is written in Go and that are fist built into a library: | |||
{{Internal|Bazel_go_binary#Example|Simple BUILD File for a Go Project}} | |||
=General Syntax= | =General Syntax= | ||
Line 48: | Line 52: | ||
===<tt>go_library</tt>=== | ===<tt>go_library</tt>=== | ||
{{Internal|Bazel_go_library#Overview|<tt>go_library</tt>}} | {{Internal|Bazel_go_library#Overview|<tt>go_library</tt>}} | ||
===<tt>go_test</tt>=== | |||
{{Internal|Bazel_go_test#Overview|<tt>go_test</tt>}} | |||
===<tt>go_binary</tt>=== | ===<tt>go_binary</tt>=== | ||
{{Internal|Bazel_go_binary#Overview|<tt>go_binary</tt>}} | {{Internal|Bazel_go_binary#Overview|<tt>go_binary</tt>}} | ||
===<tt>go_repository</tt>=== | ===<tt>go_repository</tt>=== | ||
Line 85: | Line 90: | ||
<font color=darkkhaki>TODO: https://bazel.build/concepts/build-files#load</font> | <font color=darkkhaki>TODO: https://bazel.build/concepts/build-files#load</font> | ||
=Dependency Management= | |||
The dependencies can be autogenerated with Gazelle. Add them to the Go source file and run gazelle. |
Latest revision as of 21:35, 3 November 2024
External
Internal
Overview
A BUILD
is a short program, an interpretable list of Starlark statements.
The BUILD file specified of how the files of a Bazel package can be used to produce artifacts.
There is a one-to-one relationship between a BUILD file and a Bazel package. The BUILD file resides in the package directory.
The BUILD file defines the package's targets.
The BUILD file can be named BUILD
or BUILD.bazel
and where both exist, BUILD.bazel
takes precedence.
Example
A simple BUILD file that produces an executable whose source is written in Go and that are fist built into a library:
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
Reconcile with Bazel_Concepts#rules_go.
When a build rule function is executed, it creates a new target in the graph, which can later referred using a label.
The majority of build rules come in families, grouped together by language. For example, go_binary
, go_library
and cc_test
are the build rules for Go binaries, libraries, and tests, respectively.
*_binary
rules build executable in a given language. After a build, the executable will reside in the build tool's binary output tree at the corresponding name for the rule's label, so //my:program
would appear at $(BINDIR)/my/program
.
*_test
rules are a specialization of a *_binary
rule used for automated testing.
*_library
rules specify separately-compiled modules in the given programming language.
Go Rules
go_library
go_test
go_binary
go_repository
Other Rules
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(
...
)
This can be used to lad new build rules, functions or constants.
TODO: https://bazel.build/concepts/build-files#load
Dependency Management
The dependencies can be autogenerated with Gazelle. Add them to the Go source file and run gazelle.