-->

Use of [bazel] restricted_to attribute

2019-08-04 08:28发布

问题:

I'm trying to use the bazel restricted_to attribute for a test.

I want the test to only run on a specific cpu = build.

To make this somewhat more complicated, the cpu type is defined in our

/tools/cpp/CROSSTOOL file (cpu=armhf-debian).

I've had no luck with guessing the syntax of the restricted_to parameter (my first guess was //cpu:armhf-debian, which just looked for a cpu package)

Any Suggestions?

回答1:

There's not a lot of documentation on restricted_to, and the other rules it works with, environment and environment_group. Mostly this is because the use case they are for is very specific to Google's repository setup, and we're in the process of replacing them with a more flexible system.

To use restricted_to, you would need to define several environment rules, and an environment_group to contain them, and then specify which environment the test is restricted to, and finally always use the "--target_environment" flag to specify the current environment group. That would look something like this:

environment(name = "x86")
environment(name = "ppc")
environment_group(
  name = "cpus",
  defaults = [":x86"],
  environments = [
    ":x86",
    ":ppc",
  ])

cc_test(
  name = "test",
  other config
  restricted_to = [":ppc"],)

You could then run the test as so:

bazel test --target_environment=//:ppc //:test

to get the environment checking.

This isn't terribly useful, as whoever is running the test has to also remember to set "--target_environment" properly.

A better way to disable the test, using currently supported code, is to use config_setting and select, like this:

config_setting(
  name = "k8",
  values = {"cpu": "k8"})
config_setting(
  name = "ppc",
  values = {"cpu":, "ppc")

cc_test(
  name = "test",
  other config
  srcs = [other sources] +
    select({
      "//:x86": ["x86_test_src.cpp"],
      "//:ppc": ["ppc_test_src.cpp"],
      "//conditions:default": ["default_test_src.cpp"],
    })

config_setting will take a value based on the current "--cpu" flag. By changing the files included in the select, you can control what files are included in the test for each cpu setting.

Obviously, these don't have to be in the same package, and the usual Bazel visibility rules apply. See Bazel's src/BUILD for an example of config_setting, and src/test/cpp/BUILD for an example of using it in select.

We're working hard on platforms, which is a better way to describe and query Bazel's execution environment, and we'll make sure to post documentation and a blog post when that's ready for people to test.



标签: c++ bazel