• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Function for preserving `select` entries for Cargo cfg expressions which did
2not match any enabled target triple / Bazel platform.
3
4For example we might generate:
5
6    rust_library(
7        ...
8        deps = [
9            "//common:unconditional_dep",
10        ] + selects.with_unmapped({
11            "@rules_rust//rust/platform:x86_64-pc-windows-msvc": [
12                "//third-party/rust:windows-sys",  # cfg(windows)
13            ],
14            "@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [
15                "//third-party/rust:libc",  # cfg(any(unix, target_os = "wasi"))
16            ],
17            "//conditions:default": [],
18            selects.NO_MATCHING_PLATFORM_TRIPLES: [
19                "//third-party/rust:hermit-abi",  # cfg(target_os = "hermit")
20            ],
21        })
22    )
23"""
24
25_SENTINEL = struct()
26
27def _with_unmapped(configurations):
28    configurations.pop(_SENTINEL)
29    return select(configurations)
30
31selects = struct(
32    with_unmapped = _with_unmapped,
33    NO_MATCHING_PLATFORM_TRIPLES = _SENTINEL,
34)
35
36# TODO: No longer used by the serde_starlark-based renderer. Delete after all
37# BUILD files using it have been regenerated.
38#
39# buildifier: disable=function-docstring
40def select_with_or(input_dict, no_match_error = ""):
41    output_dict = {}
42    for (key, value) in input_dict.items():
43        if type(key) == type(()):
44            for config_setting in key:
45                if config_setting in output_dict:
46                    output_dict[config_setting].extend(value)
47                else:
48                    output_dict[config_setting] = list(value)
49        elif key in output_dict:
50            output_dict[key].extend(value)
51        else:
52            output_dict[key] = list(value)
53
54    # return a dict with deduped lists
55    return select(
56        {key: depset(value).to_list() for key, value in output_dict.items()},
57        no_match_error = no_match_error,
58    )
59