blob: beb2e1d5082321e9bde1df1d920bcb79a4e7a47c [file] [log] [blame]
kmbbfd89d62018-04-11 14:26:56 -07001#!/bin/bash
2# Copyright 2018 The Bazel Authors. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
kmbcd76c1f2018-05-16 08:51:43 -07008# http://www.apache.org/licenses/LICENSE-2.0
kmbbfd89d62018-04-11 14:26:56 -07009#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14
15set -eu
16
kmbcd76c1f2018-05-16 08:51:43 -070017RUNFILES="${RUNFILES:-$0.runfiles}"
18RUNFILES_MANIFEST_FILE="${RUNFILES_MANIFEST_FILE:-$RUNFILES/MANIFEST}"
19
20IS_WINDOWS=false
21case "$(uname | tr [:upper:] [:lower:])" in
22msys*|mingw*|cygwin*)
23 IS_WINDOWS=true
24esac
25
26if "$IS_WINDOWS" && ! type rlocation &> /dev/null; then
27 function rlocation() {
28 # Use 'sed' instead of 'awk', so if the absolute path ($2) has spaces, it
29 # will be printed completely.
30 local result="$(grep "$1" "${RUNFILES_MANIFEST_FILE}" | head -1)"
31 # If the entry has a space, it is a mapping from a runfiles-path to absolute
32 # path, otherwise it resolves to itself.
33 echo "$result" | grep -q " " \
34 && echo "$result" | sed 's/^[^ ]* //' \
35 || echo "$result"
36 }
37fi
38
39# Find helper artifacts:
40# Windows (in MANIFEST): <repository_name>/<path/to>/file
41# Linux/MacOS (symlink): ${RUNFILES}/<repository_name>/<path/to>/file
42if "$IS_WINDOWS"; then
43 INPUT="$(rlocation "[^/]*/tools/android/desugared_java8_legacy_libs.jar")"
44 CONFIG="$(rlocation "[^/]*/tools/android/minify_java8_legacy_libs.cfg")"
45 SCAN="$(rlocation "[^/]*/src/tools/android/java/com/google/devtools/build/android/desugar/scan/KeepScanner")"
46 PG="$(rlocation "[^/]*/third_party/java/proguard/proguard")"
47 DEXER="$(rlocation "[^/]*/tools/android/dexer")"
48else
49 INPUT="$(find "${RUNFILES}" -path "*/tools/android/desugared_java8_legacy_libs.jar" | head -1)"
50 CONFIG="$(find "${RUNFILES}" -path "*/tools/android/minify_java8_legacy_libs.cfg" | head -1)"
51 SCAN="$(find "${RUNFILES}" -path "*/src/tools/android/java/com/google/devtools/build/android/desugar/scan/KeepScanner" | head -1)"
52 PG="$(find "${RUNFILES}" -path "*/third_party/java/proguard/proguard" | head -1)"
53 DEXER="$(find "${RUNFILES}" -path "*/tools/android/dexer" | head -1)"
54fi
55
kmbbfd89d62018-04-11 14:26:56 -070056android_jar=
57binary_jar=
58dest=
59while [[ "$#" -gt 0 ]]; do
60 arg="$1"; shift;
61 case "${arg}" in
62 --binary) binary_jar="$1"; shift ;;
63 --binary=*) binary_jar="${arg:9}" ;;
64 --output) dest="$1"; shift ;;
65 --output=*) dest="${arg:9}" ;;
66 --android_jar) android_jar="$1"; shift ;;
67 --android_jar=*) android_jar="${arg:14}" ;;
68 *) echo "Unknown flag: ${arg}"; exit 1 ;;
69 esac
70done
71
kmbcd76c1f2018-05-16 08:51:43 -070072todex="${INPUT}"
73if [[ -n "${binary_jar}" ]]; then
74 tmpdir=$(mktemp -d)
75 trap "rm -rf ${tmpdir}" EXIT
76
77 # Minification requested
78 # 1. compute -keep rules from binary
79 seeds="${tmpdir}/seeds.cfg"
80 "${SCAN}" \
81 --input "${binary_jar}" \
82 --classpath_entry "${todex}" \
83 --bootclasspath_entry "${android_jar}" \
84 --keep_file "${seeds}"
85
86 # 2. proguard with -keep rules generated above and standard config file.
87 # Use app's android.jar as -libraryjar.
88 todex="${tmpdir}/proguarded.jar"
89 "${PG}" \
90 -injars "${INPUT}" \
91 -outjars "${todex}" \
92 -libraryjars "${android_jar}" \
93 "@${CONFIG}" \
94 "@${seeds}"
95fi
96# Convert .jar file to .dex
97"${DEXER}" --dex "--output=${dest}" "${todex}"