blob: b1941a4f2a0adcce181636007fe195b60ef5db98 [file] [log] [blame]
mikaelpeltier4d1fcc42015-04-07 11:15:06 +02001#!/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#
Yohann Roussel1b697cb2015-04-15 15:01:33 +020017# v 1.1-a11
18#
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020019set -o nounset
20umask 077
21
22#
23# Settings
24#
25LOCAL_SETTING="$HOME/.jack"
26TMPDIR=${TMPDIR:=/tmp}
27SERVER_DIR=$TMPDIR/jack-$USER
28
29#
30# Load local settings
31#
32source "$LOCAL_SETTING" 2>/dev/null
33
34#
35# Create or update local settings if needed
36#
37if [[ ! -f "$LOCAL_SETTING" || $SETTING_VERSION -lt 2 ]]; then
38 echo "Writing local settings in" $LOCAL_SETTING
39 cat >"$LOCAL_SETTING.$$" <<-EOT
40 # Server settings
41 SERVER=${SERVER:=true}
42 SERVER_PORT_SERVICE=${SERVER_PORT_SERVICE:=8072}
43 SERVER_PORT_ADMIN=${SERVER_PORT_ADMIN:=8073}
44 SERVER_COUNT=${SERVER_COUNT:=1}
45 SERVER_NB_COMPILE=${SERVER_NB_COMPILE:=4}
46 SERVER_TIMEOUT=${SERVER_TIMEOUT:=60}
47 SERVER_LOG=\${SERVER_LOG:=\$SERVER_DIR/jack-\$SERVER_PORT_SERVICE.log}
48 JACK_VM_COMMAND=\${JACK_VM_COMMAND:=java}
49 # Internal, do not touch
50 SETTING_VERSION=2
51EOT
52 ln -f "$LOCAL_SETTING.$$" "$LOCAL_SETTING"
53 rm "$LOCAL_SETTING.$$"
54 source "$LOCAL_SETTING"
55fi
56
57#
58# If not in server mode, exec jack
59#
60if [ "$SERVER" != "true" ]; then
Yohann Roussel30b5f312015-04-16 18:19:35 +020061 exec $JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.Main "$@"
Yohann Roussel1b697cb2015-04-15 15:01:33 +020062 echo "Cannot succeed to launch Jack" >&2
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020063 exit 255
64fi
65
66#
67# Static setting
68#
69SERVER_PRG="$JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.server.JackSimpleServer"
70
71#
72# Prepare compilation
73#
74JACK_DIR="$SERVER_DIR/jack-task-$$/"
75JACK_OUT="$JACK_DIR/out"
76JACK_ERR="$JACK_DIR/err"
77JACK_CLI="$JACK_DIR/cli"
78JACK_EXIT="$JACK_DIR/exit"
79JACK_PWD="$PWD"
80
Yohann Roussel1b697cb2015-04-15 15:01:33 +020081mkdir "$SERVER_DIR" 2>/dev/null
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020082
83# Cleanup
84trap 'rm -f "$JACK_OUT" "$JACK_ERR" "$JACK_CLI" "$JACK_EXIT" 2>/dev/null; rmdir "$JACK_DIR" 2>/dev/null' EXIT
85
86set -o errexit
87
88# Create fifos and files for a task
89mkdir "$JACK_DIR"
90mkfifo "$JACK_OUT"
91mkfifo "$JACK_ERR"
92touch "$JACK_CLI" "$JACK_EXIT"
93
94# Try to cleanup if interrupted
Yohann Roussel1b697cb2015-04-15 15:01:33 +020095abort () { kill -9 $PID_OUT $PID_ERR 2>/dev/null; wait $PID_OUT $PID_ERR 2>/dev/null; exit 255; }
96trap 'abort' SIGHUP SIGINT SIGQUIT SIGTERM ERR
97
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020098# Redirect output and error
99cat <"$JACK_OUT" >&1 &
100PID_OUT=$!
101cat <"$JACK_ERR" >&2 &
102PID_ERR=$!
103
104# Prepare the working directory and command line
105echo -n \"$PWD\" "" >"$JACK_CLI"
106for i in "$@"; do
107 echo -n \"$i\" "" >>"$JACK_CLI"
108done
109echo >>"$JACK_CLI"
110
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200111#
112# Launch the compilation
113#
114
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200115set +o errexit
116trap ERR
117
118RETRY_LAUNCH=3
119RETRY_SESSION=3
120DELAY_CONNECT=3
121
122
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200123# Launch compilation
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200124DATE_CONNECT=$(date +%s)
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200125while true; do
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200126 HTTP_CODE=$(curl --fail --silent --data @- --output "$JACK_EXIT" --write-out %{http_code} --connect-timeout 5 --no-proxy 127.0.0.1:$SERVER_PORT_SERVICE http://127.0.0.1:$SERVER_PORT_SERVICE/jack <<< "+ $JACK_OUT $JACK_ERR $JACK_CLI")
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200127 CURL_CODE=$?
128 JACK_CODE=$(cat "$JACK_EXIT")
129 if [ $CURL_CODE -eq 0 ]; then
130 # No problem, let's go
131 break;
132 elif [ $CURL_CODE -eq 7 ]; then
133 # Failed to connect
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200134 if [ $(date +%s) -ge $DATE_CONNECT ]; then
135 if [ $RETRY_LAUNCH -eq 0 ]; then
136 echo "Cannot launch background server" >&2
137 abort
138 else
139 let RETRY_LAUNCH=RETRY_LAUNCH-1
140 echo "Launching background server" $SERVER_PRG
141 $SERVER_PRG $SERVER_PORT_SERVICE $SERVER_PORT_ADMIN $SERVER_COUNT $SERVER_NB_COMPILE $SERVER_TIMEOUT >>$SERVER_LOG 2>&1 &
142 # New server, let's try a bit to connect
143 let DATE_CONNECT=$(date +%s)+$DELAY_CONNECT;
144 fi
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200145 else
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200146 sleep 0.2 2>/dev/null
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200147 fi
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200148 # Trying with a new connection, let's retry session 3 times max
149 RETRY_SESSION=3
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200150 elif [ $CURL_CODE -eq 22 ]; then
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200151 # Http code not OK, let's decode and abort
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200152 if [ $HTTP_CODE -eq 401 ]; then
153 # 401: Unauthorized
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200154 echo "Security problem, see server log ($SERVER_LOG)" >&2
155 abort
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200156 elif [ $HTTP_CODE -eq 400 ]; then
157 # 400: Bad request
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200158 echo "Bad request, see server log ($SERVER_LOG)" >&2
159 abort
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200160 else
161 # Other
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200162 echo "Internal unknown error ($HTTP_CODE), try other ports in ~/.jack, or see server log ($SERVER_LOG)" >&2
163 abort
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200164 fi
165 else
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200166 # In case of partial, timeout, empty respond, network error, let's retry
167 if [ $RETRY_SESSION -eq 0 ]; then
168 echo "Communication error with server ($CURL_CODE)" >&2
169 abort
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200170 else
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200171 let RETRY_SESSION=RETRY_SESSION-1
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200172 fi
173 fi
174done
175
176# Wait for termination
177wait $PID_OUT
178wait $PID_ERR
179
180# Exit
181exit $JACK_CODE