2
0
Fork 0
mirror of https://github.com/bazel-contrib/bazel-lib synced 2024-12-03 11:52:43 +00:00
bazel-lib/lib/private/expand_variables.bzl

87 lines
3.3 KiB
Python
Raw Normal View History

2021-11-08 14:40:36 +00:00
"Helpers to expand make variables"
load("@bazel_skylib//lib:paths.bzl", _spaths = "paths")
def expand_variables(ctx, s, outs = [], attribute_name = "args"):
2021-11-08 14:40:36 +00:00
"""Expand make variables and substitute like genrule does.
Bazel [pre-defined variables](https://bazel.build/reference/be/make-variables#predefined_variables)
are expanded however only `$@`, `$(@D)` and `$(RULEDIR)` of
[pre-defined genrule variables](https://bazel.build/reference/be/make-variables#predefined_genrule_variables)
are supported.
2021-11-08 14:40:36 +00:00
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
2021-11-08 14:40:36 +00:00
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
2021-11-08 14:40:36 +00:00
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.
Args:
ctx: starlark rule context
s: expression to expand
outs: declared outputs of the rule, for expanding references to outputs
attribute_name: name of the attribute containing the expression. Used for error reporting.
2021-11-08 14:40:36 +00:00
Returns:
`s` with the variables expanded
2021-11-08 14:40:36 +00:00
"""
rule_dir = _spaths.join(
2021-11-08 14:40:36 +00:00
ctx.bin_dir.path,
ctx.label.workspace_root,
ctx.label.package,
)
2021-11-08 14:40:36 +00:00
additional_substitutions = {}
if s.find("$@") != -1 or s.find("$(@)") != -1:
if len(outs) > 1:
fail("$@ substitution may only be used with a single out.")
if len(outs) == 1:
additional_substitutions["@"] = outs[0].path
if outs[0].is_directory:
output_dir = outs[0].path
2021-11-08 14:40:36 +00:00
else:
output_dir = outs[0].dirname
else:
output_dir = rule_dir
2021-11-08 14:40:36 +00:00
additional_substitutions["@D"] = output_dir
additional_substitutions["RULEDIR"] = rule_dir
2021-11-08 14:40:36 +00:00
# Add some additional make variable substitutions for common useful values in the context
additional_substitutions["BUILD_FILE_PATH"] = ctx.build_file_path
additional_substitutions["VERSION_FILE"] = ctx.version_file.path
additional_substitutions["INFO_FILE"] = ctx.info_file.path
additional_substitutions["TARGET"] = "{}//{}:{}".format(
"@" + ctx.label.workspace_name if ctx.label.workspace_name else "",
ctx.label.package,
ctx.label.name,
)
additional_substitutions["WORKSPACE"] = ctx.workspace_name
2021-11-08 14:40:36 +00:00
return ctx.expand_make_variables(attribute_name, s, additional_substitutions)