test-pkg 7.6 KB

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