2
1

test-pkg 9.5 KB

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