1#!/bin/bash
2set -e
3
4function help() {
5    echo "Usage: $0 [build|test] [all|<build_configuration> <build_configuration>...]"
6    echo "Available build_configuration:"
7    for build in ${build_configurations[*]}; do
8        echo "  $build"
9    done
10    exit 1
11}
12
13function validate() {
14    for build in ${build_configurations[*]}; do
15        if [ "$1" == "$build" ]; then
16            return
17        fi
18    done
19    help
20}
21
22function generate() {
23    build=$1
24    cmake -Bbuild/$build -GNinja -DCMAKE_BUILD_TYPE=$build .
25}
26
27function build() {
28    cmake --build build/$1
29}
30
31function test() {
32    pushd build/$1
33    [ -z "${CTEST_PARALLEL_LEVEL}" ] && parallel="-j$2"
34    if [ -z "${CTEST_REPEAT_FAIL}" ];
35    then
36        repeat_fail=2
37    else
38        repeat_fail=${CTEST_REPEAT_FAIL}
39    fi
40    ctest $parallel --timeout 1000 -O $1.txt -T test --no-compress-output --test-output-size-passed 4194304 --test-output-size-failed 4194304 --output-on-failure --repeat until-pass:${repeat_fail} --output-junit $1.xml
41    popd
42    grep -E "^(\s*[0-9]+|Total)" build/$1/$1.txt >build/$1.txt
43    sed -i "s/\x1B\[[0-9;]*[JKmsu]//g" build/$1.txt
44    if [[ $1 = *"_coverage" ]]; then
45        ./coverage.sh $1
46    fi
47}
48
49cd $(dirname $0)
50
51# if threadx repo does not exist, clone it
52[ -d ../threadx ] || git clone https://github.com/eclipse-threadx/threadx.git ../threadx --depth 1
53
54result=$(sed -n "/(BUILD_CONFIGURATIONS/,/)/p" CMakeLists.txt|sed ':label;N;s/\n/ /;b label'|grep -Pzo "[a-zA-Z0-9_]*build[a-zA-Z0-9_]*\s*"| tr -d '\0')
55IFS=' '
56read -ra build_configurations <<< "$result"
57
58if [ $# -lt 1 ]; then
59    help
60fi
61
62command=$1
63shift
64
65if [ "$#" == "0" ]; then
66    builds=${build_configurations[0]}
67elif [ "$*" == "all" ]; then
68    builds=${build_configurations[@]}
69else
70    for item in $*; do
71        validate $item
72    done
73    builds=$*
74fi
75
76if [ "$command" == "build" ]; then
77    for item in $builds; do
78        generate $item
79        echo ""
80    done
81
82    for item in $builds; do
83        echo "Building $item"
84        build $item
85        echo ""
86    done
87elif [ "$command" == "test" ]; then
88    cores=$(nproc)
89    if [ -z "${CTEST_PARALLEL_LEVEL}" ];
90    then
91        # Run builds in parallel
92        build_counts=$(echo $builds | wc -w)
93        parallel_jobs=$(($cores / $build_counts))
94        parallel_jobs=$(($parallel_jobs + 2))
95        pids=""
96        for item in $builds; do
97            echo "Testing $item"
98            test $item $parallel_jobs &
99            pids+=" $!"
100        done
101        exit_code=0
102        for p in $pids; do
103            wait $p || exit_code=$?
104        done
105        exit $exit_code
106    else
107        # Run builds in serial
108        for item in $builds; do
109            echo "Testing $item"
110            test $item $parallel_jobs
111        done
112    fi
113else
114    help
115fi
116