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"
64else
65    echo "Unrecognized OS"
66    exit
67fi
68
69# check if in Android build env
70if [ ! -z "${ANDROID_BUILD_TOP}" ]; then
71    if [ ! -z "${ANDROID_HOST_OUT}" ]; then
72      VTS_ROOT=${ANDROID_HOST_OUT}/vts
73    else
74      VTS_ROOT=${ANDROID_BUILD_TOP}/${OUT_DIR:-out}/host/${OS}/vts
75    fi
76    if [ ! -d ${VTS_ROOT} ]; then
77        echo "Could not find $VTS_ROOT in Android build environment. Try 'make vts'"
78        exit
79    fi;
80fi;
81
82if [ -z ${VTS_ROOT} ]; then
83    # assume we're in an extracted vts install
84    VTS_ROOT="$(dirname $(realpath $0))/../.."
85fi;
86
87JAR_DIR=${VTS_ROOT}/android-vts/tools
88JARS="tradefed
89  loganalysis
90  hosttestlib
91  compatibility-host-util
92  compatibility-host-util-tests
93  vts-tradefed
94  vts-tradefed-tests
95  compatibility-common-util-tests
96  compatibility-tradefed-tests"
97
98for JAR in $JARS; do
99    checkFile ${JAR_DIR}/${JAR}.jar
100    JAR_PATH=${JAR_PATH}:${JAR_DIR}/${JAR}.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 $RDBG_FLAG -Xmx4096m -XX:+HeapDumpOnOutOfMemoryError -cp ${JAR_PATH} -DVTS_ROOT=${VTS_ROOT} com.android.compatibility.common.tradefed.command.CompatibilityConsole "$@"
135