Skip to content

Commit

Permalink
Ensure Bazel Android tools use the proper configuration transition.
Browse files Browse the repository at this point in the history
Part of #16285.

Fixes #19600.

PiperOrigin-RevId: 572680900
Change-Id: I0ef3b00ac70043dea81bcd39764f0ab2c6be2d2a
  • Loading branch information
katre authored and copybara-github committed Oct 11, 2023
1 parent d1238a2 commit 1f12a4d
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/test/java/com/google/devtools/build/android/r8/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_java//java:defs.bzl", "java_library", "java_test")
load("//tools/android:android_genrule.bzl", "android_genrule")

package(
default_testonly = 1,
Expand Down Expand Up @@ -111,15 +112,17 @@ java_library(
srcs = glob(["testdata/lambda/*.java"]),
)

genrule(
# Needs to switch to dependencies to an android config.
android_genrule(
name = "desugar_testdata_lambda",
srcs = [
":testdata_lambda",
"@bazel_tools//tools/android:android_jar",
],
outs = ["testdata_lambda_desugared.jar"],
out = "testdata_lambda_desugared.jar",
cmd = "$(location //src/tools/android/java/com/google/devtools/build/android/r8:desugar) " +
"-i $(location :testdata_lambda) -o $@ " +
"--bootclasspath_entry $(location @bazel_tools//tools/android:android_jar)",
platform = "//tools/android:arm64-v8a",
tools = ["//src/tools/android/java/com/google/devtools/build/android/r8:desugar"],
)
9 changes: 9 additions & 0 deletions tools/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,14 @@ filegroup(
],
)

platform(
name = "arm64-v8a",
constraint_values = [
"@platforms//os:android",
"@platforms//cpu:arm64",
],
visibility = ["//visibility:public"],
)

# Exported for AndroidSdkRepositoryTest to use it instead of mocking it out.
exports_files(["android_sdk_repository_template.bzl"])
102 changes: 102 additions & 0 deletions tools/android/android_genrule.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# 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.

"""Allows building Android-specific dependencies with the correct configuration."""

def _android_transition_impl(settings, attr):
if not settings["//command_line_option:incompatible_enable_android_toolchain_resolution"]:
return {}
return {
"//command_line_option:platforms": str(attr.platform),
"//command_line_option:android_platforms": str(attr.platform),
}

_android_transition = transition(
implementation = _android_transition_impl,
inputs = [
"//command_line_option:incompatible_enable_android_toolchain_resolution",
],
outputs = [
"//command_line_option:platforms",
"//command_line_option:android_platforms",
],
)

# TODO(blaze-configurability-team): Consider replacing with platform_data when that is available.
def _android_platform_data_impl(ctx):
result = []

new_file = ctx.outputs.out
original_file = ctx.file.target

ctx.actions.symlink(
output = new_file,
target_file = original_file,
)

files = depset(direct = [new_file])

result.append(
DefaultInfo(
files = files,
),
)

return result

_android_platform_data = rule(
implementation = _android_platform_data_impl,
attrs = {
"target": attr.label(
allow_single_file = True,
cfg = _android_transition,
),
"platform": attr.label(
mandatory = True,
),
"out": attr.output(),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
)

def android_genrule(
name = "",
platform = "",
out = "",
tags = [],
visibility = [],
**kwargs):
# TODO(jcater): Check for empty name, platform, and outs.

# Transition to an Android config.
_android_platform_data(
name = name,
target = ":%s_for_android_%s" % (name, out),
out = out,
platform = platform,
visibility = visibility,
)

# The actual genrule.
native.genrule(
name = "%s_for_android" % name,
outs = [
":%s_for_android_%s" % (name, out),
],
tags = tags + ["manual"],
visibility = ["//visibility:private"],
**kwargs
)
7 changes: 5 additions & 2 deletions tools/android/runtime_deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# and tools_android.

load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("//tools/android:android_genrule.bzl", "android_genrule")

filegroup(
name = "srcs",
Expand Down Expand Up @@ -67,11 +68,13 @@ EOF
stamp = True,
)

genrule(
# Needs to switch to dependencies to an android config.
android_genrule(
name = "desugar_jdk_libs",
srcs = ["@desugar_jdk_libs"],
outs = ["desugar_jdk_libs.jar"],
out = "desugar_jdk_libs.jar",
cmd = "cp $< $@",
platform = "//tools/android:arm64-v8a",
)

pkg_tar(
Expand Down

0 comments on commit 1f12a4d

Please sign in to comment.