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_STR="This is JFlex 1.4.3"
15
16# Get the location of this script used to find locations of other things in the tree.
17SCRIPT_DIR=`dirname $0`
18echo $SCRIPT_DIR
19
20TOP_PATH="$SCRIPT_DIR/../../.."
21
22# All specifying jflex but fallback to default location
23if [ -z  "$JFLEX" ]
24then
25  JFLEX=$TOP_PATH/external/jflex/bin/jflex
26fi
27
28JFLEX_VERSION=`"$JFLEX" --version`
29
30if [ "$JFLEX_VERSION" = "" ]
31then
32  echo "ERROR: Failed to execute jflex at \"$JFLEX\""
33  exit 1
34fi
35
36if [ "$EXPECTED_JFLEX_VERSION_STR" != "$JFLEX_VERSION" ]
37then
38  echo "ERROR: Wrong version of jflex: \"$JFLEX_VERSION\". Expected: \"$EXPECTED_JFLEX_VERSION_STR\""
39  exit 1
40fi
41
42JAVA_FILE=$SCRIPT_DIR/src/main/java/org/jf/smali/smaliFlexLexer.java
43rm -f "$JAVA_FILE"
44
45$JFLEX --nobak -d $SCRIPT_DIR/src/main/java/org/jf/smali $SCRIPT_DIR/src/main/jflex/smaliLexer.flex
46
47# delete trailing space from end of each line to make gerrit happy
48sed 's/[ ]*$//' "$JAVA_FILE" > "$JAVA_FILE.tmp"
49rm "$JAVA_FILE"
50mv "$JAVA_FILE.tmp" "$JAVA_FILE"
51