xref: /OK3568_Linux_fs/buildroot/support/scripts/generate-gitlab-ci-yml (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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=( DEVELOPERS flake8 package )
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          (*-*_defconfig)
70            defconfigs=( "${CI_COMMIT_REF_NAME##*-}" )
71            do_defconfigs=base
72            ;;
73          (*-runtime-tests)
74            do_runtime=true
75            ;;
76          (*-tests.*)
77            runtimes=( $(./support/testing/run-tests -l 2>&1 \
78                         | sed -r -e '/^test_run \((.*)\).*/!d; s//\1/' \
79                         | LC_ALL=C sort \
80                         | grep "^${CI_COMMIT_REF_NAME##*-}")
81                     )
82            do_runtime=true
83            ;;
84        esac
85    fi
86
87    # Retrieve defconfig for test-pkg from the git commit message (if any)
88    if grep -q -E '^test-pkg config:$' <<<"${CI_COMMIT_DESCRIPTION}"; then
89        sed -r -n -e '/^test-pkg config:$/{:a;n;p;ba;}' \
90            <<<"${CI_COMMIT_DESCRIPTION}" \
91            >defconfig.frag
92        if [ ! -s defconfig.frag ]; then
93            printf "Empty configuration fragment.\n" >&2; exit 1
94        fi
95        # Use --all since we expect the user having already pre-tested the
96        # new package with the default subset of toolchains.
97        ./utils/test-pkg \
98            --all --prepare-only \
99            --config-snippet defconfig.frag \
100            --build-dir br-test-pkg >&2
101        do_testpkg=( $(ls -1 br-test-pkg/*/.config 2>/dev/null |xargs -r dirname ) )
102        if [ "${#do_testpkg[@]}" -eq 0 ]; then
103            printf "Configuration fragment enables no test.\n" >&2; exit 1
104        fi
105    fi
106
107    # If nothing else, at least do the basics to generate a valid pipeline
108    if [    -z "${do_defconfigs}" \
109         -a -z "${do_runtime}" \
110         -a -z "${do_testpkg}" \
111       ]
112    then
113        do_basics=true
114    fi
115
116    if ${do_basics:-false}; then
117        for tst in "${basics[@]}"; do
118            printf 'check-%s: { extends: .check-%s_base }\n' "${tst}" "${tst}"
119        done
120    fi
121
122    if [ -n "${do_defconfigs}" ]; then
123        for cfg in "${defconfigs[@]}"; do
124            printf '%s%s: { extends: .defconfig_%s }\n' \
125                   "${cfg}" "${defconfigs_ext}" "${do_defconfigs}"
126        done
127    fi
128
129    if ${do_runtime:-false}; then
130        printf '%s: { extends: .runtime_test_base }\n' "${runtimes[@]}"
131    fi
132
133    if [ -n "${do_testpkg}" ]; then
134        printf '%s: { extends: .test_pkg }\n' "${do_testpkg[@]}"
135    fi
136}
137
138main "${@}"
139