Bazel BUILD Files: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(56 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=
* https://bazel.build/concepts/build-files
* https://bazel.build/concepts/build-files
* https://bazel.build/reference/be/overview
=Internal=
=Internal=
* [[Bazel_Concepts#BUILD_Files|Bazel Concepts]]
* [[Bazel_Concepts#BUILD_Files|Bazel Concepts]]
=Overview=
=Overview=
A <code>BUILD</code> is a short program, an interpretable list of [[Starlark|Starlark]] statements. It can be named <code>BUILD</code> or <code>BUILD.bazel</code> and where both exist, <code>BUILD.bazel</code> takes precedence.
A <code>BUILD</code> is a short program, an interpretable list of [[Starlark|Starlark]] statements.  
 
The BUILD file specified of how the files of a [[Bazel_Concepts#Package|Bazel package]] can be used to produce artifacts.
 
There is a one-to-one relationship between a BUILD file and a [[Bazel_Concepts#Package|Bazel package]]. The BUILD file resides in the [[Bazel_Concepts#Package_Directory|package directory]].
 
The BUILD file defines the package's [[Bazel_Concepts#Target|targets]].
 
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 18: Line 34:
<code>BUILD</code> files need to be updated whenever the dependencies of the underlying code change. IntelliJ does that via the "Sync" functionality.
<code>BUILD</code> files need to be updated whenever the dependencies of the underlying code change. IntelliJ does that via the "Sync" functionality.


=<span id='Build_Rule'></span><span id='Rule'></span>Build Rules=
=<span id='Build_Rule'></span><span id='Rule'></span><span id='Build_Rules'></span>Rules=
 
A rule specifies the relationship between a set of input and a set of output files.
 
The inputs to a rule may be [[Bazel_Concepts#Source_File|source files]], but they also may be the outputs of other rules. <font color=darkkhaki>It this case is said that the input of a rule is other rule.</font>
 
The output files of rule always belong to the same [[Bazel_Concepts#Package|package]] as the rule itself. It is not possible to generate files into another package. It is not uncommon for a rule's inputs to come from another package, though.
 
When a build rule [[#Function|function]] is executed, it creates a new target in the graph, which can later referred using a label, declared as <code>[[Bazel_go_library#name|name]]</code> in the rule declaration.
 
The majority of build rules come in families, grouped together by language. For example, <code>[[#go_binary|go_binary]]</code>, <code>[[#go_library|go_library]]</code> and <code>[[#go_test|go_test]]</code> are the build rules for Go binaries, libraries, and tests, respectively.
 
<code>*_binary</code> 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 <code>//my:program</code> would appear at <code>$(BINDIR)/my/program</code>. <code>*_test</code> rules are a specialization of a <code>*_binary</code> rule used for automated testing. <code>*_library</code> rules specify separately-compiled modules in the given programming language.
 
==Go Rules==
{{External|https://github.com/bazelbuild/rules_go}}
===Core Rules===
{{External|https://github.com/bazel-contrib/rules_go/blob/master/docs/go/core/rules.md}}
Three core rules may be used to build most projects: <code>[[Bazel_go_library#Overview|go_library]]</code>, <code>[[Bazel_go_binary#Overview|go_binary]]</code> and <code>[[Bazel_go_test#Overview|go_test]]</code>. These rules reimplement the low level commands of a normal <code>go build</code> invocation: compiling package's source files to archives, then linking archives into go binary.
====<tt>go_library</tt>====
{{Internal|Bazel_go_library#Overview|<tt>go_library</tt>}}
====<tt>go_binary</tt>====
{{Internal|Bazel_go_binary#Overview|<tt>go_binary</tt>}}
====<tt>go_test</tt>====
{{Internal|Bazel_go_test#Overview|<tt>go_test</tt>}}
====<tt>go_source</tt>====
{{External|https://github.com/bazel-contrib/rules_go/blob/master/docs/go/core/rules.md#go_source}}
====<tt>go_path</tt>====
{{External|https://github.com/bazel-contrib/rules_go/blob/master/docs/go/core/rules.md#go_path}}


When a build rule [[#Function|function]] is executed, it creates a new target in the graph, which can later referred using a label.
===Gazelle===
====Generating <tt>BUILD</tt> files with Gazelle====
{{Internal|Gazelle#Generating_BUILD_Files|Gazelle &#124; Generating <tt>BUILD</tt> Files}}
====Repository Rules with Gazelle====
{{Internal|Gazelle#Repository_Rules|Gazelle &#124; Repository Rules}}
 
===Build-time Code Analysis with <tt>nogo</tt>===
{{Internal|Nogo#Overview|<tt>nogo</tt>}}
 
===Protocol Buffers===
{{Internal|Bazel_Protocol_Buffers_Rules#Overview|Protocol Buffers Rules}}
 
====<tt>go_repository</tt>====
 
===Other Go Rules===
====Uber <tt>gomock</tt>====
{{Internal|Github.com/uber/mock#Bazel_Integration_with_the_gomock_Rule|Uber <tt>gomock</tt> Rule}}


=<span id='Function'></span>Functions=
=<span id='Function'></span>Functions=
Line 48: Line 108:


This can be used to lad new [[#Rule|build rules]], functions or constants.
This can be used to lad new [[#Rule|build rules]], functions or constants.
<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 07:24, 23 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:

Simple BUILD File for a Go Project

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.

Rules

A rule specifies the relationship between a set of input and a set of output files.

The inputs to a rule may be source files, but they also may be the outputs of other rules. It this case is said that the input of a rule is other rule.

The output files of rule always belong to the same package as the rule itself. It is not possible to generate files into another package. It is not uncommon for a rule's inputs to come from another package, though.

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

The majority of build rules come in families, grouped together by language. For example, go_binary, go_library and go_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

https://github.com/bazelbuild/rules_go

Core Rules

https://github.com/bazel-contrib/rules_go/blob/master/docs/go/core/rules.md

Three core rules may be used to build most projects: go_library, go_binary and go_test. These rules reimplement the low level commands of a normal go build invocation: compiling package's source files to archives, then linking archives into go binary.

go_library

go_library

go_binary

go_binary

go_test

go_test

go_source

https://github.com/bazel-contrib/rules_go/blob/master/docs/go/core/rules.md#go_source

go_path

https://github.com/bazel-contrib/rules_go/blob/master/docs/go/core/rules.md#go_path

Gazelle

Generating BUILD files with Gazelle

Gazelle | Generating BUILD Files

Repository Rules with Gazelle

Gazelle | Repository Rules

Build-time Code Analysis with nogo

nogo

Protocol Buffers

Protocol Buffers Rules

go_repository

Other Go Rules

Uber gomock

Uber gomock Rule

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.