aboutsummaryrefslogtreecommitdiff
path: root/rules/cc/cc_library_static.bzl
blob: 2bbe748f50445fc9054131f067aefdcd5551c0b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# Copyright (C) 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load(":lto_transitions.bzl", "lto_deps_transition")
load(
    ":cc_library_common.bzl",
    "CPP_EXTENSIONS",
    "C_EXTENSIONS",
    "CcAndroidMkInfo",
    "check_absolute_include_dirs_disabled",
    "create_cc_androidmk_provider",
    "create_ccinfo_for_includes",
    "get_non_header_srcs",
    "get_sanitizer_lib_info",
    "is_external_directory",
    "parse_sdk_version",
    "system_dynamic_deps_defaults",
)
load(":stl.bzl", "stl_info_from_attr")
load(":clang_tidy.bzl", "ClangTidyInfo", "clang_tidy_for_dir", "generate_clang_tidy_actions")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("//build/bazel/rules:common.bzl", "get_dep_targets")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")

CcStaticLibraryInfo = provider(fields = ["root_static_archive", "objects"])

def cc_library_static(
        name,
        deps = [],
        implementation_deps = [],
        dynamic_deps = [],
        implementation_dynamic_deps = [],
        whole_archive_deps = [],
        implementation_whole_archive_deps = [],
        system_dynamic_deps = None,
        runtime_deps = [],
        export_absolute_includes = [],
        export_includes = [],
        export_system_includes = [],
        local_includes = [],
        absolute_includes = [],
        hdrs = [],
        native_bridge_supported = False,  # TODO: not supported yet.
        rtti = False,
        stl = "",
        cpp_std = "",
        c_std = "",
        # Flags for C and C++
        copts = [],
        # C++ attributes
        srcs = [],
        cppflags = [],
        # C attributes
        srcs_c = [],
        conlyflags = [],
        # asm attributes
        srcs_as = [],
        asflags = [],
        features = [],
        linkopts = [],
        alwayslink = None,
        target_compatible_with = [],
        # TODO(b/202299295): Handle data attribute.
        data = [],
        sdk_version = "",
        min_sdk_version = "",
        tags = [],
        tidy = None,
        tidy_checks = None,
        tidy_checks_as_errors = None,
        tidy_flags = None,
        tidy_disabled_srcs = None,
        tidy_timeout_srcs = None,
        tidy_gen_header_filter = None,
        native_coverage = True):
    "Bazel macro to correspond with the cc_library_static Soong module."

    exports_name = "%s_exports" % name
    locals_name = "%s_locals" % name
    cpp_name = "%s_cpp" % name
    c_name = "%s_c" % name
    asm_name = "%s_asm" % name

    toolchain_features = []

    toolchain_features.append("pic")

    if is_external_directory(native.package_name()):
        toolchain_features += [
            "-non_external_compiler_flags",
            "external_compiler_flags",
        ]
    else:
        toolchain_features += [
            "non_external_compiler_flags",
            "-external_compiler_flags",
        ]

    if rtti:
        toolchain_features += ["rtti"]
    if cpp_std:
        toolchain_features += [cpp_std, "-cpp_std_default"]
    if c_std:
        toolchain_features += [c_std, "-c_std_default"]

    if min_sdk_version:
        toolchain_features += parse_sdk_version(min_sdk_version) + ["-sdk_version_default"]
    toolchain_features += features

    if not native_coverage:
        toolchain_features += ["-coverage"]

    if system_dynamic_deps == None:
        system_dynamic_deps = system_dynamic_deps_defaults

    _cc_includes(
        name = exports_name,
        includes = export_includes,
        absolute_includes = export_absolute_includes,
        system_includes = export_system_includes,
        # whole archive deps always re-export their includes, etc
        deps = deps + whole_archive_deps + dynamic_deps,
        target_compatible_with = target_compatible_with,
        tags = ["manual"],
    )

    stl_info = stl_info_from_attr(stl, False)
    linkopts = linkopts + stl_info.linkopts
    copts = copts + stl_info.cppflags

    _cc_includes(
        name = locals_name,
        includes = local_includes,
        absolute_includes = absolute_includes,
        deps = (
            implementation_deps +
            implementation_dynamic_deps +
            system_dynamic_deps +
            stl_info.static_deps +
            stl_info.shared_deps +
            implementation_whole_archive_deps
        ),
        target_compatible_with = target_compatible_with,
        tags = ["manual"],
    )

    # Silently drop these attributes for now:
    # - native_bridge_supported
    common_attrs = dict(
        [
            # TODO(b/199917423): This may be superfluous. Investigate and possibly remove.
            ("linkstatic", True),
            ("hdrs", hdrs),
            # Add dynamic_deps to implementation_deps, as the include paths from the
            # dynamic_deps are also needed.
            ("implementation_deps", [locals_name]),
            ("deps", [exports_name]),
            ("features", toolchain_features),
            ("toolchains", ["//build/bazel/product_config:product_vars"]),
            ("target_compatible_with", target_compatible_with),
            ("linkopts", linkopts),
        ],
    )

    # TODO(b/231574899): restructure this to handle other images
    copts += select({
        "//build/bazel/rules/apex:non_apex": [],
        "//conditions:default": [
            "-D__ANDROID_APEX__",
        ],
    })

    native.cc_library(
        name = cpp_name,
        srcs = srcs,
        copts = copts + cppflags,
        tags = ["manual"],
        alwayslink = True,
        **common_attrs
    )
    native.cc_library(
        name = c_name,
        srcs = srcs_c,
        copts = copts + conlyflags,
        tags = ["manual"],
        alwayslink = True,
        **common_attrs
    )
    native.cc_library(
        name = asm_name,
        srcs = srcs_as,
        copts = asflags,
        tags = ["manual"],
        alwayslink = True,
        **common_attrs
    )

    # Root target to handle combining of the providers of the language-specific targets.
    _cc_library_combiner(
        name = name,
        roots = [cpp_name, c_name, asm_name],
        deps = whole_archive_deps + implementation_whole_archive_deps,
        additional_sanitizer_deps = (
            deps +
            stl_info.static_deps +
            implementation_deps
        ),
        runtime_deps = runtime_deps,
        target_compatible_with = target_compatible_with,
        alwayslink = alwayslink,
        static_deps = deps + implementation_deps + whole_archive_deps + implementation_whole_archive_deps,
        androidmk_static_deps = deps + implementation_deps + stl_info.static_deps,
        androidmk_whole_archive_deps = whole_archive_deps + implementation_whole_archive_deps,
        androidmk_dynamic_deps = dynamic_deps + implementation_dynamic_deps + system_dynamic_deps + stl_info.shared_deps,
        exports = exports_name,
        tags = tags,
        features = toolchain_features,

        # clang-tidy attributes
        tidy = tidy,
        srcs_cpp = srcs,
        srcs_c = srcs_c,
        copts_cpp = copts + cppflags,
        copts_c = copts + conlyflags,
        hdrs = hdrs,
        includes = [locals_name, exports_name],
        tidy_flags = tidy_flags,
        tidy_checks = tidy_checks,
        tidy_checks_as_errors = tidy_checks_as_errors,
        tidy_disabled_srcs = tidy_disabled_srcs,
        tidy_timeout_srcs = tidy_timeout_srcs,
        tidy_gen_header_filter = tidy_gen_header_filter,
    )

