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"""Rule used to generate a Cuttlefish device environment. 16 17This rule creates a device environment rule to run tests on a Cuttlefish Android 18Virtual Device. Test targets that run in this environment will start a new 19dedicated virtual device for each execution. 20 21Device properties such as the image used can be configured via an attribute. 22""" 23 24load("//bazel/rules:platform_transitions.bzl", "host_transition") 25load("//bazel/rules:device_test.bzl", "DeviceEnvironment") 26load("@device_infra//remote_device:download_cvd_artifact.bzl", "ImageProvider") 27load( 28 "//:constants.bzl", 29 "adb_label", 30) 31 32_BAZEL_WORK_DIR = "${TEST_SRCDIR}/${TEST_WORKSPACE}/" 33 34def _cuttlefish_device_impl(ctx): 35 path_additions = [_BAZEL_WORK_DIR + ctx.file._adb.dirname] 36 image_file = ctx.attr.build_files[ImageProvider].image 37 cvd_host_file = ctx.attr.build_files[ImageProvider].cvd_host_package 38 ctx.actions.expand_template( 39 template = ctx.file._create_script_template, 40 output = ctx.outputs.out, 41 is_executable = True, 42 substitutions = { 43 "{img_path}": _BAZEL_WORK_DIR + image_file.short_path, 44 "{cvd_host_package_path}": _BAZEL_WORK_DIR + cvd_host_file.short_path, 45 "{path_additions}": ":".join(path_additions), 46 }, 47 ) 48 49 return DeviceEnvironment( 50 runner = depset([ctx.outputs.out]), 51 data = ctx.runfiles(files = [ 52 cvd_host_file, 53 ctx.outputs.out, 54 image_file, 55 ]), 56 ) 57 58cuttlefish_device = rule( 59 attrs = { 60 "build_files": attr.label( 61 providers = [ImageProvider], 62 mandatory = True, 63 ), 64 "out": attr.output(mandatory = True), 65 "_create_script_template": attr.label( 66 default = "//bazel/rules/device:create_cuttlefish.sh.template", 67 allow_single_file = True, 68 ), 69 # This attribute is required to use Starlark transitions. It allows 70 # allowlisting usage of this rule. For more information, see 71 # https://docs.bazel.build/versions/master/skylark/config.html#user-defined-transitions 72 "_allowlist_function_transition": attr.label( 73 default = "@bazel_tools//tools/allowlists/function_transition_allowlist", 74 ), 75 "_adb": attr.label( 76 default = adb_label, 77 allow_single_file = True, 78 cfg = host_transition, 79 ), 80 }, 81 implementation = _cuttlefish_device_impl, 82) 83