xref: /rust-libc-0.2.174/ci/run.sh (revision 1ae3ff23)
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 -eux
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 [ -n "${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        base_file="${QEMU%.gz}"
25        pipe_cmd="gunzip -d"
26    elif [ -z "${QEMU#*.xz}" ]; then
27        # image is .xz : download and uncompress it
28        base_file="${QEMU%.xz}"
29        pipe_cmd="unxz"
30    else
31        # plain qcow2 image: just download it
32        base_file="$QEMU"
33        pipe_cmd="cat" # nop to forward the result
34    fi
35
36    qemufile="$(echo "$base_file" | sed 's/\//__/g')"
37    if [ ! -f "${tmpdir}/${qemufile}" ]; then
38        curl --retry 5 "${mirrors_url}/${QEMU}" | $pipe_cmd > "${tmpdir}/${qemufile}"
39    fi
40
41    # Create a mount a fresh new filesystem image that we'll later pass to QEMU.
42    # This will have a `run.sh` script will which use the artifacts inside to run
43    # on the host.
44    rm -f "${tmpdir}/libc-test.img"
45    mkdir "${tmpdir}/mount"
46
47    # Do the standard rigamarole of cross-compiling an executable and then the
48    # script to run just executes the binary.
49    cargo build \
50        --manifest-path libc-test/Cargo.toml \
51        --target "$target" \
52        --test main ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
53    rm "${CARGO_TARGET_DIR}/${target}"/debug/main-*.d
54    cp "${CARGO_TARGET_DIR}/${target}"/debug/main-* "${tmpdir}"/mount/libc-test
55    # shellcheck disable=SC2016
56    echo 'exec $1/libc-test' > "${tmpdir}/mount/run.sh"
57
58    du -sh "${tmpdir}/mount"
59    genext2fs \
60        --root "${tmpdir}/mount" \
61        --size-in-blocks 100000 \
62        "${tmpdir}/libc-test.img"
63
64    # Pass -snapshot to prevent tampering with the disk images, this helps when
65    # running this script in development. The two drives are then passed next,
66    # first is the OS and second is the one we just made. Next the network is
67    # configured to work (I'm not entirely sure how), and then finally we turn off
68    # graphics and redirect the serial console output to out.log.
69    qemu-system-x86_64 \
70        -m 1024 \
71        -snapshot \
72        -drive if=virtio,file="${tmpdir}/${qemufile}" \
73        -drive if=virtio,file="${tmpdir}/libc-test.img" \
74        -net nic,model=virtio \
75        -net user \
76        -nographic \
77        -vga none 2>&1 | tee "${CARGO_TARGET_DIR}/out.log"
78    exec grep -E "^(PASSED)|(test result: ok)" "${CARGO_TARGET_DIR}/out.log"
79fi
80
81if [ "$target" = "s390x-unknown-linux-gnu" ]; then
82    # FIXME: s390x-unknown-linux-gnu often fails to test due to timeout,
83    # so we retry this N times.
84    N=5
85    n=0
86    passed=0
87    until [ $n -ge $N ]; do
88        if [ "$passed" = "0" ]; then
89            if cargo test \
90                --no-default-features \
91                --manifest-path libc-test/Cargo.toml \
92                --target "$target" \
93                ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
94             then
95                passed=$((passed+1))
96                continue
97            fi
98        elif [ "$passed" = "1" ]; then
99            if cargo test \
100                --manifest-path libc-test/Cargo.toml \
101                --target "$target" \
102                ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
103            then
104                passed=$((passed+1))
105                continue
106            fi
107        elif [ "$passed" = "2" ]; then
108            if cargo test \
109                --features extra_traits \
110                --manifest-path libc-test/Cargo.toml \
111                --target "$target" \
112                ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
113            then
114                break
115            fi
116        fi
117        n=$((n+1))
118        sleep 1
119    done
120else
121    cargo test \
122        --no-default-features \
123        --manifest-path libc-test/Cargo.toml \
124        --target "$target" \
125        ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
126
127    cargo test \
128        --manifest-path libc-test/Cargo.toml \
129        --target "$target" \
130        ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
131
132    RUST_BACKTRACE=1 cargo test \
133        --features extra_traits \
134        --manifest-path libc-test/Cargo.toml \
135        --target "$target" \
136        ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
137fi
138