1# Copyright (C) 2021 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("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
16load(":cc_library_common.bzl", "create_cc_prebuilt_library_info")
17load(":stripped_cc_common.bzl", "common_strip_attrs", "stripped_impl")
18
19def _cc_prebuilt_library_shared_impl(ctx):
20    lib = ctx.file.shared_library
21    files = []
22    if lib:
23        lib = stripped_impl(ctx, ctx.file.shared_library, suffix = ".so", subdir = ctx.attr.name)
24        files.append(lib)
25
26    cc_toolchain = find_cpp_toolchain(ctx)
27    feature_configuration = cc_common.configure_features(
28        ctx = ctx,
29        cc_toolchain = cc_toolchain,
30    )
31    cc_info, linker_input = create_cc_prebuilt_library_info(
32        ctx,
33        cc_common.create_library_to_link(
34            actions = ctx.actions,
35            dynamic_library = lib,
36            feature_configuration = feature_configuration,
37            cc_toolchain = cc_toolchain,
38        ) if lib != None else None,
39    )
40
41    return [
42        DefaultInfo(
43            files = depset(direct = files),
44            runfiles = ctx.runfiles(files),
45        ),
46        cc_info,
47        CcSharedLibraryInfo(
48            dynamic_deps = depset(),
49            exports = [],
50            link_once_static_libs = [],
51            linker_input = linker_input,
52        ),
53        OutputGroupInfo(
54            # TODO(b/279433767): remove once cc_library_shared is stable
55            rule_impl_debug_files = [],
56        ),
57    ]
58
59cc_prebuilt_library_shared = rule(
60    implementation = _cc_prebuilt_library_shared_impl,
61    attrs = dict(
62        common_strip_attrs,
63        shared_library = attr.label(
64            providers = [CcInfo],
65            allow_single_file = True,
66        ),
67        export_includes = attr.string_list(),
68        export_system_includes = attr.string_list(),
69    ),
70    toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
71    fragments = ["cpp"],
72    provides = [CcInfo, CcSharedLibraryInfo],
73)
74