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 | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 33 | # Global exit code. |
| 34 | exit_code=0 |
| 35 | |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 36 | # List of supported tests. |
| 37 | test_list=( |
| 38 | {% for config in data %} |
| 39 | "{{config.name}}" |
| 40 | {% endfor %} |
| 41 | ) |
| 42 | |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 43 | # Convert test name to build directory |
| 44 | function name-to-bdir() { |
| 45 | printf "./build-%s" "$1" |
| 46 | } |
| 47 | |
| 48 | # Wrap cmake to allow verbose vs non-verbose mode |
| 49 | function _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 Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 71 | echo "########################## $COLOR_YELLOW {{config.name}} skipped $COLOR_RESET" |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 72 | 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 Szing | b164e39 | 2021-09-04 01:51:57 +0200 | [diff] [blame] | 81 | if [ -n "$_ccache" ] |
| 82 | then |
| 83 | ccache_option=-DCMAKE_C_COMPILER_LAUNCHER=$_ccache |
| 84 | fi |
| 85 | |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 86 | local retval=0 |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 87 | |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 88 | # jinja2 is removing single newlines. Adding a comment stops this behavior. |
Gyorgy Szing | a962db7 | 2023-03-21 03:11:00 +0100 | [diff] [blame] | 89 | 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] | 90 | then |
Gyorgy Szing | a962db7 | 2023-03-21 03:11:00 +0100 | [diff] [blame] | 91 | if _cmake "$log_file" --build "$b_dir" -j "${NUMBER_OF_PROCESSORS}" --verbose |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 92 | then |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 93 | 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 Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 101 | else |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 102 | retval=$? |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 103 | echo "For details see: $log_file" |
| 104 | echo "########################## $COLOR_RED {{config.name}} failed $COLOR_RESET" |
| 105 | fi |
| 106 | else |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 107 | retval=$? |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 108 | echo "For details see: $log_file" |
| 109 | echo "########################## $COLOR_RED {{config.name}} failed $COLOR_RESET" |
| 110 | fi |
| 111 | echo "##################################################################" |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 112 | return $retval |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | {% endfor %} |
| 116 | |
| 117 | # Clean intermediate files |
| 118 | do_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 Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 124 | rm -rf "$b_dir" |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 125 | fi |
| 126 | {% endfor %} |
| 127 | } |
| 128 | |
| 129 | # Print usage info |
| 130 | do_help() { |
| 131 | cat <<END_HELP |
| 132 | Build test runner |
| 133 | ================= |
| 134 | |
| 135 | Invocation:: |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 136 | ``$0 [<options>] [<command>]`` |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 137 | |
| 138 | The file "user.env" is sourced from the current directory. Use it to set |
| 139 | environment specific defaults. For config variables see the start of this script |
| 140 | and any "$<XXXX>" in the "params" array of any command in test_data.yaml |
| 141 | Some variables to note |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 142 | - 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 Szing | e1ccb2d | 2023-03-21 03:05:23 +0100 | [diff] [blame] | 151 | - FAIL_FAST: <0/1> see -f |
| 152 | FAIL_FAST=$FAIL_FAST |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 153 | |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 154 | Options: |
Gyorgy Szing | 324e8af | 2023-03-21 03:07:20 +0100 | [diff] [blame] | 155 | --color|--no-color |
| 156 | - force or disable output coloring |
Gyorgy Szing | e1ccb2d | 2023-03-21 03:05:23 +0100 | [diff] [blame] | 157 | -f|--fail-fast |
| 158 | - exit after the first test failure |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 159 | -p <regexp> |
| 160 | - filter out all test cases whose name is not matching regexp. |
| 161 | regexp is a regular expression understood by grep |
| 162 | Commands: |
| 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 Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 170 | END_HELP |
| 171 | } |
| 172 | |
Gyorgy Szing | 324e8af | 2023-03-21 03:07:20 +0100 | [diff] [blame] | 173 | set_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 Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 214 | #################### Entry point ################################### |
| 215 | |
| 216 | OS_ID=$(uname -o ) |
| 217 | |
Gyorgy Szing | 324e8af | 2023-03-21 03:07:20 +0100 | [diff] [blame] | 218 | set_color_mode "auto" |
Gyorgy Szing | ba1095c | 2020-11-24 00:33:11 +0100 | [diff] [blame] | 219 | |
Gyorgy Szing | 7ddb28a | 2023-01-17 15:55:36 +0100 | [diff] [blame] | 220 | _ccache=$(which ccache 2>/dev/null) || true "never mind" |
Gyorgy Szing | b164e39 | 2021-09-04 01:51:57 +0200 | [diff] [blame] | 221 | |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 222 | process_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 Szing | e1ccb2d | 2023-03-21 03:05:23 +0100 | [diff] [blame] | 231 | [ "$FAIL_FAST" -ne 0 ] && break |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 232 | } |
| 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 Szing | e1ccb2d | 2023-03-21 03:05:23 +0100 | [diff] [blame] | 250 | [ "$FAIL_FAST" -ne 0 ] && break |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 251 | } |
| 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 | |
| 268 | while true |
| 269 | do |
| 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 Szing | 324e8af | 2023-03-21 03:07:20 +0100 | [diff] [blame] | 291 | --no-color) |
| 292 | set_color_mode "off" |
| 293 | ;; |
| 294 | --color) |
| 295 | set_color_mode "on" |
| 296 | ;; |
Gyorgy Szing | e1ccb2d | 2023-03-21 03:05:23 +0100 | [diff] [blame] | 297 | --f|--fail-fast) |
| 298 | FAIL_FAST=1 |
| 299 | ;; |
Gyorgy Szing | f24a03a | 2023-03-21 03:00:40 +0100 | [diff] [blame] | 300 | *) |
| 301 | # parse commands |
| 302 | process_commands "$@" || exit_code=$? |
| 303 | break |
| 304 | ;; |
| 305 | esac |
| 306 | shift |
| 307 | done |
Gyorgy Szing | 54071d9 | 2022-01-04 12:34:14 +0100 | [diff] [blame] | 308 | |
| 309 | exit $exit_code |