def _generate_tidy_files(ctx):
    disabled_srcs = [] + ctx.files.tidy_disabled_srcs
    tidy_timeout = ctx.attr._tidy_timeout[BuildSettingInfo].value
    if tidy_timeout != "":
        disabled_srcs.extend(ctx.attr.tidy_timeout_srcs)

    if ctx.attr.tidy_gen_header_filter:
        if ctx.attr.tidy_flags:
            fail("tidy_flags cannot be set when also using tidy_gen_header_filter")
        tidy_flags = ["-header-filter=" + paths.join(ctx.genfiles_dir.path, ctx.label.package) + ".*"]
    else:
        tidy_flags = ctx.attr.tidy_flags

    cpp_srcs, cpp_hdrs = get_non_header_srcs(
        ctx.files.srcs_cpp,
        ctx.files.tidy_disabled_srcs,
        source_extensions = CPP_EXTENSIONS,
    )
    c_srcs, c_hdrs = get_non_header_srcs(
        ctx.files.srcs_cpp + ctx.files.srcs_c,
        ctx.files.tidy_disabled_srcs,
        source_extensions = C_EXTENSIONS,
    )
    hdrs = ctx.files.hdrs + cpp_hdrs + c_hdrs
    cpp_tidy_outs = generate_clang_tidy_actions(
        ctx,
        ctx.attr.copts_cpp,
        ctx.attr.deps + ctx.attr.includes,
        cpp_srcs,
        hdrs,
        "c++",
        tidy_flags,
        ctx.attr.tidy_checks,
        ctx.attr.tidy_checks_as_errors,
        tidy_timeout,
    )
    c_tidy_outs = generate_clang_tidy_actions(
        ctx,
        ctx.attr.copts_c,
        ctx.attr.deps + ctx.attr.includes,
        c_srcs,
        hdrs,
        "c",
        tidy_flags,
        ctx.attr.tidy_checks,
        ctx.attr.tidy_checks_as_errors,
        tidy_timeout,
    )
    return cpp_tidy_outs + c_tidy_outs

