1#!/usr/bin/env sh 2 3# Builds and runs tests for a particular target passed as an argument to this 4# script. 5 6set -ex 7 8MIRRORS_URL="https://ci-mirrors.rust-lang.org/libc" 9 10TARGET="${1}" 11 12# If we're going to run tests inside of a qemu image, then we don't need any of 13# the scripts below. Instead, download the image, prepare a filesystem which has 14# the current state of this repository, and then run the image. 15# 16# It's assume that all images, when run with two disks, will run the `run.sh` 17# script from the second which we place inside. 18if [ "$QEMU" != "" ]; then 19 tmpdir=/tmp/qemu-img-creation 20 mkdir -p "${tmpdir}" 21 22 if [ -z "${QEMU#*.gz}" ]; then 23 # image is .gz : download and uncompress it 24 qemufile="$(echo "${QEMU%.gz}" | sed 's/\//__/g')" 25 if [ ! -f "${tmpdir}/${qemufile}" ]; then 26 curl --retry 5 "${MIRRORS_URL}/${QEMU}" | \ 27 gunzip -d > "${tmpdir}/${qemufile}" 28 fi 29 elif [ -z "${QEMU#*.xz}" ]; then 30 # image is .xz : download and uncompress it 31 qemufile="$(echo "${QEMU%.xz}" | sed 's/\//__/g')" 32 if [ ! -f "${tmpdir}/${qemufile}" ]; then 33 curl --retry 5 "${MIRRORS_URL}/${QEMU}" | \ 34 unxz > "${tmpdir}/${qemufile}" 35 fi 36 else 37 # plain qcow2 image: just download it 38 qemufile="$(echo "${QEMU}" | sed 's/\//__/g')" 39 if [ ! -f "${tmpdir}/${qemufile}" ]; then 40 curl --retry 5 "${MIRRORS_URL}/${QEMU}" \ 41 > "${tmpdir}/${qemufile}" 42 fi 43 fi 44 45 # Create a mount a fresh new filesystem image that we'll later pass to QEMU. 46 # This will have a `run.sh` script will which use the artifacts inside to run 47 # on the host. 48 rm -f "${tmpdir}/libc-test.img" 49 mkdir "${tmpdir}/mount" 50 51 # Do the standard rigamarole of cross-compiling an executable and then the 52 # script to run just executes the binary. 53 cargo build \ 54 --manifest-path libc-test/Cargo.toml \ 55 --target "${TARGET}" \ 56 --test main ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"} 57 rm "${CARGO_TARGET_DIR}/${TARGET}"/debug/main-*.d 58 cp "${CARGO_TARGET_DIR}/${TARGET}"/debug/main-* "${tmpdir}"/mount/libc-test 59 # shellcheck disable=SC2016 60 echo 'exec $1/libc-test' > "${tmpdir}/mount/run.sh" 61 62 du -sh "${tmpdir}/mount" 63 genext2fs \ 64 --root "${tmpdir}/mount" \ 65 --size-in-blocks 100000 \ 66 "${tmpdir}/libc-test.img" 67 68 # Pass -snapshot to prevent tampering with the disk images, this helps when 69 # running this script in development. The two drives are then passed next, 70 # first is the OS and second is the one we just made. Next the network is 71 # configured to work (I'm not entirely sure how), and then finally we turn off 72 # graphics and redirect the serial console output to out.log. 73 qemu-system-x86_64 \ 74 -m 1024 \ 75 -snapshot \ 76 -drive if=virtio,file="${tmpdir}/${qemufile}" \ 77 -drive if=virtio,file="${tmpdir}/libc-test.img" \ 78 -net nic,model=virtio \ 79 -net user \ 80 -nographic \ 81 -vga none 2>&1 | tee "${CARGO_TARGET_DIR}/out.log" 82 exec grep -E "^(PASSED)|(test result: ok)" "${CARGO_TARGET_DIR}/out.log" 83fi 84 85if [ "$TARGET" = "s390x-unknown-linux-gnu" ]; then 86 # FIXME: s390x-unknown-linux-gnu often fails to test due to timeout, 87 # so we retry this N times. 88 N=5 89 n=0 90 passed=0 91 until [ $n -ge $N ] 92 do 93 if [ "$passed" = "0" ]; then 94 if cargo test --no-default-features --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"} ; then 95 passed=$((passed+1)) 96 continue 97 fi 98 elif [ "$passed" = "1" ]; then 99 if cargo test --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"} ; then 100 passed=$((passed+1)) 101 continue 102 fi 103 elif [ "$passed" = "2" ]; then 104 if cargo test --features extra_traits --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}; then 105 break 106 fi 107 fi 108 n=$((n+1)) 109 sleep 1 110 done 111else 112 cargo test --no-default-features --manifest-path libc-test/Cargo.toml \ 113 --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"} 114 115 cargo test --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"} 116 117 RUST_BACKTRACE=1 cargo test --features extra_traits --manifest-path libc-test/Cargo.toml \ 118 --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"} 119fi 120