bazel

Using select when defining a dict in Bazel


In bazel I often see following code:

srcs = [
        "foo/bar.c",
    ] + select({
        "@org_tensorflow//tensorflow:linux_x86_64": [
            "foo/baz.c",
        ],
        "//conditions:default": [],
    })

But how do I go with conditionally appending a dict like this?

subs = {
        "#undef HWLOC_VERSION_MAJOR": "#define HWLOC_VERSION_MAJOR 2",
}

Solution

  • That was completely non-intuitive, but I managed to do this in following way:

    common_subs = {"foo": "bar"}
    
    linux_subs = {"baz": "boo"}
    
    subs = select({
      "@org_tensorflow//tensorflow:linux_x86_64": dict(common_subs, **linux_subs),
      "//conditions:default": common_subs,
    })
    

    I learned that ** magic from Python.