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"""Aspects used to transform certain providers into a TradefedTestDependencyInfo.
16
17Tradefed tests require a TradefedTestDependencyInfo provider that is not
18usually returned by most rules. Instead of creating custom rules to adapt
19build rule providers, we use Bazel aspects to convert the input rule's
20provider into a suitable type.
21
22See https://docs.bazel.build/versions/main/skylark/aspects.html#aspects
23for more information on how aspects work.
24"""
25
26load("//bazel/rules:soong_prebuilt.bzl", "SoongPrebuiltInfo")
27load("//bazel/rules:tradefed_test_dependency_info.bzl", "TradefedTestDependencyInfo")
28
29def _soong_prebuilt_tradefed_aspect_impl(target, ctx):
30    runtime_jars = []
31    runtime_shared_libraries = []
32    for f in target[SoongPrebuiltInfo].transitive_runtime_outputs.to_list():
33        if f.extension == "so":
34            runtime_shared_libraries.append(f)
35        elif f.extension == "jar":
36            runtime_jars.append(f)
37
38    return [
39        TradefedTestDependencyInfo(
40            runtime_jars = depset(runtime_jars),
41            runtime_shared_libraries = depset(runtime_shared_libraries),
42            transitive_test_files = target[SoongPrebuiltInfo].transitive_test_files,
43        ),
44    ]
45
46soong_prebuilt_tradefed_test_aspect = aspect(
47    implementation = _soong_prebuilt_tradefed_aspect_impl,
48)
49