1#!/bin/bash
2
3set -e
4
5function help() {
6    echo "Usage: $0 [build|test] [all|<build_configuration> <build_configuration>...]"
7    echo "Available build_configuration:"
8    for build in ${build_configurations[*]}; do
9        echo "  $build"
10    done
11    exit 1
12}
13
14function validate() {
15    for build in ${build_configurations[*]}; do
16        if [ "$1" == "$build" ]; then
17            return
18        fi
19    done
20    help
21}
22
23function generate() {
24    build=$1
25    cmake -Bbuild/$build -GNinja -DBUILD_SHARED_LIBS=ON -DCMAKE_TOOLCHAIN_FILE=$(dirname $(realpath $0))/../cmake/linux.cmake -DCMAKE_BUILD_TYPE=$build .
26}
27
28function build() {
29    cmake --build build/$1
30}
31
32function build_libs() {
33    cmake -Bbuild/libs -GNinja -DBUILD_SHARED_LIBS=ON -DCMAKE_TOOLCHAIN_FILE=$(dirname $(realpath $0))/../cmake/linux.cmake libs
34    cmake --build build/libs
35}
36
37function test() {
38    pushd build/$1
39    [ -z "${CTEST_PARALLEL_LEVEL}" ] && parallel="-j$2"
40    if [ -z "${CTEST_REPEAT_FAIL}" ];
41    then
42        repeat_fail=2
43    else
44        repeat_fail=${CTEST_REPEAT_FAIL}
45    fi
46    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
47    popd
48    grep -E "^(\s*[0-9]+|Total)" build/$1/$1.txt >build/$1.txt
49    sed -i "s/\x1B\[[0-9;]*[JKmsu]//g" build/$1.txt
50    if [[ $1 = *"_coverage" ]]; then
51        ./coverage.sh $1
52    fi
53}
54
55cd $(dirname $0)
56
57result=$(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')
58IFS=' '
59read -ra build_configurations <<< "$result"
60
61if [ $# -lt 1 ]; then
62    help
63fi
64
65command=$1
66shift
67
68if [ "$#" == "0" ]; then
69    builds=${build_configurations[0]}
70elif [ "$*" == "all" ]; then
71    builds=${build_configurations[@]}
72else
73    for item in $*; do
74        validate $item
75    done
76    builds=$*
77fi
78
79if [ "$command" == "build" ]; then
80    for item in $builds; do
81        generate $item
82        echo ""
83    done
84
85    for item in $builds; do
86        echo "Building $item"
87        build $item
88        echo ""
89    done
90elif [ "$command" == "test" ]; then
91    cores=$(nproc)
92    if [ -z "${CTEST_PARALLEL_LEVEL}" ];
93    then
94        # Run builds in parallel
95        build_counts=$(echo $builds | wc -w)
96        parallel_jobs=$(($cores / $build_counts))
97        parallel_jobs=$(($parallel_jobs + 2))
98        pids=""
99        for item in $builds; do
100            echo "Testing $item"
101            test $item $parallel_jobs &
102            pids+=" $!"
103        done
104        exit_code=0
105        for p in $pids; do
106            wait $p || exit_code=$?
107        done
108        exit $exit_code
109    else
110        # Run builds in serial
111        for item in $builds; do
112            echo "Testing $item"
113            test $item $parallel_jobs
114        done
115    fi
116elif [ "$command" == "build_libs" ]; then
117    build_libs
118else
119    help
120fi
121