def _generate_tidy_actions(ctx):
    transitive_tidy_files = []
    for ts in get_dep_targets(ctx.attr, predicate = lambda t: ClangTidyInfo in t).values():
        for t in ts:
            transitive_tidy_files.append(t[ClangTidyInfo].transitive_tidy_files)

    with_tidy = ctx.attr._with_tidy[BuildSettingInfo].value
    allow_local_tidy_true = ctx.attr._allow_local_tidy_true[BuildSettingInfo].value
    tidy_external_vendor = ctx.attr._tidy_external_vendor[BuildSettingInfo].value
    tidy_enabled = with_tidy or (allow_local_tidy_true and ctx.attr.tidy)
    should_run_for_current_package = clang_tidy_for_dir(tidy_external_vendor, ctx.label.package)
    if tidy_enabled and should_run_for_current_package:
        direct_tidy_files = _generate_tidy_files(ctx)
    else:
        direct_tidy_files = None

    tidy_files = depset(
        direct = direct_tidy_files,
    )
    transitive_tidy_files = depset(
        direct = direct_tidy_files,
        transitive = transitive_tidy_files,
    )
    return [
        OutputGroupInfo(
            _validation = tidy_files,
        ),
        ClangTidyInfo(
            tidy_files = tidy_files,
            transitive_tidy_files = transitive_tidy_files,
        ),
    ]

def _archive_with_prebuilt_libs(ctx, prebuilt_deps, linking_outputs, cc_toolchain):
    linking_output = linking_outputs.library_to_link.static_library
    if not prebuilt_deps:
        return linking_output

    feature_configuration = cc_common.configure_features(
        ctx = ctx,
        cc_toolchain = cc_toolchain,
        requested_features = ctx.features + ["archive_with_prebuilt_flags"],
        unsupported_features = ctx.disabled_features + ["linker_flags", "archiver_flags"],
    )

    output_file = ctx.actions.declare_file("lib" + ctx.label.name + ".a")

    archiver_path = cc_common.get_tool_for_action(
        feature_configuration = feature_configuration,
        action_name = ACTION_NAMES.cpp_link_static_library,
    )
    archiver_variables = cc_common.create_link_variables(
        feature_configuration = feature_configuration,
        cc_toolchain = cc_toolchain,
        output_file = output_file.path,
        is_using_linker = False,
    )
    command_line = cc_common.get_memory_inefficient_command_line(
        feature_configuration = feature_configuration,
        action_name = ACTION_NAMES.cpp_link_static_library,
        variables = archiver_variables,
    )
    args = ctx.actions.args()
    args.add_all(command_line)
    args.add(linking_output)
    args.add_all(prebuilt_deps)

    ctx.actions.run(
        executable = archiver_path,
        arguments = [args],
        inputs = depset(
            direct = [linking_output] + prebuilt_deps,
            transitive = [
                cc_toolchain.all_files,
            ],
        ),
        outputs = [output_file],
        mnemonic = "CppArchive",
    )

    return output_file

