1#!/usr/bin/env bash
2# Copyright 2018 Oticon A/S
3# SPDX-License-Identifier: Apache-2.0
4
5# Compile with all permutations of a given set of KConfigs
6# Specifically for going through possible combinations of
7# optional control procedures
8
9#set -x #uncomment this line for debugging
10# set DEBUG_PERMUTATE to 'true' for extra debug output
11DEBUG_PERMUTATE=false
12
13: "${ZEPHYR_BASE:?ZEPHYR_BASE must be set to point to the zephyr root\
14 directory}"
15
16source ${ZEPHYR_BASE}/tests/bsim/compile.source
17BOARD_ROOT="${BOARD_ROOT:-${ZEPHYR_BASE}}"
18BOARD_TS="${BOARD//\//_}"
19
20mkdir -p ${WORK_DIR}
21
22declare -a list=(
23"CONFIG_BT_CENTRAL="
24"CONFIG_BT_PERIPHERAL="
25"CONFIG_BT_CTLR_PER_INIT_FEAT_XCHG="
26"CONFIG_BT_DATA_LEN_UPDATE="
27"CONFIG_BT_PHY_UPDATE="
28"CONFIG_BT_CTLR_MIN_USED_CHAN="
29"CONFIG_BT_CTLR_LE_PING="
30"CONFIG_BT_CTLR_LE_ENC="
31"CONFIG_BT_CTLR_CONN_PARAM_REQ="
32)
33
34perm_compile() {
35    local -a results=()
36    # We set a unique exe-name, so that we don't overwrite the executables
37    # created by the compile scripts since that may mess up other tests
38    # We also delete the executable to avoid having artifacts from
39    # a previous run
40    local exe_name="bs_${BOARD_TS}_tests_kconfig_perm"
41    local executable_name=${exe_name}
42    local executable_name=${BSIM_OUT_PATH}/bin/$executable_name
43
44    rm -f ${executable_name}
45
46    let idx=$2
47    for (( j = 0; j < $1; j++ )); do
48        if (( idx % 2 )); then
49           results=("${results[@]}" "${list[$j]}n")
50        else
51           results=("${results[@]}" "${list[$j]}y")
52        fi
53        let idx\>\>=1
54    done
55    printf '%s\n' "${results[@]}" > $3
56    if test "$DEBUG_PERMUTATE" = "true"; then
57	echo "Compile with config overlay:"
58	cat $3
59    fi
60    local app=tests/bsim/bluetooth/ll/edtt/hci_test_app
61    local conf_file=prj_dut_llcp.conf
62    local conf_overlay=$3
63# Note: we need to call '_compile' instead of 'compile' because the latter starts
64# compilations in parallel, but here we need to do it in serial since we modify
65# the configuration file between each run
66    _compile
67    if [ ! -f ${executable_name} ]; then
68    	compile_failures=$(expr $compile_failures + 1)
69    fi
70}
71let n=${#list[@]}
72temp_conf_file=$(mktemp -p ${WORK_DIR})
73# compile_failures will be equal to the number of failed compilations
74let compile_failures=0
75
76for (( i = 0; i < 2**n; i++ )); do
77    ## don't compile for CENTRAL=n AND PERIPHERAL=n
78    if (( (i & 0x3) != 0x3 )); then
79        perm_compile $n $i ${temp_conf_file}
80    fi
81done
82
83# We set exit code based on type of failure
84# 0 means all configurations compiled w/o error
85
86trap "{ rm "${temp_conf_file}" ; exit 255; }" SIGINT
87trap "{ rm "${temp_conf_file}" ; exit 254; }" SIGTERM
88trap "{ rm "${temp_conf_file}" ; exit 253; }" ERR
89trap "{ rm "${temp_conf_file}" ; exit ${compile_failures}; }" EXIT
90