run-tests.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. archs="i386 x86_64 arm64 arm mips32le mips32be ppc ppc64 ppc64le riscv s390 loongarch"
  17. TEMP=$(getopt -o 'j:d:c:b:a:m:pelh' -n "$0" -- "$@")
  18. eval set -- "$TEMP"
  19. unset TEMP
  20. print_usage() {
  21. cat <<EOF
  22. Run nolibc testsuite for multiple architectures with crosstools
  23. Usage:
  24. $0 [options] <architectures>
  25. Known architectures:
  26. ${archs}
  27. Options:
  28. -j [N] Allow N jobs at once (default: ${nproc})
  29. -p Allow download of toolchains
  30. -d [DIR] Download location for toolchains (default: ${download_location})
  31. -c [VERSION] Version of toolchains to use (default: ${crosstool_version})
  32. -a [ARCH] Host architecture of toolchains to use (default: ${hostarch})
  33. -b [DIR] Build location (default: ${build_location})
  34. -m [MODE] Test mode user/system (default: ${test_mode})
  35. -e Disable -Werror
  36. -l Build with LLVM/clang
  37. EOF
  38. }
  39. while true; do
  40. case "$1" in
  41. '-j')
  42. nproc="$2"
  43. shift 2; continue ;;
  44. '-p')
  45. perform_download=1
  46. shift; continue ;;
  47. '-d')
  48. download_location="$2"
  49. shift 2; continue ;;
  50. '-c')
  51. crosstool_version="$2"
  52. shift 2; continue ;;
  53. '-a')
  54. hostarch="$2"
  55. shift 2; continue ;;
  56. '-b')
  57. build_location="$(realpath "$2")"
  58. shift 2; continue ;;
  59. '-m')
  60. test_mode="$2"
  61. shift 2; continue ;;
  62. '-e')
  63. werror=0
  64. shift; continue ;;
  65. '-l')
  66. llvm=1
  67. shift; continue ;;
  68. '-h')
  69. print_usage
  70. exit 0
  71. ;;
  72. '--')
  73. shift; break ;;
  74. *)
  75. echo 'Internal error!' >&2; exit 1 ;;
  76. esac
  77. done
  78. if [[ -n "$*" ]]; then
  79. archs="$*"
  80. fi
  81. crosstool_arch() {
  82. case "$1" in
  83. arm64) echo aarch64;;
  84. ppc) echo powerpc;;
  85. ppc64) echo powerpc64;;
  86. ppc64le) echo powerpc64;;
  87. riscv) echo riscv64;;
  88. loongarch) echo loongarch64;;
  89. mips*) echo mips;;
  90. *) echo "$1";;
  91. esac
  92. }
  93. crosstool_abi() {
  94. case "$1" in
  95. arm) echo linux-gnueabi;;
  96. *) echo linux;;
  97. esac
  98. }
  99. download_crosstool() {
  100. arch="$(crosstool_arch "$1")"
  101. abi="$(crosstool_abi "$1")"
  102. archive_name="${hostarch}-gcc-${crosstool_version}-nolibc-${arch}-${abi}.tar.gz"
  103. url="https://mirrors.edge.kernel.org/pub/tools/crosstool/files/bin/${hostarch}/${crosstool_version}/${archive_name}"
  104. archive="${download_location}${archive_name}"
  105. stamp="${archive}.stamp"
  106. [ -f "${stamp}" ] && return
  107. echo "Downloading crosstools ${arch} ${crosstool_version}"
  108. mkdir -p "${download_location}"
  109. curl -o "${archive}" --fail --continue-at - "${url}"
  110. tar -C "${download_location}" -xf "${archive}"
  111. touch "${stamp}"
  112. }
  113. # capture command output, print it on failure
  114. # mimics chronic(1) from moreutils
  115. function swallow_output() {
  116. if ! OUTPUT="$("$@" 2>&1)"; then
  117. echo "$OUTPUT"
  118. return 1
  119. fi
  120. return 0
  121. }
  122. test_arch() {
  123. arch=$1
  124. ct_arch=$(crosstool_arch "$arch")
  125. ct_abi=$(crosstool_abi "$1")
  126. cross_compile=$(realpath "${download_location}gcc-${crosstool_version}-nolibc/${ct_arch}-${ct_abi}/bin/${ct_arch}-${ct_abi}-")
  127. build_dir="${build_location}/${arch}"
  128. if [ "$werror" -ne 0 ]; then
  129. CFLAGS_EXTRA="$CFLAGS_EXTRA -Werror"
  130. fi
  131. MAKE=(make -j"${nproc}" XARCH="${arch}" CROSS_COMPILE="${cross_compile}" LLVM="${llvm}" O="${build_dir}")
  132. mkdir -p "$build_dir"
  133. if [ "$test_mode" = "system" ] && [ ! -f "${build_dir}/.config" ]; then
  134. swallow_output "${MAKE[@]}" defconfig
  135. fi
  136. case "$test_mode" in
  137. 'system')
  138. test_target=run
  139. ;;
  140. 'user')
  141. test_target=run-user
  142. ;;
  143. *)
  144. echo "Unknown mode $test_mode"
  145. exit 1
  146. esac
  147. printf '%-15s' "$arch:"
  148. swallow_output "${MAKE[@]}" CFLAGS_EXTRA="$CFLAGS_EXTRA" "$test_target" V=1
  149. cp run.out run.out."${arch}"
  150. "${MAKE[@]}" report | grep passed
  151. }
  152. if [ "$perform_download" -ne 0 ]; then
  153. for arch in $archs; do
  154. download_crosstool "$arch"
  155. done
  156. fi
  157. for arch in $archs; do
  158. test_arch "$arch"
  159. done