使用[bazel]受限制的属性 [英] Use of [bazel] restricted_to attribute

查看:187
本文介绍了使用[bazel]受限制的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用bazel的 restricted_to 属性进行测试.

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

我希望测试仅在特定的cpu = build上运行.

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).

我猜出了stricted_to参数的语法没有运气 (我的第一个猜测是//cpu:armhf-debian,它只是在寻找cpu软件包)

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)

有什么建议吗?

推荐答案

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.

这篇关于使用[bazel]受限制的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