forked from bazelbuild/examples
-
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.
add example of using a skylib build setting (bazelbuild#166)
Work towards bazelbuild#154
- Loading branch information
Showing
4 changed files
with
45 additions
and
1 deletion.
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
17 changes: 17 additions & 0 deletions
17
rules/starlark_configurations/use_skylib_build_setting/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,17 @@ | ||
# 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_flag") | ||
load(":defs.bzl", "dessert") | ||
|
||
# This can be set on the command line by setting | ||
# //starlark_configurations/use_skylib_build_setting:flavor=<flavor>. | ||
# We declare here that its default value is "strawberry". | ||
string_flag(name = "flavor", build_setting_default = "strawberry") | ||
|
||
# A rule that depends on `:flavor`which means it can read its value. | ||
dessert(name = "ice-cream") |
9 changes: 9 additions & 0 deletions
9
rules/starlark_configurations/use_skylib_build_setting/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,9 @@ | ||
This is an example of how to use and read the pre-defined build | ||
settings in the `bazel_skylib` repo. | ||
|
||
To test it out, cd into this directory and run the following: | ||
|
||
``` | ||
$ bazel build :ice-cream // => "flavor: strawberry" | ||
$ bazel build :ice-cream --//starlark_configurations/use_skylib_build_settings:flavor=rocky-road => "flavor: rocky-road" | ||
``` |
17 changes: 17 additions & 0 deletions
17
rules/starlark_configurations/use_skylib_build_setting/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,17 @@ | ||
# Load the provider of the pre-made settings defined in bazel_skylib | ||
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") | ||
|
||
def _impl(ctx): | ||
# Access the value of --//starlark_configurations/use_skylib_build_setting:flavor for the target. | ||
print("flavor: " + ctx.attr._flavor[BuildSettingInfo].value) | ||
return [] | ||
|
||
dessert = rule( | ||
implementation = _impl, | ||
attrs = { | ||
# Depend on the build setting so that we can access it in the rule implementation. | ||
# Use a private attribute (one that is prefixed with "_") so that target writers | ||
# can't override the value. | ||
"_flavor": attr.label(default = ":flavor"), | ||
}, | ||
) |