mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2015 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | set -o nounset |
| 18 | umask 077 |
| 19 | |
| 20 | # |
| 21 | # Settings |
| 22 | # |
| 23 | LOCAL_SETTING="$HOME/.jack" |
| 24 | TMPDIR=${TMPDIR:=/tmp} |
| 25 | SERVER_DIR=$TMPDIR/jack-$USER |
| 26 | |
| 27 | # |
| 28 | # Load local settings |
| 29 | # |
| 30 | source "$LOCAL_SETTING" 2>/dev/null |
| 31 | |
| 32 | # |
| 33 | # Create or update local settings if needed |
| 34 | # |
| 35 | if [[ ! -f "$LOCAL_SETTING" || $SETTING_VERSION -lt 2 ]]; then |
| 36 | echo "Writing local settings in" $LOCAL_SETTING |
| 37 | cat >"$LOCAL_SETTING.$$" <<-EOT |
| 38 | # Server settings |
| 39 | SERVER=${SERVER:=true} |
| 40 | SERVER_PORT_SERVICE=${SERVER_PORT_SERVICE:=8072} |
| 41 | SERVER_PORT_ADMIN=${SERVER_PORT_ADMIN:=8073} |
| 42 | SERVER_COUNT=${SERVER_COUNT:=1} |
| 43 | SERVER_NB_COMPILE=${SERVER_NB_COMPILE:=4} |
| 44 | SERVER_TIMEOUT=${SERVER_TIMEOUT:=60} |
| 45 | SERVER_LOG=\${SERVER_LOG:=\$SERVER_DIR/jack-\$SERVER_PORT_SERVICE.log} |
| 46 | JACK_VM_COMMAND=\${JACK_VM_COMMAND:=java} |
| 47 | # Internal, do not touch |
| 48 | SETTING_VERSION=2 |
| 49 | EOT |
| 50 | ln -f "$LOCAL_SETTING.$$" "$LOCAL_SETTING" |
| 51 | rm "$LOCAL_SETTING.$$" |
| 52 | source "$LOCAL_SETTING" |
| 53 | fi |
| 54 | |
| 55 | # |
| 56 | # If not in server mode, exec jack |
| 57 | # |
| 58 | if [ "$SERVER" != "true" ]; then |
| 59 | exec $JACK_VM_COMMAND -jar $JACK_JAR "$@" |
| 60 | exit 255 |
| 61 | fi |
| 62 | |
| 63 | # |
| 64 | # Static setting |
| 65 | # |
| 66 | SERVER_PRG="$JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.server.JackSimpleServer" |
| 67 | |
| 68 | # |
| 69 | # Prepare compilation |
| 70 | # |
| 71 | JACK_DIR="$SERVER_DIR/jack-task-$$/" |
| 72 | JACK_OUT="$JACK_DIR/out" |
| 73 | JACK_ERR="$JACK_DIR/err" |
| 74 | JACK_CLI="$JACK_DIR/cli" |
| 75 | JACK_EXIT="$JACK_DIR/exit" |
| 76 | JACK_PWD="$PWD" |
| 77 | |
| 78 | mkdir "$SERVER_DIR" 2>/dev/null |
| 79 | |
| 80 | # Cleanup |
| 81 | trap 'rm -f "$JACK_OUT" "$JACK_ERR" "$JACK_CLI" "$JACK_EXIT" 2>/dev/null; rmdir "$JACK_DIR" 2>/dev/null' EXIT |
| 82 | |
| 83 | set -o errexit |
| 84 | |
| 85 | # Create fifos and files for a task |
| 86 | mkdir "$JACK_DIR" |
| 87 | mkfifo "$JACK_OUT" |
| 88 | mkfifo "$JACK_ERR" |
| 89 | touch "$JACK_CLI" "$JACK_EXIT" |
| 90 | |
| 91 | # Try to cleanup if interrupted |
| 92 | 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 |
| 93 | # Redirect output and error |
| 94 | cat <"$JACK_OUT" >&1 & |
| 95 | PID_OUT=$! |
| 96 | cat <"$JACK_ERR" >&2 & |
| 97 | PID_ERR=$! |
| 98 | |
| 99 | # Prepare the working directory and command line |
| 100 | echo -n \"$PWD\" "" >"$JACK_CLI" |
| 101 | for i in "$@"; do |
| 102 | echo -n \"$i\" "" >>"$JACK_CLI" |
| 103 | done |
| 104 | echo >>"$JACK_CLI" |
| 105 | |
| 106 | set +o errexit |
| 107 | trap ERR |
| 108 | |
| 109 | # |
| 110 | # Launch the compilation |
| 111 | # |
| 112 | |
| 113 | # Launch compilation |
| 114 | RETRY=3 |
| 115 | while true; do |
| 116 | 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") |
| 117 | CURL_CODE=$? |
| 118 | JACK_CODE=$(cat "$JACK_EXIT") |
| 119 | if [ $CURL_CODE -eq 0 ]; then |
| 120 | # No problem, let's go |
| 121 | break; |
| 122 | elif [ $CURL_CODE -eq 7 ]; then |
| 123 | # Failed to connect |
| 124 | if [ $RETRY -eq 0 ]; then |
| 125 | echo "Cannot launch background server" |
| 126 | kill -QUIT $$ |
| 127 | else |
| 128 | # Launch the server |
| 129 | echo "Launching background server" $SERVER_PRG |
| 130 | $SERVER_PRG $SERVER_PORT_SERVICE $SERVER_PORT_ADMIN $SERVER_COUNT $SERVER_NB_COMPILE $SERVER_TIMEOUT >>$SERVER_LOG 2>&1 & |
| 131 | let RETRY=RETRY-1 |
| 132 | sleep 3 |
| 133 | fi |
| 134 | elif [ $CURL_CODE -eq 22 ]; then |
| 135 | # Http code not OK, let's decode |
| 136 | if [ $HTTP_CODE -eq 401 ]; then |
| 137 | # 401: Unauthorized |
| 138 | echo "Security problem, see server log" >&2 |
| 139 | kill -QUIT $$ |
| 140 | elif [ $HTTP_CODE -eq 400 ]; then |
| 141 | # 400: Bad request |
| 142 | echo "Bad request, see server log" >&2 |
| 143 | kill -QUIT $$ |
| 144 | else |
| 145 | # Other |
| 146 | echo "Internal unknown error, see server log" >&2 |
| 147 | kill -QUIT $$ |
| 148 | fi |
| 149 | else |
| 150 | # In case of partial, timeout, empty, network error, let's retry |
| 151 | if [ $RETRY -eq 0 ]; then |
| 152 | echo "Communication error with server, error code " $CURL_CODE |
| 153 | kill -QUIT $$ |
| 154 | else |
| 155 | let RETRY=RETRY-1 |
| 156 | sleep 1 |
| 157 | fi |
| 158 | fi |
| 159 | done |
| 160 | |
| 161 | # Wait for termination |
| 162 | wait $PID_OUT |
| 163 | wait $PID_ERR |
| 164 | |
| 165 | # Exit |
| 166 | exit $JACK_CODE |