Skip to content

Commit

Permalink
Migrate legacy desugar wrapper to new rlocation() (bazelbuild#16025)
Browse files Browse the repository at this point in the history
RELNOTES: Fix for desugaring failure on Bazel+Android+Windows build scenario.
PiperOrigin-RevId: 464654335
Change-Id: I845093b134dc68ae541603008fe7fee701c7451c

Co-authored-by: Gowroji Sunil <48122892+sgowroji@users.noreply.github.com>
  • Loading branch information
ted-xie and sgowroji authored Aug 3, 2022
1 parent 6102d33 commit a8dacc7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
1 change: 1 addition & 0 deletions tools/android/BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
50 changes: 22 additions & 28 deletions tools/android/desugar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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): <repository_name>/<path/to>/tool
# Linux/MacOS (symlink): ${RUNFILES}/<repository_name>/<path/to>/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)"
Expand Down Expand Up @@ -118,3 +111,4 @@ fi
"--jvm_flag=-Djdk.internal.lambda.dumpProxyClasses=${TMPDIR}" \
"$@" \
"${DESUGAR_JAVA8_LIBS_CONFIG[@]}"

0 comments on commit a8dacc7

Please sign in to comment.