Search code examples
bazel

get variable from config_setting in bazel


I have the following config_setting defined:

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

print(perception_env)

However, I can't seem to print the variable, it says it doesn't exist.


Solution

  • config_setting is only used for selecting the different possible values in a select(). A config_setting doesn't really have a value, it's more an association of a variable (a Bazel flag, a Starlark-defined flag, platform constraints) and its value. The values attribute is basically for flag values ("perception" would have to be a bazel flag).

    For example,

    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 > $@",
      }),
    )
    

    The 3 config_settings declare 3 different associations of the --compilation_mode flag, one for each of its possible values (see https://bazel.build/docs/user-manual#compilation-mode)

    Then the select() declares 3 different possible values for the cmd attribute of the genrule gen_out. Then setting the --compilation_mode flag to different values changes which value for cmd is selected:

    $ 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