且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用[bazel]受限制的属性

更新时间:2023-09-28 22:49:34

restricted_to以及适用于环境和environment_group的其他规则没有很多文档.这主要是因为它们的用例非常特定于Google的存储库设置,并且我们正在用更灵活的系统替换它们.

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.

要使用stricted_to,您需要定义几个环境规则,并包含一个环境规则组以包含它们,然后指定测试限制在哪个环境中,最后总是使用"--target_environment"标志来指定当前环境.团体.看起来像这样:

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

进行环境检查.

这并不是非常有用,因为运行测试的任何人还必须记住正确设置"--target_environment".

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

使用当前支持的代码来禁用测试的更好方法是使用config_setting并选择,如下所示:

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将采用基于当前"--cpu"标志的值.通过更改选择中包含的文件,您可以控制每个cpu设置中测试中包含哪些文件.

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.

显然,这些不必放在同一包装中,并且通常的Bazel可见性规则适用.有关config_setting的示例,请参见Bazel的 src/BUILD ,以及 src/test/cpp/BUILD 在select中使用它的示例.

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.

我们正在平台上努力,这是描述和查询Bazel执行环境的一种更好的方式,并且在确保可供人们测试时,我们将确保发布文档和博客文章.

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.