• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2023 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(":host_for_device.bzl", "java_host_for_device")
16load(":rules.bzl", "java_import")
17load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
18load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
19
20Platform = provider(
21    "Platform of the leaf dependency in a linear dependency chain",
22    fields = {
23        "platform": "the target platform",
24        "host_platform": "the host platform",
25    },
26)
27
28def _host_for_device_tester_aspect_impl(target, ctx):
29    if ctx.rule.attr.exports and len(ctx.rule.attr.exports) > 0 and Platform in ctx.rule.attr.exports[0]:
30        return ctx.rule.attr.exports[0][Platform]
31    return Platform(
32        platform = ctx.fragments.platform.platform,
33        host_platform = ctx.fragments.platform.host_platform,
34    )
35
36host_for_device_tester_aspect = aspect(
37    implementation = _host_for_device_tester_aspect_impl,
38    attr_aspects = ["exports"],
39    fragments = ["platform"],
40    provides = [Platform],
41)
42
43def _host_for_device_dep_runs_in_exec_config_test_impl(ctx):
44    env = analysistest.begin(ctx)
45    target_under_test = analysistest.target_under_test(env)
46    actual_platform = target_under_test[Platform].platform
47    expected_platform = target_under_test[Platform].host_platform
48    asserts.equals(env, expected_platform, actual_platform)
49    asserts.true(env, JavaInfo in target_under_test, "Expected host_for_device to provide JavaInfo")
50    return analysistest.end(env)
51
52host_for_device_dep_runs_in_exec_config_test = analysistest.make(
53    _host_for_device_dep_runs_in_exec_config_test_impl,
54    extra_target_under_test_aspects = [host_for_device_tester_aspect],
55)
56
57def test_host_for_device(name):
58    java_host_for_device(
59        name = name + "_parent",
60        exports = [name + "_child"],
61        tags = ["manual"],
62    )
63    java_import(
64        name = name + "_child",
65        jars = ["blah.jar"],
66        tags = ["manual"],
67    )
68    host_for_device_dep_runs_in_exec_config_test(
69        name = name,
70        target_under_test = name + "_parent",
71    )
72    return name
73
74def host_for_device_test_suite(name):
75    native.test_suite(
76        name = name,
77        tests = [test_host_for_device("test_host_for_device")],
78    )
79