1#!/bin/bash
2#
3# Copyright 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Stop on failure.
18set -e
19
20export ASM_JAR="${ANDROID_BUILD_TOP}/prebuilts/misc/common/asm/asm-6.0.jar"
21
22export ORIGINAL_JAVAC="$JAVAC"
23
24# Wrapper function for javac which invokes the compiler and applies
25# transforms to class files after compilation.
26function javac_wrapper {
27  set -e # Stop on error - the caller script may not have this set.
28
29  # Update arguments to add transformer and ASM to the compiler classpath.
30  local args=()
31  local classpath="./transformer.jar:$ASM_JAR"
32  while [ $# -ne 0 ] ; do
33    case $1 in
34      -cp|-classpath|--class-path)
35        shift
36        shift
37        args+=(-cp $classpath)
38        ;;
39      *)
40        args+=("$1")
41        shift
42        ;;
43    esac
44  done
45
46  # Compile.
47  $ORIGINAL_JAVAC "${args[@]}"
48
49  # Move original classes to intermediate location.
50  mv classes intermediate-classes
51  mkdir classes
52
53  # Transform intermediate classes.
54  local transformer_args="-cp ${ASM_JAR}:transformer.jar transformer.IndyTransformer"
55  for class in intermediate-classes/*.class ; do
56    local transformed_class=classes/$(basename ${class})
57    ${JAVA:-java} ${transformer_args} $PWD/${class} ${transformed_class}
58  done
59}
60
61export -f javac_wrapper
62export JAVAC=javac_wrapper
63
64######################################################################
65
66# Build the transformer to apply to compiled classes.
67mkdir classes
68${ORIGINAL_JAVAC:-javac} ${JAVAC_ARGS} -cp "${ASM_JAR}" -d classes $(find util-src -name '*.java')
69jar -cf transformer.jar -C classes transformer/ -C classes annotations/
70rm -rf classes
71
72# Use API level 28 for invoke-custom bytecode support.
73DESUGAR=false ./default-build "$@" --api-level 28
74