aboutsummaryrefslogtreecommitdiff
path: root/rules/sandboxed_sdk_toolbox.bzl
blob: 933212dd4ae38fb6d4a5f33f18a838163b1eb92a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Bazel SandboxedSdkToolbox commands."""

load(":java.bzl", _java = "java")

def _extract_api_descriptors(
        ctx,
        output = None,
        sdk_deploy_jar = None,
        sandboxed_sdk_toolbox = None,
        host_javabase = None):
    """Extracts API descriptors from a sandboxed SDK classpath.

    The API descriptors can later be used to generate sources for communicating with this SDK.

    Args:
      ctx: The context.
      output: Output API descriptors jar file.
      sdk_deploy_jar: The SDK classpath, with transitive dependencies.
      sandboxed_sdk_toolbox: Toolbox executable files.
      host_javabase: Javabase used to run the toolbox.
    """
    args = ctx.actions.args()
    args.add("extract-api-descriptors")
    args.add("--sdk-deploy-jar", sdk_deploy_jar)
    args.add("--output-sdk-api-descriptors", output)
    _java.run(
        ctx = ctx,
        host_javabase = host_javabase,
        executable = sandboxed_sdk_toolbox,
        arguments = [args],
        inputs = [sdk_deploy_jar],
        outputs = [output],
        mnemonic = "ExtractApiDescriptors",
        progress_message = "Extract SDK API descriptors %s" % output.short_path,
    )

def _extract_api_descriptors_from_asar(
        ctx,
        output = None,
        asar = None,
        sandboxed_sdk_toolbox = None,
        host_javabase = None):
    """Extracts API descriptors from a sandboxed SDK archive.

    The API descriptors can later be used to generate sources for communicating with this SDK.

    Args:
      ctx: The context.
      output: Output API descriptors jar file.
      asar: The sandboxed sdk archive.
      sandboxed_sdk_toolbox: Toolbox executable files.
      host_javabase: Javabase used to run the toolbox.
    """
    args = ctx.actions.args()
    args.add("extract-api-descriptors-from-asar")
    args.add("--asar", asar)
    args.add("--output-sdk-api-descriptors", output)
    _java.run(
        ctx = ctx,
        host_javabase = host_javabase,
        executable = sandboxed_sdk_toolbox,
        arguments = [args],
        inputs = [asar],
        outputs = [output],
        mnemonic = "ExtractApiDescriptorsFromAsar",
        progress_message = "Extract SDK API descriptors from ASAR %s" % output.short_path,
    )

def _generate_client_sources(
        ctx,
        output_kotlin_dir = None,
        output_java_dir = None,
        sdk_api_descriptors = None,
        aidl_compiler = None,
        framework_aidl = None,
        sandboxed_sdk_toolbox = None,
        host_javabase = None):
    """Generate Kotlin and Java sources for SDK communication.

    Args:
      ctx: The context.
      output_kotlin_dir: Directory for Kotlin source tree. It depends on the Java sources.
      output_java_dir: Directory for Java source tree. Doesn't depend on Kotlin sources.
      sdk_api_descriptors: SDK API descriptor jar.
      aidl_compiler: Executable files for the AOSP AIDL compiler.
      framework_aidl: Framework.aidl file used to compile AIDL sources.
      sandboxed_sdk_toolbox: Toolbox executable files.
      host_javabase: Javabase used to run the toolbox.
    """
    args = ctx.actions.args()
    args.add("generate-client-sources")
    args.add("--sdk-api-descriptors", sdk_api_descriptors)
    args.add("--aidl-compiler", aidl_compiler)
    args.add("--framework-aidl", framework_aidl)
    args.add("--output-kotlin-dir", output_kotlin_dir.path)
    args.add("--output-java-dir", output_java_dir.path)
    _java.run(
        ctx = ctx,
        host_javabase = host_javabase,
        executable = sandboxed_sdk_toolbox,
        arguments = [args],
        inputs = [
            sdk_api_descriptors,
            aidl_compiler,
            framework_aidl,
        ],
        outputs = [output_kotlin_dir, output_java_dir],
        mnemonic = "GenClientSources",
        progress_message = "Generate client sources for %s" % output_kotlin_dir.short_path,
    )

def _generate_sdk_dependencies_manifest(
        ctx,
        output = None,
        manifest_package = None,
        sdk_module_configs = None,
        sdk_archives = None,
        debug_key = None,
        sandboxed_sdk_toolbox = None,
        host_javabase = None):
    """Generates a manifest that lists all sandboxed SDK dependencies.

    The generated manifest will contain <uses-sdk-library> tags for each SDK. This is required for
    loading the SDK in the Privacy Sandbox.

    Args:
      ctx: The context.
      output: File where the final manifest will be written.
      manifest_package: The package used in the manifest.
      sdk_module_configs: List of SDK Module config JSON files with SDK packages and versions.
      sdk_archives: List of SDK archives, as ASAR files. They will also be listed as dependencies.
      debug_key: Debug keystore that will later be used to sign the SDK APKs.
      sandboxed_sdk_toolbox: Toolbox executable files.
      host_javabase: Javabase used to run the toolbox.
    """
    inputs = [debug_key]
    args = ctx.actions.args()
    args.add("generate-sdk-dependencies-manifest")
    args.add("--manifest-package", manifest_package)
    if sdk_module_configs:
        args.add("--sdk-module-configs", ",".join([config.path for config in sdk_module_configs]))
        inputs.extend(sdk_module_configs)
    if sdk_archives:
        args.add("--sdk-archives", ",".join([archive.path for archive in sdk_archives]))
        inputs.extend(sdk_archives)
    args.add("--debug-keystore", debug_key)
    args.add("--debug-keystore-pass", "android")
    args.add("--debug-keystore-alias", "androiddebugkey")
    args.add("--output-manifest", output)
    _java.run(
        ctx = ctx,
        host_javabase = host_javabase,
        executable = sandboxed_sdk_toolbox,
        arguments = [args],
        inputs = inputs,
        outputs = [output],
        mnemonic = "GenSdkDepManifest",
        progress_message = "Generate SDK dependencies manifest %s" % output.short_path,
    )

sandboxed_sdk_toolbox = struct(
    extract_api_descriptors = _extract_api_descriptors,
    extract_api_descriptors_from_asar = _extract_api_descriptors_from_asar,
    generate_client_sources = _generate_client_sources,
    generate_sdk_dependencies_manifest = _generate_sdk_dependencies_manifest,
)