1#!/usr/bin/env bash
2set -e
3set -o pipefail
4
5main() {
6    local template="${1}"
7
8    preamble "${template}"
9    gen_tests
10}
11
12preamble() {
13    local template="${1}"
14
15    cat - "${template}" <<-_EOF_
16	# This file is generated; do not edit!
17	# Builds appear on https://gitlab.com/buildroot.org/buildroot/pipelines
18
19	image: ${CI_JOB_IMAGE}
20
21_EOF_
22}
23
24gen_tests() {
25    local -a basics defconfigs runtimes
26    local do_basics do_defconfigs do_runtime do_testpkg
27    local defconfigs_ext cfg tst
28
29    basics=( check-package check-symbol DEVELOPERS package symbol )
30
31    defconfigs=( $(cd configs; LC_ALL=C ls -1 *_defconfig) )
32
33    runtimes=( $(./support/testing/run-tests -l 2>&1 \
34                 | sed -r -e '/^test_run \((.*)\).*/!d; s//\1/' \
35                 | LC_ALL=C sort)
36             )
37
38    if [ -n "${CI_COMMIT_TAG}" ]; then
39        # When a tag is added to the Buildroot git tree, we want
40        # to run the runtime tests and only test Qemu defconfigs.
41        defconfigs=( $(cd configs; LC_ALL=C ls -1 qemu_*_defconfig) )
42        do_basics=true
43        do_defconfigs=base
44        do_runtime=true
45    elif [ "${CI_PIPELINE_SOURCE}" = "trigger" ]; then
46        case "${BR_SCHEDULE_JOBS}" in
47          (basic)
48            do_basics=true
49            do_defconfigs=check
50            defconfigs_ext=_check
51            ;;
52          (defconfig)
53            do_defconfigs=base
54            ;;
55          (runtime)
56            do_runtime=true
57            ;;
58        esac
59    else
60        case "${CI_COMMIT_REF_NAME}" in
61          (*-basics)
62            do_basics=true
63            do_defconfigs=check
64            defconfigs_ext=_check
65            ;;
66          (*-defconfigs)
67            do_defconfigs=base
68            ;;
69          (*-defconfigs-*)
70            pattern=$(echo ${CI_COMMIT_REF_NAME} | sed 's%^.*-defconfigs-\(.*\)%\1%')
71            defconfigs=( $(cd configs; LC_ALL=C ls -1 | grep ^${pattern}) )
72            do_defconfigs=base
73            ;;
74          (*-*_defconfig)
75            defconfigs=( "${CI_COMMIT_REF_NAME##*-}" )
76            do_defconfigs=base
77            ;;
78          (*-runtime-tests)
79            do_runtime=true
80            ;;
81          (*-tests.*)
82            runtimes=( $(./support/testing/run-tests -l 2>&1 \
83                         | sed -r -e '/^test_run \((.*)\).*/!d; s//\1/' \
84                         | LC_ALL=C sort \
85                         | grep "^${CI_COMMIT_REF_NAME##*-}")
86                     )
87            do_runtime=true
88            ;;
89        esac
90    fi
91
92    # Retrieve defconfig for test-pkg from the git commit message (if any)
93    if grep -q -E '^test-pkg config:$' <<<"${CI_COMMIT_DESCRIPTION}"; then
94        sed -r -n -e '/^test-pkg config:$/{:a;n;s/^ +//;p;ba;}' \
95            <<<"${CI_COMMIT_DESCRIPTION}" \
96            >defconfig.frag
97        if [ ! -s defconfig.frag ]; then
98            printf "Empty configuration fragment.\n" >&2; exit 1
99        fi
100        # Use --all since we expect the user having already pre-tested the
101        # new package with the default subset of toolchains.
102        ./utils/test-pkg \
103            --all --prepare-only \
104            --config-snippet defconfig.frag \
105            --build-dir br-test-pkg >&2
106        do_testpkg=( $(ls -1 br-test-pkg/*/.config 2>/dev/null |xargs -r dirname ) )
107        if [ "${#do_testpkg[@]}" -eq 0 ]; then
108            printf "Configuration fragment enables no test.\n" >&2; exit 1
109        fi
110    fi
111
112    # If nothing else, at least do the basics to generate a valid pipeline
113    if [    -z "${do_defconfigs}" \
114         -a -z "${do_runtime}" \
115         -a -z "${do_testpkg}" \
116       ]
117    then
118        do_basics=true
119    fi
120
121    if ${do_basics:-false}; then
122        for tst in "${basics[@]}"; do
123            printf 'check-%s: { extends: .check-%s_base }\n' "${tst}" "${tst}"
124        done
125    fi
126
127    if [ -n "${do_defconfigs}" ]; then
128        for cfg in "${defconfigs[@]}"; do
129            printf '%s%s: { extends: .defconfig_%s }\n' \
130                   "${cfg}" "${defconfigs_ext}" "${do_defconfigs}"
131        done
132    fi
133
134    if ${do_runtime:-false}; then
135        printf 'runtime_test_download: { extends: .runtime_test_download }\n'
136        printf '%s: { extends: .runtime_test_base }\n' "${runtimes[@]}"
137    fi
138
139    if [ -n "${do_testpkg}" ]; then
140        printf '%s: { extends: .test_pkg }\n' "${do_testpkg[@]}"
141    fi
142}
143
144main "${@}"
145