xref: /aosp_15_r20/tools/asuite/atest/java/atest_script_help.sh (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/bin/bash
2*c2e18aaaSAndroid Build Coastguard Worker
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright (C) 2021 The Android Open Source Project
4*c2e18aaaSAndroid Build Coastguard Worker#
5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*c2e18aaaSAndroid Build Coastguard Worker#
9*c2e18aaaSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
10*c2e18aaaSAndroid Build Coastguard Worker#
11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard Worker
18*c2e18aaaSAndroid Build Coastguard Worker# A library script to help other scripts run within an Android build environment, or while deployed
19*c2e18aaaSAndroid Build Coastguard Worker# on a target host.  Intended to be used with the `source` bash built-in command.  Defines the
20*c2e18aaaSAndroid Build Coastguard Worker# following environment variables:
21*c2e18aaaSAndroid Build Coastguard Worker# JAVA_VERSION, RDBG_FLAG, TF_PATH, TRADEFED_OPTS
22*c2e18aaaSAndroid Build Coastguard Worker#
23*c2e18aaaSAndroid Build Coastguard Worker# It will react to the following environment variables, if they are set:
24*c2e18aaaSAndroid Build Coastguard Worker# TF_DEBUG, TRADEFED_OPTS_FILE
25*c2e18aaaSAndroid Build Coastguard Worker
26*c2e18aaaSAndroid Build Coastguard Worker
27*c2e18aaaSAndroid Build Coastguard WorkercheckPath() {
28*c2e18aaaSAndroid Build Coastguard Worker    if ! type -P "$1" &> /dev/null; then
29*c2e18aaaSAndroid Build Coastguard Worker        >&2 echo "Unable to find $1."
30*c2e18aaaSAndroid Build Coastguard Worker        exit 1
31*c2e18aaaSAndroid Build Coastguard Worker    fi;
32*c2e18aaaSAndroid Build Coastguard Worker}
33*c2e18aaaSAndroid Build Coastguard Worker
34*c2e18aaaSAndroid Build Coastguard Worker# All to specify an alternative Java binary, other than the one on PATH
35*c2e18aaaSAndroid Build Coastguard WorkerTF_JAVA="java"
36*c2e18aaaSAndroid Build Coastguard Workerif [ -n "${TF_JAVA_HOME}" ]; then
37*c2e18aaaSAndroid Build Coastguard Worker  # following similar convention as JAVA_HOME
38*c2e18aaaSAndroid Build Coastguard Worker  TF_JAVA=${TF_JAVA_HOME}/bin/java
39*c2e18aaaSAndroid Build Coastguard Workerfi
40*c2e18aaaSAndroid Build Coastguard Worker
41*c2e18aaaSAndroid Build Coastguard WorkercheckPath ${TF_JAVA}
42*c2e18aaaSAndroid Build Coastguard Worker
43*c2e18aaaSAndroid Build Coastguard Worker# check java version
44*c2e18aaaSAndroid Build Coastguard Workerjava_version_string=$(${TF_JAVA} -version 2>&1)
45*c2e18aaaSAndroid Build Coastguard WorkerJAVA_VERSION=$(echo "$java_version_string" | grep 'version [ "]\(1\.8\|9\|11\|17\|21\).*[ "]')
46*c2e18aaaSAndroid Build Coastguard Workerif [ "${JAVA_VERSION}" == "" ]; then
47*c2e18aaaSAndroid Build Coastguard Worker    >&2 echo "Wrong java version. 1.8, 9, 11, 17 or 21 is required. Found $java_version_string"
48*c2e18aaaSAndroid Build Coastguard Worker    >&2 echo "PATH value:"
49*c2e18aaaSAndroid Build Coastguard Worker    >&2 echo "$PATH"
50*c2e18aaaSAndroid Build Coastguard Worker    >&2 echo "You may encounter unexpected behavior. We recommend you use the one of the known Java versions."
51*c2e18aaaSAndroid Build Coastguard Workerfi
52*c2e18aaaSAndroid Build Coastguard Worker
53*c2e18aaaSAndroid Build Coastguard Worker# java versions below 1.8 are not supported, java versions above 1.8 need add-opens
54*c2e18aaaSAndroid Build Coastguard WorkerJAVA_VERSION=$(echo "$java_version_string" | grep 'version [ "]1\.8.*[ "]')
55*c2e18aaaSAndroid Build Coastguard Workerif [ "${JAVA_VERSION}" == "" ]; then
56*c2e18aaaSAndroid Build Coastguard Worker    ADD_OPENS_FLAG="--add-opens=java.base/java.nio=ALL-UNNAMED --add-opens=java.base/sun.reflect.annotation=ALL-UNNAMED"
57*c2e18aaaSAndroid Build Coastguard Workerfi
58*c2e18aaaSAndroid Build Coastguard Worker
59*c2e18aaaSAndroid Build Coastguard Worker# check debug flag and set up remote debugging
60*c2e18aaaSAndroid Build Coastguard Workerif [ -n "${TF_DEBUG}" ]; then
61*c2e18aaaSAndroid Build Coastguard Worker    if [ -z "${TF_DEBUG_PORT}" ]; then
62*c2e18aaaSAndroid Build Coastguard Worker        TF_DEBUG_PORT=10088
63*c2e18aaaSAndroid Build Coastguard Worker    fi
64*c2e18aaaSAndroid Build Coastguard Worker    RDBG_FLAG="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=${TF_DEBUG_PORT}"
65*c2e18aaaSAndroid Build Coastguard Workerfi
66*c2e18aaaSAndroid Build Coastguard Worker
67*c2e18aaaSAndroid Build Coastguard Worker# first try to find TF jars in same dir as this script
68*c2e18aaaSAndroid Build Coastguard WorkerCUR_DIR=$(dirname "$0")
69*c2e18aaaSAndroid Build Coastguard WorkerTF_JAR_DIR=$(dirname "$0")
70*c2e18aaaSAndroid Build Coastguard Workerif [ -f "${CUR_DIR}/tradefed.jar" ]; then
71*c2e18aaaSAndroid Build Coastguard Worker    TF_PATH="${CUR_DIR}/*"
72*c2e18aaaSAndroid Build Coastguard Workerelif [ ! -z "${ANDROID_HOST_OUT}" ]; then
73*c2e18aaaSAndroid Build Coastguard Worker    # in an Android build env, tradefed.jar should be in
74*c2e18aaaSAndroid Build Coastguard Worker    # $ANDROID_HOST_OUT/tradefed/
75*c2e18aaaSAndroid Build Coastguard Worker    if [ -f "${ANDROID_HOST_OUT}/tradefed/tradefed.jar" ]; then
76*c2e18aaaSAndroid Build Coastguard Worker        # We intentionally pass the asterisk through without shell expansion
77*c2e18aaaSAndroid Build Coastguard Worker        TF_PATH="${ANDROID_HOST_OUT}/tradefed/*"
78*c2e18aaaSAndroid Build Coastguard Worker        TF_JAR_DIR="${ANDROID_HOST_OUT}/tradefed/"
79*c2e18aaaSAndroid Build Coastguard Worker    elif [ -f "${ANDROID_HOST_OUT}/framework/tradefed.jar" ]; then
80*c2e18aaaSAndroid Build Coastguard Worker        # in an Android build env which only has tradefed prebuilt,
81*c2e18aaaSAndroid Build Coastguard Worker        # tradefed.jar should be in $ANDROID_HOST_OUT/framework/
82*c2e18aaaSAndroid Build Coastguard Worker        TF_PATH="${ANDROID_HOST_OUT}/framework/*"
83*c2e18aaaSAndroid Build Coastguard Worker        TF_JAR_DIR="${ANDROID_HOST_OUT}/framework/"
84*c2e18aaaSAndroid Build Coastguard Worker    fi
85*c2e18aaaSAndroid Build Coastguard Workerfi
86*c2e18aaaSAndroid Build Coastguard Worker
87*c2e18aaaSAndroid Build Coastguard Workerif [ -z "${TF_PATH}" ]; then
88*c2e18aaaSAndroid Build Coastguard Worker    >&2 echo "ERROR: Could not find tradefed jar files"
89*c2e18aaaSAndroid Build Coastguard Worker    exit 1
90*c2e18aaaSAndroid Build Coastguard Workerfi
91*c2e18aaaSAndroid Build Coastguard Worker
92*c2e18aaaSAndroid Build Coastguard Worker# include any host-side test jars from suite
93*c2e18aaaSAndroid Build Coastguard Workerif [ -n "${ANDROID_HOST_OUT_TESTCASES}" ]; then
94*c2e18aaaSAndroid Build Coastguard Worker    for folder in ${ANDROID_HOST_OUT_TESTCASES}/*; do
95*c2e18aaaSAndroid Build Coastguard Worker        for entry in "$folder"/*; do
96*c2e18aaaSAndroid Build Coastguard Worker            if [[ "$entry" = *".jar"* ]]; then
97*c2e18aaaSAndroid Build Coastguard Worker                TF_PATH=${TF_PATH}:$entry
98*c2e18aaaSAndroid Build Coastguard Worker            fi
99*c2e18aaaSAndroid Build Coastguard Worker        done
100*c2e18aaaSAndroid Build Coastguard Worker    done
101*c2e18aaaSAndroid Build Coastguard Workerfi
102*c2e18aaaSAndroid Build Coastguard Worker
103*c2e18aaaSAndroid Build Coastguard Worker# set any host specific options
104*c2e18aaaSAndroid Build Coastguard Worker# file format for file at $TRADEFED_OPTS_FILE is one line per host with the following format:
105*c2e18aaaSAndroid Build Coastguard Worker# <hostname>=<options>
106*c2e18aaaSAndroid Build Coastguard Worker# for example:
107*c2e18aaaSAndroid Build Coastguard Worker# hostname.domain.com=-Djava.io.tmpdir=/location/on/disk -Danother=false ...
108*c2e18aaaSAndroid Build Coastguard Worker# hostname2.domain.com=-Djava.io.tmpdir=/different/location -Danother=true ...
109*c2e18aaaSAndroid Build Coastguard Workerif [ -e "${TRADEFED_OPTS_FILE}" ]; then
110*c2e18aaaSAndroid Build Coastguard Worker    # pull the line for this host and take everything after the first =
111*c2e18aaaSAndroid Build Coastguard Worker    export TRADEFED_OPTS=`grep "^$HOSTNAME=" "$TRADEFED_OPTS_FILE" | cut -d '=' -f 2-`
112*c2e18aaaSAndroid Build Coastguard Workerfi
113