test-pkg 7.2 KB

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