Using select when defining a dict in Bazel

邮差的信 提交于 2019-12-11 15:52:02

问题


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",
}

回答1:


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.



来源:https://stackoverflow.com/questions/56065453/using-select-when-defining-a-dict-in-bazel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!