test-pkg 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #!/usr/bin/env bash
  2. set -e
  3. TOOLCHAINS_CSV='support/config-fragments/autobuild/toolchain-configs.csv'
  4. TEMP_CONF=""
  5. abort=0
  6. do_abort() {
  7. abort=1
  8. }
  9. do_clean() {
  10. if [ -n "${TEMP_CONF}" ]; then
  11. rm -f "${TEMP_CONF}"
  12. fi
  13. }
  14. main() {
  15. local o O opts
  16. local cfg dir pkg random toolchains_csv toolchain all number mode prepare_only
  17. local ret nb nb_skip nb_fail nb_legal nb_show nb_tc build_dir keep
  18. local -a toolchains
  19. local pkg_br_name
  20. o='hakc:d:n:p:r:t:'
  21. O='help,all,keep,prepare-only,config-snippet:,build-dir:,number:,package:,random:,toolchains-csv:'
  22. opts="$(getopt -n "${my_name}" -o "${o}" -l "${O}" -- "${@}")"
  23. eval set -- "${opts}"
  24. random=0
  25. all=0
  26. keep=0
  27. number=0
  28. mode=0
  29. prepare_only=0
  30. toolchains_csv="${TOOLCHAINS_CSV}"
  31. while [ ${#} -gt 0 ]; do
  32. case "${1}" in
  33. (-h|--help)
  34. help; exit 0
  35. ;;
  36. (-a|--all)
  37. all=1; shift 1
  38. ;;
  39. (-k|--keep)
  40. keep=1; shift 1
  41. ;;
  42. (--prepare-only)
  43. prepare_only=1; shift 1
  44. ;;
  45. (-c|--config-snippet)
  46. cfg="${2}"; shift 2
  47. ;;
  48. (-d|--build-dir)
  49. dir="${2}"; shift 2
  50. ;;
  51. (-n|--number)
  52. number="${2}"; shift 2
  53. ;;
  54. (-p|--package)
  55. pkg="${2}"; shift 2
  56. ;;
  57. (-r|--random)
  58. random="${2}"; shift 2
  59. ;;
  60. (-t|--toolchains-csv)
  61. toolchains_csv="${2}"; shift 2
  62. ;;
  63. (--)
  64. shift; break
  65. ;;
  66. esac
  67. done
  68. trap do_abort INT
  69. trap do_clean TERM HUP EXIT
  70. if [ -z "${cfg}" ]; then
  71. pkg_br_name="${pkg//-/_}"
  72. pkg_br_name="BR2_PACKAGE_${pkg_br_name^^}"
  73. TEMP_CONF="$(mktemp /tmp/test-"${pkg}"-config.XXXXXX)"
  74. echo "${pkg_br_name}=y" > "${TEMP_CONF}"
  75. cfg="${TEMP_CONF}"
  76. fi
  77. if [ ! -e "${cfg}" ]; then
  78. printf "error: %s: no such file\n" "${cfg}" >&2; exit 1
  79. fi
  80. if [ -z "${dir}" ]; then
  81. dir="${HOME}/br-test-pkg"
  82. fi
  83. if [ "${random}" -gt 0 ]; then
  84. mode=$((mode+1))
  85. fi
  86. if [ "${number}" -gt 0 ]; then
  87. mode=$((mode+1))
  88. fi
  89. if [ "${all}" -eq 1 ]; then
  90. mode=$((mode+1))
  91. fi
  92. # Default mode is to test the N first toolchains, which have been
  93. # chosen to be a good selection of toolchains.
  94. if [ ${mode} -eq 0 ] ; then
  95. number=6
  96. elif [ ${mode} -gt 1 ] ; then
  97. printf "error: --all, --number and --random are mutually exclusive\n" >&2; exit 1
  98. fi
  99. # Extract the URLs of the toolchains; drop internal toolchains
  100. # E.g.: http://server/path/to/name.config,arch,libc
  101. # --> http://server/path/to/name.config
  102. mapfile -t toolchains < <(sed -r -e 's/,.*//; /internal/d; /^#/d; /^$/d;' "${toolchains_csv}" \
  103. | if [ "${random}" -gt 0 ]; then \
  104. sort -R | head -n "${random}"
  105. elif [ "${number}" -gt 0 ]; then \
  106. head -n "${number}"
  107. else
  108. sort
  109. fi
  110. )
  111. nb_tc="${#toolchains[@]}"
  112. if [ "${nb_tc}" -eq 0 ]; then
  113. printf "error: no toolchain found (networking issue?)\n" >&2; exit 1
  114. fi
  115. nb=0
  116. nb_skip=0
  117. nb_fail=0
  118. nb_legal=0
  119. nb_show=0
  120. for toolchainconfig in "${toolchains[@]}"; do
  121. : $((nb++))
  122. toolchain="$(basename "${toolchainconfig}" .config)"
  123. build_dir="${dir}/${toolchain}"
  124. printf "%40s [%*d/%d]: " "${toolchain}" ${#nb_tc} "${nb}" "${nb_tc}"
  125. build_one "${build_dir}" "${toolchainconfig}" "${cfg}" "${pkg}" "${prepare_only}" && ret=0 || ret=${?}
  126. case ${ret} in
  127. (0) printf "OK\n";;
  128. (1) : $((nb_skip++)); printf "SKIPPED\n";;
  129. (2) : $((nb_fail++)); printf "FAILED\n";;
  130. (3) : $((nb_legal++)); printf "FAILED\n";;
  131. (4) : $((nb_show++)); printf "FAILED\n";;
  132. esac
  133. if [ "${abort}" -eq 1 ]; then
  134. return 1
  135. fi
  136. done
  137. printf "%d builds, %d skipped, %d build failed, %d legal-info failed, %d show-info failed\n" \
  138. "${nb}" "${nb_skip}" "${nb_fail}" "${nb_legal}" "${nb_show}"
  139. return $((nb_fail + nb_legal))
  140. }
  141. build_one() {
  142. local dir="${1}"
  143. local toolchainconfig="${2}"
  144. local cfg="${3}"
  145. local pkg="${4}"
  146. local prepare_only="${5}"
  147. mkdir -p "${dir}"
  148. CONFIG_="" support/kconfig/merge_config.sh -O "${dir}" \
  149. "${toolchainconfig}" "support/config-fragments/minimal.config" "${cfg}" \
  150. >> "${dir}/logfile" 2>&1
  151. # We want all the options from the snippet to be present as-is (set
  152. # or not set) in the actual .config; if one of them is not, it means
  153. # some dependency from the toolchain or arch is not available, in
  154. # which case this config is untestable and we skip it.
  155. # We don't care about the locale to sort in, as long as both sort are
  156. # done in the same locale.
  157. comm -23 <(sort "${cfg}") <(sort "${dir}/.config") >"${dir}/missing.config"
  158. if [ -s "${dir}/missing.config" ]; then
  159. if [ ${keep} -ne 1 ]; then
  160. # Invalid configuration, drop it
  161. rm -f "${dir}/.config"
  162. fi
  163. return 1
  164. fi
  165. # Remove file, it's empty anyway.
  166. rm -f "${dir}/missing.config"
  167. # Defer building the job to the caller (e.g. a gitlab pipeline)
  168. if [ "${prepare_only}" -eq 1 ]; then
  169. return 0
  170. fi
  171. if [ -n "${pkg}" ]; then
  172. if ! make O="${dir}" "${pkg}-dirclean" >> "${dir}/logfile" 2>&1; then
  173. return 2
  174. fi
  175. fi
  176. # shellcheck disable=SC2086
  177. if ! BR_FORCE_CHECK_DEPENDENCIES=YES make O="${dir}" ${pkg} >> "${dir}/logfile" 2>&1; then
  178. return 2
  179. fi
  180. # legal-info done systematically, because some packages have different
  181. # sources depending on the configuration (e.g. lua-5.2 vs. lua-5.3)
  182. if ! make O="${dir}" legal-info >> "${dir}/logfile" 2>&1; then
  183. return 3
  184. fi
  185. # Validate that we generate proper json as show-info
  186. { tput smso; printf '>>> Running show-info\n'; tput rmso; } >> "${dir}/logfile" 2> /dev/null;
  187. JQ="$(which jq 2> /dev/null)"
  188. if [ -z "${JQ}" ]; then
  189. make O="${dir}" host-jq >> "${dir}/logfile" 2>&1
  190. JQ="${dir}/host/bin/jq"
  191. fi
  192. if ! make O="${dir}" "${pkg:+${pkg}-}show-info" > "${dir}/info.json" 2>> "${dir}/logfile"; then
  193. return 4
  194. fi
  195. if ! "${JQ}" . < "${dir}/info.json" >> "${dir}/logfile" 2>&1; then
  196. return 4
  197. fi
  198. # If we get here, the build was successful. Clean up the build/host
  199. # directories to save disk space, unless 'keep' was set.
  200. if [ ${keep} -ne 1 ]; then
  201. make O="${dir}" clean >> "${dir}/logfile" 2>&1
  202. fi
  203. }
  204. help() {
  205. cat <<_EOF_
  206. test-pkg: test-build a package against various toolchains and architectures
  207. The supplied config snippet is appended to each toolchain config, the
  208. resulting configuration is checked to ensure it still contains all options
  209. specified in the snippet; if any is missing, the build is skipped, on the
  210. assumption that the package under test requires a toolchain or architecture
  211. feature that is missing.
  212. In case failures are noticed, you can fix the package and just re-run the
  213. same command again; it will re-run the test where it failed. If you did
  214. specify a package (with -p), the package build dir will be removed first.
  215. The list of toolchains is retrieved from ${TOOLCHAINS_CSV}.
  216. Only the external toolchains are tried, because building a Buildroot toolchain
  217. would take too long. An alternative toolchains CSV file can be specified with
  218. the -t option. This file should have lines consisting of the path to the
  219. toolchain config fragment and the required host architecture, separated by a
  220. comma. The config fragments should contain only the toolchain and architecture
  221. settings.
  222. By default, a useful subset of toolchains is tested. If needed, all
  223. toolchains can be tested (-a), an arbitrary number of toolchains (-n
  224. in order, -r for random).
  225. Options:
  226. -h, --help
  227. Print this help.
  228. -c CFG, --config-snippet CFG
  229. Use the CFG file as the source for the config snippet. This file
  230. should contain all the config options required to build a package.
  231. -d DIR, --build-dir DIR
  232. Do the builds in directory DIR, one sub-dir per toolchain.
  233. If not specified, defaults to \${HOME}/br-test-pkg
  234. -p PKG, --package PKG
  235. Test-build the package PKG, by running 'make PKG'; if not specified,
  236. just runs 'make'.
  237. -a, --all
  238. Test all toolchains, instead of the default subset defined by
  239. Buildroot developers.
  240. -n N, --number N
  241. Test N toolchains, in the order defined in the toolchain CSV
  242. file.
  243. -r N, --random N
  244. Limit the tests to the N randomly selected toolchains.
  245. -t CSVFILE, --toolchains-csv CSVFILE
  246. CSV file containing the paths to config fragments of toolchains to
  247. try. If not specified, the toolchains in ${TOOLCHAINS_CSV} will be
  248. used.
  249. -k, --keep
  250. Keep the build directories even if the build succeeds.
  251. Note: the logfile and configuration is always retained, even without
  252. this option.
  253. --prepare-only
  254. Only prepare the .config files, but do not build them. Output the
  255. list of build directories to stdout, and the status on stderr.
  256. Example:
  257. Testing libcec would require a config snippet that contains:
  258. BR2_PACKAGE_LIBCEC=y
  259. Testing libcurl with openSSL support would require a snippet such as:
  260. BR2_PACKAGE_OPENSSL=y
  261. BR2_PACKAGE_LIBCURL=y
  262. _EOF_
  263. }
  264. my_name="${0##*/}"
  265. main "${@}"