summaryrefslogtreecommitdiff
path: root/benchmarking/java-ubenchs/build.sh
blob: 829488e2d76914aa996fb9dd3f6c6abee9b2e0e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash

#    Copyright 2015 ARM Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

SCRIPT_PATH=$(dirname $(readlink -e $0))

DIR_BUILD=$SCRIPT_PATH/build
DIR_BENCHMARKS=benchmarks


# Set to true to only build for the host and not use any android specific
# commands.
HOST_BUILD=false
# Set to true to print the commands executed.
VERBOSE=false
# Set to true to treat warnings as errors, in which case hitting a warning will
# cause the script to exit.
WERROR=false




# Helpers.

CRED="\033[0;31m"
CORANGE="\033[0;33m"
CGREEN="\033[0;32m"
CNC="\033[0m"

# Disable colour output when redirected.
if [ ! -t 1 ]; then
  CRED=
  CORANGE=
  CGREEN=
  CNC=
fi

error() {
  echo -e "${CRED}ERROR: $*${CNC}" >&2
  exit 1
}

warning() {
  echo -e "${CORANGE}WARNING: $*${CNC}" >&2
  $WERROR && exit 2
}

verbose_safe() {
  if $VERBOSE; then
    echo "$@"
  fi
  "$@" || error "FAILED command:\n$*";
}




# Arguments handling

usage="Usage: $(basename "$0") [FILTER...]
Build Java benchmark class, APK, and jar files.
Output files are produced in $DIR_BUILD.

Options:
	-h	Show this help message.
	-H	Only build for the host.
	  	This allows compiling for the host outside of an Android environment.
	-v	Verbose. Print the commands executed.
	-w	Treat warnings as errors, causing them to abort.
"

while getopts ':hHlvw' option; do
  case "$option" in
    h) echo "$usage"; exit ;;
    H) HOST_BUILD=true ;;
    v) VERBOSE=true ;;
    w) WERROR=true ;;
    \?)
      printf "Illegal option: -%s\n" "$OPTARG" >&2
      echo "$usage"
      exit 1
      ;;
  esac
done

shift $((OPTIND - 1))




# Find what Java files we need to compile.

# `javac` requires paths to match class names, so we work directly from the
# benchmarks directory.
pushd $DIR_BENCHMARKS &> /dev/null

RDIR_BENCHMARKS=.
RDIR_FRAMEWORK=$RDIR_BENCHMARKS/org/linaro/bench

JAVA_BENCHMARK_FILES=
FIND_BENCHMARKS_COMMAND="find $RDIR_BENCHMARKS ! -path $RDIR_FRAMEWORK/* -type f"
set -f
if [ $# -eq 0 ]; then
  # No filters have been provided, so compile all benchmarks.
  JAVA_BENCHMARK_FILES+="$($FIND_BENCHMARKS_COMMAND -name '*'.java) "
else
  # Find benchmarks matching the provided filters.
  FILTERS=("$@")
  # Disable globbing for the `find` commands.
  for filter in "${FILTERS[@]}"; do
    JAVA_BENCHMARK_FILES+="$($FIND_BENCHMARKS_COMMAND -name $filter) "
  done
fi
set +f

# Transform the list of java files in a list of strings that will be provided to
# the benchmark framework to indicate what benchmark classes are available.
# Remove the `.java` extension.
JAVA_BENCHMARK_CLASSES=${JAVA_BENCHMARK_FILES//.java/}
# Remove the leading `./`.
JAVA_BENCHMARK_CLASSES=${JAVA_BENCHMARK_CLASSES//.\//}
# Trim trailing whitespaces.
JAVA_BENCHMARK_CLASSES=${JAVA_BENCHMARK_CLASSES/%[[:space:]]/}
# TODO: The java code only knows about basenames. Synchronize the 'names' of
# benchmarks between the java app, run.py, and the information printed when
# running benchmarks.
IFS=' ' read -a array <<< $JAVA_BENCHMARK_CLASSES
for i in "${!array[@]}"; do
  array[$i]=$(basename ${array[$i]})
done
# Sort the names.
readarray -t sorted < <(printf '%s\0' "${array[@]}" | sort -z | xargs -0n1)
JAVA_BENCHMARK_CLASSES=$(echo ${sorted[@]})
# Make it a list of literal string.
JAVA_BENCHMARK_CLASSES="\""${JAVA_BENCHMARK_CLASSES//[[:space:]]/\", \"}"\""
# Write the result file.
BENCHMARK_LIST_TEMPLATE="$(cat $RDIR_FRAMEWORK/BenchmarkList.java.template)"
BENCHMARK_LIST_TEMPLATE=${BENCHMARK_LIST_TEMPLATE/<to be filled by the build system>/$JAVA_BENCHMARK_CLASSES}
echo "$BENCHMARK_LIST_TEMPLATE" > $RDIR_FRAMEWORK/BenchmarkList.java

# Framework java files are compiled unconditionally.
JAVA_FRAMEWORK_FILES="$(find $RDIR_FRAMEWORK -type f -name '*'.java)"



# Build everything.

verbose_safe rm --recursive --force $DIR_BUILD
verbose_safe mkdir --parents $DIR_BUILD/classes/
verbose_safe javac -d $DIR_BUILD/classes/ $JAVA_FRAMEWORK_FILES $JAVA_BENCHMARK_FILES
verbose_safe jar cf $DIR_BUILD/bench.jar $DIR_BUILD/classes/
if ! $HOST_BUILD; then
  if hash dx 2> /dev/null; then
    verbose_safe dx --dex --output $DIR_BUILD/bench.apk $DIR_BUILD/classes/
  else
    warning "\`dx\` command not found. bench.apk won't be generated." \
      "Are you running from an Android environment?"
  fi
fi

# Leave $DIR_BENCHMARKS
popd &> /dev/null