1#!/bin/bash
2
3# Copyright (C) 2021 Google Inc.
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 cat-tradefed harness
18# can be used from an Android build environment, or a standalone cat 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
34checkPath adb
35checkPath java
36
37# check debug flag and set up remote debugging
38if [ -n "${TF_DEBUG}" ]; then
39  if [ -z "${TF_DEBUG_PORT}" ]; then
40    TF_DEBUG_PORT=10088
41  fi
42  RDBG_FLAG=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=${TF_DEBUG_PORT}
43fi
44
45# get OS
46HOST=`uname`
47if [ "$HOST" == "Linux" ]; then
48    OS="linux-x86"
49elif [ "$HOST" == "Darwin" ]; then
50    OS="darwin-x86"
51else
52    echo "Unrecognized OS"
53    exit
54fi
55
56# check if in Android build env
57if [ ! -z "${ANDROID_BUILD_TOP}" ]; then
58    if [ ! -z "${ANDROID_HOST_OUT}" ]; then
59      CATBOX_ROOT=${ANDROID_HOST_OUT}/catbox
60    else
61      CATBOX_ROOT=${ANDROID_BUILD_TOP}/${OUT_DIR:-out}/host/${OS}/catbox
62    fi
63    if [ ! -d ${CATBOX_ROOT} ]; then
64        echo "Could not find $CATBOX_ROOT in Android build environment. Try 'make catbox'"
65        exit
66    fi;
67fi;
68
69if [ -z ${CATBOX_ROOT} ]; then
70    # assume we're in an extracted cat install
71    CATBOX_ROOT="$(dirname $0)/../.."
72fi;
73
74JAR_DIR=${CATBOX_ROOT}/android-catbox/tools
75JARS="tradefed
76  loganalysis
77  compatibility-host-util
78  catbox-tradefed
79  catbox-tradefed-tests
80  catbox-report-lib"
81
82for JAR in $JARS; do
83    checkFile ${JAR_DIR}/${JAR}.jar
84    JAR_PATH=${JAR_PATH}:${JAR_DIR}/${JAR}.jar
85done
86
87OPTIONAL_JARS="
88  google-tradefed
89  google-tradefed-tests
90  google-tf-prod-tests"
91
92for JAR in $OPTIONAL_JARS; do
93    if [ -f "$JAR_DIR/$JAR.jar" ]; then
94        JAR_PATH=${JAR_PATH}:${JAR_DIR}/${JAR}.jar
95    fi;
96done
97
98# load any shared libraries for host-side executables
99LIB_DIR=${CATBOX_ROOT}/android-catbox/lib
100if [ "$HOST" == "Linux" ]; then
101    LD_LIBRARY_PATH=${LIB_DIR}:${LIB_DIR}64:${LD_LIBRARY_PATH}
102    export LD_LIBRARY_PATH
103elif [ "$HOST" == "Darwin" ]; then
104    DYLD_LIBRARY_PATH=${LIB_DIR}:${LIB_DIR}64:${DYLD_LIBRARY_PATH}
105    export DYLD_LIBRARY_PATH
106fi
107
108# include any host-side test jars
109for j in ${CATBOX_ROOT}/android-catbox/testcases/*.jar; do
110    JAR_PATH=${JAR_PATH}:$j
111done
112
113if [ "$HOST" == "Darwin" ]; then
114    local_tmp_dir="${ANDROID_HOST_OUT}/tmp"
115    if [ ! -f "${local_tmp_dir}" ]; then
116        mkdir -p "${local_tmp_dir}"
117    fi
118    java_tmp_dir_opt="-Djava.io.tmpdir=${local_tmp_dir}"
119fi
120
121java $RDBG_FLAG -cp ${JAR_PATH} -DCATBOX_ROOT=${CATBOX_ROOT} ${java_tmp_dir_opt} com.android.compatibility.common.tradefed.command.CompatibilityConsole "$@"
122