-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix make var expansion in expand_template (#213)
- Loading branch information
1 parent
b3235b8
commit 804f321
Showing
11 changed files
with
165 additions
and
119 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,9 @@ | ||
"Public API for expanding variables" | ||
|
||
load( | ||
"//lib/private:expand_make_vars.bzl", | ||
_expand_locations = "expand_locations", | ||
_expand_template = "expand_template", | ||
_expand_variables = "expand_variables", | ||
) | ||
load("//lib/private:expand_locations.bzl", _expand_locations = "expand_locations") | ||
load("//lib/private:expand_variables.bzl", _expand_variables = "expand_variables") | ||
load("//lib/private:expand_template.bzl", _expand_template = "expand_template") | ||
|
||
expand_locations = _expand_locations | ||
expand_variables = _expand_variables | ||
|
||
expand_template = rule( | ||
doc = _expand_template.doc, | ||
implementation = _expand_template.implementation, | ||
attrs = _expand_template.attrs, | ||
) | ||
expand_template = _expand_template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"Helpers to expand location" | ||
|
||
def 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. | ||
Args: | ||
ctx: context | ||
input: String to be expanded | ||
targets: List of targets for additional lookup information. | ||
Returns: | ||
The expanded path or the original path | ||
""" | ||
|
||
return ctx.expand_location(input, targets = targets) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"expand_template rule" | ||
|
||
load(":expand_locations.bzl", _expand_locations = "expand_locations") | ||
load(":expand_variables.bzl", _expand_variables = "expand_variables") | ||
|
||
def _expand_template_impl(ctx): | ||
template = ctx.file.template | ||
|
||
substitutions = {} | ||
for k, v in ctx.attr.substitutions.items(): | ||
substitutions[k] = " ".join([_expand_variables(ctx, e, outs = [ctx.outputs.out], attribute_name = "substitutions") for e in _expand_locations(ctx, v, ctx.attr.data).split(" ")]) | ||
|
||
ctx.actions.expand_template( | ||
template = template, | ||
output = ctx.outputs.out, | ||
substitutions = substitutions, | ||
is_executable = ctx.attr.is_executable, | ||
) | ||
|
||
expand_template_lib = struct( | ||
doc = """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](https://github.com/aspect-build/bazel-lib/blob/main/docs/expand_make_vars.md#expand_locations) | ||
as well as [configuration variables](https://docs.bazel.build/versions/main/skylark/lib/ctx.html#var) | ||
such as `$(BINDIR)`, `$(TARGET_CPU)`, and `$(COMPILATION_MODE)` as documented in | ||
[expand_variables](https://github.com/aspect-build/bazel-lib/blob/main/docs/expand_make_vars.md#expand_variables). | ||
""", | ||
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, | ||
), | ||
}, | ||
) | ||
|
||
expand_template = rule( | ||
doc = expand_template_lib.doc, | ||
implementation = expand_template_lib.implementation, | ||
attrs = expand_template_lib.attrs, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.