1# Copyright (C) 2023 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15set -e # Exit at failure
16shopt -s globstar # Enable double-star wildcards (**)
17
18cd "${0%/*}" # Move to the script dir
19
20fail() {
21  echo "Error: $*" 1>&2
22  exit 1
23}
24
25# Print the arguments and then execute.
26run() {
27  echo "Running: $*" 1>&2
28  "$@"
29}
30
31# Concatenate the second and subsequent args with the first arg as a separator.
32# e.g. `join : a b c` -> prints `a:b:c`
33join() {
34  local IFS="$1"
35  shift
36  echo "$*"
37}
38
39abspath() {
40  for name in "${@}"; do
41    readlink -f $name
42  done
43}
44
45m() {
46  if (( $SKIP_BUILD )) ; then
47    echo "Skipping build: $*" 1>&2
48    return 0
49  fi
50  run ${ANDROID_BUILD_TOP}/build/soong/soong_ui.bash --make-mode "$@"
51}
52
53# Extract given jar files
54extract() {
55  for f in "${@}"; do
56    local out=$f.ext
57    run rm -fr $out
58    run mkdir -p $out
59
60    # It's too noisy, so only show the first few lines.
61    {
62      # Hmm unzipping kotlin jar files may produce a warning? Let's just add `|| true`...
63      run unzip $f -d $out || true
64    } |& sed -e '5,$d'
65    echo '  (omitting remaining output)'
66
67  done
68}
69
70# Find all *.java files in $1, and print them as Java class names.
71# For example, if there's a file `src/com/android/test/Test.java`, and you run
72# `list_all_classes_under_dir src`, then it'll print `com.android.test.Test`.
73list_all_classes_under_dir() {
74  local dir="$1"
75  ( # Use a subshell, so we won't change the current directory on the caller side.
76    cd "$dir"
77
78    # List the java files, but replace the slashes with dots, and remove the `.java` suffix.
79    ls **/*.java | sed -e 's!/!.!g' -e 's!.java$!!'
80  )
81}
82
83checkenv() {
84  # Make sure $ANDROID_BUILD_TOP is set.
85  : ${ANDROID_BUILD_TOP:?}
86
87  # Make sure ANDROID_BUILD_TOP doesn't contain whitespace.
88  set ${ANDROID_BUILD_TOP}
89  if [[ $# != 1 ]] ; then
90    fail "\$ANDROID_BUILD_TOP cannot contain whitespace."
91  fi
92}
93
94checkenv
95
96JAVAC=${JAVAC:-javac}
97JAVA=${JAVA:-java}
98JAR=${JAR:-jar}
99
100JAVAC_OPTS=${JAVAC_OPTS:--Xmaxerrs 99999 -Xlint:none}
101
102SOONG_INT=$ANDROID_BUILD_TOP/out/soong/.intermediates
103
104JUNIT_TEST_MAIN_CLASS=com.android.hoststubgen.hosthelper.HostTestSuite
105
106run_junit_test_jar() {
107  local jar="$1"
108  echo "Starting test: $jar ..."
109  run cd "${jar%/*}"
110
111  run $JAVA $JAVA_OPTS \
112      -cp $jar \
113      org.junit.runner.JUnitCore \
114      $main_class || return 1
115  return 0
116}
117