1#!/bin/bash 2# Copyright (C) 2022 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16# Utils library script for the tradefed harnesses. Its functions can be used 17# once the script is sourced, for example by adding 18# source ${ANDROID_BUILD_TOP}/platform_testing/scripts/test-utils-script 19# in a script. 20# 21# It provides a collection of functions which can be called from the other 22# scripts. 23 24checkFile() { 25 if [ ! -f "$1" ]; then 26 echo "Unable to locate $1" 27 exit 28 fi; 29} 30 31checkPath() { 32 if ! type -P $1 &> /dev/null; then 33 echo "Unable to find $1 in path." 34 exit 35 fi; 36} 37 38# readlink does not work on MacOS so rely on our own realpath 39realpath() { 40 [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}" 41} 42 43checkJavaVersion() { 44 # check java version 45 local java_cmd=$1 46 local java_version_string=$(${java_cmd} -version 2>&1 | grep -E "\<version\>") 47 local JAVA_VERSION=$(echo "$java_version_string" | grep -E 'version [ "](1\.8|9|11|17|21).*[ "]') 48 if [ "${JAVA_VERSION}" == "" ]; then 49 >&2 echo "Wrong java version. Allowed versions: 1.8, 9, 11, 17, 21. Found $java_version_string" 50 >&2 echo "Java command: $java_cmd" 51 >&2 echo "PATH value:" 52 >&2 echo "$PATH" 53 if [ "${IGNORE_JAVA_VERSION_ERROR}" == "y" ]; then 54 >&2 echo "Ignoring the wrong java version error as \$IGNORE_JAVA_VERSION_ERROR is set to 'y'" 55 else 56 >&2 echo "If you want to ignore this error, set \$IGNORE_JAVA_VERSION_ERROR to 'y'" 57 exit 8 58 fi 59 fi 60} 61 62getAddOpensFlag() { 63 local java_cmd=$1 64 local java_version_string=$(${java_cmd} -version 2>&1 | grep -E "\<version\>") 65 # java versions below 1.8 are not supported, java versions above 1.8 need add-opens 66 local JAVA_VERSION=$(echo "$java_version_string" | grep -E 'version [ "]1\.8.*[ "]') 67 if [ "${JAVA_VERSION}" == "" ]; then 68 echo "--add-opens=java.base/java.nio=ALL-UNNAMED --add-opens=java.base/sun.reflect.annotation=ALL-UNNAMED" 69 fi 70} 71 72getRemoteDbgFlag() { 73 # check debug flag and set up remote debugging 74 # depends on $TF_DEBUG and $TF_DEBUG_PORT 75 if [ -n "${TF_DEBUG}" ]; then 76 if [ -z "${TF_DEBUG_PORT}" ]; then 77 TF_DEBUG_PORT=10088 78 fi 79 echo "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=${TF_DEBUG_PORT}" 80 fi 81} 82 83loadSharedLibraries() { 84 # load any shared libraries for host-side executables 85 local HOST=$1 86 local LIB_DIR=$2 87 if [ "$HOST" == "Linux" ]; then 88 LD_LIBRARY_PATH=${LIB_DIR}:${LIB_DIR}64:${LD_LIBRARY_PATH} 89 export LD_LIBRARY_PATH 90 elif [ "$HOST" == "Darwin" ]; then 91 DYLD_LIBRARY_PATH=${LIB_DIR}:${LIB_DIR}64:${DYLD_LIBRARY_PATH} 92 export DYLD_LIBRARY_PATH 93 fi 94} 95