xref: /aosp_15_r20/external/bazelbuild-rules_android/rules/sandboxed_sdk_toolbox.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
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 SandboxedSdkToolbox commands."""
16
17load(":java.bzl", _java = "java")
18
19def _extract_api_descriptors(
20        ctx,
21        output = None,
22        sdk_deploy_jar = None,
23        sandboxed_sdk_toolbox = None,
24        host_javabase = None):
25    """Extracts API descriptors from a sandboxed SDK classpath.
26
27    The API descriptors can later be used to generate sources for communicating with this SDK.
28
29    Args:
30      ctx: The context.
31      output: Output API descriptors jar file.
32      sdk_deploy_jar: The SDK classpath, with transitive dependencies.
33      sandboxed_sdk_toolbox: Toolbox executable files.
34      host_javabase: Javabase used to run the toolbox.
35    """
36    args = ctx.actions.args()
37    args.add("extract-api-descriptors")
38    args.add("--sdk-deploy-jar", sdk_deploy_jar)
39    args.add("--output-sdk-api-descriptors", output)
40    _java.run(
41        ctx = ctx,
42        host_javabase = host_javabase,
43        executable = sandboxed_sdk_toolbox,
44        arguments = [args],
45        inputs = [sdk_deploy_jar],
46        outputs = [output],
47        mnemonic = "ExtractApiDescriptors",
48        progress_message = "Extract SDK API descriptors %s" % output.short_path,
49    )
50
51def _extract_api_descriptors_from_asar(
52        ctx,
53        output = None,
54        asar = None,
55        sandboxed_sdk_toolbox = None,
56        host_javabase = None):
57    """Extracts API descriptors from a sandboxed SDK archive.
58
59    The API descriptors can later be used to generate sources for communicating with this SDK.
60
61    Args:
62      ctx: The context.
63      output: Output API descriptors jar file.
64      asar: The sandboxed sdk archive.
65      sandboxed_sdk_toolbox: Toolbox executable files.
66      host_javabase: Javabase used to run the toolbox.
67    """
68    args = ctx.actions.args()
69    args.add("extract-api-descriptors-from-asar")
70    args.add("--asar", asar)
71    args.add("--output-sdk-api-descriptors", output)
72    _java.run(
73        ctx = ctx,
74        host_javabase = host_javabase,
75        executable = sandboxed_sdk_toolbox,
76        arguments = [args],
77        inputs = [asar],
78        outputs = [output],
79        mnemonic = "ExtractApiDescriptorsFromAsar",
80        progress_message = "Extract SDK API descriptors from ASAR %s" % output.short_path,
81    )
82
83def _generate_client_sources(
84        ctx,
85        output_kotlin_dir = None,
86        output_java_dir = None,
87        sdk_api_descriptors = None,
88        aidl_compiler = None,
89        framework_aidl = None,
90        sandboxed_sdk_toolbox = None,
91        host_javabase = None):
92    """Generate Kotlin and Java sources for SDK communication.
93
94    Args:
95      ctx: The context.
96      output_kotlin_dir: Directory for Kotlin source tree. It depends on the Java sources.
97      output_java_dir: Directory for Java source tree. Doesn't depend on Kotlin sources.
98      sdk_api_descriptors: SDK API descriptor jar.
99      aidl_compiler: Executable files for the AOSP AIDL compiler.
100      framework_aidl: Framework.aidl file used to compile AIDL sources.
101      sandboxed_sdk_toolbox: Toolbox executable files.
102      host_javabase: Javabase used to run the toolbox.
103    """
104    args = ctx.actions.args()
105    args.add("generate-client-sources")
106    args.add("--sdk-api-descriptors", sdk_api_descriptors)
107    args.add("--aidl-compiler", aidl_compiler)
108    args.add("--framework-aidl", framework_aidl)
109    args.add("--output-kotlin-dir", output_kotlin_dir.path)
110    args.add("--output-java-dir", output_java_dir.path)
111    _java.run(
112        ctx = ctx,
113        host_javabase = host_javabase,
114        executable = sandboxed_sdk_toolbox,
115        arguments = [args],
116        inputs = [
117            sdk_api_descriptors,
118            aidl_compiler,
119            framework_aidl,
120        ],
121        outputs = [output_kotlin_dir, output_java_dir],
122        mnemonic = "GenClientSources",
123        progress_message = "Generate client sources for %s" % output_kotlin_dir.short_path,
124    )
125
126def _generate_sdk_dependencies_manifest(
127        ctx,
128        output = None,
129        manifest_package = None,
130        sdk_module_configs = None,
131        sdk_archives = None,
132        debug_key = None,
133        sandboxed_sdk_toolbox = None,
134        host_javabase = None):
135    """Generates a manifest that lists all sandboxed SDK dependencies.
136
137    The generated manifest will contain <uses-sdk-library> tags for each SDK. This is required for
138    loading the SDK in the Privacy Sandbox.
139
140    Args:
141      ctx: The context.
142      output: File where the final manifest will be written.
143      manifest_package: The package used in the manifest.
144      sdk_module_configs: List of SDK Module config JSON files with SDK packages and versions.
145      sdk_archives: List of SDK archives, as ASAR files. They will also be listed as dependencies.
146      debug_key: Debug keystore that will later be used to sign the SDK APKs.
147      sandboxed_sdk_toolbox: Toolbox executable files.
148      host_javabase: Javabase used to run the toolbox.
149    """
150    inputs = [debug_key]
151    args = ctx.actions.args()
152    args.add("generate-sdk-dependencies-manifest")
153    args.add("--manifest-package", manifest_package)
154    if sdk_module_configs:
155        args.add("--sdk-module-configs", ",".join([config.path for config in sdk_module_configs]))
156        inputs.extend(sdk_module_configs)
157    if sdk_archives:
158        args.add("--sdk-archives", ",".join([archive.path for archive in sdk_archives]))
159        inputs.extend(sdk_archives)
160    args.add("--debug-keystore", debug_key)
161    args.add("--debug-keystore-pass", "android")
162    args.add("--debug-keystore-alias", "androiddebugkey")
163    args.add("--output-manifest", output)
164    _java.run(
165        ctx = ctx,
166        host_javabase = host_javabase,
167        executable = sandboxed_sdk_toolbox,
168        arguments = [args],
169        inputs = inputs,
170        outputs = [output],
171        mnemonic = "GenSdkDepManifest",
172        progress_message = "Generate SDK dependencies manifest %s" % output.short_path,
173    )
174
175sandboxed_sdk_toolbox = struct(
176    extract_api_descriptors = _extract_api_descriptors,
177    extract_api_descriptors_from_asar = _extract_api_descriptors_from_asar,
178    generate_client_sources = _generate_client_sources,
179    generate_sdk_dependencies_manifest = _generate_sdk_dependencies_manifest,
180)
181