1#!/bin/bash
2
3# Copyright (C) 2019 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# launcher script for vts-tradefed harness
18# can be used from an Android build environment, or a standalone vts zip
19
20checkFile() {
21    if [ ! -f "$1" ]; then
22        echo "Unable to locate $1"
23        exit
24    fi;
25}
26
27checkPath() {
28    if ! type -P $1 &> /dev/null; then
29        echo "Unable to find $1 in path."
30        exit
31    fi;
32}
33
34# readlink does not work on MacOS so rely on our own realpath
35realpath() {
36    [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
37}
38
39checkPath aapt
40checkPath adb
41checkPath java
42
43# check java version
44JAVA_VERSION=$(java -version 2>&1 | grep 'version [ "]\(1\.8\|9\|11\).*[ "]' | head -n 1)
45if [ "${JAVA_VERSION}" == "" ]; then
46    echo "Wrong java version. 1.8, 9, or 11 is required."
47    exit
48fi
49
50# check debug flag and set up remote debugging
51if [ -n "${TF_DEBUG}" ]; then
52  if [ -z "${TF_DEBUG_PORT}" ]; then
53    TF_DEBUG_PORT=10088
54  fi
55  RDBG_FLAG=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=${TF_DEBUG_PORT}
56fi
57
58# get OS
59HOST=`uname`
60if [ "$HOST" == "Linux" ]; then
61    OS="linux-x86"
62elif [ "$HOST" == "Darwin" ]; then
63    OS="darwin-x86"
64    # Bundled java is for linux so use host JDK on Darwin
65    JAVA_BINARY=java
66else
67    echo "Unrecognized OS"
68    exit
69fi
70
71# check if in Android build env
72if [ ! -z "${ANDROID_BUILD_TOP}" ]; then
73    if [ ! -z "${ANDROID_HOST_OUT}" ]; then
74      VTS_ROOT=${ANDROID_HOST_OUT}/vts
75    else
76      VTS_ROOT=${ANDROID_BUILD_TOP}/${OUT_DIR:-out}/host/${OS}/vts
77    fi
78    if [ ! -d ${VTS_ROOT} ]; then
79        echo "Could not find $VTS_ROOT in Android build environment. Try 'make vts'"
80        exit
81    fi;
82fi;
83
84if [ -z ${VTS_ROOT} ]; then
85    # assume we're in an extracted vts install
86    VTS_ROOT="$(dirname $(realpath $0))/../.."
87fi;
88
89if [ -z ${JAVA_BINARY} ]; then
90      JAVA_BINARY=${VTS_ROOT}/android-vts/jdk/bin/java
91fi;
92
93if [ ! -f "${JAVA_BINARY}" ]; then
94      JAVA_BINARY=java
95fi
96
97JAR_DIR=${VTS_ROOT}/android-vts/tools
98
99for JAR in ${JAR_DIR}/*.jar; do
100    JAR_PATH=${JAR_PATH}:${JAR}
101done
102JAR_PATH=${JAR_PATH:1} # Strip off leading ':'
103
104OPTIONAL_JARS="
105  google-tradefed
106  google-tradefed-tests
107  google-tf-prod-tests"
108
109STANDALONE_JAR_DIR=${ANDROID_HOST_OUT}/framework
110for JAR in $OPTIONAL_JARS; do
111    if [ -f "${JAR_DIR}/${JAR}.jar" ]; then
112        JAR_PATH=${JAR_PATH}:${JAR_DIR}/${JAR}.jar
113    elif [ -f "${STANDALONE_JAR_DIR}/${JAR}.jar" ]; then
114        JAR_PATH=${JAR_PATH}:${STANDALONE_JAR_DIR}/${JAR}.jar
115    fi;
116done
117
118# load any shared libraries for host-side executables
119LIB_DIR=${VTS_ROOT}/android-vts/lib
120if [ "$HOST" == "Linux" ]; then
121    LD_LIBRARY_PATH=${LIB_DIR}:${LIB_DIR}64:${LD_LIBRARY_PATH}
122    export LD_LIBRARY_PATH
123elif [ "$HOST" == "Darwin" ]; then
124    DYLD_LIBRARY_PATH=${LIB_DIR}:${LIB_DIR}64:${DYLD_LIBRARY_PATH}
125    export DYLD_LIBRARY_PATH
126fi
127
128# include any host-side test jars
129for j in $(find ${VTS_ROOT}/android-vts/testcases -type f -name '*.jar'); do
130    JAR_PATH=${JAR_PATH}:$j
131done
132
133VTS_TESTCASES=${VTS_ROOT}/android-vts/testcases/
134VTS_TESTCASES=${VTS_TESTCASES} ${JAVA_BINARY} $RDBG_FLAG -Xmx4096m -XX:+HeapDumpOnOutOfMemoryError -cp ${JAR_PATH} -DVTS_ROOT=${VTS_ROOT} com.android.compatibility.common.tradefed.command.CompatibilityConsole "$@"
135