从 bazel 中的 config_setting 获取变量

get variable from config_setting in bazel

我有以下 config_setting 定义:

config_setting(
    name = "perception_env",
    values = {"perception": "true"},
)

print(perception_env)

然而,我似乎无法打印变量,它说它不存在。

config_setting仅用于在select()中选择不同的可能值。 config_setting 并没有真正的值,它更像是一个变量(一个 Bazel 标志、一个 Starlark-defined 标志、平台约束)及其值的关联。 values 属性基本上用于标志值(“感知”必须是 bazel 标志)。

例如,

config_setting(
    name = "my_config_setting_opt",
    values = {"compilation_mode": "opt"}
)

config_setting(
    name = "config_setting_dbg",
    values = {"compilation_mode": "dbg"}
)

config_setting(
    name = "config_setting_fastbuild",
    values = {"compilation_mode": "fastbuild"}
)

genrule(
  name = "gen_out",
  outs = ["out"],
  cmd = select({
    ":my_config_setting_opt": "echo Opt mode > $@",
    ":config_setting_dbg": "echo Dbg mode > $@",
    ":config_setting_fastbuild": "echo Fastbuild mode > $@",
  }),
)

这 3 个 config_setting 声明了 --compilation_mode 标志的 3 种不同关联,一种对应于它的每个可能值(参见 https://bazel.build/docs/user-manual#compilation-mode

然后 select() 为类 gen_outcmd 属性声明了 3 个不同的可能值。然后将 --compilation_mode 标志设置为不同的值会更改选择 cmd 的值:

$ bazel build out --compilation_mode=dbg && cat bazel-bin/out
INFO: Build option --compilation_mode has changed, discarding analysis cache.
INFO: Analyzed target //:out (0 packages loaded, 11 targets configured).
INFO: Found 1 target...
Target //:out up-to-date:
  bazel-bin/out
INFO: Elapsed time: 0.145s, Critical Path: 0.01s
INFO: 2 processes: 1 internal, 1 linux-sandbox.
INFO: Build completed successfully, 2 total actions
Dbg mode

$ bazel build out --compilation_mode=opt && cat bazel-bin/out
INFO: Build option --compilation_mode has changed, discarding analysis cache.
INFO: Analyzed target //:out (0 packages loaded, 11 targets configured).
INFO: Found 1 target...
Target //:out up-to-date:
  bazel-bin/out
INFO: Elapsed time: 0.111s, Critical Path: 0.01s
INFO: 2 processes: 1 internal, 1 linux-sandbox.
INFO: Build completed successfully, 2 total actions
Opt mode

$ bazel build out --compilation_mode=fastbuild && cat bazel-bin/out
INFO: Build option --compilation_mode has changed, discarding analysis cache.
INFO: Analyzed target //:out (0 packages loaded, 11 targets configured).
INFO: Found 1 target...
Target //:out up-to-date:
  bazel-bin/out
INFO: Elapsed time: 0.145s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
Fastbuild mode