Skip to content

Commit

Permalink
An example of reading settings/attributes in the transition (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliexxia committed Jun 8, 2020
1 parent 2143d40 commit e5d7782
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ tasks:
working_directory: rules
build_targets:
- "..."
- "-//starlark_configurations/read_attr_in_transition:will-break"
# These targest are not supposed to build at the top level without flags being set
- "-//starlark_configurations/cc_binary_selectable_copts:app_forgets_to_set_features"
- "-//starlark_configurations/cc_binary_selectable_copts:app_forgets_to_set_features_native_binary"
Expand All @@ -156,6 +157,7 @@ tasks:
working_directory: rules
build_targets:
- "..."
- "-//starlark_configurations/read_attr_in_transition:will-break"
# These targest are not supposed to build at the top level without flags being set
- "-//starlark_configurations/cc_binary_selectable_copts:app_forgets_to_set_features"
- "-//starlark_configurations/cc_binary_selectable_copts:app_forgets_to_set_features_native_binary"
Expand All @@ -172,6 +174,7 @@ tasks:
working_directory: rules
build_targets:
- "..."
- "-//starlark_configurations/read_attr_in_transition:will-break"
# These targest are not supposed to build at the top level without flags being set
- "-//starlark_configurations/cc_binary_selectable_copts:app_forgets_to_set_features"
- "-//starlark_configurations/cc_binary_selectable_copts:app_forgets_to_set_features_native_binary"
Expand Down
34 changes: 34 additions & 0 deletions rules/starlark_configurations/read_attr_in_transition/BUILD
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 rules/starlark_configurations/read_attr_in_transition/README.md
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 rules/starlark_configurations/read_attr_in_transition/defs.bzl
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",
),
}
)

0 comments on commit e5d7782

Please sign in to comment.