xref: /aosp_15_r20/external/pigweed/pw_build/pw_facade.bzl (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker# Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker#
3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker# the License at
6*61c4878aSAndroid Build Coastguard Worker#
7*61c4878aSAndroid Build Coastguard Worker#     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker#
9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker# the License.
14*61c4878aSAndroid Build Coastguard Worker"""Bazel rules for declaring Pigweed facade interface layers."""
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard Workerdef pw_facade(name, srcs = None, backend = None, **kwargs):
17*61c4878aSAndroid Build Coastguard Worker    """Create a cc_library with a facade.
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker    This macro simplifies instantiating Pigweed's facade pattern. It generates
20*61c4878aSAndroid Build Coastguard Worker    two targets:
21*61c4878aSAndroid Build Coastguard Worker
22*61c4878aSAndroid Build Coastguard Worker    * cc_library with the label "name". This is the complete library target.
23*61c4878aSAndroid Build Coastguard Worker      Users of the functionality provided by this library should depend on this
24*61c4878aSAndroid Build Coastguard Worker      target.  It has a public dependency on the "backend".
25*61c4878aSAndroid Build Coastguard Worker    * cc_library with the label "name.facade". This library exposes only the
26*61c4878aSAndroid Build Coastguard Worker      headers. Implementations of the backend should depend on it.
27*61c4878aSAndroid Build Coastguard Worker
28*61c4878aSAndroid Build Coastguard Worker    Args:
29*61c4878aSAndroid Build Coastguard Worker      name: The name of the cc_library.
30*61c4878aSAndroid Build Coastguard Worker      srcs: The source files of the cc_library.
31*61c4878aSAndroid Build Coastguard Worker      backend: The backend for the facade. This should be a label_flag or other
32*61c4878aSAndroid Build Coastguard Worker        target that allows swapping out the backend implementation at build
33*61c4878aSAndroid Build Coastguard Worker        time. (In a downstream project an alias with an "actual = select(...)"
34*61c4878aSAndroid Build Coastguard Worker        attribute may also be appropriate, but in upstream Pigweed use only a
35*61c4878aSAndroid Build Coastguard Worker        label_flag.).
36*61c4878aSAndroid Build Coastguard Worker      **kwargs: Passed on to cc_library.
37*61c4878aSAndroid Build Coastguard Worker    """
38*61c4878aSAndroid Build Coastguard Worker    if type(backend) != "string":
39*61c4878aSAndroid Build Coastguard Worker        fail(
40*61c4878aSAndroid Build Coastguard Worker            "The 'backend' attribute must be a single label, " +
41*61c4878aSAndroid Build Coastguard Worker            "got {} of type {}".format(backend, type(backend)),
42*61c4878aSAndroid Build Coastguard Worker        )
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard Worker    facade_kwargs = dict(**kwargs)
45*61c4878aSAndroid Build Coastguard Worker
46*61c4878aSAndroid Build Coastguard Worker    # This is a workaround for layering_check, which appears to only allow
47*61c4878aSAndroid Build Coastguard Worker    # two libraries to export the same headers if one lists it as textual_hdrs
48*61c4878aSAndroid Build Coastguard Worker    # for unclear reasons (b/142314377#comment11).
49*61c4878aSAndroid Build Coastguard Worker    facade_kwargs["textual_hdrs"] = facade_kwargs.pop("hdrs", [])
50*61c4878aSAndroid Build Coastguard Worker
51*61c4878aSAndroid Build Coastguard Worker    # TODO: https://github.com/bazelbuild/bazel/issues/12424 - Unfortunately,
52*61c4878aSAndroid Build Coastguard Worker    # strip_include_prefix does not work correctly with textual_hdrs.
53*61c4878aSAndroid Build Coastguard Worker    if "strip_include_prefix" in facade_kwargs:
54*61c4878aSAndroid Build Coastguard Worker        facade_kwargs["includes"] = [facade_kwargs["strip_include_prefix"]]
55*61c4878aSAndroid Build Coastguard Worker
56*61c4878aSAndroid Build Coastguard Worker    # A facade has no srcs, so it can only have public deps. Don't specify any
57*61c4878aSAndroid Build Coastguard Worker    # implementation_deps on the facade target.
58*61c4878aSAndroid Build Coastguard Worker    facade_kwargs.pop("implementation_deps", [])
59*61c4878aSAndroid Build Coastguard Worker    native.cc_library(
60*61c4878aSAndroid Build Coastguard Worker        name = name + ".facade",
61*61c4878aSAndroid Build Coastguard Worker        **facade_kwargs
62*61c4878aSAndroid Build Coastguard Worker    )
63*61c4878aSAndroid Build Coastguard Worker
64*61c4878aSAndroid Build Coastguard Worker    kwargs["deps"] = kwargs.get("deps", []) + [backend]
65*61c4878aSAndroid Build Coastguard Worker    native.cc_library(
66*61c4878aSAndroid Build Coastguard Worker        name = name,
67*61c4878aSAndroid Build Coastguard Worker        srcs = srcs,
68*61c4878aSAndroid Build Coastguard Worker        **kwargs
69*61c4878aSAndroid Build Coastguard Worker    )
70