From 2035e8b6d0ec9b9f5f4f3917b6683e69e52e2ec4 Mon Sep 17 00:00:00 2001 From: Ted Xie Date: Mon, 1 Aug 2022 16:46:12 -0700 Subject: [PATCH] Migrate legacy desugar wrapper to new rlocation() RELNOTES: Fix for desugaring failure on Bazel+Android+Windows build scenario. PiperOrigin-RevId: 464654335 Change-Id: I845093b134dc68ae541603008fe7fee701c7451c --- tools/android/BUILD.tools | 1 + tools/android/desugar.sh | 50 +++++++++++++++++---------------------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/tools/android/BUILD.tools b/tools/android/BUILD.tools index fd6f0ec18bdbb0..45ccf4360c160b 100644 --- a/tools/android/BUILD.tools +++ b/tools/android/BUILD.tools @@ -118,6 +118,7 @@ sh_binary( name = "desugar_java8", srcs = ["desugar.sh"], data = ["//src/tools/android/java/com/google/devtools/build/android/desugar:Desugar"], + deps = ["@bazel_tools//tools/bash/runfiles"], ) alias( diff --git a/tools/android/desugar.sh b/tools/android/desugar.sh index 74eee3d1173224..fd24e346e37200 100644 --- a/tools/android/desugar.sh +++ b/tools/android/desugar.sh @@ -17,38 +17,31 @@ # jdk.internal.lambda.dumpProxyClasses and configures Java 8 library rewriting # through additional flags. -# exit on errors and uninitialized variables -set -eu - RUNFILES="${RUNFILES:-$0.runfiles}" -RUNFILES_MANIFEST_FILE="${RUNFILES_MANIFEST_FILE:-$RUNFILES/MANIFEST}" - -IS_WINDOWS=false -case "$(uname | tr [:upper:] [:lower:])" in -msys*|mingw*|cygwin*) - IS_WINDOWS=true -esac - -if "$IS_WINDOWS" && ! type rlocation &> /dev/null; then - function rlocation() { - # Use 'sed' instead of 'awk', so if the absolute path ($2) has spaces, it - # will be printed completely. - local result="$(grep "$1" "${RUNFILES_MANIFEST_FILE}" | head -1)" - # If the entry has a space, it is a mapping from a runfiles-path to absolute - # path, otherwise it resolves to itself. - echo "$result" | grep -q " " \ - && echo "$result" | sed 's/^[^ ]* //' \ - || echo "$result" - } +CHECK_FOR_EXE=0 +if [[ ! -d $RUNFILES ]]; then + # Try the Windows path + RUNFILES="${RUNFILES:-$0.exe.runfiles}" + CHECK_FOR_EXE=1 fi +RUNFILES_MANIFEST_FILE="${RUNFILES_MANIFEST_FILE:-$RUNFILES/MANIFEST}" +export JAVA_RUNFILES=$RUNFILES +export RUNFILES_LIB_DEBUG=1 +# --- begin runfiles.bash initialization v2 --- +# Copy-pasted from the Bazel Bash runfiles library v2. +set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash +source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ + source "$0.runfiles/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e +# --- end runfiles.bash initialization v2 --- -# Find script to call: -# Windows (in MANIFEST): //tool -# Linux/MacOS (symlink): ${RUNFILES}///tool -if "$IS_WINDOWS"; then - DESUGAR="$(rlocation "[^/]*/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar")" +if [[ $CHECK_FOR_EXE -eq 0 ]]; then + DESUGAR="$(rlocation "bazel_tools/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar")" else - DESUGAR="$(find "${RUNFILES}" -path "*/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar" | head -1)" + DESUGAR="$(rlocation "bazel_tools/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.exe")" fi readonly TMPDIR="$(mktemp -d)" @@ -118,3 +111,4 @@ fi "--jvm_flag=-Djdk.internal.lambda.dumpProxyClasses=${TMPDIR}" \ "$@" \ "${DESUGAR_JAVA8_LIBS_CONFIG[@]}" +