xref: /rust-libc-0.2.174/ci/run.sh (revision 8d68ec6f)
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 ]; do
92        if [ "$passed" = "0" ]; then
93            if cargo test \
94                --no-default-features \
95                --manifest-path libc-test/Cargo.toml \
96                --target "$target" \
97                ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
98             then
99                passed=$((passed+1))
100                continue
101            fi
102        elif [ "$passed" = "1" ]; then
103            if cargo test \
104                --manifest-path libc-test/Cargo.toml \
105                --target "$target" \
106                ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
107            then
108                passed=$((passed+1))
109                continue
110            fi
111        elif [ "$passed" = "2" ]; then
112            if cargo test \
113                --features extra_traits \
114                --manifest-path libc-test/Cargo.toml \
115                --target "$target" \
116                ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
117            then
118                break
119            fi
120        fi
121        n=$((n+1))
122        sleep 1
123    done
124else
125    cargo test \
126        --no-default-features \
127        --manifest-path libc-test/Cargo.toml \
128        --target "$target" \
129        ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
130
131    cargo test \
132        --manifest-path libc-test/Cargo.toml \
133        --target "$target" \
134        ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
135
136    RUST_BACKTRACE=1 cargo test \
137        --features extra_traits \
138        --manifest-path libc-test/Cargo.toml \
139        --target "$target" \
140        ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
141fi
142