1*37f5703cSAndroid Build Coastguard Worker#!/bin/bash 2*37f5703cSAndroid Build Coastguard Worker# 3*37f5703cSAndroid Build Coastguard Worker# This script runs antlr3 generating java code based on .g (ANLTR3) files. 4*37f5703cSAndroid Build Coastguard Worker# antlr3 tool itself can be downloaded by running the gradle build. 5*37f5703cSAndroid Build Coastguard Worker# 6*37f5703cSAndroid Build Coastguard Worker# The script can be run from anywhere (it does not depend on current working directory) 7*37f5703cSAndroid Build Coastguard Worker# Set $ANTLR to overwrite antlr location, if desired 8*37f5703cSAndroid Build Coastguard Worker# 9*37f5703cSAndroid Build Coastguard Worker# After making any changes to the lexer, the update source file(s) generated by 10*37f5703cSAndroid Build Coastguard Worker# this script should be checked in to the repository 11*37f5703cSAndroid Build Coastguard Worker 12*37f5703cSAndroid Build Coastguard Worker# Update when switching to a different version of antlr 13*37f5703cSAndroid Build Coastguard WorkerEXPECTED_ANTLR_VERSION_STR="ANTLR Parser Generator Version 3.5.2" 14*37f5703cSAndroid Build Coastguard Worker 15*37f5703cSAndroid Build Coastguard Worker# Get the location of this script used to find locations of other things in the tree. 16*37f5703cSAndroid Build Coastguard WorkerSCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 17*37f5703cSAndroid Build Coastguard Worker 18*37f5703cSAndroid Build Coastguard Worker# Point to the directory which contains the ANTLR jars. 19*37f5703cSAndroid Build Coastguard Workerif [[ -z "$ANTLR" ]] 20*37f5703cSAndroid Build Coastguard Workerthen 21*37f5703cSAndroid Build Coastguard Worker # Best effort to find it inside of the gradle cache 22*37f5703cSAndroid Build Coastguard Worker ANTLR="$(find $HOME/.gradle/caches/modules-* -name 'org.antlr' | head -n 1)" 23*37f5703cSAndroid Build Coastguard Workerfi 24*37f5703cSAndroid Build Coastguard Worker 25*37f5703cSAndroid Build Coastguard Worker# Class that contains the static main function. 26*37f5703cSAndroid Build Coastguard WorkerANTLR_MAIN="org.antlr.Tool" 27*37f5703cSAndroid Build Coastguard Worker 28*37f5703cSAndroid Build Coastguard Workerif ! [[ -d "$ANTLR" ]]; then 29*37f5703cSAndroid Build Coastguard Worker echo >&2 "ERROR: Could not find ANTLR jars directory" 30*37f5703cSAndroid Build Coastguard Worker exit 1 31*37f5703cSAndroid Build Coastguard Workerfi 32*37f5703cSAndroid Build Coastguard Worker 33*37f5703cSAndroid Build Coastguard Worker# Build up the classpath by finding all the JARs 34*37f5703cSAndroid Build Coastguard WorkerANTLR_JARS="" 35*37f5703cSAndroid Build Coastguard Worker 36*37f5703cSAndroid Build Coastguard Workerfor jar_file_name in $(find "$ANTLR" -name '*.jar'); do 37*37f5703cSAndroid Build Coastguard Worker if ! [[ -z "$ANTLR_JARS" ]]; then 38*37f5703cSAndroid Build Coastguard Worker ANTLR_JARS+=":" 39*37f5703cSAndroid Build Coastguard Worker fi 40*37f5703cSAndroid Build Coastguard Worker ANTLR_JARS+="$jar_file_name" 41*37f5703cSAndroid Build Coastguard Workerdone 42*37f5703cSAndroid Build Coastguard Worker 43*37f5703cSAndroid Build Coastguard Workerif [[ -z "$ANTLR_JARS" ]]; then 44*37f5703cSAndroid Build Coastguard Worker echo >&2 "Could not find any JARs in the ANTLR directory" 45*37f5703cSAndroid Build Coastguard Worker echo >&2 "Is '"$ANTLR"' the correct path to the JARs?" 46*37f5703cSAndroid Build Coastguard Worker exit 1 47*37f5703cSAndroid Build Coastguard Workerfi 48*37f5703cSAndroid Build Coastguard Worker 49*37f5703cSAndroid Build Coastguard Workerfunction run_antlr() { 50*37f5703cSAndroid Build Coastguard Worker CLASSPATH="$ANTLR_JARS" java 2>&1 "$ANTLR_MAIN" "$@" 51*37f5703cSAndroid Build Coastguard Worker} 52*37f5703cSAndroid Build Coastguard Worker 53*37f5703cSAndroid Build Coastguard WorkerANTLR_VERSION="$(run_antlr -version)" 54*37f5703cSAndroid Build Coastguard Worker 55*37f5703cSAndroid Build Coastguard Workerif [[ -z "$ANTLR_VERSION" ]] 56*37f5703cSAndroid Build Coastguard Workerthen 57*37f5703cSAndroid Build Coastguard Worker echo >&2 "ERROR: Failed to execute antlr at \"$ANTLR\"" 58*37f5703cSAndroid Build Coastguard Worker exit 1 59*37f5703cSAndroid Build Coastguard Workerfi 60*37f5703cSAndroid Build Coastguard Worker 61*37f5703cSAndroid Build Coastguard Workerif [[ "$EXPECTED_ANTLR_VERSION_STR" != "$ANTLR_VERSION" ]] 62*37f5703cSAndroid Build Coastguard Workerthen 63*37f5703cSAndroid Build Coastguard Worker echo >&2 "ERROR: Wrong version of jflex: \"$ANTLR_VERSION\". Expected: \"$EXPECTED_ANTLR_VERSION_STR\"" 64*37f5703cSAndroid Build Coastguard Worker exit 1 65*37f5703cSAndroid Build Coastguard Workerfi 66*37f5703cSAndroid Build Coastguard Worker 67*37f5703cSAndroid Build Coastguard Worker 68*37f5703cSAndroid Build Coastguard Workerfunction generate_file { 69*37f5703cSAndroid Build Coastguard Worker local JAVA_FILE="$1" 70*37f5703cSAndroid Build Coastguard Worker local G_FILE="$2" 71*37f5703cSAndroid Build Coastguard Worker 72*37f5703cSAndroid Build Coastguard Worker if ! [[ -f "$JAVA_FILE" ]]; then 73*37f5703cSAndroid Build Coastguard Worker echo >&2 "ERROR: File \"$JAVA_FILE\" not found" 74*37f5703cSAndroid Build Coastguard Worker exit 1 75*37f5703cSAndroid Build Coastguard Worker fi 76*37f5703cSAndroid Build Coastguard Worker 77*37f5703cSAndroid Build Coastguard Worker echo "Re-generating \"$JAVA_FILE\"..." 78*37f5703cSAndroid Build Coastguard Worker 79*37f5703cSAndroid Build Coastguard Worker [[ -f "$JAVA_FILE" ]] && rm -f "$JAVA_FILE" 80*37f5703cSAndroid Build Coastguard Worker 81*37f5703cSAndroid Build Coastguard Worker local JAVA_DIR="$(dirname "$JAVA_FILE")" 82*37f5703cSAndroid Build Coastguard Worker # Generate the java file from the antlr file 83*37f5703cSAndroid Build Coastguard Worker run_antlr -verbose -fo "$JAVA_DIR" "$G_FILE" 84*37f5703cSAndroid Build Coastguard Worker 85*37f5703cSAndroid Build Coastguard Worker # delete trailing space from end of each line to make gerrit happy 86*37f5703cSAndroid Build Coastguard Worker sed 's/[ ]*$//' "$JAVA_FILE" > "$JAVA_FILE.tmp" 87*37f5703cSAndroid Build Coastguard Worker [[ -f "$JAVA_FILE" ]] && rm "$JAVA_FILE" 88*37f5703cSAndroid Build Coastguard Worker mv "$JAVA_FILE.tmp" "$JAVA_FILE" 89*37f5703cSAndroid Build Coastguard Worker 90*37f5703cSAndroid Build Coastguard Worker echo "DONE" 91*37f5703cSAndroid Build Coastguard Worker echo "" 92*37f5703cSAndroid Build Coastguard Worker echo "" 93*37f5703cSAndroid Build Coastguard Worker} 94*37f5703cSAndroid Build Coastguard Worker 95*37f5703cSAndroid Build Coastguard Workerfunction cleanup_tokens { 96*37f5703cSAndroid Build Coastguard Worker local JAVA_FILE="$1" 97*37f5703cSAndroid Build Coastguard Worker 98*37f5703cSAndroid Build Coastguard Worker # delete the tokens file, they are not necessary to actually build from Android.mk 99*37f5703cSAndroid Build Coastguard Worker local TOKEN_FILE="${JAVA_FILE%%\.java}.tokens" 100*37f5703cSAndroid Build Coastguard Worker [[ -f "$TOKEN_FILE" ]] && rm "$TOKEN_FILE" 101*37f5703cSAndroid Build Coastguard Worker} 102*37f5703cSAndroid Build Coastguard Worker 103*37f5703cSAndroid Build Coastguard Workergenerate_file "$SCRIPT_DIR/src/main/java/com/android/tools/smali/smali/smaliParser.java" "$SCRIPT_DIR/src/main/antlr/smaliParser.g" 104*37f5703cSAndroid Build Coastguard Workergenerate_file "$SCRIPT_DIR/src/main/java/com/android/tools/smali/smali/smaliTreeWalker.java" "$SCRIPT_DIR/src/main/antlr/smaliTreeWalker.g" 105*37f5703cSAndroid Build Coastguard Worker 106*37f5703cSAndroid Build Coastguard Worker# Clean up the tokens, no longer necessary once the tree walker is generated 107*37f5703cSAndroid Build Coastguard Workercleanup_tokens "$SCRIPT_DIR/src/main/java/com/android/tools/smali/smali/smaliParser.java" 108*37f5703cSAndroid Build Coastguard Workercleanup_tokens "$SCRIPT_DIR/src/main/java/com/android/tools/smali/smali/smaliTreeWalker.java" 109*37f5703cSAndroid Build Coastguard Worker 110*37f5703cSAndroid Build Coastguard Worker# Uncomment to run interactively 111*37f5703cSAndroid Build Coastguard Worker#run_antlr "$@"