1# Copyright 2023 Nordic Semiconductor ASA
2# SPDX-License-Identifier: Apache-2.0
3
4_process_ids="";
5
6#All user scripts require these variable, let's check for them here already
7: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
8
9#Give a default value to BOARD if it does not have one yet:
10BOARD="${BOARD:-nrf52_bsim/native}"
11
12#The board target full string (with qualifiers):
13BOARD_TS="${BOARD//\//_}"
14
15function run_in_background(){
16  $@ & _process_ids="$_process_ids $!"
17}
18
19function wait_for_background_jobs(){
20  exit_code=0
21  for process_id in $_process_ids; do
22    wait $process_id || let "exit_code=$?"
23  done
24  [ $exit_code -eq 0 ] || exit $exit_code
25}
26
27trap ctrl_c INT
28
29function ctrl_c() {
30  echo "Aborted by CTRL-C"
31
32  for process_id in $_process_ids; do
33    kill -15 $process_id
34  done
35}
36
37function check_program_exists() {
38  if [ ! -f $1 ]; then
39    echo -e "  \e[91m`pwd`/`basename $1` cannot be found (did you forget to\
40 compile it?)\e[39m"
41    exit 1
42  fi
43}
44
45function Execute() {
46  EXECUTE_TIMEOUT="${EXECUTE_TIMEOUT:-30}"
47
48  check_program_exists $1
49  run_in_background timeout --kill-after=5 -v ${EXECUTE_TIMEOUT} $@
50}
51
52function _guess_test_relpath(){
53  local PA="$(cd -- "$(dirname "${BASH_SOURCE[2]}")" && pwd)"
54  PA="$(realpath --relative-to "${ZEPHYR_BASE}" "${PA}")"
55  echo $PA | sed -E 's/\/tests?_scripts//'
56}
57
58# For a caller running from a tests_script/ folder, get the path of its parent
59# relative to $ZEPHYR_BASE
60# This must be run without/before cd'ing into another directory
61function guess_test_relpath(){
62  echo $(_guess_test_relpath)
63}
64
65# For a caller running from a tests_script/ folder, get the path of its parent
66# relative to $ZEPHYR_BASE, replacing "/" with "_"
67# This must be run without/before cd'ing into another directory
68function guess_test_long_name(){
69  echo $(_guess_test_relpath) | tr / _
70}
71