-
-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An example of reading settings/attributes in the transition (#170)
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
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
34 changes: 34 additions & 0 deletions
34
rules/starlark_configurations/read_attr_in_transition/BUILD
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,34 @@ | ||
# We don't actually need an external repo to define Starlark settings. But | ||
# Skylib provides convenience macros that reduce boilerplate, so we'll use that | ||
# here. | ||
# | ||
# See https://docs.bazel.build/versions/master/skylark/config.html and | ||
# https://github.com/bazelbuild/bazel-skylib) for more info. | ||
load("@bazel_skylib//rules:common_settings.bzl", "string_setting") | ||
load(":defs.bzl", "my_rule") | ||
|
||
string_setting( | ||
name = "some-string", | ||
build_setting_default = "abc", | ||
) | ||
|
||
my_rule(name = "dont-do-transition", do_transition = False) | ||
my_rule(name = "do-transition", do_transition = True) | ||
|
||
# This target will cause an issue because it tries to implement | ||
# a rule transition that reads a configured attribute. | ||
my_rule( | ||
name = "will-break", | ||
do_transition = select({ | ||
":true": True, | ||
"//conditions:default": False, | ||
}) | ||
) | ||
|
||
config_setting( | ||
name = "true", | ||
flag_values = { | ||
":some-string": "abc", | ||
} | ||
) | ||
|
17 changes: 17 additions & 0 deletions
17
rules/starlark_configurations/read_attr_in_transition/README.md
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,17 @@ | ||
This is an example of a transition that reads both the current configuration and | ||
an attribute. Transitions have access to both those pieces of information in their | ||
implementation functions. | ||
|
||
To try it out, cd to this directory and run the following: | ||
``` | ||
$ bazel build :dont-do-transition # "value of some-string: abc" | ||
$ bazel build :do-transition # "value of some-string: abc-transitioned" | ||
``` | ||
|
||
Caveat: <b>You cannot read a [configured attribute] | ||
(https://docs.bazel.build/versions/master/configurable-attributes.html) in a | ||
rule transition.</b> This can create a dependency cycle between attribute values | ||
and configuration. To see an example of this cycle, run the following: | ||
``` | ||
$ bazel build :will-break # error message | ||
``` |
33 changes: 33 additions & 0 deletions
33
rules/starlark_configurations/read_attr_in_transition/defs.bzl
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,33 @@ | ||
# Load the provider of the pre-made settings defined in bazel_skylib. | ||
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") | ||
|
||
def _transition_impl(settings, attr): | ||
new_val = settings["//starlark_configurations/read_attr_in_transition:some-string"] | ||
|
||
if attr.do_transition: | ||
new_val = new_val + "-transitioned" | ||
|
||
return {"//starlark_configurations/read_attr_in_transition:some-string": new_val} | ||
|
||
my_transition = transition( | ||
implementation = _transition_impl, | ||
inputs = ["//starlark_configurations/read_attr_in_transition:some-string"], | ||
outputs = ["//starlark_configurations/read_attr_in_transition:some-string"], | ||
) | ||
|
||
def _impl(ctx): | ||
print(ctx.attr.do_transition) | ||
print("value of some-string: " + ctx.attr._some_string[BuildSettingInfo].value) | ||
return [] | ||
|
||
my_rule = rule( | ||
implementation = _impl, | ||
cfg = my_transition, | ||
attrs = { | ||
"do_transition": attr.bool(), | ||
"_some_string": attr.label(default = Label("//starlark_configurations/read_attr_in_transition:some-string")), | ||
"_whitelist_function_transition": attr.label( | ||
default = "@bazel_tools//tools/whitelists/function_transition_whitelist", | ||
), | ||
} | ||
) |