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