blob: ed6968c893b858e09a6e793d8436263b27f25dc8 [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#
17set -o nounset
18umask 077
19
20#
21# Settings
22#
23LOCAL_SETTING="$HOME/.jack"
24TMPDIR=${TMPDIR:=/tmp}
25SERVER_DIR=$TMPDIR/jack-$USER
26
27#
28# Load local settings
29#
30source "$LOCAL_SETTING" 2>/dev/null
31
32#
33# Create or update local settings if needed
34#
35if [[ ! -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
49EOT
50 ln -f "$LOCAL_SETTING.$$" "$LOCAL_SETTING"
51 rm "$LOCAL_SETTING.$$"
52 source "$LOCAL_SETTING"
53fi
54
55#
56# If not in server mode, exec jack
57#
58if [ "$SERVER" != "true" ]; then
59 exec $JACK_VM_COMMAND -jar $JACK_JAR "$@"
60 exit 255
61fi
62
63#
64# Static setting
65#
66SERVER_PRG="$JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.server.JackSimpleServer"
67
68#
69# Prepare compilation
70#
71JACK_DIR="$SERVER_DIR/jack-task-$$/"
72JACK_OUT="$JACK_DIR/out"
73JACK_ERR="$JACK_DIR/err"
74JACK_CLI="$JACK_DIR/cli"
75JACK_EXIT="$JACK_DIR/exit"
76JACK_PWD="$PWD"
77
78mkdir "$SERVER_DIR" 2>/dev/null
79
80# Cleanup
81trap 'rm -f "$JACK_OUT" "$JACK_ERR" "$JACK_CLI" "$JACK_EXIT" 2>/dev/null; rmdir "$JACK_DIR" 2>/dev/null' EXIT
82
83set -o errexit
84
85# Create fifos and files for a task
86mkdir "$JACK_DIR"
87mkfifo "$JACK_OUT"
88mkfifo "$JACK_ERR"
89touch "$JACK_CLI" "$JACK_EXIT"
90
91# Try to cleanup if interrupted
92trap '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
94cat <"$JACK_OUT" >&1 &
95PID_OUT=$!
96cat <"$JACK_ERR" >&2 &
97PID_ERR=$!
98
99# Prepare the working directory and command line
100echo -n \"$PWD\" "" >"$JACK_CLI"
101for i in "$@"; do
102 echo -n \"$i\" "" >>"$JACK_CLI"
103done
104echo >>"$JACK_CLI"
105
106set +o errexit
107trap ERR
108
109#
110# Launch the compilation
111#
112
113# Launch compilation
114RETRY=3
115while 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
159done
160
161# Wait for termination
162wait $PID_OUT
163wait $PID_ERR
164
165# Exit
166exit $JACK_CODE