xref: /aosp_15_r20/build/bazel/rules/tradefed/cc_aspects.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1*7594170eSAndroid Build Coastguard Worker# Copyright (C) 2023 The Android Open Source Project
2*7594170eSAndroid Build Coastguard Worker#
3*7594170eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*7594170eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*7594170eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*7594170eSAndroid Build Coastguard Worker#
7*7594170eSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*7594170eSAndroid Build Coastguard Worker#
9*7594170eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*7594170eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*7594170eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*7594170eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*7594170eSAndroid Build Coastguard Worker# limitations under the License.
14*7594170eSAndroid Build Coastguard Worker
15*7594170eSAndroid Build Coastguard Workerload("//build/bazel/rules/cc:cc_library_shared.bzl", "CcSharedLibraryOutputInfo")
16*7594170eSAndroid Build Coastguard Worker
17*7594170eSAndroid Build Coastguard WorkerCcTestSharedLibsInfo = provider(
18*7594170eSAndroid Build Coastguard Worker    "Shared lib dependencies needed to run the cc_test targets",
19*7594170eSAndroid Build Coastguard Worker    fields = {
20*7594170eSAndroid Build Coastguard Worker        "shared_libs": "Shared libs that this cc test needs.",
21*7594170eSAndroid Build Coastguard Worker    },
22*7594170eSAndroid Build Coastguard Worker)
23*7594170eSAndroid Build Coastguard Worker
24*7594170eSAndroid Build Coastguard Workerdef _collect_cc_libs_aspect_impl(target, ctx):
25*7594170eSAndroid Build Coastguard Worker    shared_libs = []
26*7594170eSAndroid Build Coastguard Worker    transitive_deps = []
27*7594170eSAndroid Build Coastguard Worker
28*7594170eSAndroid Build Coastguard Worker    rules_propagate_src = [
29*7594170eSAndroid Build Coastguard Worker        "_bssl_hash_injection",
30*7594170eSAndroid Build Coastguard Worker        "stripped_shared_library",
31*7594170eSAndroid Build Coastguard Worker        "versioned_shared_library",
32*7594170eSAndroid Build Coastguard Worker        "stripped_test",
33*7594170eSAndroid Build Coastguard Worker        "versioned_binary",
34*7594170eSAndroid Build Coastguard Worker    ]
35*7594170eSAndroid Build Coastguard Worker
36*7594170eSAndroid Build Coastguard Worker    if ctx.rule.kind == "_cc_library_shared_proxy":
37*7594170eSAndroid Build Coastguard Worker        shared_libs.append(target[CcSharedLibraryOutputInfo].output_file)
38*7594170eSAndroid Build Coastguard Worker        if hasattr(ctx.rule.attr, "shared"):
39*7594170eSAndroid Build Coastguard Worker            transitive_deps.append(ctx.rule.attr.shared[0])
40*7594170eSAndroid Build Coastguard Worker    elif ctx.rule.kind in ["cc_shared_library", "cc_test"]:
41*7594170eSAndroid Build Coastguard Worker        # Propagate along the dynamic_deps edges for binaries and shared libs
42*7594170eSAndroid Build Coastguard Worker        if hasattr(ctx.rule.attr, "dynamic_deps"):
43*7594170eSAndroid Build Coastguard Worker            for dep in ctx.rule.attr.dynamic_deps:
44*7594170eSAndroid Build Coastguard Worker                transitive_deps.append(dep)
45*7594170eSAndroid Build Coastguard Worker        if ctx.rule.kind == "cc_test" and hasattr(ctx.rule.attr, "deps"):
46*7594170eSAndroid Build Coastguard Worker            for dep in ctx.rule.attr.deps:
47*7594170eSAndroid Build Coastguard Worker                transitive_deps.append(dep)
48*7594170eSAndroid Build Coastguard Worker    elif ctx.rule.kind == "_cc_library_combiner" and hasattr(ctx.rule.attr, "androidmk_dynamic_deps"):
49*7594170eSAndroid Build Coastguard Worker        for dep in ctx.rule.attr.androidmk_dynamic_deps:
50*7594170eSAndroid Build Coastguard Worker            transitive_deps.append(dep)
51*7594170eSAndroid Build Coastguard Worker    elif ctx.rule.kind in rules_propagate_src and hasattr(ctx.rule.attr, "src"):
52*7594170eSAndroid Build Coastguard Worker        if ctx.rule.kind == "stripped_test":
53*7594170eSAndroid Build Coastguard Worker            transitive_deps.append(ctx.rule.attr.src[0])
54*7594170eSAndroid Build Coastguard Worker        else:
55*7594170eSAndroid Build Coastguard Worker            transitive_deps.append(ctx.rule.attr.src)
56*7594170eSAndroid Build Coastguard Worker
57*7594170eSAndroid Build Coastguard Worker    if ctx.rule.kind in ["stripped_test", "_cc_library_shared_proxy"] and hasattr(ctx.rule.attr, "runtime_deps"):
58*7594170eSAndroid Build Coastguard Worker        for dep in ctx.rule.attr.runtime_deps:
59*7594170eSAndroid Build Coastguard Worker            for output_file in dep[DefaultInfo].files.to_list():
60*7594170eSAndroid Build Coastguard Worker                if output_file.extension == "so":
61*7594170eSAndroid Build Coastguard Worker                    shared_libs.append(output_file)
62*7594170eSAndroid Build Coastguard Worker            transitive_deps.append(dep)
63*7594170eSAndroid Build Coastguard Worker
64*7594170eSAndroid Build Coastguard Worker    return [
65*7594170eSAndroid Build Coastguard Worker        CcTestSharedLibsInfo(
66*7594170eSAndroid Build Coastguard Worker            shared_libs = depset(
67*7594170eSAndroid Build Coastguard Worker                shared_libs,
68*7594170eSAndroid Build Coastguard Worker                transitive = [info[CcTestSharedLibsInfo].shared_libs for info in transitive_deps],
69*7594170eSAndroid Build Coastguard Worker            ),
70*7594170eSAndroid Build Coastguard Worker        ),
71*7594170eSAndroid Build Coastguard Worker    ]
72*7594170eSAndroid Build Coastguard Worker
73*7594170eSAndroid Build Coastguard Worker# The list of attributes in a cc dep graph where this aspect will traverse on.
74*7594170eSAndroid Build Coastguard WorkerCC_ATTR_ASPECTS = [
75*7594170eSAndroid Build Coastguard Worker    "dynamic_deps",
76*7594170eSAndroid Build Coastguard Worker    "deps",
77*7594170eSAndroid Build Coastguard Worker    "shared",
78*7594170eSAndroid Build Coastguard Worker    "src",
79*7594170eSAndroid Build Coastguard Worker    "runtime_deps",
80*7594170eSAndroid Build Coastguard Worker    "static_deps",
81*7594170eSAndroid Build Coastguard Worker    "whole_archive_deps",
82*7594170eSAndroid Build Coastguard Worker    "androidmk_dynamic_deps",
83*7594170eSAndroid Build Coastguard Worker]
84*7594170eSAndroid Build Coastguard Worker
85*7594170eSAndroid Build Coastguard Workercollect_cc_libs_aspect = aspect(
86*7594170eSAndroid Build Coastguard Worker    implementation = _collect_cc_libs_aspect_impl,
87*7594170eSAndroid Build Coastguard Worker    provides = [CcTestSharedLibsInfo],
88*7594170eSAndroid Build Coastguard Worker    attr_aspects = CC_ATTR_ASPECTS,
89*7594170eSAndroid Build Coastguard Worker)
90