# Returns a CcInfo object which combines one or more CcInfo objects, except that all
# linker inputs owned by  owners in `old_owner_labels` are relinked and owned by the current target.
#
# This is useful in the "macro with proxy rule" pattern, as some rules upstream
# may expect they are depending directly on a target which generates linker inputs,
# as opposed to a proxy target which is a level of indirection to such a target.
def _cc_library_combiner_impl(ctx):
    old_owner_labels = []
    cc_infos = []
    for dep in ctx.attr.deps:
        old_owner_labels.append(dep.label)
        cc_info = dep[CcInfo]

        # do not propagate includes, hdrs, etc, already handled by roots
        cc_infos.append(CcInfo(linking_context = cc_info.linking_context))

    # we handle roots after deps to mimic Soong handling objects from whole archive deps prior to objects from the target itself
    for dep in ctx.attr.roots:
        old_owner_labels.append(dep.label)
        cc_infos.append(dep[CcInfo])

    combined_info = cc_common.merge_cc_infos(cc_infos = cc_infos)

    objects_to_link = []

    prebuilt_deps = []

    # This is not ideal, as it flattens a depset.
    for old_linker_input in combined_info.linking_context.linker_inputs.to_list():
        if old_linker_input.owner in old_owner_labels:
            for lib in old_linker_input.libraries:
                # These objects will be recombined into the root archive.
                objects_to_link.extend(lib.objects)

                # This is a prebuilt library, we have to handle it separately
                if not lib.objects and lib.static_library:
                    prebuilt_deps.append(lib.static_library)
        else:
            # Android macros don't handle transitive linker dependencies because
            # it's unsupported in legacy. We may want to change this going forward,
            # but for now it's good to validate that this invariant remains.
            fail("cc_static_library %s given transitive linker dependency from %s" % (ctx.label, old_linker_input.owner))

    cc_toolchain = find_cpp_toolchain(ctx)

    feature_configuration = cc_common.configure_features(
        ctx = ctx,
        cc_toolchain = cc_toolchain,
        requested_features = ctx.features + ["archiver_flags"],
        unsupported_features = ctx.disabled_features + ["linker_flags"],
    )

    out_name = ctx.label.name
    if prebuilt_deps:
        out_name += "_objs_only"
    linking_context, linking_outputs = cc_common.create_linking_context_from_compilation_outputs(
        actions = ctx.actions,
        name = out_name,
        feature_configuration = feature_configuration,
        cc_toolchain = cc_toolchain,
        alwayslink = ctx.attr.alwayslink,
        disallow_dynamic_library = True,
        compilation_outputs = cc_common.create_compilation_outputs(objects = depset(direct = objects_to_link)),
    )

    output_file = _archive_with_prebuilt_libs(ctx, prebuilt_deps, linking_outputs, cc_toolchain)
    linker_input = cc_common.create_linker_input(
        owner = ctx.label,
        libraries = depset(direct = [
            cc_common.create_library_to_link(
                actions = ctx.actions,
                feature_configuration = feature_configuration,
                cc_toolchain = cc_toolchain,
                static_library = output_file,
                objects = objects_to_link,
                alwayslink = ctx.attr.alwayslink,
            ),
        ]),
    )
    linking_context = cc_common.create_linking_context(linker_inputs = depset(direct = [linker_input]))

    providers = [
        DefaultInfo(files = depset(direct = [output_file]), data_runfiles = ctx.runfiles(files = [output_file])),
        CcInfo(compilation_context = combined_info.compilation_context, linking_context = linking_context),
        CcStaticLibraryInfo(root_static_archive = output_file, objects = objects_to_link),
        get_sanitizer_lib_info(ctx.attr.features, ctx.attr.deps + ctx.attr.additional_sanitizer_deps),
        create_cc_androidmk_provider(
            static_deps = ctx.attr.androidmk_static_deps,
            whole_archive_deps = ctx.attr.androidmk_whole_archive_deps,
            dynamic_deps = ctx.attr.androidmk_dynamic_deps,
        ),
    ]
    providers.extend(_generate_tidy_actions(ctx))

    return providers

