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 classpath="./transformer.jar:$ASM_JAR"
31  local args=(-cp $classpath)
32  while [ $# -ne 0 ] ; do
33    case $1 in
34      -cp|-classpath|--class-path)
35        shift
36        shift
37        ;;
38      *)
39        args+=("$1")
40        shift
41        ;;
42    esac
43  done
44
45  # Compile.
46  $ORIGINAL_JAVAC "${args[@]}"
47
48  # Move original classes to intermediate location.
49  mv classes intermediate-classes
50  mkdir classes
51
52  # Transform intermediate classes.
53  local transformer_args="-cp ${ASM_JAR}:transformer.jar transformer.IndyTransformer"
54  for class in intermediate-classes/*.class ; do
55    local transformed_class=classes/$(basename ${class})
56    ${JAVA:-java} ${transformer_args} $PWD/${class} ${transformed_class}
57  done
58}
59
60export -f javac_wrapper
61export JAVAC=javac_wrapper
62
63######################################################################
64
65# Build the transformer to apply to compiled classes.
66mkdir classes
67${ORIGINAL_JAVAC:-javac} ${JAVAC_ARGS} -cp "${ASM_JAR}" -d classes $(find util-src -name '*.java')
68jar -cf transformer.jar -C classes transformer/ -C classes annotations/
69rm -rf classes
70
71# Use API level 28 for invoke-custom bytecode support.
72DESUGAR=false ./default-build "$@" --api-level 28
73