1#!/bin/bash
2#
3# This script runs jflex generating java code based on .jflex files.
4# jflex tool itself resides in external/jflex. At the time of this writing
5# it's not a part of jflex manifest and needs to be checked out manually.
6#
7# The script can be run from anywhere (it does not depend on current working directory)
8# Set $JFLEX to overwrite jflex location, if desired
9#
10# After making any changes to the lexer, the update source file(s) generated by
11# this script should be checked in to the repository
12
13# Update when switching to a different version of jflex
14EXPECTED_JFLEX_VERSION="1.6.1"
15EXPECTED_JFLEX_VERSION_STR="This is JFlex $EXPECTED_JFLEX_VERSION"
16
17# Get the location of this script used to find locations of other things in the tree.
18SCRIPT_DIR=`dirname $0`
19echo $SCRIPT_DIR
20
21TOP_PATH="$SCRIPT_DIR/../../.."
22
23# Run the java jar when 'JFLEX' is not in the environment
24function exec_jar_jflex {
25  java -jar "$jflex_location" "$@"
26}
27
28# All specifying jflex but fallback to default location
29if [ -z  "$JFLEX" ]
30then
31  # Best effort to find it inside of the gradle cache
32  jflex_jar_name="jflex-${EXPECTED_JFLEX_VERSION}.jar"
33  jflex_location="$(find $HOME/.gradle/caches/artifacts-* -name "$jflex_jar_name" | head -n 1)"
34  if [ -z $jflex_location ]; then
35    echo "ERROR: Could not find jflex location, consider setting the \$JFLEX variable to point to it"
36    exit 1
37  fi
38  JFLEX=exec_jar_jflex
39fi
40
41JFLEX_VERSION=`"$JFLEX" --version`
42
43if [ "$JFLEX_VERSION" = "" ]
44then
45  echo "ERROR: Failed to execute jflex at \"$JFLEX\""
46  exit 1
47fi
48
49if [ "$EXPECTED_JFLEX_VERSION_STR" != "$JFLEX_VERSION" ]
50then
51  echo "ERROR: Wrong version of jflex: \"$JFLEX_VERSION\". Expected: \"$EXPECTED_JFLEX_VERSION_STR\""
52  exit 1
53fi
54
55JAVA_FILE=$SCRIPT_DIR/src/main/java/org/jf/smali/smaliFlexLexer.java
56rm -f "$JAVA_FILE"
57
58"$JFLEX" --nobak -d "$SCRIPT_DIR/src/main/java/org/jf/smali" "$SCRIPT_DIR/src/main/jflex/smaliLexer.jflex"
59
60# delete trailing space from end of each line to make gerrit happy
61sed 's/[ ]*$//' "$JAVA_FILE" > "$JAVA_FILE.tmp"
62rm "$JAVA_FILE"
63mv "$JAVA_FILE.tmp" "$JAVA_FILE"
64