|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +function 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 | + |
| 14 | +function validate() { |
| 15 | + for build in ${build_configurations[*]}; do |
| 16 | + if [ "$1" == "$build" ]; then |
| 17 | + return |
| 18 | + fi |
| 19 | + done |
| 20 | + help |
| 21 | +} |
| 22 | + |
| 23 | +function 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 | + |
| 28 | +function build() { |
| 29 | + cmake --build build/$1 |
| 30 | +} |
| 31 | + |
| 32 | +function 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 | + |
| 37 | +function 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} |
| 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 | + |
| 55 | +cd $(dirname $0) |
| 56 | + |
| 57 | +result=$(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') |
| 58 | +IFS=' ' |
| 59 | +read -ra build_configurations <<< "$result" |
| 60 | + |
| 61 | +if [ $# -lt 1 ]; then |
| 62 | + help |
| 63 | +fi |
| 64 | + |
| 65 | +command=$1 |
| 66 | +shift |
| 67 | + |
| 68 | +if [ "$#" == "0" ]; then |
| 69 | + builds=${build_configurations[0]} |
| 70 | +elif [ "$*" == "all" ]; then |
| 71 | + builds=${build_configurations[@]} |
| 72 | +else |
| 73 | + for item in $*; do |
| 74 | + validate $item |
| 75 | + done |
| 76 | + builds=$* |
| 77 | +fi |
| 78 | + |
| 79 | +if [ "$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 |
| 90 | +elif [ "$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 |
| 116 | +elif [ "$command" == "build_libs" ]; then |
| 117 | + build_libs |
| 118 | +else |
| 119 | + help |
| 120 | +fi |
0 commit comments