test-pkg 8.1 KB

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