xref: /aosp_15_r20/build/bazel/rules/cc/cc_aidl_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/cc:cc_aidl_code_gen.bzl", "cc_aidl_code_gen")
16load("//build/bazel/rules/cc:cc_library_static.bzl", "cc_library_static")
17load("//build/bazel/rules/cc:cc_library_shared.bzl", "cc_library_shared")
18
19def cc_aidl_library(
20        name,
21        deps = [],
22        lang = "cpp",
23        make_shared = False,
24        **kwargs):
25    """
26    Generate AIDL stub code for C++ and wrap it in a cc_library_static target
27
28    Args:
29        name (str):               name of the cc_library_static target
30        deps (list[AidlGenInfo]): list of all aidl_libraries that this cc_aidl_library depends on
31        make_shared (bool):       if true, `name` will refer to a cc_library_shared,
32                                  and an additional cc_library_static will be created
33                                  if false, `name` will refer to a cc_library_static
34        **kwargs:                 extra arguments that will be passesd to cc_library_{static,shared}.
35    """
36
37    if lang not in ["cpp", "ndk"]:
38        fail("lang {} is unsupported. Allowed lang: ndk, cpp.".format(lang))
39
40    aidl_code_gen = name + "_aidl_code_gen"
41    cc_aidl_code_gen(
42        name = aidl_code_gen,
43        deps = deps,
44        lang = lang,
45        min_sdk_version = kwargs.get("min_sdk_version", None),
46        tags = kwargs.get("tags", []) + ["manual"],
47    )
48
49    arguments_with_kwargs = dict(
50        kwargs,
51        srcs = [":" + aidl_code_gen],
52        deps = [aidl_code_gen],
53    )
54
55    static_name = name
56    if make_shared:
57        cc_library_shared(
58            name = name,
59            **arguments_with_kwargs
60        )
61        static_name = name + "_bp2build_cc_library_static"
62
63    cc_library_static(
64        name = static_name,
65        **arguments_with_kwargs
66    )
67