# A rule which combines objects of oen or more cc_library targets into a single
# static linker input. This outputs a single archive file combining the objects
# of its direct deps, and propagates Cc providers describing that these objects
# should be linked for linking rules upstream.
# This rule is useful for maintaining the illusion that the target's deps are
# comprised by a single consistent rule:
#   - A single archive file is always output by this rule.
#   - A single linker input struct is always output by this rule, and it is 'owned'
#       by this rule.
_cc_library_combiner = rule(
    implementation = _cc_library_combiner_impl,
    attrs = {
        "roots": attr.label_list(
            providers = [CcInfo],
            cfg = lto_deps_transition,
        ),
        "deps": attr.label_list(
            providers = [CcInfo],
            cfg = lto_deps_transition,
        ),
        "additional_sanitizer_deps": attr.label_list(
            providers = [CcInfo],
            cfg = lto_deps_transition,
            doc = "Deps used only to check for sanitizer enablement",
        ),
        "runtime_deps": attr.label_list(
            providers = [CcInfo],
            doc = "Deps that should be installed along with this target. Read by the apex cc aspect.",
        ),
        "static_deps": attr.label_list(
            providers = [CcInfo],
            doc = "All the static deps of the lib. This is used by" +
                  " abi_dump_aspect to travel along the static_deps edges" +
                  " to create abi dump files.",
        ),
        "androidmk_static_deps": attr.label_list(
            providers = [CcInfo],
            doc = "All the whole archive deps of the lib. This is used to propagate" +
                  " information to AndroidMk about LOCAL_STATIC_LIBRARIES.",
        ),
        "androidmk_whole_archive_deps": attr.label_list(
            providers = [CcInfo],
            doc = "All the whole archive deps of the lib. This is used to propagate" +
                  " information to AndroidMk about LOCAL_WHOLE_STATIC_LIBRARIES.",
        ),
        "androidmk_dynamic_deps": attr.label_list(
            providers = [CcInfo],
            doc = "All the dynamic deps of the lib. This is used to propagate" +
                  " information to AndroidMk about LOCAL_SHARED_LIBRARIES." +
                  " The attribute name is prefixed with androidmk to avoid" +
                  " collision with the dynamic_deps attribute used in APEX" +
                  " aspects' propagation.",
        ),
        "exports": attr.label(
            providers = [CcInfo],
            cfg = lto_deps_transition,
        ),
        "_cc_toolchain": attr.label(
            default = Label("@local_config_cc//:toolchain"),
            providers = [cc_common.CcToolchainInfo],
            doc = "The exported includes used by abi_dump_aspect to retrieve" +
                  " and use as the inputs of abi dumper binary.",
        ),
        "alwayslink": attr.bool(
            doc = """At link time, whether these libraries should be wrapped in
            the --whole_archive block. This causes all libraries in the static
            archive to be unconditionally linked, regardless of whether the
            symbols in these object files are being searched by the linker.""",
            default = False,
        ),

        # Clang-tidy attributes
        "tidy": attr.bool(),
        "srcs_cpp": attr.label_list(allow_files = True),
        "srcs_c": attr.label_list(allow_files = True),
        "copts_cpp": attr.string_list(),
        "copts_c": attr.string_list(),
        "hdrs": attr.label_list(allow_files = True),
        "includes": attr.label_list(cfg = lto_deps_transition),
        "tidy_checks": attr.string_list(),
        "tidy_checks_as_errors": attr.string_list(),
        "tidy_flags": attr.string_list(),
        "tidy_disabled_srcs": attr.label_list(allow_files = True),
        "tidy_timeout_srcs": attr.label_list(allow_files = True),
        "tidy_gen_header_filter": attr.bool(),
        "_clang_tidy_sh": attr.label(
            default = Label("@//prebuilts/clang/host/linux-x86:clang-tidy.sh"),
            allow_single_file = True,
            executable = True,
            cfg = "exec",
            doc = "The clang tidy shell wrapper",
        ),
        "_clang_tidy": attr.label(
            default = Label("@//prebuilts/clang/host/linux-x86:clang-tidy"),
            allow_single_file = True,
            executable = True,
            cfg = "exec",
            doc = "The clang tidy executable",
        ),
        "_clang_tidy_real": attr.label(
            default = Label("@//prebuilts/clang/host/linux-x86:clang-tidy.real"),
            allow_single_file = True,
            executable = True,
            cfg = "exec",
        ),
        "_with_tidy": attr.label(
            default = "//build/bazel/flags/cc/tidy:with_tidy",
        ),
        "_allow_local_tidy_true": attr.label(
            default = "//build/bazel/flags/cc/tidy:allow_local_tidy_true",
        ),
        "_with_tidy_flags": attr.label(
            default = "//build/bazel/flags/cc/tidy:with_tidy_flags",
        ),
        "_default_tidy_header_dirs": attr.label(
            default = "//build/bazel/flags/cc/tidy:default_tidy_header_dirs",
        ),
        "_tidy_timeout": attr.label(
            default = "//build/bazel/flags/cc/tidy:tidy_timeout",
        ),
        "_tidy_external_vendor": attr.label(
            default = "//build/bazel/flags/cc/tidy:tidy_external_vendor",
        ),
        "_allowlist_function_transition": attr.label(
            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
        ),
    },
    toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
    provides = [CcInfo, CcAndroidMkInfo],
    fragments = ["cpp"],
)

