kmb | bfd89d6 | 2018-04-11 14:26:56 -0700 | [diff] [blame] | 1 | #!/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 | # |
kmb | cd76c1f | 2018-05-16 08:51:43 -0700 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
kmb | bfd89d6 | 2018-04-11 14:26:56 -0700 | [diff] [blame] | 9 | # |
| 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 | |
| 15 | set -eu |
| 16 | |
kmb | cd76c1f | 2018-05-16 08:51:43 -0700 | [diff] [blame] | 17 | RUNFILES="${RUNFILES:-$0.runfiles}" |
| 18 | RUNFILES_MANIFEST_FILE="${RUNFILES_MANIFEST_FILE:-$RUNFILES/MANIFEST}" |
| 19 | |
| 20 | IS_WINDOWS=false |
| 21 | case "$(uname | tr [:upper:] [:lower:])" in |
| 22 | msys*|mingw*|cygwin*) |
| 23 | IS_WINDOWS=true |
| 24 | esac |
| 25 | |
| 26 | if "$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 | } |
| 37 | fi |
| 38 | |
| 39 | # Find helper artifacts: |
| 40 | # Windows (in MANIFEST): <repository_name>/<path/to>/file |
| 41 | # Linux/MacOS (symlink): ${RUNFILES}/<repository_name>/<path/to>/file |
| 42 | if "$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")" |
| 48 | else |
| 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)" |
| 54 | fi |
| 55 | |
kmb | bfd89d6 | 2018-04-11 14:26:56 -0700 | [diff] [blame] | 56 | android_jar= |
| 57 | binary_jar= |
| 58 | dest= |
| 59 | while [[ "$#" -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 |
| 70 | done |
| 71 | |
kmb | cd76c1f | 2018-05-16 08:51:43 -0700 | [diff] [blame] | 72 | todex="${INPUT}" |
| 73 | if [[ -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}" |
| 95 | fi |
| 96 | # Convert .jar file to .dex |
| 97 | "${DEXER}" --dex "--output=${dest}" "${todex}" |