run-tests.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Test runner for nolibc tests
  4. set -e
  5. trap 'echo Aborting...' 'ERR'
  6. crosstool_version=13.2.0
  7. hostarch=x86_64
  8. nproc=$(( $(nproc) + 2))
  9. cache_dir="${XDG_CACHE_HOME:-"$HOME"/.cache}"
  10. download_location="${cache_dir}/crosstools/"
  11. build_location="$(realpath "${cache_dir}"/nolibc-tests/)"
  12. perform_download=0
  13. test_mode=system
  14. werror=1
  15. llvm=
  16. all_archs=(
  17. i386 x86_64 x32
  18. arm64 arm armthumb
  19. mips32le mips32be mipsn32le mipsn32be mips64le mips64be
  20. ppc ppc64 ppc64le
  21. riscv32 riscv64
  22. s390x
  23. loongarch
  24. sparc32 sparc64
  25. m68k
  26. sh4
  27. )
  28. archs="${all_archs[@]}"
  29. TEMP=$(getopt -o 'j:d:c:b:a:m:pelh' -n "$0" -- "$@")
  30. eval set -- "$TEMP"
  31. unset TEMP
  32. print_usage() {
  33. cat <<EOF
  34. Run nolibc testsuite for multiple architectures with crosstools
  35. Usage:
  36. $0 [options] <architectures>
  37. Known architectures:
  38. ${archs}
  39. Options:
  40. -j [N] Allow N jobs at once (default: ${nproc})
  41. -p Allow download of toolchains
  42. -d [DIR] Download location for toolchains (default: ${download_location})
  43. -c [VERSION] Version of toolchains to use (default: ${crosstool_version})
  44. -a [ARCH] Host architecture of toolchains to use (default: ${hostarch})
  45. -b [DIR] Build location (default: ${build_location})
  46. -m [MODE] Test mode user/system (default: ${test_mode})
  47. -e Disable -Werror
  48. -l Build with LLVM/clang
  49. EOF
  50. }
  51. while true; do
  52. case "$1" in
  53. '-j')
  54. nproc="$2"
  55. shift 2; continue ;;
  56. '-p')
  57. perform_download=1
  58. shift; continue ;;
  59. '-d')
  60. download_location="$2"
  61. shift 2; continue ;;
  62. '-c')
  63. crosstool_version="$2"
  64. shift 2; continue ;;
  65. '-a')
  66. hostarch="$2"
  67. shift 2; continue ;;
  68. '-b')
  69. build_location="$(realpath "$2")"
  70. shift 2; continue ;;
  71. '-m')
  72. test_mode="$2"
  73. shift 2; continue ;;
  74. '-e')
  75. werror=0
  76. shift; continue ;;
  77. '-l')
  78. llvm=1
  79. shift; continue ;;
  80. '-h')
  81. print_usage
  82. exit 0
  83. ;;
  84. '--')
  85. shift; break ;;
  86. *)
  87. echo 'Internal error!' >&2; exit 1 ;;
  88. esac
  89. done
  90. if [[ -n "$*" ]]; then
  91. archs="$*"
  92. fi
  93. crosstool_arch() {
  94. case "$1" in
  95. arm64) echo aarch64;;
  96. armthumb) echo arm;;
  97. ppc) echo powerpc;;
  98. ppc64) echo powerpc64;;
  99. ppc64le) echo powerpc64;;
  100. riscv) echo riscv64;;
  101. loongarch) echo loongarch64;;
  102. mips*) echo mips;;
  103. s390*) echo s390;;
  104. sparc*) echo sparc64;;
  105. x32*) echo x86_64;;
  106. *) echo "$1";;
  107. esac
  108. }
  109. crosstool_abi() {
  110. case "$1" in
  111. arm | armthumb) echo linux-gnueabi;;
  112. *) echo linux;;
  113. esac
  114. }
  115. download_crosstool() {
  116. arch="$(crosstool_arch "$1")"
  117. abi="$(crosstool_abi "$1")"
  118. archive_name="${hostarch}-gcc-${crosstool_version}-nolibc-${arch}-${abi}.tar.gz"
  119. url="https://mirrors.edge.kernel.org/pub/tools/crosstool/files/bin/${hostarch}/${crosstool_version}/${archive_name}"
  120. archive="${download_location}${archive_name}"
  121. stamp="${archive}.stamp"
  122. [ -f "${stamp}" ] && return
  123. echo "Downloading crosstools ${arch} ${crosstool_version}"
  124. mkdir -p "${download_location}"
  125. curl -o "${archive}" --fail --continue-at - "${url}"
  126. tar -C "${download_location}" -xf "${archive}"
  127. touch "${stamp}"
  128. }
  129. # capture command output, print it on failure
  130. # mimics chronic(1) from moreutils
  131. function swallow_output() {
  132. if ! OUTPUT="$("$@" 2>&1)"; then
  133. echo "$OUTPUT"
  134. return 1
  135. fi
  136. return 0
  137. }
  138. test_arch() {
  139. arch=$1
  140. ct_arch=$(crosstool_arch "$arch")
  141. ct_abi=$(crosstool_abi "$1")
  142. if [ ! -d "${download_location}gcc-${crosstool_version}-nolibc/${ct_arch}-${ct_abi}/bin/." ]; then
  143. echo "No toolchain found in ${download_location}gcc-${crosstool_version}-nolibc/${ct_arch}-${ct_abi}."
  144. echo "Did you install the toolchains or set the correct arch ? Rerun with -h for help."
  145. return 1
  146. fi
  147. cross_compile=$(realpath "${download_location}gcc-${crosstool_version}-nolibc/${ct_arch}-${ct_abi}/bin/${ct_arch}-${ct_abi}-")
  148. build_dir="${build_location}/${arch}"
  149. if [ "$werror" -ne 0 ]; then
  150. CFLAGS_EXTRA="$CFLAGS_EXTRA -Werror -Wl,--fatal-warnings"
  151. fi
  152. MAKE=(make -f Makefile.nolibc -j"${nproc}" XARCH="${arch}" CROSS_COMPILE="${cross_compile}" LLVM="${llvm}" O="${build_dir}")
  153. case "$test_mode" in
  154. 'system')
  155. test_target=run
  156. ;;
  157. 'user')
  158. test_target=run-user
  159. ;;
  160. *)
  161. echo "Unknown mode $test_mode"
  162. exit 1
  163. esac
  164. printf '%-15s' "$arch:"
  165. if [ "$arch" = "m68k" -o "$arch" = "sh4" ] && [ "$llvm" = "1" ]; then
  166. echo "Unsupported configuration"
  167. return
  168. fi
  169. if [ "$arch" = "x32" ] && [ "$test_mode" = "user" ]; then
  170. echo "Unsupported configuration"
  171. return
  172. fi
  173. mkdir -p "$build_dir"
  174. swallow_output "${MAKE[@]}" defconfig
  175. swallow_output "${MAKE[@]}" CFLAGS_EXTRA="$CFLAGS_EXTRA" "$test_target" V=1
  176. cp run.out run.out."${arch}"
  177. "${MAKE[@]}" report | grep passed
  178. }
  179. if [ "$perform_download" -ne 0 ]; then
  180. for arch in $archs; do
  181. download_crosstool "$arch"
  182. done
  183. fi
  184. for arch in $archs; do
  185. test_arch "$arch"
  186. done