This is the reference for the complete set of builtin rules & functions.
The build language largely resembles Python. As such, following python inspired functions are available as builtins:
len(x)
- returns the length of x
.
chr(i)
- returns a string containing the character whose Unicode code point
is the integer i
.
ord(c)
- returns an integer representing the Unicode code point of the (single)
character in the string c
.
enumerate(seq)
- returns a list of pairs of the index and object for each item in
seq
.
zip(x, y,
...)
- returns a list in which each element has one item from each argument.
isinstance(x, type)
- returns True if x
is of the given type.
range([start, ]stop[,
step])
- returns a list of integers up to stop
(or
from start
to stop
).
any(seq)
- returns true if any of the items in seq
are
considered true.
all(seq)
- returns true if all of the items in seq
are
considered true.
min(seq[,key])
- returns the least item in seq
.
key
is a function that is applied to each
item before comparison.
max(seq[,key])
- returns the greatest item in seq
.
key
is a function that is applied to each
item before comparison.
sorted(seq)
- returns a copy of the given list with the contents sorted.
reversed(seq)
- returns a copy of the given list with the items in reverse order.
filter(filter, seq)
- returns a copy of the given list with the items filtered using function.
map(mapper, seq)
- returns a copy of the given list with the items transformed using function.
reduce(reducer, seq, initializer=None)
- returns a single value by applying function cumulatively to the list items, from left to right.
package_name()
- returns the package being currently parsed.
subrepo_name()
- returns the subrepo of the package being currently parsed or the empty
string if this is not a subrepo.
join_path(x, ...)
- joins the given path elements using the OS separator. It will
intelligently handle repeated or missing separators.
split_path(path)
- splits the given path into the directory and filename.
splitext(filename)
- splits the given filename into base name and extension at the final
dot.
basename(path)
- returns the basename of a file.
dirname(path)
- returns the directory name of a file.
breakpoint()
- breaks into an interactive debugger allowing inspection of the current
scope. To enable breakpoints, pass the --debug
flag.
json(x)
- returns a JSON-formatted representation of a plz value.
is_semver(s)
- returns true if s
is a semantic version (either
with or without a leading "v"), or false if not.
semver_check(version, constraint)
- checks if a semantic version
meets a constraint. Supported version and constraint formats are listed on this site.
The following are available as member functions of strings:
join(seq)
- joins the elements of seq
together with
this string as a separator.
split(sep)
- splits this string at each occurrence of the given separator.
replace(old, new)
- returns a copy of this string with all instances of
old
replaced with
new
.
partition(sep)
- breaks this string around the first occurrence of
sep
and returns a 3-tuple of (before, sep,
after).
rpartition(sep)
- breaks this string around the last occurrence of
sep
and returns a 3-tuple of (before, sep,
after).
startswith(prefix)
- returns true if this string begins with
prefix
endswith(suffix)
- returns true if this string ends with
suffix
format(arg1=val1,
arg2=val2,
...)
- Replaces named parameters in the string.
lstrip(cutset)
- strips all characters in cutset
from the
beginning of this string.
rstrip(cutset)
- strips all characters in cutset
from the
end of this string.
strip(cutset)
- strips all characters in cutset
from the
beginning & end of this string.
removeprefix(prefix)
- strips the exact string prefix
from the
beginning of this string.
removesuffix(suffix)
- strips the exact string suffix
from the
end of this string.
find(needle)
- returns the index of the first occurrence of
needle
in this string.
rfind(needle)
- returns the index of the last occurrence of
needle
in this string.
count(needle)
- returns the number of times needle
occurs
in this string.
upper()
- returns a copy of this string converted to uppercase.
lower()
- returns a copy of this string converted to lowercase.
The following are available as member functions of dictionaries:
get(key[,
default])
- returns the item with the given key, or the default (None
if that is not given).
setdefault(key[,
default])
- If the given key is in the dict, return its value, otherwise insert
it with the given value (None
if that is not
given).
keys()
- returns an iterable sequence of the keys of this dictionary, in a
consistent order.
values()
- returns an iterable sequence of the values of this dictionary, in a
consistent order.
items()
- returns an iterable sequence of pairs of the keys and values of this
dictionary, in a consistent order.
copy()
- deprecated, use a comprehension if needed.
Returns a shallow copy of this dictionary.
In addition to the python builtins, Please also has a rich set of its own functions.
Messages can be logged to Please's usual logging mechanism. These may or
may not be displayed depending on the -v
flag;
by default only warning
and above are visible.
log.debug(msg[,
args...])
- the lowest level of messages, output at this level is very verbose.
log.info(msg[,
args...])
- Informational messages, typically this is the highest level
per-target information is logged at.
log.notice(msg[,
args...])
- Messages of general interest during the build; it is rare for this
to be appropriate from a BUILD file.
log.warning(msg[,
args...])
- A warning message that something seems wrong, but it may be
recoverable.
log.error(msg[,
args...])
- A message indicating that something has gone wrong and it will not
be recoverable. This is usually followed shortly by some kind of
failure.
log.fatal(msg[,
args...])
- Indicates that something has gone catastrophically wrong, and causes
the process to exit immediately and unsuccessfully. Usually you are
better off using assert
or
fail()
to indicate failure, since plz can
handle those and annotate with additional output.
subinclude(target)
Includes the output of a build target as extra rules in this one.
This is the closest equivalent to import
in the
BUILD language. It behaves somewhat differently though in that the
contents of the subincluded file are added to the globals of the current
module so can be called directly.
The target that you attempt to subinclude is simply a normal build target, with the restriction that it must have exactly one output. The simplest form of this is a single-file filegroup or export_file rule, but it is possible to have more complex rules generating your BUILD definitions.
For example:
subinclude('//build_defs:my_build_rules')
glob(include, exclude=None, hidden=False)
Matches all filenames by a pattern, in a manner reminiscent of shell-style pattern expansion or Python's builtin glob module.
Note that the expansion patterns accepted are
*
to match an arbitrary number of non-separator
characters (i.e. not /
),
**
to match any number of
complete path components, ? to match a single character,
and [class] to match a class of character e.g. [a-z], [^0-9], or [abc].
These are often referred to as "Ant-style" patterns since Ant
introduced them.
Glob exclude patterns can be evaluated relative to the matched file or the
package root. If the exclude pattern does not contain a path separator, it
is considered relative and will be evaluated against the file name of the
match only, otherwise it will be evaluated from the directory of the build
file. This means that
glob(["**/*.go"], exclude = ["*_test.go"])
will
exclude all files ending in "test.go".
Hidden files (those starting with a .
) are not
matched by any patterns unless you pass
hidden=True
.
It bears noting that you cannot glob generated files, only source files.
Also glob will not descend into any directories that contain a BUILD file;
this maintains an invariant that each file is owned by exactly one package
(or potentially none, but then Please doesn't know or care about them). If
you want to pull in files from another package, export them there using a
filegroup
and depend on that in the package you want it.
Argument | Default | Type | |
---|---|---|---|
include | list | List of paths to include. Each is globbed separately. | |
exclude | None | list |
List of glob patterns to exclude from anything matched by
include .
|
hidden | False | bool | Set to True to include hidden files / folders. |
get_labels(target, prefix, all=False)
Gets the unique set of labels for a rule and all its transitive dependencies.
Two formats are accepted for target: the first is a string containing just
the target name, which is resolved in the current package. This
facilitates calling them from a pre-build function, which is in fact the
only time it's safe to call this way.
The other is a full build target, which should be a transitive dependency
of the target whose pre/post build function you're in. If the target isn't
built when you ask for the labels the build will terminate.
In either case this is only safe to call from a pre / post-build function
and should never be called at initial parse time, because at that point
you generally don't have the full set of labels available yet.
Uses for this are normally fairly language-specific. The clearest example
is maybe the Python rules, where
python_binary
and
python_test
use this to identify if any of their
dependencies have marked them as not being zip-safe.
Argument | Default | Type | |
---|---|---|---|
target | str | Label of the target to get labels for. | |
prefix | None | str | Filters the returned labels to only ones starting with this prefix. |
all | False | bool |
Returns labels from all dependencies, even past those marked as
output_is_complete .
|
has_label(target, prefix)
Returns True if the target has any matching label that would be returned by get_labels.
Argument | Default | Type | |
---|---|---|---|
target | str | Label of the target to get labels for. | |
prefix | None | str | Checks only labels that start with this prefix. |
add_licence(target, licence)
Adds a new licence to a target. The assumption (as usual) is that if multiple are added, they are options, so any one can be accepted.
Argument | Default | Type | |
---|---|---|---|
target | str | Label of the target to add the licence to. | |
licence | str | Name of the licence to add. |
get_licences(target)
Returns all the licences that are currently known to apply to a target.
Argument | Default | Type | |
---|---|---|---|
target | str | Label of the target to get licences for. |
package(...)
Defines settings affecting the current package - for example, default visibility.
With this you can override any current value in
CONFIG
by name for all subsequent targets in the
current package. Only existing values can be replaced.
There are also a couple of special values which aren't normally in
CONFIG
:
default_licences
and
default_visibility
. As the names suggest these
set defaults for those attributes for all following targets that don't set
them.
This function must be called before any targets are defined.
log.warning(message, [args...])
Logs an arbitrary message at some given level. The available levels, from most quiet to most severe, are:
log.debug
log.info
log.notice
log.warning
log.error
log.fatal
These correspond to Please's built in messages and are controlled as usual
by the
-v
command-line argument.
As the name suggests, log.fatal
immediately
terminates the program with a fatal error. The others have no side effect
other than showing the message.
The message and arguments together are interpolated like Python's normal
string interpolation, similar to the builtin
logging
package.
decompose(label)
Decomposes a build label into the package and name parts.
Consider carefully when you should use this - command replacements may be
more appropriate.
Most rules should be able to accept labels without having to know anything
about their structure.
The input label can be given in either relative or absolute form. It will fail with an error if it's not a structurally valid label.
canonicalise(label)
Converts the given build label to its full form.
For example:
//package:target
->
//package:target
//package
->
//package:package
:target
->
//current_package:target
tag(name, tag)
Tags a build label name with a given tag. This can be useful to keep following the intermediate build target
naming convention when a build definition doesn't expose the tag
parameter.
For example:
tag("name", "foo")
->
"_name#foo"
tag("_name#foo", "bar")
->
"_name#foo_bar"
fail(message)
Causes an immediate failure in parsing of the current build file.
Use this where you might raise
in Python.
Miscellaneous rules that aren't language-specific.
{{ template "lexicon_entry.html" .Named "genrule" }} {{ template "lexicon_entry.html" .Named "gentest" }} {{ template "lexicon_entry.html" .Named "export_file" }} {{ template "lexicon_entry.html" .Named "filegroup" }} {{ template "lexicon_entry.html" .Named "hash_filegroup" }} {{ template "lexicon_entry.html" .Named "system_library" }} {{ template "lexicon_entry.html" .Named "remote_file" }} {{ template "lexicon_entry.html" .Named "tarball" }} {{ template "lexicon_entry.html" .Named "text_file" }}git_branch(short:bool=True)
Calls git symbolic-ref -q HEAD
and returns the
corresponding git branch. If short
is false,
show the full symbolic ref for a branch.
Argument | Default | Type | |
---|---|---|---|
short | True | bool | Uses the shortened symbolic ref for a branch. |
git_commit()
Calls git rev-parse HEAD
and returns the
corresponding git commit SHA. To return a shortened commit use
git_commit()[0:8]
.
git_show(format:str)
Calls git show -s --format={format}
and returns
the result. format
is limited to a subset of
values accepted by git show
.
Argument | Default | Type | |
---|---|---|---|
format | str | String format passed to git show . |
Supported format verbs are limited to:
%H
commit hash
%T
tree hash
%P
parent hashes
%an
author name
%ae
author email
%at
author date, UNIX timestamp
%cn
committer name
%ce
committer email
%ct
committer date, UNIX timestamp
%D
ref names
%e
encoding
%s
subject of commit message
%f
sanitized subject line, suitable for a
filename
%b
body of commit message
%B
raw body (unwrapped subject and body)
%N
commit notes
%GG
raw verification message from GPG for a
signed commit
%G?
show "G" for a good (valid) signature,
"B" for a bad signature, "U" for a good signature with unknown
validity, "X" for a good signature that has expired, "Y" for a good
signature made by an expired key, "R" for a good signature made by a
revoked key, "E" if the signature cannot be checked (e.g. missing key)
and "N" for no signature
%GS
show the name of the signer for a
signed commit
%GK
show the key used to sign a signed
commit
%n
newline
%%
a raw %
git_state(clean_label:str="clean", dirty_label:str="dirty")
Calls git status --porcelain
and returns the
corresponding string. If the repository is clean, the string set in
clean_label
is returned. If the repository is
dirty, the string set in dirty_label
is
returned.
Argument | Default | Type | |
---|---|---|---|
clean_label | clean | str | String returned if the repository is clean. |
dirty_label | dirty | str | String returned if the repository is dirty. |
Rules for defining subrepos. See the docs for more information on subrepos in general.
{{ template "lexicon_entry.html" .Named "http_archive" }} {{ template "lexicon_entry.html" .Named "new_http_archive" }} {{ template "lexicon_entry.html" .Named "github_repo" }} {{ template "lexicon_entry.html" .Named "arch" }}