def _cc_includes_impl(ctx):
    check_absolute_include_dirs_disabled(
        ctx.label.package,
        ctx.attr.absolute_includes,
    )

    return [create_ccinfo_for_includes(
        ctx,
        includes = ctx.attr.includes,
        absolute_includes = ctx.attr.absolute_includes,
        system_includes = ctx.attr.system_includes,
        deps = ctx.attr.deps,
    )]

# Bazel's native cc_library rule supports specifying include paths two ways:
# 1. non-exported includes can be specified via copts attribute
# 2. exported -isystem includes can be specified via includes attribute
#
# In order to guarantee a correct inclusion search order, we need to export
# includes paths for both -I and -isystem; however, there is no native Bazel
# support to export both of these, this rule provides a CcInfo to propagate the
# given package-relative include/system include paths as exec root relative
# include/system include paths.
_cc_includes = rule(
    implementation = _cc_includes_impl,
    attrs = {
        "absolute_includes": attr.string_list(doc = "List of exec-root relative or absolute search paths for headers, usually passed with -I"),
        "includes": attr.string_list(doc = "Package-relative list of search paths for headers, usually passed with -I"),
        "system_includes": attr.string_list(doc = "Package-relative list of search paths for headers, usually passed with -isystem"),
        "deps": attr.label_list(doc = "Re-propagates the includes obtained from these dependencies.", providers = [CcInfo]),
    },
    toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
    fragments = ["cpp"],
    provides = [CcInfo],
)