xref: /aosp_15_r20/build/bazel/rules/cc/cc_hidl_library.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2022 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15load("//build/bazel/rules:hidl_file_utils.bzl", "LANGUAGE_CC_HEADERS", "LANGUAGE_CC_SOURCES", "hidl_file_utils")
16load("//build/bazel/rules/cc:cc_library_shared.bzl", "cc_library_shared")
17load("//build/bazel/rules/cc:cc_library_static.bzl", "cc_library_static")
18load("//build/bazel/rules/hidl:hidl_library.bzl", "HidlInfo")
19load(":cc_library_common.bzl", "create_ccinfo_for_includes")
20
21CC_SOURCE_SUFFIX = "_genc++"
22CC_HEADER_SUFFIX = "_genc++_headers"
23CORE_PACKAGES = ["android.hidl.base@", "android.hidl.manager@"]
24
25def _cc_hidl_code_gen_rule_impl(ctx):
26    hidl_info = ctx.attr.dep[HidlInfo]
27    outs = hidl_file_utils.generate_hidl_action(
28        hidl_info,
29        ctx.attr.language,
30        ctx,
31    )
32
33    return [
34        DefaultInfo(files = depset(direct = outs)),
35        create_ccinfo_for_includes(ctx, includes = [ctx.label.name]),
36    ]
37
38_cc_hidl_code_gen = rule(
39    implementation = _cc_hidl_code_gen_rule_impl,
40    attrs = {
41        "dep": attr.label(
42            providers = [HidlInfo],
43            doc = "hidl_library that exposes HidlInfo provider with *.hal files",
44            mandatory = True,
45        ),
46        "language": attr.string(
47            mandatory = True,
48            values = ["c++-headers", "c++-sources"],
49        ),
50        "_hidl_gen": attr.label(
51            allow_single_file = True,
52            default = Label("//prebuilts/build-tools:linux-x86/bin/hidl-gen"),
53            executable = True,
54            cfg = "exec",
55        ),
56    },
57    toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
58)
59
60def cc_hidl_library(
61        name,
62        interface,
63        dynamic_deps = [],
64        min_sdk_version = "",
65        tags = []):
66    srcs_name = name + CC_SOURCE_SUFFIX
67    hdrs_name = name + CC_HEADER_SUFFIX
68
69    _cc_hidl_code_gen(
70        name = srcs_name,
71        dep = interface,
72        language = LANGUAGE_CC_SOURCES,
73        tags = ["manual"],
74    )
75
76    _cc_hidl_code_gen(
77        name = hdrs_name,
78        dep = interface,
79        language = LANGUAGE_CC_HEADERS,
80        tags = ["manual"],
81    )
82
83    # Don't generate the cc library target for the core interfaces, they are parts
84    # of the libhidlbase
85    if _is_core_package(name):
86        return
87
88    combined_dynamic_deps = [
89        "//system/libhidl:libhidlbase",
90        "//system/core/libutils:libutils",
91    ]
92    implementation_dynamic_deps = [
93        "//system/core/libcutils:libcutils",
94    ] + select({
95        "//build/bazel/rules/apex:android-in_apex": ["//system/logging/liblog:liblog_stub_libs_current"],
96        "//conditions:default": ["//system/logging/liblog:liblog"],
97    })
98
99    for dep in dynamic_deps:
100        # Break up something like: //system/libhidl/transport/base/1.0:[email protected]
101        # and get the interface name such as [email protected].
102        parts = dep.split(":")
103        dep_name = parts[1] if len(parts) == 2 else dep
104
105        # core packages will be provided by libhidlbase
106        if not _is_core_package(dep_name):
107            combined_dynamic_deps.append(dep)
108
109    common_attrs = dict(
110        [
111            ("srcs", [":" + srcs_name]),
112            ("hdrs", [":" + hdrs_name]),
113            ("dynamic_deps", combined_dynamic_deps),
114            ("implementation_dynamic_deps", implementation_dynamic_deps),
115            ("export_includes", ["."]),
116            ("local_includes", ["."]),
117            ("copts", [
118                "-Wall",
119                "-Werror",
120                "-Wextra-semi",
121            ] + select({
122                "//build/bazel/product_config/config_settings:debuggable": ["-D__ANDROID_DEBUGGABLE__"],
123                "//conditions:default": [],
124            })),
125            ("min_sdk_version", min_sdk_version),
126            ("tags", tags),
127        ],
128    )
129
130    cc_library_shared(
131        name = name,
132        **common_attrs
133    )
134
135    cc_library_static(
136        name = name + "_bp2build_cc_library_static",
137        **common_attrs
138    )
139
140def _is_core_package(name):
141    for pkg in CORE_PACKAGES:
142        if name.startswith(pkg):
143            return True
144
145    return False
146