diff --git a/rules/WORKSPACE b/rules/WORKSPACE index 9ac530d34..addf21a39 100644 --- a/rules/WORKSPACE +++ b/rules/WORKSPACE @@ -30,4 +30,5 @@ http_archive( sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44", ) load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") -bazel_skylib_workspace() \ No newline at end of file + +bazel_skylib_workspace() diff --git a/rules/starlark_configurations/use_skylib_build_setting/BUILD b/rules/starlark_configurations/use_skylib_build_setting/BUILD new file mode 100644 index 000000000..08465fa9b --- /dev/null +++ b/rules/starlark_configurations/use_skylib_build_setting/BUILD @@ -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=. +# 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") \ No newline at end of file diff --git a/rules/starlark_configurations/use_skylib_build_setting/README.md b/rules/starlark_configurations/use_skylib_build_setting/README.md new file mode 100644 index 000000000..0b6df9b90 --- /dev/null +++ b/rules/starlark_configurations/use_skylib_build_setting/README.md @@ -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" +``` diff --git a/rules/starlark_configurations/use_skylib_build_setting/defs.bzl b/rules/starlark_configurations/use_skylib_build_setting/defs.bzl new file mode 100644 index 000000000..11503c7aa --- /dev/null +++ b/rules/starlark_configurations/use_skylib_build_setting/defs.bzl @@ -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"), + }, +)