xref: /aosp_15_r20/build/bazel/rules/cc/cc_prebuilt_library_static.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
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")
17
18def _cc_prebuilt_library_static_impl(ctx):
19    lib = ctx.file.static_library
20    files = ctx.attr.static_library.files if lib != None else None
21    cc_toolchain = find_cpp_toolchain(ctx)
22    feature_configuration = cc_common.configure_features(
23        ctx = ctx,
24        cc_toolchain = cc_toolchain,
25    )
26    cc_info, _ = create_cc_prebuilt_library_info(
27        ctx,
28        cc_common.create_library_to_link(
29            actions = ctx.actions,
30            static_library = lib,
31            alwayslink = ctx.attr.alwayslink,
32            feature_configuration = feature_configuration,
33            cc_toolchain = cc_toolchain,
34        ) if lib != None else None,
35    )
36    return [DefaultInfo(files = files), cc_info]
37
38cc_prebuilt_library_static = rule(
39    implementation = _cc_prebuilt_library_static_impl,
40    attrs = dict(
41        static_library = attr.label(
42            providers = [CcInfo],
43            allow_single_file = True,
44        ),
45        alwayslink = attr.bool(default = False),
46        export_includes = attr.string_list(),
47        export_system_includes = attr.string_list(),
48    ),
49    toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
50    fragments = ["cpp"],
51    provides = [CcInfo],
52)
53