8.3 KiB
Public API for expanding variables
expand_template
expand_template(name, data, is_executable, out, stamp, stamp_substitutions, substitutions, template)
Template expansion
This performs a simple search over the template file for the keys in substitutions, and replaces them with the corresponding values.
Values may also use location templates as documented in
expand_locations
as well as configuration variables
such as $(BINDIR)
, $(TARGET_CPU)
, and $(COMPILATION_MODE)
as documented in
expand_variables.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
data | List of targets for additional lookup information. | List of labels | optional | [] |
is_executable | Whether to mark the output file as executable. | Boolean | optional | False |
out | Where to write the expanded file. | Label | required | |
stamp | Whether to encode build information into the output. Possible values: - stamp = 1 : Always stamp the build information into the output, even in --nostamp builds. This setting should be avoided, since it is non-deterministic. It potentially causes remote cache misses for the target and any downstream actions that depend on the result. - stamp = 0 : Never stamp, instead replace build information by constant values. This gives good build result caching. - stamp = -1 : Embedding of build information is controlled by the --[no]stamp flag. Stamped targets are not rebuilt unless their dependencies change. |
Integer | optional | -1 |
stamp_substitutions | Mapping of strings to substitutions. There are overlayed on top of substitutions when stamping is enabled for the target. Substitutions can contain (execpath :target) and (rootpath :target) expansions, $(MAKEVAR) expansions and {{STAMP_VAR}} expansions when stamping is enabled for the target. |
Dictionary: String -> String | optional | {} |
substitutions | Mapping of strings to substitutions. Substitutions can contain (execpath :target) and (rootpath :target) expansions, $(MAKEVAR) expansions and {{STAMP_VAR}} expansions when stamping is enabled for the target. |
Dictionary: String -> String | optional | {} |
template | The template file to expand. | Label | required |
expand_locations
expand_locations(ctx, input, targets)
Expand location templates.
Expands all $(execpath ...)
, $(rootpath ...)
and deprecated $(location ...)
templates in the
given string by replacing with the expanded path. Expansion only works for labels that point to direct dependencies
of this rule or that are explicitly listed in the optional argument targets.
See https://docs.bazel.build/versions/main/be/make-variables.html#predefined_label_variables.
Use $(rootpath)
and $(rootpaths)
to expand labels to the runfiles path that a built binary can use
to find its dependencies. This path is of the format:
./file
path/to/file
../external_repo/path/to/file
Use $(execpath)
and $(execpaths)
to expand labels to the execroot (where Bazel runs build actions).
This is of the format:
./file
path/to/file
external/external_repo/path/to/file
<bin_dir>/path/to/file
<bin_dir>/external/external_repo/path/to/file
The deprecated $(location)
and $(locations)
expansions returns either the execpath or rootpath depending on the context.
PARAMETERS
Name | Description | Default Value |
---|---|---|
ctx | context | none |
input | String to be expanded | none |
targets | List of targets for additional lookup information. | [] |
RETURNS
The expanded path or the original path
expand_variables
expand_variables(ctx, s, outs, output_dir, attribute_name)
Expand make variables and substitute like genrule does.
Bazel pre-defined variables
are expanded however only $@
, $(@D)
and $(RULEDIR)
of
pre-defined genrule variables
are supported.
This function is the same as ctx.expand_make_variables with the additional genrule-like substitutions of:
-
$@
: The output file if it is a single file. Else triggers a build error. -
$(@D)
: The output directory.If there is only one file name in outs, this expands to the directory containing that file.
If there is only one directory in outs, this expands to the single output directory.
If there are multiple files, this instead expands to the package's root directory in the bin tree, even if all generated files belong to the same subdirectory!
-
$(RULEDIR)
: The output directory of the rule, that is, the directory corresponding to the name of the package containing the rule under the bin tree. -
$(BUILD_FILE_PATH)
: ctx.build_file_path -
$(VERSION_FILE)
: ctx.version_file.path -
$(INFO_FILE)
: ctx.info_file.path -
$(TARGET)
: ctx.label -
$(WORKSPACE)
: ctx.workspace_name
See https://docs.bazel.build/versions/main/be/general.html#genrule.cmd and https://docs.bazel.build/versions/main/be/make-variables.html#predefined_genrule_variables for more information of how these special variables are expanded.
PARAMETERS
RETURNS
s
with the variables expanded