Skip to content

Commit

Permalink
Migrate library desugaring logic to rules_android
Browse files Browse the repository at this point in the history
Also contains various fixes for desugaring logic in OSS Android builds. NOTE: library desugaring doesn't work with persistent worker mode.

#122

PiperOrigin-RevId: 638095332
Change-Id: Idff5af5142e2debcf9b81b49f7e671020ad4d0ad
  • Loading branch information
ted-xie authored and copybara-github committed May 29, 2024
1 parent b816ac2 commit 613b117
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions rules/dex_desugar_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ dex_desugar_aspect = aspect(
{
"_desugar_java8": attr.label(
default = Label("@bazel_tools//tools/android:desugar_java8"),
allow_files = True,
cfg = "exec",
executable = True,
),
Expand Down
2 changes: 1 addition & 1 deletion toolchains/android/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ _ATTRS = dict(
),
desugar = attr.label(
cfg = "exec",
default = Label("@bazel_tools//tools/android:desugar_java8"),
default = Label("//tools/android:desugar_java8"),
executable = True,
),
desugar_java8_extra_bootclasspath = attr.label(
Expand Down
14 changes: 11 additions & 3 deletions tools/android/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_java//java:defs.bzl", "java_binary")
load(":defs.bzl", "android_jar", "run_singlejar")
load(":defs.bzl", "android_jar", "run_ijar", "run_singlejar")

config_setting(
name = "minimal_desugaring",
Expand Down Expand Up @@ -180,9 +180,17 @@ sh_binary(
visibility = ["//visibility:public"],
)

alias(
run_ijar(
name = "ijar_desugared_java8_legacy_libs",
jar = ":desugared_jdk_libs.jar",
visibility = ["//visibility:public"],
)

java_import(
name = "desugared_java8_legacy_apis",
actual = ":gen_fail", # TODO(#122): Fix library desugaring
constraints = ["android"],
jars = [":ijar_desugared_java8_legacy_libs"],
neverlink = 1,
visibility = ["//visibility:public"],
)

Expand Down
10 changes: 9 additions & 1 deletion tools/android/d8_desugar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---

export JAVA_RUNFILES="$0.runfiles"

# exit on errors and uninitialized variables
set -eu

Expand Down Expand Up @@ -70,13 +72,19 @@ fi
# Some unit tests pass an explicit --desugared_lib_config, in that case don't
# add the default one.
has_desugared_lib_config=false
has_persistent_worker=false
for arg in "$@"; do
if [[ "$arg" == "--desugared_lib_config" ]]; then
has_desugared_lib_config=true
fi
if [[ "$arg" == "--persistent_worker" ]]; then
has_persistent_worker=true
fi
done

if [[ "$has_desugared_lib_config" == "true" ]]; then
if [[ "$has_persistent_worker" == "true" ]]; then
"$DESUGAR_BINARY" --persistent_worker
elif [[ "$has_desugared_lib_config" == "true" ]]; then
"$DESUGAR_BINARY" \
"$@"
else
Expand Down
21 changes: 21 additions & 0 deletions tools/android/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,24 @@ run_singlejar = rule(
"_java_toolchain": attr.label(default = Label("//tools/jdk:toolchain")),
},
)

def _run_ijar(ctx):
ijar_jar = java_common.run_ijar(
ctx.actions,
jar = ctx.file.jar,
java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo],
)
return [DefaultInfo(files = depset([ijar_jar]))]

run_ijar = rule(
implementation = _run_ijar,
doc = "Runs ijar over the given jar.",
attrs = {
"jar": attr.label(mandatory = True, allow_single_file = True),
"_java_toolchain": attr.label(
default = "//tools/jdk:current_java_toolchain",
providers = [java_common.JavaToolchainInfo],
),
},
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
)

0 comments on commit 613b117

Please sign in to comment.