blob: 87abb3eb3bcd8aac5e78a85745bbeb3776be6ab5 [file] [log] [blame]
Gyorgy Szingba1095c2020-11-24 00:33:11 +01001#!/bin/bash
2#
Gyorgy Szing7ddb28a2023-01-17 15:55:36 +01003# Copyright (c) 2020-2023, Arm Limited and contributors. All rights reserved.
Gyorgy Szingba1095c2020-11-24 00:33:11 +01004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8# Exit with error if any command fails
9set -e
10
11# Read in user specific environment settings
12if [ -e user.env ]
13then
14 echo "Reding user specific settings from user.env"
15 source user.env
16fi
17
18# Default to non-verbose mode.
19VERBOSE=${VERBOSE:-0}
20
Imre Kisf9dda4a2022-01-18 18:54:15 +010021# Additional environment specific CMake flags
22CMAKE_EXTRA_FLAGS=${CMAKE_EXTRA_FLAGS:-""}
23
Gyorgy Szingba1095c2020-11-24 00:33:11 +010024# Get root of TS repo.
25TS_ROOT=${TS_ROOT:-$(git rev-parse --show-toplevel)}
26
27# Number of threads to use in parallel
28NUMBER_OF_PROCESSORS=${NUMBER_OF_PROCESSORS:-$(( $(nproc) * 2 )) }
29
Gyorgy Szinge1ccb2d2023-03-21 03:05:23 +010030# By default run as much as we can
31FAIL_FAST=${FAIL_FAST:-0}
32
Gyorgy Szing54071d92022-01-04 12:34:14 +010033# Global exit code.
34exit_code=0
35
Gyorgy Szingf24a03a2023-03-21 03:00:40 +010036# List of supported tests.
37test_list=(
38{% for config in data %}
39 "{{config.name}}"
40{% endfor %}
41)
42
Gyorgy Szingba1095c2020-11-24 00:33:11 +010043# Convert test name to build directory
44function name-to-bdir() {
45 printf "./build-%s" "$1"
46}
47
48# Wrap cmake to allow verbose vs non-verbose mode
49function _cmake() {
50 log_file=$1
51 shift
52 if [ "$VERBOSE" != "0" ]
53 then
54 cmake "$@" 2>&1 | tee -a "$log_file"
55 return ${PIPESTATUS[0]}
56 else
57 cmake "$@" >>"$log_file" 2>&1
58 fi
59}
60
61{% for config in data %}
62# Run build test "{{config.name}}"
63{{config.name}}() {
64 echo "##################################################################"
65 echo "########################## {{config.name}} started."
66
67 {% if config.os_id is defined %}
68 if [ "$OS_ID" != "{{config.os_id}}" ]
69 then
70 echo "Test case is not supported on this host."
Gyorgy Szing54071d92022-01-04 12:34:14 +010071 echo "########################## $COLOR_YELLOW {{config.name}} skipped $COLOR_RESET"
Gyorgy Szingba1095c2020-11-24 00:33:11 +010072 echo "##################################################################"
73 return
74 fi
75 {% endif %}
76 b_dir=$(name-to-bdir "{{config.name}}")
77 log_file=$b_dir/build.log
78 rm -rf "$b_dir"
79 mkdir -p "$b_dir"
80
Gyorgy Szingb164e392021-09-04 01:51:57 +020081 if [ -n "$_ccache" ]
82 then
83 ccache_option=-DCMAKE_C_COMPILER_LAUNCHER=$_ccache
84 fi
85
Gyorgy Szing54071d92022-01-04 12:34:14 +010086 local retval=0
Gyorgy Szingba1095c2020-11-24 00:33:11 +010087
Gyorgy Szing54071d92022-01-04 12:34:14 +010088 # jinja2 is removing single newlines. Adding a comment stops this behavior.
Gyorgy Szinga962db72023-03-21 03:11:00 +010089 if _cmake "$log_file" -S "{{config.src}}" -B "$b_dir" $ccache_option {% for param in config.params %} "{{param}}" {% endfor %} ${CMAKE_EXTRA_FLAGS} #keep newline
Gyorgy Szingba1095c2020-11-24 00:33:11 +010090 then
Gyorgy Szinga962db72023-03-21 03:11:00 +010091 if _cmake "$log_file" --build "$b_dir" -j "${NUMBER_OF_PROCESSORS}" --verbose
Gyorgy Szingba1095c2020-11-24 00:33:11 +010092 then
Gyorgy Szing54071d92022-01-04 12:34:14 +010093 if _cmake "$log_file" --install "$b_dir" --prefix ./install
94 then
95 echo "########################## $COLOR_GREEN {{config.name}} passed $COLOR_RESET"
96 else
97 retval=$?
98 echo "For details see: $log_file"
99 echo "########################## $COLOR_RED {{config.name}} failed $COLOR_RESET"
100 fi
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100101 else
Gyorgy Szing54071d92022-01-04 12:34:14 +0100102 retval=$?
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100103 echo "For details see: $log_file"
104 echo "########################## $COLOR_RED {{config.name}} failed $COLOR_RESET"
105 fi
106 else
Gyorgy Szing54071d92022-01-04 12:34:14 +0100107 retval=$?
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100108 echo "For details see: $log_file"
109 echo "########################## $COLOR_RED {{config.name}} failed $COLOR_RESET"
110 fi
111 echo "##################################################################"
Gyorgy Szing54071d92022-01-04 12:34:14 +0100112 return $retval
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100113}
114
115{% endfor %}
116
117# Clean intermediate files
118do_clean() {
119 {% for config in data %}
120 b_dir=$(name-to-bdir "{{config.name}}")
121 if [ -d "$b_dir" ]
122 then
123 echo "Removing $b_dir"
Gyorgy Szing54071d92022-01-04 12:34:14 +0100124 rm -rf "$b_dir"
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100125 fi
126 {% endfor %}
127}
128
129# Print usage info
130do_help() {
131 cat <<END_HELP
132Build test runner
133=================
134
135Invocation::
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100136 ``$0 [<options>] [<command>]``
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100137
138The file "user.env" is sourced from the current directory. Use it to set
139environment specific defaults. For config variables see the start of this script
140and any "$<XXXX>" in the "params" array of any command in test_data.yaml
141Some variables to note
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100142 - VERBOSE : <0/1> make the script output more info.
143 VERBOSE=$VERBOSE
144 - TS_ROOT : Root directory of the TS repo.
145 TS_ROOT=$TS_ROOT
146 - NUMBER_OF_PROCESSORS: number of processors in the system. Used for setting
147 the number of parallel processes during build
148 NUMBER_OF_PROCESSORS=$NUMBER_OF_PROCESSORS
149 - CMAKE_EXTRA_FLAGS: additional environment specific CMake flags
150 CMAKE_EXTRA_FLAGS=-DNEWLIB_LIBC_PATH=/path/to/newlib
Gyorgy Szinge1ccb2d2023-03-21 03:05:23 +0100151 - FAIL_FAST: <0/1> see -f
152 FAIL_FAST=$FAIL_FAST
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100153
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100154Options:
Gyorgy Szing324e8af2023-03-21 03:07:20 +0100155 --color|--no-color
156 - force or disable output coloring
Gyorgy Szinge1ccb2d2023-03-21 03:05:23 +0100157 -f|--fail-fast
158 - exit after the first test failure
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100159 -p <regexp>
160 - filter out all test cases whose name is not matching regexp.
161 regexp is a regular expression understood by grep
162Commands:
163 "" - no command/default -> run all test cases
164 clean - remove build directories
165 help - print this text
166 <test case> - run the specified test. This can be specified multiple times
167 to allow running a list of tests.
168 available test cases $([ -n "$pattern" ] && echo "( matching '$pattern' )"):
169 $(echo "${test_list[@]}" | sed "s/ /\n /g")
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100170END_HELP
171}
172
Gyorgy Szing324e8af2023-03-21 03:07:20 +0100173set_color_mode() {
174 _tput=$(which tput 2>/dev/null) || true "never mind"
175
176 local mode="$1"
177
178 [ "$mode" = "auto" ] || [ "$mode" = "on" ] || [ "$mode" = "off" ] || {
179 echo "ERROR: invalid argument. Valid values are auto, on and off."
180 return 1
181 }
182
183
184 # If requested mode is on, but cannot use colors, fail with an error.
185 [ -z "$_tput" ] && [ "$mode" = "on" ] && {
186 echo "set_color_mode(), ERROR: command tput can not be found, failed to force colors to on."
187 return 1
188 }
189
190 # If tput is not available turn colors off
191 [ -z "$_tput" ] && mode="off"
192
193 # If mode == auto and running in a terminal, turn colors on
194 [ "$mode" = "auto" ] && [ -t 0 ] && mode="on"
195
196
197 # By default enable colored output if tput is present and running in a terminal
198 if [ "$mode" = "on" ]
199 then
200 COLOR_YELLOW=$(tput setaf 3)
201 COLOR_RESET=$(tput sgr0)
202 COLOR_RED=$(tput setaf 1)
203 COLOR_GREEN=$(tput setaf 2)
204 else
205 COLOR_YELLOW=
206 COLOR_RESET=
207 COLOR_RED=
208 COLOR_GREEN=
209 fi
210
211 return 0
212}
213
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100214#################### Entry point ###################################
215
216OS_ID=$(uname -o )
217
Gyorgy Szing324e8af2023-03-21 03:07:20 +0100218set_color_mode "auto"
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100219
Gyorgy Szing7ddb28a2023-01-17 15:55:36 +0100220_ccache=$(which ccache 2>/dev/null) || true "never mind"
Gyorgy Szingb164e392021-09-04 01:51:57 +0200221
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100222process_commands() {
223 local exit_code=0
224 while true
225 do
226 case $1 in
227 {% for config in data %}
228 {{config.name}})
229 {{config.name}} || {
230 exit_code=$?
Gyorgy Szinge1ccb2d2023-03-21 03:05:23 +0100231 [ "$FAIL_FAST" -ne 0 ] && break
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100232 }
233 ;;
234 {% endfor %}
235 clean)
236 do_clean || {
237 exit_code=$?
238 break
239 }
240 ;;
241 help)
242 do_help
243 break
244 ;;
245 "")
246 for _test in "${test_list[@]}"
247 do
248 "$_test" || {
249 exit_code=$?
Gyorgy Szinge1ccb2d2023-03-21 03:05:23 +0100250 [ "$FAIL_FAST" -ne 0 ] && break
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100251 }
252 done
253 break
254 ;;
255 *)
256 echo "${COLOR_RED}Error: invalid command: $1 $COLOR_RESET"
257 do_help
258 exit_code=1
259 break
260 ;;
261 esac
262 shift
263 [ 0 -eq $# ] && break
264 done
265 return $exit_code
266}
267
268while true
269do
270 # parse options
271 case $1 in
272 -p)
273 shift
274 pattern=$1
275 if [ -z "$pattern" ]
276 then
277 echo "${COLOR_RED}Error: missing argument <search regexp>. $COLOR_RESET"
278 exit_code=1
279 break
280 else
281 readarray -t test_list < <(printf "%s\n" "${test_list[@]}" | grep -- "$pattern")
282 {% raw %}
283 [ 0 -eq ${#test_list[@]} ] && {
284 {% endraw %}
285 echo "${COLOR_RED}Error: -p <regexp> matches no tests. $COLOR_RESET"
286 exit_code=1
287 break
288 }
289 fi
290 ;;
Gyorgy Szing324e8af2023-03-21 03:07:20 +0100291 --no-color)
292 set_color_mode "off"
293 ;;
294 --color)
295 set_color_mode "on"
296 ;;
Gyorgy Szinge1ccb2d2023-03-21 03:05:23 +0100297 --f|--fail-fast)
298 FAIL_FAST=1
299 ;;
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100300 *)
301 # parse commands
302 process_commands "$@" || exit_code=$?
303 break
304 ;;
305 esac
306 shift
307done
Gyorgy Szing54071d92022-01-04 12:34:14 +0100308
309exit $exit_code