1#!/bin/bash
2
3# Copyright (C) 2015 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 cts-tradefed harness
18# can be used from an Android build environment, or a standalone cts 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 -m 1 'version [ "]\(1\.8\|9\|11\).*[ "]')
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      CTS_ROOT=${ANDROID_HOST_OUT}/cts
75    else
76      CTS_ROOT=${ANDROID_BUILD_TOP}/${OUT_DIR:-out}/host/${OS}/cts
77    fi
78    if [ ! -d ${CTS_ROOT} ]; then
79        echo "Could not find $CTS_ROOT in Android build environment. Try 'make cts'"
80        exit
81    fi;
82fi;
83
84if [ -z ${CTS_ROOT} ]; then
85    # assume we're in an extracted cts install
86    CTS_ROOT="$(dirname $(realpath $0))/../.."
87fi;
88
89if [ -z ${JAVA_BINARY} ]; then
90    JAVA_BINARY=${CTS_ROOT}/android-cts/jdk/bin/java
91fi;
92
93if [ ! -f "${JAVA_BINARY}" ]; then
94    JAVA_BINARY=java
95fi
96
97JAR_DIR=${CTS_ROOT}/android-cts/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
104# load any shared libraries for host-side executables
105LIB_DIR=${CTS_ROOT}/android-cts/lib
106if [ "$HOST" == "Linux" ]; then
107    LD_LIBRARY_PATH=${LIB_DIR}:${LIB_DIR}64:${LD_LIBRARY_PATH}
108    export LD_LIBRARY_PATH
109elif [ "$HOST" == "Darwin" ]; then
110    DYLD_LIBRARY_PATH=${LIB_DIR}:${LIB_DIR}64:${DYLD_LIBRARY_PATH}
111    export DYLD_LIBRARY_PATH
112fi
113
114# include any host-side test jars
115for j in ${CTS_ROOT}/android-cts/testcases/*.jar; do
116    JAR_PATH=${JAR_PATH}:$j
117done
118
119${JAVA_BINARY} $RDBG_FLAG -Xmx6g -XX:+HeapDumpOnOutOfMemoryError -cp ${JAR_PATH} -DCTS_ROOT=${CTS_ROOT} com.android.compatibility.common.tradefed.command.CompatibilityConsole "$@"
120
121