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-eng 18 # 19 set -o nounset 20 umask 077 21 22 # 23 # Settings 24 # 25 JACK_VERSION=${JACK_VERSION:=4.31.CANDIDATE} 26 JACK_HOME="${JACK_HOME:=$HOME/.jack-server}" 27 JACK_CLIENT_SETTING="${JACK_CLIENT_SETTING:=$HOME/.jack-settings}" 28 TMPDIR=${TMPDIR:=/tmp} 29 # This is half the timeout since the script will make a second attempt collecting debugs when 30 # the first attempt fails on connection timeout 31 JACK_CONNECTION_TIMEOUT=150 32 JACK_EXTRA_CURL_OPTIONS=${JACK_EXTRA_CURL_OPTIONS:=} 33 JACK_ASSERTION_ENABLED="${JACK_ASSERTION_ENABLED:=false}" 34 35 36 abort () { exit 255; } 37 38 39 # 40 # Load client settings 41 # 42 if [ -f "$JACK_CLIENT_SETTING" ]; then 43 source "$JACK_CLIENT_SETTING" 44 else 45 echo "Cannot find settings at '$JACK_CLIENT_SETTING'" >&2 46 abort 47 fi 48 49 50 JACK_SERVER=${JACK_SERVER:=true} 51 JACK_MAIN_COMMAND=${JACK_MAIN_COMMAND:="java -Djava.io.tmpdir=$TMPDIR -Dfile.encoding=UTF-8 -XX:+TieredCompilation"} 52 JACK_REPOSITORY=${JACK_REPOSITORY:=} 53 54 55 # 56 # If not in server mode, exec jack 57 # 58 if [ "$JACK_SERVER" != "true" ]; then 59 if [ -z "$JACK_REPOSITORY" ]; then 60 echo "Running Jack without Jack server requires definition of JACK_REPOSITORY" >&2 61 abort 62 fi 63 JACK_JAR=$JACK_REPOSITORY/jack-$JACK_VERSION.jar 64 if [ ! -r "$JACK_JAR" ]; then 65 echo "Jack jar \"$JACK_JAR\" is not readable" >&2 66 abort 67 fi 68 69 70 exec $JACK_MAIN_COMMAND -jar $JACK_JAR "$@" 71 echo "Cannot succeed to launch Jack without Jack server" >&2 72 abort 73 fi 74 75 76 # 77 # Prepare compilation 78 # 79 JACK_PWD="$PWD" 80 JACK_EXIT="$TMPDIR/jack-task-$USER-$$-exit" 81 82 # Cleanup 83 trap 'rm -f "$JACK_EXIT" 2>/dev/null;' EXIT 84 85 set -o errexit 86 87 # Check that we can create temp file for exit status 88 rm -rf "$JACK_EXIT" 89 echo "" > "$JACK_EXIT" 90 91 # 92 # Launch the compilation 93 # 94 95 set +o errexit 96 trap ERR 97 98 # put arguments in a non array variable 99 ARGS="" 100 for i in "$@"; do 101 ARGS="$ARGS $i" 102 done 103 104 105 CURRENT_CHARSET=$(locale charmap) 106 if [ -z "$CURRENT_CHARSET" ]; then 107 CHARSET_ARGUMENT= 108 else 109 CHARSET_ARGUMENT=";charset=$CURRENT_CHARSET" 110 fi 111 112 # Check base64 availability 113 BASE64_CHECK=$((echo amFjaw==;echo LXNlcnZlcg==) | base64 --decode 2>&1) 114 115 sendRequest() { 116 RETRY_OPTION=$1 117 shift 118 119 # Launch compilation 120 exec 3>&1 121 exec 4>&2 122 if [ "$BASE64_CHECK" = jack-server ]; then 123 HTTP_CODE=$(curl -f $JACK_EXTRA_CURL_OPTIONS $RETRY_OPTION \ 124 --cert "${JACK_HOME}/client.pem" \ 125 --cacert "${JACK_HOME}/server.pem" \ 126 --output >(tee >(sed -n -e 's/^E|\(.*\)$/\1/p' | base64 --decode >&4 ) | tee >(sed -n -e 's/^X|\(.*\)$/\1/p' >>$JACK_EXIT) | sed -n -e 's/^O|\(.*\)$/\1/p' | base64 --decode >&3) \ 127 --no-buffer --write-out '%{http_code}' --silent --connect-timeout $JACK_CONNECTION_TIMEOUT \ 128 -X POST \ 129 -H "Accept: application/vnd.jack.command-out-base64;version=1$CHARSET_ARGUMENT" \ 130 -F "cli=$ARGS;type=text/plain$CHARSET_ARGUMENT" \ 131 -F "version=$JACK_VERSION;type=application/vnd.jack.select-exact;version=1" \ 132 -F "pwd=$JACK_PWD;type=text/plain$CHARSET_ARGUMENT" \ 133 -F "assert=$JACK_ASSERTION_ENABLED;type=text/plain$CHARSET_ARGUMENT" \ 134 --noproxy ${SERVER_HOST} \ 135 https://${SERVER_HOST}:$SERVER_PORT_SERVICE/jack \ 136 ) 137 else 138 HTTP_CODE=$(curl -f $JACK_EXTRA_CURL_OPTIONS $RETRY_OPTION \ 139 --cert "${JACK_HOME}/client.pem" \ 140 --cacert "${JACK_HOME}/server.pem" \ 141 --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) \ 142 --no-buffer --write-out '%{http_code}' --silent --connect-timeout $JACK_CONNECTION_TIMEOUT \ 143 -X POST \ 144 -H "Accept: application/vnd.jack.command-out;version=1$CHARSET_ARGUMENT" \ 145 -F "cli=$ARGS;type=text/plain$CHARSET_ARGUMENT" \ 146 -F "version=$JACK_VERSION;type=application/vnd.jack.select-exact;version=1" \ 147 -F "pwd=$JACK_PWD;type=text/plain$CHARSET_ARGUMENT" \ 148 -F "assert=$JACK_ASSERTION_ENABLED;type=text/plain$CHARSET_ARGUMENT" \ 149 --noproxy ${SERVER_HOST} \ 150 https://${SERVER_HOST}:$SERVER_PORT_SERVICE/jack \ 151 ) 152 fi 153 CURL_CODE=$? 154 exec 3>&- 155 exec 4>&- 156 157 set -o errexit 158 159 if [ $CURL_CODE -eq 0 ]; then 160 # No problem, let's go 161 JACK_CODE=$(cat "$JACK_EXIT") 162 exit $JACK_CODE 163 elif [ $CURL_CODE -eq 7 ]; then 164 # Failed to connect 165 echo "No Jack server running. Try 'jack-admin start-server'" >&2 166 abort 167 elif [ $CURL_CODE -eq 28 ]; then 168 if [ "$RETRY_OPTION" == "" ]; then 169 echo "Connection to the Jack server timeout, retrying with debug" 170 sendRequest -v $@ 171 else 172 echo "Connection to the Jack server timeout" >&2 173 abort 174 fi 175 elif [ $CURL_CODE -eq 35 ]; then 176 if [ "$RETRY_OPTION" == "" ]; then 177 echo "SSL error when connecting to the Jack server, retrying with debug" 178 sendRequest -v $@ 179 else 180 echo "SSL error when connecting to the Jack server. Try 'jack-diagnose'" >&2 181 abort 182 fi 183 elif [ $CURL_CODE -eq 58 ]; then 184 echo "Failed to contact Jack server: Problem reading ${JACK_HOME}/client.pem. Try 'jack-diagnose'" >&2 185 abort 186 elif [ $CURL_CODE -eq 60 ]; then 187 echo "Failed to authenticate Jack server certificate. Try 'jack-diagnose'" >&2 188 abort 189 elif [ $CURL_CODE -eq 77 ]; then 190 echo "Failed to contact Jack server: Problem reading ${JACK_HOME}/server.pem. Try 'jack-diagnose'" >&2 191 abort 192 elif [ $CURL_CODE -eq 22 ]; then 193 # Http code not OK, let's decode and abort 194 if [ $HTTP_CODE -eq 400 ]; then 195 # 400: Bad request 196 echo "Bad request, see server log" >&2 197 abort 198 else 199 # Other 200 echo "Internal unknown error ($HTTP_CODE), try 'jack-diagnose' or see Jack server log" >&2 201 abort 202 fi 203 else 204 echo "Communication error with Jack server ($CURL_CODE). Try 'jack-diagnose'" >&2 205 abort 206 fi 207 } 208 209 sendRequest "" $@ 210