]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - utils/test-pkg
check-package: prepare to extend to other directories
[coffee/buildroot.git] / utils / test-pkg
1 #!/usr/bin/env bash
2 set -e
3
4 TOOLCHAINS_CSV='support/config-fragments/autobuild/toolchain-configs.csv'
5
6 main() {
7     local o O opts
8     local cfg dir pkg random toolchains_dir toolchain
9     local ret nb nb_skip nb_fail nb_legal nb_tc build_dir
10     local -a toolchains
11
12     o='hc:d:p:r:t:'
13     O='help,config-snippet:build-dir:package:,random:,toolchains-dir:'
14     opts="$(getopt -n "${my_name}" -o "${o}" -l "${O}" -- "${@}")"
15     eval set -- "${opts}"
16
17     random=0
18     toolchains_csv="${TOOLCHAINS_CSV}"
19     while [ ${#} -gt 0 ]; do
20         case "${1}" in
21         (-h|--help)
22             help; exit 0
23             ;;
24         (-c|--config-snippet)
25             cfg="${2}"; shift 2
26             ;;
27         (-d|--build-dir)
28             dir="${2}"; shift 2
29             ;;
30         (-p|--package)
31             pkg="${2}"; shift 2
32             ;;
33         (-r|--random)
34             random="${2}"; shift 2
35             ;;
36         (-t|--toolchains-csv)
37             toolchains_csv="${2}"; shift 2
38             ;;
39         (--)
40             shift; break
41             ;;
42         esac
43     done
44     if [ -z "${cfg}" ]; then
45         printf "error: no config snippet specified\n" >&2; exit 1
46     fi
47     if [ ! -e "${cfg}" ]; then
48         printf "error: %s: no such file\n" "${cfg}" >&2; exit 1
49     fi
50     if [ -z "${dir}" ]; then
51         dir="${HOME}/br-test-pkg"
52     fi
53
54     # Extract the URLs of the toolchains; drop internal toolchains
55     # E.g.: http://server/path/to/name.config,arch,libc
56     #  -->  http://server/path/to/name.config
57     toolchains=($(sed -r -e 's/,.*//; /internal/d; /^#/d; /^$/d;' "${toolchains_csv}" \
58                   |if [ ${random} -gt 0 ]; then \
59                       sort -R |head -n ${random}
60                    else
61                       cat
62                    fi |sort
63                  )
64                )
65
66     nb_tc="${#toolchains[@]}"
67     if [ ${nb_tc} -eq 0 ]; then
68         printf "error: no toolchain found (networking issue?)\n" >&2; exit 1
69     fi
70
71     nb=0
72     nb_skip=0
73     nb_fail=0
74     nb_legal=0
75     for toolchainconfig in "${toolchains[@]}"; do
76         : $((nb++))
77         toolchain="$(basename "${toolchainconfig}" .config)"
78         build_dir="${dir}/${toolchain}"
79         printf "%40s [%*d/%d]: " "${toolchain}" ${#nb_tc} ${nb} ${nb_tc}
80         build_one "${build_dir}" "${toolchainconfig}" "${cfg}" "${pkg}" && ret=0 || ret=${?}
81         case ${ret} in
82         (0) printf "OK\n";;
83         (1) : $((nb_skip++)); printf "SKIPPED\n";;
84         (2) : $((nb_fail++)); printf "FAILED\n";;
85         (3) : $((nb_legal++)); printf "FAILED\n";;
86         esac
87     done
88
89     printf "%d builds, %d skipped, %d build failed, %d legal-info failed\n" \
90         ${nb} ${nb_skip} ${nb_fail} ${nb_legal}
91 }
92
93 build_one() {
94     local dir="${1}"
95     local toolchainconfig="${2}"
96     local cfg="${3}"
97     local pkg="${4}"
98
99     mkdir -p "${dir}"
100
101     support/kconfig/merge_config.sh -O "${dir}" \
102         "${toolchainconfig}" "support/config-fragments/minimal.config" "${cfg}" \
103         > /dev/null
104     # We want all the options from the snippet to be present as-is (set
105     # or not set) in the actual .config; if one of them is not, it means
106     # some dependency from the toolchain or arch is not available, in
107     # which case this config is untestable and we skip it.
108     # We don't care about the locale to sort in, as long as both sort are
109     # done in the same locale.
110     comm -23 <(sort "${cfg}") <(sort "${dir}/.config") >"${dir}/missing.config"
111     if [ -s "${dir}/missing.config" ]; then
112         return 1
113     fi
114     # Remove file, it's empty anyway.
115     rm -f "${dir}/missing.config"
116
117     if [ -n "${pkg}" ]; then
118         if ! make O="${dir}" "${pkg}-dirclean" >> "${dir}/logfile" 2>&1; then
119             return 2
120         fi
121     fi
122
123     # shellcheck disable=SC2086
124     if ! make O="${dir}" ${pkg} >> "${dir}/logfile" 2>&1; then
125         return 2
126     fi
127
128     # legal-info done systematically, because some packages have different
129     # sources depending on the configuration (e.g. lua-5.2 vs. lua-5.3)
130     if ! make O="${dir}" legal-info >> "${dir}/logfile" 2>&1; then
131         return 3
132     fi
133 }
134
135 help() {
136     cat <<_EOF_
137 test-pkg: test-build a package against various toolchains and architectures
138
139 The supplied config snippet is appended to each toolchain config, the
140 resulting configuration is checked to ensure it still contains all options
141 specified in the snippet; if any is missing, the build is skipped, on the
142 assumption that the package under test requires a toolchain or architecture
143 feature that is missing.
144
145 In case failures are noticed, you can fix the package and just re-run the
146 same command again; it will re-run the test where it failed. If you did
147 specify a package (with -p), the package build dir will be removed first.
148
149 The list of toolchains is retrieved from ${TOOLCHAINS_CSV}.
150 Only the external toolchains are tried, because building a Buildroot toolchain
151 would take too long. An alternative toolchains CSV file can be specified with
152 the -t option. This file should have lines consisting of the path to the
153 toolchain config fragment and the required host architecture, separated by a
154 comma. The config fragments should contain only the toolchain and architecture
155 settings.
156
157 Options:
158
159     -h, --help
160         Print this help.
161
162     -c CFG, --config-snippet CFG
163         Use the CFG file as the source for the config snippet. This file
164         should contain all the config options required to build a package.
165
166     -d DIR, --build-dir DIR
167         Do the builds in directory DIR, one sub-dir per toolchain.
168
169     -p PKG, --package PKG
170         Test-build the package PKG, by running 'make PKG'; if not specified,
171         just runs 'make'.
172
173     -r N, --random N
174         Limit the tests to the N randomly selected toolchains, instead of
175         building with all toolchains.
176
177     -t CSVFILE, --toolchains-csv CSVFILE
178         CSV file containing the paths to config fragments of toolchains to
179         try. If not specified, the toolchains in ${TOOLCHAINS_CSV} will be
180         used.
181
182 Example:
183
184     Testing libcec would require a config snippet that contains:
185         BR2_PACKAGE_LIBCEC=y
186
187     Testing libcurl with openSSL support would require a snippet such as:
188         BR2_PACKAGE_OPENSSL=y
189         BR2_PACKAGE_LIBCURL=y
190
191 _EOF_
192 }
193
194 my_name="${0##*/}"
195 main "${@}"