Please's BUILD files typically contain a series of build rule declarations. These are
invocations of builtins like java_binary
which create new BUILD targets.
However, you can do much more with it; it is a fully capable programming language with which it's possible to script the creation of build targets in elaborate ways. See below for a formal description of the grammar; it is a subset of Python so should be fairly familiar.
You can do most things that one might expect in such a language; for
and
if
statements, define functions, create lists and dicts, etc. Conventionally
we keep complex logic in build_defs files but at present there is no difference in accepted
syntax between the two.
One obviously needs a mechanism to import new code; in Please that is subinclude. This function takes the output of a build rule elsewhere in the repo and makes it available in the context of the currently executing file - for example, if it has defined a function, that function is now available in your BUILD file at the top level.
See the built in rules & languages for a list of built in rules, as well as any extra rules from the pleasings repo.
The set of builtin types are again fairly familiar:
True
and False
)There are no floating-point numbers or class types. In some cases lists and dicts can be "frozen" to prohibit modification when they may be shared between files; that's done implicitly by the runtime when appropriate.
Dictionaries are somewhat restricted in function; they may only be keyed by strings and cannot
be iterated directly - i.e. one must use keys()
, values()
or
items()
. The results of all these functions are always consistently ordered.
They support PEP-584 style unions (although not the |= form).
We normally write BUILD files in an idiom which doesn't quite match standard Python styles. The justification is that these are mostly just inherited from working on Blaze, but a brief explanation follows after an example:
# Taken from //src/core/BUILD in the Please repo
go_library(
name = "core",
srcs = glob(["*.go"], exclude=["*_test.go", "version.go"]) + [":version"],
visibility = ["PUBLIC"],
deps = [
"//third_party/go:gcfg",
"//third_party/go:logging",
"//third_party/go:queue",
]
)
All arguments to build rules are passed as keywords. This is pretty important since (1) nobody will be able to read your BUILD file otherwise and (2) we don't guarantee not to change the order of arguments when we insert new ones. Fortunately Please will check this for you at runtime.
Arguments to functions like glob()
and subinclude()
are not
necessarily passed as keywords.
We put spaces around the =
for each argument to the build rule - we think it's
easier to read this way.
Either single or double quotes work, as usual, but don't mix both in one file. We usually prefer double because that's what Buildifier (see below) prefers.
Lists either go all on one line:
["*_test.go", "version.go"]
or are broken across multiple lines like so:
[
"//third_party/go:gcfg",
"//third_party/go:logging",
"//third_party/go:queue",
]
Indentation is normally four spaces. Tabs will be rejected by the parser.
Dealing with indentation in a whitespace-significant language is tricky enough without
introducing tabs to complicate the situation as well.
We generally try to order lists lexicographically where it does not matter (for example
deps
or visibility
).
If you'd like an autoformatter for BUILD files, Google's Buildifier is very good & fast. We use it both internally & on the Please repo.
The grammar is defined as (more or less) the following in EBNF, where Ident
,
String
, Int
and EOL
are token types emitted by the lexer.
{{ .Grammar }}
As mentioned above, this is similar to Python but lacks the import
, try
,
except
, finally
, class
, global
,
nonlocal
, while
and async
keywords. The implementation
disallows using these as identifiers nonetheless since some tools might
attempt to operate on the file using Python's ast
module for convenience, which would not
be possible if those keywords are used.
As a result, while raise
and assert
are supported, it's
not possible to catch and handle the resulting exceptions. These hence function only to
signal an error condition which results in immediate termination.
Note that assert
is never optimised out, as it can be in Python.
A more limited set of operators than in Python are available. The provided set are considered sufficient for use in BUILD files.
Function annotations similar to PEP-3107
/ PEP-484 are available, although
they have first-class meaning as type hints. The arguments are annotated with the expected
type or types (separated by |
) and when called the type of the argument will
be verified to match. This makes it easier to give useful feedback to users if they
make mistakes in their BUILD files (e.g. passing a string where a list is required).
User-defined varargs and kwargs functions are not supported.
PEP-498 style "f-string" interpolation is available, but it is deliberately much more limited than in Python; it can only interpolate variable names rather than arbitrary expressions.