xref: /aosp_15_r20/build/bazel/rules/cc/static_libc.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
15# Rules and macros to define a cc toolchain with a static libc.
16# Used to bootstrap cc development using the bionic lib build by Soong.
17# Rule: _libc_config
18#   Provides information needed by CcToolchainConfigInfo to configure the cc_toolchain properly.
19# Macro: static_libc
20#   Creates the libc_config target and filegroups needed by cc_toolchain.
21LibcConfigInfo = provider(fields = ["include_dirs", "system_libraries"])
22
23def _libc_config_impl(ctx):
24    include_dirs = ctx.attr.include_dirs
25    system_libraries = [file.path for file in ctx.files.system_libraries]
26    provider = LibcConfigInfo(
27        include_dirs = include_dirs,
28        system_libraries = system_libraries,
29    )
30    return [provider]
31
32_libc_config = rule(
33    implementation = _libc_config_impl,
34    attrs = {
35        "include_dirs": attr.string_list(default = []),
36        "system_libraries": attr.label_list(default = [], allow_files = True),
37    },
38)
39
40def static_libc(
41        name,
42        include_dirs = {},
43        system_libraries = []):
44    # Create the filegroups
45    include_srcs = []
46    include_globs = []
47    for value in include_dirs.values():
48        if "*" in value:
49            # It must be a glob.
50            include_globs.append(value)
51        else:
52            # Assume it's a label.
53            include_srcs.append(value)
54    native.filegroup(
55        name = "%s_includes" % name,
56        srcs = include_srcs + native.glob(include_globs),
57    )
58    native.filegroup(
59        name = "%s_system_libraries" % name,
60        srcs = system_libraries,
61    )
62
63    # Create the libc config.
64    include_paths = [path for path in include_dirs.keys()]
65    _libc_config(
66        name = name,
67        include_dirs = include_paths,
68        system_libraries = system_libraries,
69    )
70
71    # Also create cc_library target for direct dependencies.
72    native.cc_library(
73        name = "%s_library" % name,
74        hdrs = [
75            ":%s_includes" % name,
76        ],
77        includes = include_paths,
78        srcs = [
79            ":%s_system_libraries" % name,
80        ],
81    )
82