1# Copyright 2023 The Bazel Authors. All rights reserved. 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"""Bazel rule for defining an Android Sandboxed SDK.""" 16 17load( 18 "//rules:common.bzl", 19 _common = "common", 20) 21load("//rules:java.bzl", _java = "java") 22load( 23 "//rules:sandboxed_sdk_toolbox.bzl", 24 _sandboxed_sdk_toolbox = "sandboxed_sdk_toolbox", 25) 26load( 27 "//rules:utils.bzl", 28 _get_android_toolchain = "get_android_toolchain", 29) 30load(":providers.bzl", "AndroidSandboxedSdkInfo") 31 32_ATTRS = dict( 33 sdk_modules_config = attr.label( 34 allow_single_file = [".pb.json"], 35 ), 36 internal_android_binary = attr.label(), 37 sdk_deploy_jar = attr.label( 38 allow_single_file = [".jar"], 39 ), 40 _host_javabase = attr.label( 41 cfg = "exec", 42 default = Label("//tools/jdk:current_java_runtime"), 43 ), 44) 45 46def _impl(ctx): 47 sdk_api_descriptors = ctx.actions.declare_file(ctx.label.name + "_sdk_api_descriptors.jar") 48 _sandboxed_sdk_toolbox.extract_api_descriptors( 49 ctx, 50 output = sdk_api_descriptors, 51 sdk_deploy_jar = ctx.file.sdk_deploy_jar, 52 sandboxed_sdk_toolbox = _get_android_toolchain(ctx).sandboxed_sdk_toolbox.files_to_run, 53 host_javabase = _common.get_host_javabase(ctx), 54 ) 55 return [ 56 DefaultInfo( 57 files = depset([sdk_api_descriptors]), 58 ), 59 AndroidSandboxedSdkInfo( 60 internal_apk_info = ctx.attr.internal_android_binary[ApkInfo], 61 sdk_module_config = ctx.file.sdk_modules_config, 62 sdk_api_descriptors = sdk_api_descriptors, 63 ), 64 ] 65 66_android_sandboxed_sdk = rule( 67 attrs = _ATTRS, 68 executable = False, 69 implementation = _impl, 70 provides = [ 71 AndroidSandboxedSdkInfo, 72 ], 73 toolchains = [ 74 "//toolchains/android:toolchain_type", 75 "@bazel_tools//tools/jdk:toolchain_type", 76 ], 77) 78 79def android_sandboxed_sdk_macro( 80 name, 81 sdk_modules_config, 82 deps, 83 min_sdk_version = 21, 84 visibility = None, 85 testonly = None, 86 tags = [], 87 custom_package = None, 88 android_binary = None): 89 """Macro for an Android Sandboxed SDK. 90 91 Args: 92 name: Unique name of this target. 93 sdk_modules_config: Module config for this SDK. 94 deps: Set of android libraries that make up this SDK. 95 min_sdk_version: Min SDK version for the SDK. 96 visibility: A list of targets allowed to depend on this rule. 97 testonly: Whether this library is only for testing. 98 tags: A list of string tags passed to generated targets. 99 custom_package: Java package for resources, 100 android_binary: android_binary rule used to create the intermediate SDK APK. 101 """ 102 fully_qualified_name = "//%s:%s" % (native.package_name(), name) 103 package = _java.resolve_package_from_label(Label(fully_qualified_name), custom_package) 104 105 manifest_label = Label("%s_gen_manifest" % fully_qualified_name) 106 native.genrule( 107 name = manifest_label.name, 108 outs = [name + "/AndroidManifest.xml"], 109 cmd = """cat > $@ <<EOF 110<?xml version="1.0" encoding="utf-8"?> 111<manifest xmlns:android="http://schemas.android.com/apk/res/android" 112 package="{package}"> 113 <uses-sdk android:minSdkVersion="{min_sdk_version}"/> 114 <application /> 115</manifest> 116EOF 117""".format(package = package, min_sdk_version = min_sdk_version), 118 ) 119 120 bin_fqn = "%s_bin" % fully_qualified_name 121 bin_label = Label(bin_fqn) 122 android_binary( 123 name = bin_label.name, 124 manifest = str(manifest_label), 125 generate_art_profile = False, 126 deps = deps, 127 testonly = testonly, 128 tags = tags, 129 ) 130 131 sdk_deploy_jar = Label("%s_deploy.jar" % bin_fqn) 132 _android_sandboxed_sdk( 133 name = name, 134 sdk_modules_config = sdk_modules_config, 135 visibility = visibility, 136 testonly = testonly, 137 tags = tags, 138 internal_android_binary = bin_label, 139 sdk_deploy_jar = sdk_deploy_jar, 140 ) 141