xref: /aosp_15_r20/external/pigweed/pw_build/merge_flags.bzl (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2024 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Functions for merging flags for platforms and transitions."""
15
16def flags_from_dict(flags_dict):
17    """Convert a dict of flags into a list.
18
19    Args:
20      flags_dict: Dict mapping flag labels to values.
21
22    Returns:
23       A list of flags in a format accepted by the `flags` attribute of `platform`.
24    """
25    return ["--{}={}".format(k, v) for k, v in flags_dict.items()]
26
27def merge_flags(base, override):
28    """Merge flags for platforms.
29
30    See https://pigweed.dev/bazel_compatibility.html#provide-default-backend-collections-as-dicts
31    for a usage example.
32
33    Args:
34       base: Dict of flags to use as the starting point. Typically one of
35          the dictionaries provided in @pigweed//pw_build:backends.bzl.
36       override: Dict of overrides or additions to the base dict.
37
38    Returns:
39       A list of flags in a format accepted by the `flags` attribute of `platform`.
40    """
41    return flags_from_dict(base | override)
42
43def merge_flags_for_transition_impl(base, override):
44    """Merge flags for a transition's impl function.
45
46    Args:
47       base: Dict of flags to use as the starting point. Typically one of
48          the dictionaries provided in @pigweed//pw_build:backends.bzl.
49       override: Dict of overrides or additions to the base dict.
50    """
51    return base | override
52
53def merge_flags_for_transition_outputs(base, override):
54    """Merge flags for a transition's output function.
55
56    Args:
57       base: Dict of flags to use as the starting point. Typically one of
58          the dictionaries provided in @pigweed//pw_build:backends.bzl.
59       override: Dict of overrides or additions to the base dict.
60    """
61    return (base | override).keys()
62