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:hidl_package_root.bzl", "HidlPackageRoot")
16
17HidlInfo = provider(fields = [
18    "srcs",
19    "transitive_srcs",
20    "transitive_roots",
21    "transitive_root_interface_files",
22    "fq_name",
23])
24
25def _hidl_library_rule_impl(ctx):
26    transitive_srcs = []
27    transitive_root_interface_files = []
28    transitive_roots = []
29
30    for dep in ctx.attr.deps:
31        transitive_srcs.append(dep[HidlInfo].transitive_srcs)
32        transitive_root_interface_files.append(dep[HidlInfo].transitive_root_interface_files)
33        transitive_roots.append(dep[HidlInfo].transitive_roots)
34
35    root = ctx.attr.root[HidlPackageRoot]
36    root_interface_files = []
37    if root.root_interface_file:
38        root_interface_files.append(root.root_interface_file)
39    return [
40        DefaultInfo(files = depset(ctx.files.srcs)),
41        HidlInfo(
42            srcs = depset(ctx.files.srcs),
43            transitive_srcs = depset(
44                direct = ctx.files.srcs,
45                transitive = transitive_srcs,
46            ),
47            # These transitive roots will be used as -r arguments later when calling
48            # hidl-gen, for example, -r android.hardware:hardware/interfaces
49            transitive_roots = depset(
50                direct = [root.root + ":" + root.root_path],
51                transitive = transitive_roots,
52            ),
53            transitive_root_interface_files = depset(
54                direct = root_interface_files,
55                transitive = transitive_root_interface_files,
56            ),
57            fq_name = ctx.attr.fq_name,
58        ),
59    ]
60
61hidl_library = rule(
62    implementation = _hidl_library_rule_impl,
63    attrs = {
64        "srcs": attr.label_list(
65            allow_files = [".hal"],
66        ),
67        "deps": attr.label_list(
68            providers = [HidlInfo],
69            doc = "hidl_interface targets that this one depends on",
70        ),
71        "fq_name": attr.string(),
72        "root": attr.label(),
73    },
74    provides = [HidlInfo],
75)
76