Import Jack and Jill brest-1.1-a9

Jack version: UJZ02A 6e5e84331939f8d031ac68225a5f4cbf5356c584
Jill version: UJZ02A 5c94b46ffaa674064fac6cdd8e8f0e9b459d6f9a

Change-Id: I22dba94b3759f2150dc84386315a9b61e851f3f4
diff --git a/tools/jack b/tools/jack
new file mode 100755
index 0000000..ed6968c
--- /dev/null
+++ b/tools/jack
@@ -0,0 +1,166 @@
+#!/bin/bash
+#
+# Copyright (C) 2015 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+set -o nounset
+umask 077
+
+#
+# Settings
+#
+LOCAL_SETTING="$HOME/.jack"
+TMPDIR=${TMPDIR:=/tmp}
+SERVER_DIR=$TMPDIR/jack-$USER
+
+#
+# Load local settings
+#
+source "$LOCAL_SETTING" 2>/dev/null
+
+#
+# Create or update local settings if needed
+#
+if [[ ! -f "$LOCAL_SETTING" || $SETTING_VERSION -lt 2 ]]; then
+  echo "Writing local settings in" $LOCAL_SETTING
+  cat >"$LOCAL_SETTING.$$" <<-EOT
+	# Server settings
+	SERVER=${SERVER:=true}
+	SERVER_PORT_SERVICE=${SERVER_PORT_SERVICE:=8072}
+	SERVER_PORT_ADMIN=${SERVER_PORT_ADMIN:=8073}
+	SERVER_COUNT=${SERVER_COUNT:=1}
+	SERVER_NB_COMPILE=${SERVER_NB_COMPILE:=4}
+	SERVER_TIMEOUT=${SERVER_TIMEOUT:=60}
+	SERVER_LOG=\${SERVER_LOG:=\$SERVER_DIR/jack-\$SERVER_PORT_SERVICE.log}
+	JACK_VM_COMMAND=\${JACK_VM_COMMAND:=java}
+	# Internal, do not touch
+	SETTING_VERSION=2
+EOT
+  ln -f "$LOCAL_SETTING.$$" "$LOCAL_SETTING"
+  rm "$LOCAL_SETTING.$$"
+  source "$LOCAL_SETTING"
+fi
+
+#
+# If not in server mode, exec jack
+#
+if [ "$SERVER" != "true" ]; then
+  exec $JACK_VM_COMMAND -jar $JACK_JAR "$@"
+  exit 255
+fi
+
+#
+# Static setting
+#
+SERVER_PRG="$JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.server.JackSimpleServer"
+
+#
+# Prepare compilation
+#
+JACK_DIR="$SERVER_DIR/jack-task-$$/"
+JACK_OUT="$JACK_DIR/out"
+JACK_ERR="$JACK_DIR/err"
+JACK_CLI="$JACK_DIR/cli"
+JACK_EXIT="$JACK_DIR/exit"
+JACK_PWD="$PWD"
+
+mkdir "$SERVER_DIR"  2>/dev/null
+
+# Cleanup
+trap 'rm -f "$JACK_OUT" "$JACK_ERR" "$JACK_CLI" "$JACK_EXIT" 2>/dev/null; rmdir "$JACK_DIR" 2>/dev/null' EXIT
+
+set -o errexit
+
+# Create fifos and files for a task
+mkdir  "$JACK_DIR"
+mkfifo "$JACK_OUT"
+mkfifo "$JACK_ERR"
+touch  "$JACK_CLI" "$JACK_EXIT"
+
+# Try to cleanup if interrupted
+trap 'kill -9 $PID_OUT $PID_ERR 2>/dev/null; wait $PID_OUT $PID_ERR 2>/dev/null; exit 255' SIGHUP SIGINT SIGQUIT SIGTERM ERR
+# Redirect output and error
+cat <"$JACK_OUT" >&1 &
+PID_OUT=$!
+cat <"$JACK_ERR" >&2 &
+PID_ERR=$!
+
+# Prepare the working directory and command line
+echo -n \"$PWD\" "" >"$JACK_CLI"
+for i in "$@"; do
+  echo -n \"$i\" "" >>"$JACK_CLI"
+done
+echo >>"$JACK_CLI"
+
+set +o errexit
+trap ERR
+
+#
+# Launch the compilation
+#
+
+# Launch compilation
+RETRY=3
+while true; do
+  HTTP_CODE=$(curl --fail --silent --data @- --output "$JACK_EXIT" --write-out %{http_code} http://127.0.0.1:$SERVER_PORT_SERVICE/jack <<< "+ $JACK_OUT $JACK_ERR $JACK_CLI")
+  CURL_CODE=$?
+  JACK_CODE=$(cat "$JACK_EXIT")
+  if [ $CURL_CODE -eq 0 ]; then
+    # No problem, let's go
+    break;
+  elif [ $CURL_CODE -eq 7 ]; then
+    # Failed to connect
+    if [ $RETRY -eq 0 ]; then
+      echo "Cannot launch background server"
+      kill -QUIT $$
+    else
+      # Launch the server
+      echo "Launching background server" $SERVER_PRG
+      $SERVER_PRG $SERVER_PORT_SERVICE $SERVER_PORT_ADMIN $SERVER_COUNT $SERVER_NB_COMPILE $SERVER_TIMEOUT >>$SERVER_LOG 2>&1 &
+      let RETRY=RETRY-1
+      sleep 3
+    fi
+  elif  [ $CURL_CODE -eq 22 ]; then
+    # Http code not OK, let's decode
+    if [ $HTTP_CODE -eq 401 ]; then
+      # 401: Unauthorized
+      echo "Security problem, see server log" >&2
+      kill -QUIT $$
+    elif [ $HTTP_CODE -eq 400 ]; then
+      # 400: Bad request
+      echo "Bad request, see server log" >&2
+      kill -QUIT $$
+    else
+      # Other
+      echo "Internal unknown error, see server log" >&2
+      kill -QUIT $$
+    fi
+  else
+    # In case of partial, timeout, empty, network error, let's retry
+    if [ $RETRY -eq 0 ]; then
+      echo "Communication error with server, error code " $CURL_CODE
+      kill -QUIT $$
+    else
+      let RETRY=RETRY-1
+      sleep 1
+    fi
+  fi
+done
+
+# Wait for termination
+wait $PID_OUT
+wait $PID_ERR
+
+# Exit
+exit $JACK_CODE