2021-11-08 14:40:36 +00:00
|
|
|
"Helpers to expand make variables"
|
|
|
|
|
2022-06-16 23:26:29 +00:00
|
|
|
load("@bazel_skylib//lib:paths.bzl", _spaths = "paths")
|
|
|
|
|
2021-11-08 14:40:36 +00:00
|
|
|
def expand_locations(ctx, input, targets = []):
|
|
|
|
"""Expand location templates.
|
|
|
|
|
2022-04-01 03:04:35 +00:00
|
|
|
Expands all `$(execpath ...)`, `$(rootpath ...)` and deprecated `$(location ...)` templates in the
|
2021-11-08 14:40:36 +00:00
|
|
|
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`
|
|
|
|
|
2022-04-01 03:04:35 +00:00
|
|
|
The deprecated `$(location)` and `$(locations)` expansions returns either the execpath or rootpath depending on the context.
|
2021-11-08 14:40:36 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: context
|
|
|
|
input: String to be expanded
|
|
|
|
targets: List of targets for additional lookup information.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The expanded path or the original path
|
|
|
|
"""
|
|
|
|
|
2022-04-01 03:04:35 +00:00
|
|
|
return ctx.expand_location(input, targets = targets)
|
2021-11-08 14:40:36 +00:00
|
|
|
|
|
|
|
def expand_variables(ctx, s, outs = [], output_dir = False, attribute_name = "args"):
|
|
|
|
"""Expand make variables and substitute like genrule does.
|
|
|
|
|
|
|
|
This function is the same as ctx.expand_make_variables with the additional
|
|
|
|
genrule-like substitutions of:
|
|
|
|
|
2021-11-10 14:34:00 +00:00
|
|
|
- `$@`: The output file if it is a single file. Else triggers a build error.
|
2022-06-16 23:26:29 +00:00
|
|
|
|
|
|
|
- `$(@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!
|
|
|
|
|
2021-11-10 14:34:00 +00:00
|
|
|
- `$(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.
|
|
|
|
|
|
|
|
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
|
|
|
|
output_dir: whether the rule is expected to output a directory (TreeArtifact)
|
2022-06-16 23:26:29 +00:00
|
|
|
Deprecated. For backward compatability with @aspect_bazel_lib 1.x. Pass
|
|
|
|
output tree artifacts to outs instead.
|
2021-11-08 14:40:36 +00:00
|
|
|
attribute_name: name of the attribute containing the expression
|
|
|
|
|
|
|
|
Returns:
|
2021-11-10 14:34:00 +00:00
|
|
|
`s` with the variables expanded
|
2021-11-08 14:40:36 +00:00
|
|
|
"""
|
2022-06-16 23:26:29 +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,
|
2022-06-16 23:26:29 +00:00
|
|
|
)
|
2021-11-08 14:40:36 +00:00
|
|
|
additional_substitutions = {}
|
|
|
|
|
2022-06-16 23:26:29 +00:00
|
|
|
# TODO: remove output_dir in 2.x release
|
2021-11-08 14:40:36 +00:00
|
|
|
if output_dir:
|
|
|
|
if s.find("$@") != -1 or s.find("$(@)") != -1:
|
|
|
|
fail("$@ substitution may only be used with output_dir=False.")
|
|
|
|
|
|
|
|
# We'll write into a newly created directory named after the rule
|
2022-06-16 23:26:29 +00:00
|
|
|
output_dir = _spaths.join(
|
2021-11-08 14:40:36 +00:00
|
|
|
ctx.bin_dir.path,
|
|
|
|
ctx.label.workspace_root,
|
|
|
|
ctx.label.package,
|
|
|
|
ctx.label.name,
|
2022-06-16 23:26:29 +00:00
|
|
|
)
|
2021-11-08 14:40:36 +00:00
|
|
|
else:
|
|
|
|
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
|
2022-06-16 23:26:29 +00:00
|
|
|
if outs[0].is_directory:
|
|
|
|
output_dir = outs[0].path
|
|
|
|
else:
|
|
|
|
output_dir = outs[0].dirname
|
2021-11-08 14:40:36 +00:00
|
|
|
else:
|
2022-06-16 23:26:29 +00:00
|
|
|
output_dir = rule_dir
|
2021-11-08 14:40:36 +00:00
|
|
|
|
2022-06-16 23:26:29 +00:00
|
|
|
additional_substitutions["@D"] = output_dir
|
|
|
|
additional_substitutions["RULEDIR"] = rule_dir
|
2021-11-08 14:40:36 +00:00
|
|
|
|
2022-04-14 14:05:28 +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"] = "@%s//%s:%s" % (ctx.label.workspace_name, 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)
|
2021-11-08 15:22:47 +00:00
|
|
|
|
|
|
|
def _expand_template_impl(ctx):
|
2021-11-11 13:54:42 +00:00
|
|
|
template = ctx.file.template
|
|
|
|
substitutions = ctx.attr.substitutions
|
|
|
|
|
|
|
|
subs = dict({
|
|
|
|
k: expand_locations(ctx, v, ctx.attr.data)
|
|
|
|
for k, v in substitutions.items()
|
|
|
|
}, **ctx.var)
|
|
|
|
|
2021-11-08 15:22:47 +00:00
|
|
|
ctx.actions.expand_template(
|
2021-11-11 13:54:42 +00:00
|
|
|
template = template,
|
2021-11-08 15:22:47 +00:00
|
|
|
output = ctx.outputs.out,
|
2021-11-11 13:54:42 +00:00
|
|
|
substitutions = subs,
|
2021-11-08 15:22:47 +00:00
|
|
|
is_executable = ctx.attr.is_executable,
|
|
|
|
)
|
|
|
|
|
2021-11-10 14:30:07 +00:00
|
|
|
expand_template = struct(
|
2021-11-08 15:22:47 +00:00
|
|
|
doc = """Template expansion
|
|
|
|
|
|
|
|
This performs a simple search over the template file for the keys in substitutions,
|
|
|
|
and replaces them with the corresponding values.
|
2021-11-11 13:54:42 +00:00
|
|
|
|
|
|
|
Values may also use location templates as documented in [expand_locations](#expand_locations)
|
|
|
|
as well as [configuration variables] such as `$(BINDIR)`, `$(TARGET_CPU)`, and `$(COMPILATION_MODE)`.
|
|
|
|
|
|
|
|
[configuration variables]: https://docs.bazel.build/versions/main/skylark/lib/ctx.html#var
|
2021-11-08 15:22:47 +00:00
|
|
|
""",
|
|
|
|
implementation = _expand_template_impl,
|
|
|
|
attrs = {
|
|
|
|
"template": attr.label(
|
|
|
|
doc = "The template file to expand.",
|
|
|
|
mandatory = True,
|
|
|
|
allow_single_file = True,
|
|
|
|
),
|
|
|
|
"substitutions": attr.string_dict(
|
|
|
|
doc = "Mapping of strings to substitutions.",
|
|
|
|
mandatory = True,
|
|
|
|
),
|
|
|
|
"out": attr.output(
|
|
|
|
doc = "Where to write the expanded file.",
|
|
|
|
mandatory = True,
|
|
|
|
),
|
|
|
|
"is_executable": attr.bool(
|
|
|
|
doc = "Whether to mark the output file as executable.",
|
|
|
|
default = False,
|
|
|
|
mandatory = False,
|
|
|
|
),
|
|
|
|
"data": attr.label_list(
|
|
|
|
doc = "List of targets for additional lookup information.",
|
|
|
|
allow_files = True,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|