Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
Gyorgy Szing | 7ddb28a | 2023-01-17 15:55:36 +0100 | [diff] [blame] | 3 | # Copyright (c) 2020-2023, Arm Limited and contributors. All rights reserved. |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
| 8 | # Exit with error if any command fails |
| 9 | set -e |
| 10 | |
| 11 | # Read in user specific environment settings |
| 12 | if [ -e user.env ] |
| 13 | then |
| 14 | echo "Reding user specific settings from user.env" |
| 15 | source user.env |
| 16 | fi |
| 17 | |
| 18 | # Default to non-verbose mode. |
| 19 | VERBOSE=${VERBOSE:-0} |
| 20 | |
Imre Kis | f9dda4a | 2022-01-18 18:54:15 +0100 | [diff] [blame] | 21 | # Additional environment specific CMake flags |
| 22 | CMAKE_EXTRA_FLAGS=${CMAKE_EXTRA_FLAGS:-""} |
| 23 | |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 24 | # Get root of TS repo. |
| 25 | TS_ROOT=${TS_ROOT:-$(git rev-parse --show-toplevel)} |
| 26 | |
| 27 | # Number of threads to use in parallel |
Imre Kis | 76999fc | 2023-03-31 12:32:10 +0200 | [diff] [blame] | 28 | NUMBER_OF_PROCESSORS=${NUMBER_OF_PROCESSORS:-$(( $(nproc) * 2 ))} |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 29 | |
Gyorgy Szing | e1ccb2d | 2023-03-21 03:05:23 +0100 | [diff] [blame] | 30 | # By default run as much as we can |
| 31 | FAIL_FAST=${FAIL_FAST:-0} |
| 32 | |
Gyorgy Szing | b73b5c2 | 2024-02-01 16:30:04 +0000 | [diff] [blame] | 33 | # Directory to install projects to. |
| 34 | INSTALL_PREFIX=${INSTALL_PREFIX:-./install} |
| 35 | |
| 36 | # Directory to place cmake build directories to |
| 37 | BUILD_PREFIX=${BUILD_PREFIX:-.} |
| 38 | |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 39 | # Global exit code. |
| 40 | exit_code=0 |
| 41 | |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 42 | # List of supported tests. |
| 43 | test_list=( |
| 44 | {% for config in data %} |
| 45 | "{{config.name}}" |
| 46 | {% endfor %} |
| 47 | ) |
| 48 | |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 49 | # Convert test name to build directory |
| 50 | function name-to-bdir() { |
Gyorgy Szing | b73b5c2 | 2024-02-01 16:30:04 +0000 | [diff] [blame] | 51 | printf "$BUILD_PREFIX/build-%s" "$1" |
| 52 | } |
| 53 | |
| 54 | unset btest_logdir_root |
| 55 | function 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 Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | # Wrap cmake to allow verbose vs non-verbose mode |
| 66 | function _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 Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 88 | echo "########################## $COLOR_YELLOW {{config.name}} skipped $COLOR_RESET" |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 89 | echo "##################################################################" |
| 90 | return |
| 91 | fi |
| 92 | {% endif %} |
| 93 | b_dir=$(name-to-bdir "{{config.name}}") |
Gyorgy Szing | b73b5c2 | 2024-02-01 16:30:04 +0000 | [diff] [blame] | 94 | log_file=$(name-to-logfile "{{config.name}}") |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 95 | rm -rf "$b_dir" |
| 96 | mkdir -p "$b_dir" |
| 97 | |
Gyorgy Szing | b164e39 | 2021-09-04 01:51:57 +0200 | [diff] [blame] | 98 | if [ -n "$_ccache" ] |
| 99 | then |
| 100 | ccache_option=-DCMAKE_C_COMPILER_LAUNCHER=$_ccache |
| 101 | fi |
| 102 | |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 103 | local retval=0 |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 104 | |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 105 | # jinja2 is removing single newlines. Adding a comment stops this behavior. |
Gyorgy Szing | a962db7 | 2023-03-21 03:11:00 +0100 | [diff] [blame] | 106 | 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 Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 107 | then |
Gyorgy Szing | a962db7 | 2023-03-21 03:11:00 +0100 | [diff] [blame] | 108 | if _cmake "$log_file" --build "$b_dir" -j "${NUMBER_OF_PROCESSORS}" --verbose |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 109 | then |
Gyorgy Szing | b73b5c2 | 2024-02-01 16:30:04 +0000 | [diff] [blame] | 110 | if _cmake "$log_file" --install "$b_dir" --prefix "$INSTALL_PREFIX" |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 111 | 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 Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 118 | else |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 119 | retval=$? |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 120 | echo "For details see: $log_file" |
| 121 | echo "########################## $COLOR_RED {{config.name}} failed $COLOR_RESET" |
| 122 | fi |
| 123 | else |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 124 | retval=$? |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 125 | echo "For details see: $log_file" |
| 126 | echo "########################## $COLOR_RED {{config.name}} failed $COLOR_RESET" |
| 127 | fi |
| 128 | echo "##################################################################" |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 129 | return $retval |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | {% endfor %} |
| 133 | |
| 134 | # Clean intermediate files |
| 135 | do_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 Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 141 | rm -rf "$b_dir" |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 142 | fi |
| 143 | {% endfor %} |
| 144 | } |
| 145 | |
| 146 | # Print usage info |
| 147 | do_help() { |
| 148 | cat <<END_HELP |
| 149 | Build test runner |
| 150 | ================= |
| 151 | |
| 152 | Invocation:: |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 153 | ``$0 [<options>] [<command>]`` |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 154 | |
| 155 | The file "user.env" is sourced from the current directory. Use it to set |
| 156 | environment specific defaults. For config variables see the start of this script |
| 157 | and any "$<XXXX>" in the "params" array of any command in test_data.yaml |
| 158 | Some variables to note |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 159 | - 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 Szing | b73b5c2 | 2024-02-01 16:30:04 +0000 | [diff] [blame] | 168 | - 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 Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 174 | |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 175 | Options: |
Gyorgy Szing | b73b5c2 | 2024-02-01 16:30:04 +0000 | [diff] [blame] | 176 | -b|--build-prefix: <path> |
| 177 | - directory for cmake build directories |
Gyorgy Szing | 324e8af | 2023-03-21 03:07:20 +0100 | [diff] [blame] | 178 | --color|--no-color |
Gyorgy Szing | b73b5c2 | 2024-02-01 16:30:04 +0000 | [diff] [blame] | 179 | - 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 Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 187 | -p <regexp> |
| 188 | - filter out all test cases whose name is not matching regexp. |
| 189 | regexp is a regular expression understood by grep |
| 190 | Commands: |
| 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 Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 198 | END_HELP |
| 199 | } |
| 200 | |
Gyorgy Szing | 324e8af | 2023-03-21 03:07:20 +0100 | [diff] [blame] | 201 | set_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 Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 242 | #################### Entry point ################################### |
| 243 | |
| 244 | OS_ID=$(uname -o ) |
| 245 | |
Gyorgy Szing | 324e8af | 2023-03-21 03:07:20 +0100 | [diff] [blame] | 246 | set_color_mode "auto" |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 247 | |
Gyorgy Szing | 7ddb28a | 2023-01-17 15:55:36 +0100 | [diff] [blame] | 248 | _ccache=$(which ccache 2>/dev/null) || true "never mind" |
Gyorgy Szing | b164e39 | 2021-09-04 01:51:57 +0200 | [diff] [blame] | 249 | |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 250 | process_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 Szing | e1ccb2d | 2023-03-21 03:05:23 +0100 | [diff] [blame] | 259 | [ "$FAIL_FAST" -ne 0 ] && break |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 260 | } |
| 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 Szing | e1ccb2d | 2023-03-21 03:05:23 +0100 | [diff] [blame] | 278 | [ "$FAIL_FAST" -ne 0 ] && break |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 279 | } |
| 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 | |
| 296 | while true |
| 297 | do |
| 298 | # parse options |
Gyorgy Szing | b73b5c2 | 2024-02-01 16:30:04 +0000 | [diff] [blame] | 299 | arg=$1 |
| 300 | case $arg in |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 301 | -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 Szing | 324e8af | 2023-03-21 03:07:20 +0100 | [diff] [blame] | 320 | --no-color) |
| 321 | set_color_mode "off" |
| 322 | ;; |
| 323 | --color) |
| 324 | set_color_mode "on" |
| 325 | ;; |
Gyorgy Szing | b73b5c2 | 2024-02-01 16:30:04 +0000 | [diff] [blame] | 326 | -f|--fail-fast) |
Gyorgy Szing | e1ccb2d | 2023-03-21 03:05:23 +0100 | [diff] [blame] | 327 | FAIL_FAST=1 |
| 328 | ;; |
Gyorgy Szing | b73b5c2 | 2024-02-01 16:30:04 +0000 | [diff] [blame] | 329 | -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 Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 372 | *) |
| 373 | # parse commands |
| 374 | process_commands "$@" || exit_code=$? |
| 375 | break |
| 376 | ;; |
| 377 | esac |
| 378 | shift |
| 379 | done |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 380 | |
| 381 | exit $exit_code |