blob: 0651f394356d7fcf1f455c4b7c78a96f585fb685 [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
Imre Kis76999fc2023-03-31 12:32:10 +020028NUMBER_OF_PROCESSORS=${NUMBER_OF_PROCESSORS:-$(( $(nproc) * 2 ))}
Gyorgy Szingba1095c2020-11-24 00:33:11 +010029
Gyorgy Szinge1ccb2d2023-03-21 03:05:23 +010030# By default run as much as we can
31FAIL_FAST=${FAIL_FAST:-0}
32
Gyorgy Szingb73b5c22024-02-01 16:30:04 +000033# Directory to install projects to.
34INSTALL_PREFIX=${INSTALL_PREFIX:-./install}
35
36# Directory to place cmake build directories to
37BUILD_PREFIX=${BUILD_PREFIX:-.}
38
Gyorgy Szing54071d92022-01-04 12:34:14 +010039# Global exit code.
40exit_code=0
41
Gyorgy Szingf24a03a2023-03-21 03:00:40 +010042# List of supported tests.
43test_list=(
44{% for config in data %}
45 "{{config.name}}"
46{% endfor %}
47)
48
Gyorgy Szingba1095c2020-11-24 00:33:11 +010049# Convert test name to build directory
50function name-to-bdir() {
Gyorgy Szingb73b5c22024-02-01 16:30:04 +000051 printf "$BUILD_PREFIX/build-%s" "$1"
52}
53
54unset btest_logdir_root
55function name-to-logfile {
56 local config_name="$1"
57 if [ -z "$btest_logdir_root" ]
58 then
59 printf "%s/build.log" $(name-to-bdir "$1")
60 else
61 printf "$btest_logdir_root/build-%s.log" "$1"
62 fi
Gyorgy Szingba1095c2020-11-24 00:33:11 +010063}
64
65# Wrap cmake to allow verbose vs non-verbose mode
66function _cmake() {
67 log_file=$1
68 shift
69 if [ "$VERBOSE" != "0" ]
70 then
71 cmake "$@" 2>&1 | tee -a "$log_file"
72 return ${PIPESTATUS[0]}
73 else
74 cmake "$@" >>"$log_file" 2>&1
75 fi
76}
77
78{% for config in data %}
79# Run build test "{{config.name}}"
80{{config.name}}() {
81 echo "##################################################################"
82 echo "########################## {{config.name}} started."
83
84 {% if config.os_id is defined %}
85 if [ "$OS_ID" != "{{config.os_id}}" ]
86 then
87 echo "Test case is not supported on this host."
Gyorgy Szing54071d92022-01-04 12:34:14 +010088 echo "########################## $COLOR_YELLOW {{config.name}} skipped $COLOR_RESET"
Gyorgy Szingba1095c2020-11-24 00:33:11 +010089 echo "##################################################################"
90 return
91 fi
92 {% endif %}
93 b_dir=$(name-to-bdir "{{config.name}}")
Gyorgy Szingb73b5c22024-02-01 16:30:04 +000094 log_file=$(name-to-logfile "{{config.name}}")
Gyorgy Szingba1095c2020-11-24 00:33:11 +010095 rm -rf "$b_dir"
96 mkdir -p "$b_dir"
97
Gyorgy Szingb164e392021-09-04 01:51:57 +020098 if [ -n "$_ccache" ]
99 then
100 ccache_option=-DCMAKE_C_COMPILER_LAUNCHER=$_ccache
101 fi
102
Gyorgy Szing54071d92022-01-04 12:34:14 +0100103 local retval=0
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100104
Gyorgy Szing54071d92022-01-04 12:34:14 +0100105 # jinja2 is removing single newlines. Adding a comment stops this behavior.
Gyorgy Szinga962db72023-03-21 03:11:00 +0100106 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 +0100107 then
Gyorgy Szinga962db72023-03-21 03:11:00 +0100108 if _cmake "$log_file" --build "$b_dir" -j "${NUMBER_OF_PROCESSORS}" --verbose
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100109 then
Gyorgy Szingb73b5c22024-02-01 16:30:04 +0000110 if _cmake "$log_file" --install "$b_dir" --prefix "$INSTALL_PREFIX"
Gyorgy Szing54071d92022-01-04 12:34:14 +0100111 then
112 echo "########################## $COLOR_GREEN {{config.name}} passed $COLOR_RESET"
113 else
114 retval=$?
115 echo "For details see: $log_file"
116 echo "########################## $COLOR_RED {{config.name}} failed $COLOR_RESET"
117 fi
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100118 else
Gyorgy Szing54071d92022-01-04 12:34:14 +0100119 retval=$?
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100120 echo "For details see: $log_file"
121 echo "########################## $COLOR_RED {{config.name}} failed $COLOR_RESET"
122 fi
123 else
Gyorgy Szing54071d92022-01-04 12:34:14 +0100124 retval=$?
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100125 echo "For details see: $log_file"
126 echo "########################## $COLOR_RED {{config.name}} failed $COLOR_RESET"
127 fi
128 echo "##################################################################"
Gyorgy Szing54071d92022-01-04 12:34:14 +0100129 return $retval
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100130}
131
132{% endfor %}
133
134# Clean intermediate files
135do_clean() {
136 {% for config in data %}
137 b_dir=$(name-to-bdir "{{config.name}}")
138 if [ -d "$b_dir" ]
139 then
140 echo "Removing $b_dir"
Gyorgy Szing54071d92022-01-04 12:34:14 +0100141 rm -rf "$b_dir"
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100142 fi
143 {% endfor %}
144}
145
146# Print usage info
147do_help() {
148 cat <<END_HELP
149Build test runner
150=================
151
152Invocation::
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100153 ``$0 [<options>] [<command>]``
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100154
155The file "user.env" is sourced from the current directory. Use it to set
156environment specific defaults. For config variables see the start of this script
157and any "$<XXXX>" in the "params" array of any command in test_data.yaml
158Some variables to note
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100159 - VERBOSE : <0/1> make the script output more info.
160 VERBOSE=$VERBOSE
161 - TS_ROOT : Root directory of the TS repo.
162 TS_ROOT=$TS_ROOT
163 - NUMBER_OF_PROCESSORS: number of processors in the system. Used for setting
164 the number of parallel processes during build
165 NUMBER_OF_PROCESSORS=$NUMBER_OF_PROCESSORS
166 - CMAKE_EXTRA_FLAGS: additional environment specific CMake flags
167 CMAKE_EXTRA_FLAGS=-DNEWLIB_LIBC_PATH=/path/to/newlib
Gyorgy Szingb73b5c22024-02-01 16:30:04 +0000168 - FAIL_FAST: <0/1> see -f
169 FAIL_FAST=$FAIL_FAST
170 - INSTALL_PREFIX: <path> see -i
171 INSTALL_PREFIX=$INSTALL_PREFIX
172 - BUILD_PREFIX: <path> see -b
173 BUILD_PREFIX=$BUILD_PREFIX
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100174
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100175Options:
Gyorgy Szingb73b5c22024-02-01 16:30:04 +0000176 -b|--build-prefix: <path>
177 - directory for cmake build directories
Gyorgy Szing324e8af2023-03-21 03:07:20 +0100178 --color|--no-color
Gyorgy Szingb73b5c22024-02-01 16:30:04 +0000179 - force or disable output coloring
180 -f|--fail-fast
181 - exit after the first test failure
182 -i|--install-prefix <path>
183 - directory to install build results to.
184 -l|--log-dir <path>
185 - directory to save log files to. If not set, files are saved
186 to the build directory.
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100187 -p <regexp>
188 - filter out all test cases whose name is not matching regexp.
189 regexp is a regular expression understood by grep
190Commands:
191 "" - no command/default -> run all test cases
192 clean - remove build directories
193 help - print this text
194 <test case> - run the specified test. This can be specified multiple times
195 to allow running a list of tests.
196 available test cases $([ -n "$pattern" ] && echo "( matching '$pattern' )"):
197 $(echo "${test_list[@]}" | sed "s/ /\n /g")
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100198END_HELP
199}
200
Gyorgy Szing324e8af2023-03-21 03:07:20 +0100201set_color_mode() {
202 _tput=$(which tput 2>/dev/null) || true "never mind"
203
204 local mode="$1"
205
206 [ "$mode" = "auto" ] || [ "$mode" = "on" ] || [ "$mode" = "off" ] || {
207 echo "ERROR: invalid argument. Valid values are auto, on and off."
208 return 1
209 }
210
211
212 # If requested mode is on, but cannot use colors, fail with an error.
213 [ -z "$_tput" ] && [ "$mode" = "on" ] && {
214 echo "set_color_mode(), ERROR: command tput can not be found, failed to force colors to on."
215 return 1
216 }
217
218 # If tput is not available turn colors off
219 [ -z "$_tput" ] && mode="off"
220
221 # If mode == auto and running in a terminal, turn colors on
222 [ "$mode" = "auto" ] && [ -t 0 ] && mode="on"
223
224
225 # By default enable colored output if tput is present and running in a terminal
226 if [ "$mode" = "on" ]
227 then
228 COLOR_YELLOW=$(tput setaf 3)
229 COLOR_RESET=$(tput sgr0)
230 COLOR_RED=$(tput setaf 1)
231 COLOR_GREEN=$(tput setaf 2)
232 else
233 COLOR_YELLOW=
234 COLOR_RESET=
235 COLOR_RED=
236 COLOR_GREEN=
237 fi
238
239 return 0
240}
241
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100242#################### Entry point ###################################
243
244OS_ID=$(uname -o )
245
Gyorgy Szing324e8af2023-03-21 03:07:20 +0100246set_color_mode "auto"
Gyorgy Szingba1095c2020-11-24 00:33:11 +0100247
Gyorgy Szing7ddb28a2023-01-17 15:55:36 +0100248_ccache=$(which ccache 2>/dev/null) || true "never mind"
Gyorgy Szingb164e392021-09-04 01:51:57 +0200249
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100250process_commands() {
251 local exit_code=0
252 while true
253 do
254 case $1 in
255 {% for config in data %}
256 {{config.name}})
257 {{config.name}} || {
258 exit_code=$?
Gyorgy Szinge1ccb2d2023-03-21 03:05:23 +0100259 [ "$FAIL_FAST" -ne 0 ] && break
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100260 }
261 ;;
262 {% endfor %}
263 clean)
264 do_clean || {
265 exit_code=$?
266 break
267 }
268 ;;
269 help)
270 do_help
271 break
272 ;;
273 "")
274 for _test in "${test_list[@]}"
275 do
276 "$_test" || {
277 exit_code=$?
Gyorgy Szinge1ccb2d2023-03-21 03:05:23 +0100278 [ "$FAIL_FAST" -ne 0 ] && break
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100279 }
280 done
281 break
282 ;;
283 *)
284 echo "${COLOR_RED}Error: invalid command: $1 $COLOR_RESET"
285 do_help
286 exit_code=1
287 break
288 ;;
289 esac
290 shift
291 [ 0 -eq $# ] && break
292 done
293 return $exit_code
294}
295
296while true
297do
298 # parse options
Gyorgy Szingb73b5c22024-02-01 16:30:04 +0000299 arg=$1
300 case $arg in
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100301 -p)
302 shift
303 pattern=$1
304 if [ -z "$pattern" ]
305 then
306 echo "${COLOR_RED}Error: missing argument <search regexp>. $COLOR_RESET"
307 exit_code=1
308 break
309 else
310 readarray -t test_list < <(printf "%s\n" "${test_list[@]}" | grep -- "$pattern")
311 {% raw %}
312 [ 0 -eq ${#test_list[@]} ] && {
313 {% endraw %}
314 echo "${COLOR_RED}Error: -p <regexp> matches no tests. $COLOR_RESET"
315 exit_code=1
316 break
317 }
318 fi
319 ;;
Gyorgy Szing324e8af2023-03-21 03:07:20 +0100320 --no-color)
321 set_color_mode "off"
322 ;;
323 --color)
324 set_color_mode "on"
325 ;;
Gyorgy Szingb73b5c22024-02-01 16:30:04 +0000326 -f|--fail-fast)
Gyorgy Szinge1ccb2d2023-03-21 03:05:23 +0100327 FAIL_FAST=1
328 ;;
Gyorgy Szingb73b5c22024-02-01 16:30:04 +0000329 -l|--log-dir)
330 shift
331 btest_logdir_root=$1
332 [ -z "$btest_logdir_root" ] && {
333 echo "${COLOR_RED}Error: missing argument <path>. $COLOR_RESET"
334 exit_code=1
335 break
336 }
337
338 [ -d "$btest_logdir_root" ] || {
339 mkdir -p "$btest_logdir_root"
340 }
341 ;;
342 -i|--install-prefix)
343 shift
344 INSTALL_PREFIX="$1"
345 [ -z "$INSTALL_PREFIX" ] && {
346 echo "${COLOR_RED}Error: missing argument <path> after $arg. $COLOR_RESET"
347 exit_code=1
348 break
349 }
350
351 [ -d "$INSTALL_PREFIX" ] || {
352 echo "${COLOR_RED}Error: install directory $INSTALL_PREFIX can not be found. $COLOR_RESET"
353 exit_code=1
354 break
355 }
356 ;;
357 -b|--build-prefix)
358 shift
359 BUILD_PREFIX="$1"
360 [ -z "$BUILD_PREFIX" ] && {
361 echo "${COLOR_RED}Error: missing argument <path> after $arg. $COLOR_RESET"
362 exit_code=1
363 break
364 }
365
366 [ -d "$BUILD_PREFIX" ] || {
367 echo "${COLOR_RED}Error: build directory $BUILD_PREFIX can not be found. $COLOR_RESET"
368 exit_code=1
369 break
370 }
371 ;;
Gyorgy Szingf24a03a2023-03-21 03:00:40 +0100372 *)
373 # parse commands
374 process_commands "$@" || exit_code=$?
375 break
376 ;;
377 esac
378 shift
379done
Gyorgy Szing54071d92022-01-04 12:34:14 +0100380
381exit $exit_code