aboutsummaryrefslogtreecommitdiff
path: root/smali/runjflex.sh
blob: 783b9122246348ec055c1eed73f2e54f654ffce9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
#
# This script runs jflex generating java code based on .jflex files.
# jflex tool itself resides in external/jflex. At the time of this writing
# it's not a part of jflex manifest and needs to be checked out manually.
#
# The script can be run from anywhere (it does not depend on current working directory)
# Set $JFLEX to overwrite jflex location, if desired
#
# After making any changes to the lexer, the update source file(s) generated by
# this script should be checked in to the repository

# Update when switching to a different version of jflex
EXPECTED_JFLEX_VERSION="1.6.1"
EXPECTED_JFLEX_VERSION_STR="This is JFlex $EXPECTED_JFLEX_VERSION"

# Get the location of this script used to find locations of other things in the tree.
SCRIPT_DIR=`dirname $0`
echo $SCRIPT_DIR

TOP_PATH="$SCRIPT_DIR/../../.."

# Run the java jar when 'JFLEX' is not in the environment
function exec_jar_jflex {
  java -jar "$jflex_location" "$@"
}

# All specifying jflex but fallback to default location
if [ -z  "$JFLEX" ]
then
  # Best effort to find it inside of the gradle cache
  jflex_jar_name="jflex-${EXPECTED_JFLEX_VERSION}.jar"
  jflex_location="$(find $HOME/.gradle/caches/artifacts-* -name "$jflex_jar_name" | head -n 1)"
  if [ -z $jflex_location ]; then
    echo "ERROR: Could not find jflex location, consider setting the \$JFLEX variable to point to it"
    exit 1
  fi
  JFLEX=exec_jar_jflex
fi

JFLEX_VERSION=`"$JFLEX" --version`

if [ "$JFLEX_VERSION" = "" ]
then
  echo "ERROR: Failed to execute jflex at \"$JFLEX\""
  exit 1
fi

if [ "$EXPECTED_JFLEX_VERSION_STR" != "$JFLEX_VERSION" ]
then
  echo "ERROR: Wrong version of jflex: \"$JFLEX_VERSION\". Expected: \"$EXPECTED_JFLEX_VERSION_STR\""
  exit 1
fi

JAVA_FILE=$SCRIPT_DIR/src/main/java/org/jf/smali/smaliFlexLexer.java
rm -f "$JAVA_FILE"

"$JFLEX" --nobak -d "$SCRIPT_DIR/src/main/java/org/jf/smali" "$SCRIPT_DIR/src/main/jflex/smaliLexer.jflex"

# delete trailing space from end of each line to make gerrit happy
sed 's/[ ]*$//' "$JAVA_FILE" > "$JAVA_FILE.tmp"
rm "$JAVA_FILE"
mv "$JAVA_FILE.tmp" "$JAVA_FILE"