12add0d91Sopenharmony_ci#!/usr/bin/env sh
22add0d91Sopenharmony_ci
32add0d91Sopenharmony_ci# Builds and runs tests for a particular target passed as an argument to this
42add0d91Sopenharmony_ci# script.
52add0d91Sopenharmony_ci
62add0d91Sopenharmony_ciset -ex
72add0d91Sopenharmony_ci
82add0d91Sopenharmony_ciMIRRORS_URL="https://ci-mirrors.rust-lang.org/libc"
92add0d91Sopenharmony_ci
102add0d91Sopenharmony_ciTARGET="${1}"
112add0d91Sopenharmony_ci
122add0d91Sopenharmony_ci# If we're going to run tests inside of a qemu image, then we don't need any of
132add0d91Sopenharmony_ci# the scripts below. Instead, download the image, prepare a filesystem which has
142add0d91Sopenharmony_ci# the current state of this repository, and then run the image.
152add0d91Sopenharmony_ci#
162add0d91Sopenharmony_ci# It's assume that all images, when run with two disks, will run the `run.sh`
172add0d91Sopenharmony_ci# script from the second which we place inside.
182add0d91Sopenharmony_ciif [ "$QEMU" != "" ]; then
192add0d91Sopenharmony_ci  tmpdir=/tmp/qemu-img-creation
202add0d91Sopenharmony_ci  mkdir -p "${tmpdir}"
212add0d91Sopenharmony_ci
222add0d91Sopenharmony_ci  if [ -z "${QEMU#*.gz}" ]; then
232add0d91Sopenharmony_ci    # image is .gz : download and uncompress it
242add0d91Sopenharmony_ci    qemufile="$(echo "${QEMU%.gz}" | sed 's/\//__/g')"
252add0d91Sopenharmony_ci    if [ ! -f "${tmpdir}/${qemufile}" ]; then
262add0d91Sopenharmony_ci      curl --retry 5 "${MIRRORS_URL}/${QEMU}" | \
272add0d91Sopenharmony_ci          gunzip -d > "${tmpdir}/${qemufile}"
282add0d91Sopenharmony_ci    fi
292add0d91Sopenharmony_ci  elif [ -z "${QEMU#*.xz}" ]; then
302add0d91Sopenharmony_ci    # image is .xz : download and uncompress it
312add0d91Sopenharmony_ci    qemufile="$(echo "${QEMU%.xz}" | sed 's/\//__/g')"
322add0d91Sopenharmony_ci    if [ ! -f "${tmpdir}/${qemufile}" ]; then
332add0d91Sopenharmony_ci      curl --retry 5 "${MIRRORS_URL}/${QEMU}" | \
342add0d91Sopenharmony_ci          unxz > "${tmpdir}/${qemufile}"
352add0d91Sopenharmony_ci    fi
362add0d91Sopenharmony_ci  else
372add0d91Sopenharmony_ci    # plain qcow2 image: just download it
382add0d91Sopenharmony_ci    qemufile="$(echo "${QEMU}" | sed 's/\//__/g')"
392add0d91Sopenharmony_ci    if [ ! -f "${tmpdir}/${qemufile}" ]; then
402add0d91Sopenharmony_ci      curl --retry 5 "${MIRRORS_URL}/${QEMU}" \
412add0d91Sopenharmony_ci        > "${tmpdir}/${qemufile}"
422add0d91Sopenharmony_ci    fi
432add0d91Sopenharmony_ci  fi
442add0d91Sopenharmony_ci
452add0d91Sopenharmony_ci  # Create a mount a fresh new filesystem image that we'll later pass to QEMU.
462add0d91Sopenharmony_ci  # This will have a `run.sh` script will which use the artifacts inside to run
472add0d91Sopenharmony_ci  # on the host.
482add0d91Sopenharmony_ci  rm -f "${tmpdir}/libc-test.img"
492add0d91Sopenharmony_ci  mkdir "${tmpdir}/mount"
502add0d91Sopenharmony_ci
512add0d91Sopenharmony_ci  # Do the standard rigamarole of cross-compiling an executable and then the
522add0d91Sopenharmony_ci  # script to run just executes the binary.
532add0d91Sopenharmony_ci  cargo build \
542add0d91Sopenharmony_ci    --manifest-path libc-test/Cargo.toml \
552add0d91Sopenharmony_ci    --target "${TARGET}" \
562add0d91Sopenharmony_ci    --test main ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
572add0d91Sopenharmony_ci  rm "${CARGO_TARGET_DIR}/${TARGET}"/debug/main-*.d
582add0d91Sopenharmony_ci  cp "${CARGO_TARGET_DIR}/${TARGET}"/debug/main-* "${tmpdir}"/mount/libc-test
592add0d91Sopenharmony_ci  # shellcheck disable=SC2016
602add0d91Sopenharmony_ci  echo 'exec $1/libc-test' > "${tmpdir}/mount/run.sh"
612add0d91Sopenharmony_ci
622add0d91Sopenharmony_ci  du -sh "${tmpdir}/mount"
632add0d91Sopenharmony_ci  genext2fs \
642add0d91Sopenharmony_ci      --root "${tmpdir}/mount" \
652add0d91Sopenharmony_ci      --size-in-blocks 100000 \
662add0d91Sopenharmony_ci      "${tmpdir}/libc-test.img"
672add0d91Sopenharmony_ci
682add0d91Sopenharmony_ci  # Pass -snapshot to prevent tampering with the disk images, this helps when
692add0d91Sopenharmony_ci  # running this script in development. The two drives are then passed next,
702add0d91Sopenharmony_ci  # first is the OS and second is the one we just made. Next the network is
712add0d91Sopenharmony_ci  # configured to work (I'm not entirely sure how), and then finally we turn off
722add0d91Sopenharmony_ci  # graphics and redirect the serial console output to out.log.
732add0d91Sopenharmony_ci  qemu-system-x86_64 \
742add0d91Sopenharmony_ci    -m 1024 \
752add0d91Sopenharmony_ci    -snapshot \
762add0d91Sopenharmony_ci    -drive if=virtio,file="${tmpdir}/${qemufile}" \
772add0d91Sopenharmony_ci    -drive if=virtio,file="${tmpdir}/libc-test.img" \
782add0d91Sopenharmony_ci    -net nic,model=virtio \
792add0d91Sopenharmony_ci    -net user \
802add0d91Sopenharmony_ci    -nographic \
812add0d91Sopenharmony_ci    -vga none 2>&1 | tee "${CARGO_TARGET_DIR}/out.log"
822add0d91Sopenharmony_ci  exec grep -E "^(PASSED)|(test result: ok)" "${CARGO_TARGET_DIR}/out.log"
832add0d91Sopenharmony_cifi
842add0d91Sopenharmony_ci
852add0d91Sopenharmony_ciif [ "$TARGET" = "s390x-unknown-linux-gnu" ]; then
862add0d91Sopenharmony_ci  # FIXME: s390x-unknown-linux-gnu often fails to test due to timeout,
872add0d91Sopenharmony_ci  # so we retry this N times.
882add0d91Sopenharmony_ci  N=5
892add0d91Sopenharmony_ci  n=0
902add0d91Sopenharmony_ci  passed=0
912add0d91Sopenharmony_ci  until [ $n -ge $N ]
922add0d91Sopenharmony_ci  do
932add0d91Sopenharmony_ci    if [ "$passed" = "0" ]; then
942add0d91Sopenharmony_ci      if cargo test --no-default-features --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"} ; then
952add0d91Sopenharmony_ci        passed=$((passed+1))
962add0d91Sopenharmony_ci        continue
972add0d91Sopenharmony_ci      fi
982add0d91Sopenharmony_ci    elif [ "$passed" = "1" ]; then
992add0d91Sopenharmony_ci      if cargo test --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"} ; then
1002add0d91Sopenharmony_ci        passed=$((passed+1))
1012add0d91Sopenharmony_ci        continue
1022add0d91Sopenharmony_ci      fi
1032add0d91Sopenharmony_ci    elif [ "$passed" = "2" ]; then
1042add0d91Sopenharmony_ci      if cargo test --features extra_traits --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}; then
1052add0d91Sopenharmony_ci        break
1062add0d91Sopenharmony_ci      fi
1072add0d91Sopenharmony_ci    fi
1082add0d91Sopenharmony_ci    n=$((n+1))
1092add0d91Sopenharmony_ci    sleep 1
1102add0d91Sopenharmony_ci  done
1112add0d91Sopenharmony_cielse
1122add0d91Sopenharmony_ci  cargo test --no-default-features --manifest-path libc-test/Cargo.toml \
1132add0d91Sopenharmony_ci    --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
1142add0d91Sopenharmony_ci
1152add0d91Sopenharmony_ci  cargo test --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
1162add0d91Sopenharmony_ci
1172add0d91Sopenharmony_ci  RUST_BACKTRACE=1 cargo test --features extra_traits --manifest-path libc-test/Cargo.toml \
1182add0d91Sopenharmony_ci    --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
1192add0d91Sopenharmony_cifi
120