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.3-a8
     18 #
     19 set -o nounset
     20 umask 077
     21 
     22 #
     23 # Settings
     24 #
     25 JACK_VERSION=${JACK_VERSION:=3.36.CANDIDATE}
     26 JACK_HOME="${JACK_HOME:=$HOME/.jack-server}"
     27 CLIENT_SETTING="${CLIENT_SETTING:=$HOME/.jack-settings}"
     28 TMPDIR=${TMPDIR:=/tmp}
     29 CLIENT_TMP_DIR=$TMPDIR/jack-$USER
     30 JACK_CONNECTION_TIMEOUT=300
     31 JACK_EXTRA_CURL_OPTIONS=${JACK_EXTRA_CURL_OPTIONS:=}
     32 
     33 #
     34 # Load client settings
     35 #
     36 if [ -f "$CLIENT_SETTING" ]; then
     37   source "$CLIENT_SETTING"
     38 fi
     39 
     40 #
     41 # Create or update client settings if needed
     42 #
     43 if [[ ! -f "$CLIENT_SETTING" || $SETTING_VERSION -lt 4 ]]; then
     44   echo "Writing client settings in" $CLIENT_SETTING
     45   cat >"$CLIENT_SETTING.$$" <<-EOT
     46 	# Server settings
     47 	SERVER_HOST=${SERVER_HOST:=127.0.0.1}
     48 	SERVER_PORT_SERVICE=${SERVER_PORT_SERVICE:=8076}
     49 	SERVER_PORT_ADMIN=${SERVER_PORT_ADMIN:=8077}
     50 
     51 	# Internal, do not touch
     52 	SETTING_VERSION=4
     53 EOT
     54   ln -f "$CLIENT_SETTING.$$" "$CLIENT_SETTING"
     55   rm "$CLIENT_SETTING.$$"
     56   source "$CLIENT_SETTING"
     57 fi
     58 
     59 
     60 abort () { exit 255; }
     61 
     62 JACK_SERVER=${JACK_SERVER:=true}
     63 JACK_MAIN_COMMAND=${JACK_MAIN_COMMAND:="java -Djava.io.tmpdir=$TMPDIR -Dfile.encoding=UTF-8 -XX:+TieredCompilation"}
     64 JACK_REPOSITORY=${JACK_REPOSITORY:=}
     65 
     66 
     67 #
     68 # If not in server mode, exec jack
     69 #
     70 if [ "$JACK_SERVER" != "true" ]; then
     71   if [ -z "$JACK_REPOSITORY" ]; then
     72     echo "Running Jack without Jack server requires definition of JACK_REPOSITORY" >&2
     73     abort
     74   fi
     75   JACK_JAR=$JACK_REPOSITORY/jack-$JACK_VERSION.jar
     76   if [ ! -r "$JACK_JAR" ]; then
     77     echo "Jack jar \"$JACK_JAR\" is not readable" >&2
     78     abort
     79   fi
     80 
     81 
     82   exec $JACK_MAIN_COMMAND -jar $JACK_JAR "$@"
     83   echo "Cannot succeed to launch Jack without Jack server" >&2
     84   abort
     85 fi
     86 
     87 
     88 #
     89 # Prepare compilation
     90 #
     91 JACK_PWD="$PWD"
     92 JACK_DIR="$CLIENT_TMP_DIR/jack-task-$$/"
     93 JACK_EXIT="$JACK_DIR/exit"
     94 
     95 # Cleanup
     96 trap 'rm -f "$JACK_EXIT" 2>/dev/null; rmdir "$JACK_DIR";' EXIT
     97 
     98 mkdir "$CLIENT_TMP_DIR" 2>/dev/null || (exit 0)
     99 set -o errexit
    100 mkdir "$JACK_DIR"
    101 
    102 #
    103 # Launch the compilation
    104 #
    105 
    106 set +o errexit
    107 trap ERR
    108 
    109 # put arguments in a non array variable
    110 ARGS=""
    111 for i in "$@"; do
    112   ARGS="$ARGS $i"
    113 done
    114 
    115 
    116 CURRENT_CHARSET=$(locale charmap)
    117 if [ -z "$CURRENT_CHARSET" ]; then
    118   CHARSET_ARGUMENT=
    119 else
    120   CHARSET_ARGUMENT=";charset=$CURRENT_CHARSET"
    121 fi
    122 
    123 # Launch compilation
    124 exec 3>&1
    125 exec 4>&2
    126 HTTP_CODE=$(curl -f $JACK_EXTRA_CURL_OPTIONS \
    127      --cert "${JACK_HOME}/client.pem" \
    128      --cacert "${JACK_HOME}/server.pem" \
    129      --output >(tee >(sed -n -e 's/^E|\(.*\)$/\1/p' >&4 ) | tee >(sed -n -e 's/^X|\(.*\)$/\1/p' >$JACK_EXIT) | sed -n -e 's/^O|\(.*\)$/\1/p' >&3) \
    130      --no-buffer --write-out '%{http_code}' --silent --connect-timeout $JACK_CONNECTION_TIMEOUT \
    131      -X POST \
    132      -H "Accept: application/vnd.jack.command-out;version=1;charset=$CURRENT_CHARSET" \
    133      -F "cli=$ARGS;type=text/plain;charset=$CURRENT_CHARSET" \
    134      -F "version=$JACK_VERSION;type=application/vnd.jack.select-exact;version=1" \
    135      -F "pwd=$JACK_PWD;type=text/plain$CHARSET_ARGUMENT" \
    136      --noproxy ${SERVER_HOST} \
    137      https://${SERVER_HOST}:$SERVER_PORT_SERVICE/jack \
    138      )
    139 
    140 CURL_CODE=$?
    141 exec 3>&-
    142 exec 4>&-
    143 JACK_CODE=$(cat "$JACK_EXIT")
    144 if [ $CURL_CODE -eq 0 ]; then
    145   # No problem, let's go
    146   exit $JACK_CODE
    147 elif [ $CURL_CODE -eq 7 ]; then
    148   # Failed to connect
    149   echo "No Jack server running. Try 'jack-admin start-server'" >&2
    150   abort
    151 elif [ $CURL_CODE -eq 35 ]; then
    152   echo "SSL error when connecting to the Jack server. Try 'jack-diagnose'" >&2
    153   abort
    154 elif [ $CURL_CODE -eq 58 ]; then
    155   echo "Failed to contact Jack server: Problem reading ${JACK_HOME}/client.pem. Try 'jack-diagnose'" >&2
    156   abort
    157 elif [ $CURL_CODE -eq 60 ]; then
    158   echo "Failed to authenticate Jack server certificate. Try 'jack-diagnose'" >&2
    159   abort
    160  elif [ $CURL_CODE -eq 77 ]; then
    161   echo "Failed to contact Jack server: Problem reading ${JACK_HOME}/server.pem. Try 'jack-diagnose'" >&2
    162   abort
    163 elif  [ $CURL_CODE -eq 22 ]; then
    164   # Http code not OK, let's decode and abort
    165   if [ $HTTP_CODE -eq 400 ]; then
    166     # 400: Bad request
    167     echo "Bad request, see server log" >&2
    168     abort
    169   else
    170     # Other
    171     echo "Internal unknown error ($HTTP_CODE), try 'jack-diagnose' or see Jack server log" >&2
    172     abort
    173   fi
    174 else
    175   echo "Communication error with Jack server ($CURL_CODE). Try 'jack-diagnose'" >&2
    176   abort
    177 fi
    178