1#!/bin/bash 2 3# Copyright (C) 2021 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 18# A library script to help other scripts run within an Android build environment, or while deployed 19# on a target host. Intended to be used with the `source` bash built-in command. Defines the 20# following environment variables: 21# JAVA_VERSION, RDBG_FLAG, TF_PATH, TRADEFED_OPTS 22# 23# It will react to the following environment variables, if they are set: 24# TF_DEBUG, TRADEFED_OPTS_FILE 25 26 27checkPath() { 28 if ! type -P "$1" &> /dev/null; then 29 >&2 echo "Unable to find $1." 30 exit 1 31 fi; 32} 33 34# All to specify an alternative Java binary, other than the one on PATH 35TF_JAVA="java" 36if [ ! -z "${TF_JAVA_HOME}" ]; then 37 # following similar convention as JAVA_HOME 38 TF_JAVA=${TF_JAVA_HOME}/bin/java 39fi 40 41checkPath ${TF_JAVA} 42 43# check java version 44java_version_string=$(${TF_JAVA} -version 2>&1) 45JAVA_VERSION=$(echo "$java_version_string" | grep 'version [ "]\(1\.8\|9\|11\).*[ "]') 46if [ "${JAVA_VERSION}" == "" ]; then 47 >&2 echo "Wrong java version. 1.8, 9 or 11 is required. Found $java_version_string" 48 >&2 echo "PATH value:" 49 >&2 echo "$PATH" 50 exit 8 51fi 52 53# check if java is above 9 and supports add-opens 54JAVA_VERSION=$(echo "$java_version_string" | grep 'version [ "]\(9\|11\).*[ "]') 55if [ "${JAVA_VERSION}" != "" ]; then 56 ADD_OPENS_FLAG="--add-opens=java.base/java.nio=ALL-UNNAMED --add-opens=java.base/sun.reflect.annotation=ALL-UNNAMED" 57fi 58 59# check debug flag and set up remote debugging 60if [ -n "${TF_DEBUG}" ]; then 61 if [ -z "${TF_DEBUG_PORT}" ]; then 62 TF_DEBUG_PORT=10088 63 fi 64 RDBG_FLAG="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=${TF_DEBUG_PORT}" 65fi 66 67# first try to find TF jars in same dir as this script 68CUR_DIR=$(dirname "$0") 69TF_JAR_DIR=$(dirname "$0") 70if [ -f "${CUR_DIR}/tradefed.jar" ]; then 71 TF_PATH="${CUR_DIR}/*" 72elif [ ! -z "${ANDROID_HOST_OUT}" ]; then 73 # in an Android build env, tradefed.jar should be in 74 # $ANDROID_HOST_OUT/tradefed/ 75 if [ -f "${ANDROID_HOST_OUT}/tradefed/tradefed.jar" ]; then 76 # We intentionally pass the asterisk through without shell expansion 77 TF_PATH="${ANDROID_HOST_OUT}/tradefed/*" 78 TF_JAR_DIR="${ANDROID_HOST_OUT}/tradefed/" 79 elif [ -f "${ANDROID_HOST_OUT}/framework/tradefed.jar" ]; then 80 # in an Android build env which only has tradefed prebuilt, 81 # tradefed.jar should be in $ANDROID_HOST_OUT/framework/ 82 TF_PATH="${ANDROID_HOST_OUT}/framework/*" 83 TF_JAR_DIR="${ANDROID_HOST_OUT}/framework/" 84 fi 85fi 86 87if [ -z "${TF_PATH}" ]; then 88 >&2 echo "ERROR: Could not find tradefed jar files" 89 exit 1 90fi 91 92# include any host-side test jars from suite 93if [ ! -z "${ANDROID_HOST_OUT_TESTCASES}" ]; then 94 for folder in ${ANDROID_HOST_OUT_TESTCASES}/*; do 95 for entry in "$folder"/*; do 96 if [[ "$entry" = *".jar"* ]]; then 97 TF_PATH=${TF_PATH}:$entry 98 fi 99 done 100 done 101fi 102 103# set any host specific options 104# file format for file at $TRADEFED_OPTS_FILE is one line per host with the following format: 105# <hostname>=<options> 106# for example: 107# hostname.domain.com=-Djava.io.tmpdir=/location/on/disk -Danother=false ... 108# hostname2.domain.com=-Djava.io.tmpdir=/different/location -Danother=true ... 109if [ -e "${TRADEFED_OPTS_FILE}" ]; then 110 # pull the line for this host and take everything after the first = 111 export TRADEFED_OPTS=`grep "^$HOSTNAME=" "$TRADEFED_OPTS_FILE" | cut -d '=' -f 2-` 112fi 113