The BUILD language
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.
Types
The set of builtin types are again fairly familiar:
- Integers (all integers are 64-bit signed integers)
- Strings
- Lists
- Dictionaries
- Functions
-
Booleans (named
True
andFalse
)
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).
Functions
The build language has a rich set of builtin functions that largely resemble those provided by python. In addition to these, there are some additional built in functions specific to the Please domain. See the built in functions documentation for more information.
Style
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.
Grammar
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.
# Start symbol for the grammar, representing the top-level structure of a file.
file_input = { statement };
# Any single statement. Must occur on its own line.
statement = ( "pass" | "continue" | "break" | func_def | for | if | return |
assert | ident_statement | expression ) EOL;
return = "return" [ expression { "," expression } ];
assert = "assert" expression [ "," expression ];
for = "for" Ident { "," Ident } "in" expression ":" EOL { statement };
if = "if" expression ":" EOL { statement }
[ "elif" expression ":" EOL { statement } ]
[ "else" ":" EOL { statement } ];
func_def = "def" Ident "(" [ argument { "," argument } ] ")" ":" EOL
[ String EOL ]
{ statement };
argument = Ident [ ":" String { "|" String } ] { "&" Ident } [ "=" expression ];
ident_statement = Ident
( { "," Ident } "=" expression
| ( "[" expression "]" ( "=" | "+=" ) expression)
| ( "." ident | call | "=" expression | "+=" expression ) );
# Any generalised expression, with all the trimmings.
expression = [ "-" | "not" ] value [ operator expression ]
[ "if" expression "else" expression ];
string = [ "f" | "r" ] String;
value = ( string | Int | "True" | "False" | "None" | list | dict | parens | lambda | ident )
[ slice ] [ ( "." ident | call ) ];
ident = Ident { "." ident | call };
call = "(" [ arg { "," arg } ] ")";
arg = expression | ident "=" expression;
list = "[" expression [ { "," expression } | comprehension ] "]";
parens = "(" expression { "," expression } ")";
dict = "{" expression ":" expression [ { "," expression ":" expression } | comprehension ] "}";
comprehension = "for" Ident { "," Ident } "in" expression
[ "for" Ident { "," Ident } "in" expression ]
[ "if" expression ];
slice = "[" [ expression ] [ ":" expression ] "]";
lambda = "lambda" [ lambda_arg { "," lambda_arg } ] ":" expression;
lambda_arg = Ident [ "=" expression ];
operator = ("+" | "-" | "*" | "/" | "%" | "<" | ">" | "and" | "or" |
"is" | "is" "not" | "in" | "not" "in" | "==" | "!=" | ">=" | "<=" | "|");
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).
Additionally, arguments can be aliased using the
def func(arg_name: str&arg_alias):...
syntax, such that
func(arg_name="example")
and
func(arg_alias="example")
are equivalent.
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.