Home | History | Annotate | Download | only in tools
      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 # Version: 1.1-mr2
     18 #
     19 set -o nounset
     20 umask 077
     21 
     22 #
     23 # Settings
     24 #
     25 LOCAL_SETTING="$HOME/.jack"
     26 TMPDIR=${TMPDIR:=/tmp}
     27 SERVER_DIR=$TMPDIR/jack-$USER
     28 
     29 #
     30 # Load local settings
     31 #
     32 source "$LOCAL_SETTING" 2>/dev/null
     33 
     34 #
     35 # Create or update local settings if needed
     36 #
     37 if [[ ! -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
     51 EOT
     52   ln -f "$LOCAL_SETTING.$$" "$LOCAL_SETTING"
     53   rm "$LOCAL_SETTING.$$"
     54   source "$LOCAL_SETTING"
     55 fi
     56 
     57 #
     58 # If not in server mode, exec jack
     59 #
     60 if [ "$SERVER" != "true" ]; then
     61   exec $JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.Main "$@"
     62   echo "ERROR: Cannot succeed to launch Jack without Jack server" >&2
     63   exit 255
     64 fi
     65 
     66 #
     67 # Static setting
     68 #
     69 SERVER_PRG="$JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.server.JackSimpleServer"
     70 
     71 #
     72 # Prepare compilation
     73 #
     74 JACK_DIR="$SERVER_DIR/jack-task-$$/"
     75 JACK_OUT="$JACK_DIR/out"
     76 JACK_ERR="$JACK_DIR/err"
     77 JACK_CLI="$JACK_DIR/cli"
     78 JACK_EXIT="$JACK_DIR/exit"
     79 JACK_PWD="$PWD"
     80 
     81 mkdir "$SERVER_DIR" 2>/dev/null
     82 
     83 # Cleanup
     84 trap 'rm -f "$JACK_OUT" "$JACK_ERR" "$JACK_CLI" "$JACK_EXIT" 2>>$SERVER_LOG; rmdir "$JACK_DIR" 2>>$SERVER_LOG' EXIT
     85 
     86 set -o errexit
     87 
     88 # Create fifos and files for a task
     89 rm -rf "$JACK_DIR"
     90 mkdir  "$JACK_DIR"
     91 mkfifo "$JACK_OUT"
     92 mkfifo "$JACK_ERR"
     93 touch  "$JACK_CLI" "$JACK_EXIT"
     94 
     95 # Try to cleanup if interrupted
     96 abort () { echo $(uptime) >>$SERVER_LOG; kill -9 $PID_OUT $PID_ERR 2>>$SERVER_LOG; wait $PID_OUT $PID_ERR 2>>$SERVER_LOG; exit 255; }
     97 trap 'abort' SIGHUP SIGINT SIGQUIT SIGTERM ERR
     98 
     99 # Redirect output and error
    100 cat <"$JACK_OUT" >&1 &
    101 PID_OUT=$!
    102 cat <"$JACK_ERR" >&2 &
    103 PID_ERR=$!
    104 
    105 # Prepare the working directory and command line
    106 echo -n \"$PWD\" "" >"$JACK_CLI"
    107 for i in "$@"; do
    108   echo -n \"$i\" "" >>"$JACK_CLI"
    109 done
    110 echo >>"$JACK_CLI"
    111 
    112 #
    113 # Launch the compilation
    114 #
    115 
    116 set +o errexit
    117 trap ERR
    118 
    119 RETRY_LAUNCH=1
    120 RETRY_SESSION=3
    121 DELAY_CONNECT=30
    122 
    123 
    124 # Launch compilation
    125 DATE_CONNECT=$(date +%s)
    126 while true; do
    127   CURL_TIME=$(date +%H:%M:%S)
    128   HTTP_CODE=$(curl --fail --silent --data @- --output "$JACK_EXIT" --write-out %{http_code} --connect-timeout 10 --no-proxy 127.0.0.1:$SERVER_PORT_SERVICE http://127.0.0.1:$SERVER_PORT_SERVICE/jack <<< "+ $JACK_OUT $JACK_ERR $JACK_CLI")
    129   CURL_CODE=$?
    130   JACK_CODE=$(cat "$JACK_EXIT")
    131   echo "CURL: $$ - $CURL_TIME - $CURL_CODE - $HTTP_CODE - $JACK_CODE" >>$SERVER_LOG
    132   if [ $CURL_CODE -eq 0 ]; then
    133     # No problem, let's go
    134     break;
    135   elif [ $CURL_CODE -eq 7 ]; then
    136     # Failed to connect
    137     if [ $(date +%s) -ge $DATE_CONNECT ]; then
    138       if [ $RETRY_LAUNCH -eq 0 ]; then
    139         echo "ERROR: Cannot launch Jack server" >&2
    140         abort
    141       else
    142         let RETRY_LAUNCH=RETRY_LAUNCH-1
    143         echo "Launching background server" $SERVER_PRG
    144         $SERVER_PRG $SERVER_PORT_SERVICE $SERVER_PORT_ADMIN $SERVER_COUNT $SERVER_NB_COMPILE $SERVER_TIMEOUT >>$SERVER_LOG 2>&1 &
    145         # New server, let's try a bit to connect
    146         let DATE_CONNECT=$(date +%s)+$DELAY_CONNECT;
    147       fi
    148     else
    149       sleep 0.2 2>/dev/null
    150     fi
    151     # Trying with a new connection, let's retry session 3 times max
    152     RETRY_SESSION=3
    153   elif  [ $CURL_CODE -eq 22 ]; then
    154     # Http code not OK, let's decode and abort
    155     if [ $HTTP_CODE -eq 401 ]; then
    156       # 401: Unauthorized
    157       echo "ERROR: Security problem, see Jack server log ($SERVER_LOG)" >&2
    158       abort
    159     elif [ $HTTP_CODE -eq 400 ]; then
    160       # 400: Bad request
    161       echo "ERROR: Bad request, see Jack server log ($SERVER_LOG)" >&2
    162       abort
    163     else
    164       # Other
    165       echo "ERROR: Internal unknown error ($HTTP_CODE), try other ports in ~/.jack, or see Jack server log ($SERVER_LOG)" >&2
    166       abort
    167     fi
    168   else
    169     # In case of partial, timeout, empty respond, network error, let's retry
    170     if [ $RETRY_SESSION -eq 0 ]; then
    171       echo "ERROR: Communication error with Jack server ($CURL_CODE)" >&2
    172       abort
    173     else
    174       let RETRY_SESSION=RETRY_SESSION-1
    175     fi
    176   fi
    177 done
    178 
    179 # Wait for termination
    180 wait $PID_OUT
    181 wait $PID_ERR
    182 
    183 # Exit
    184 exit $JACK_CODE
    185