xref: /rust-libc-0.2.174/ci/run-docker.sh (revision 54e402ff)
1#!/bin/sh
2
3# Disable SC2086 as it confuses the docker command.
4# shellcheck disable=SC2086
5
6# Small script to run tests for a target (or all targets) inside all the
7# respective docker images.
8
9set -eux
10
11target="$1"
12
13# Default to assuming the CARGO_HOME is one directory up (to account for a `bin`
14# subdir) from where the `cargo` binary in `$PATH` lives.
15default_cargo_home="$(dirname "$(dirname "$(command -v cargo)")")"
16# If the CARGO_HOME env var is already set, use that. If it isn't set use the
17# default.
18export CARGO_HOME="${CARGO_HOME:-$default_cargo_home}"
19
20echo "${HOME}"
21pwd
22
23# Avoid "no space left on device" failure if running in CI
24if [ "${CI:-0}" != "0" ] && [ "$target" = "aarch64-linux-android" ] ; then
25    docker system prune -af
26    docker system df
27fi
28
29run() {
30    echo "Building docker container for target $target"
31
32    # use -f so we can use ci/ as build context
33    docker build -t "libc-$target" -f "ci/docker/$target/Dockerfile" ci/
34    mkdir -p target
35    if [ -w /dev/kvm ]; then
36        kvm="--volume /dev/kvm:/dev/kvm"
37    else
38        kvm=""
39    fi
40
41    docker run \
42        --rm \
43        --user "$(id -u)":"$(id -g)" \
44        --env LIBC_CI \
45        --env LIBC_CI_ZBUILD_STD \
46        --env RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS \
47        --env CARGO_HOME=/cargo \
48        --env CARGO_TARGET_DIR=/checkout/target \
49        --volume "$CARGO_HOME":/cargo \
50        --volume "$(rustc --print sysroot)":/rust:ro \
51        --volume "$PWD":/checkout:ro \
52        --volume "$PWD"/target:/checkout/target \
53        $kvm \
54        --init \
55        --workdir /checkout \
56        "libc-$target" \
57        sh -c "HOME=/tmp PATH=\$PATH:/rust/bin exec ci/run.sh $target"
58}
59
60build_switch() {
61    echo "Building docker container for target switch"
62
63    # use -f so we can use ci/ as build context
64    docker build -t libc-switch -f "ci/docker/switch/Dockerfile" ci/
65    mkdir -p target
66    if [ -w /dev/kvm ]; then
67        kvm="--volume /dev/kvm:/dev/kvm"
68    else
69        kvm=""
70    fi
71
72    cp "$(command -v rustup)" "$(rustc --print sysroot)/bin"
73
74    docker run \
75        --rm \
76        --user "$(id -u)":"$(id -g)" \
77        --env LIBC_CI \
78        --env CARGO_HOME=/cargo \
79        --env CARGO_TARGET_DIR=/checkout/target \
80        --volume "$CARGO_HOME":/cargo \
81        --volume "$(rustc --print sysroot)":/rust:ro \
82        --volume "$(pwd)":/checkout:ro \
83        --volume "$(pwd)"/target:/checkout/target \
84        --volume ~/.rustup:/.rustup:Z \
85        $kvm \
86        --init \
87        --workdir /checkout \
88        libc-switch \
89        sh -c "HOME=/tmp RUSTUP_HOME=/tmp PATH=\$PATH:/rust/bin rustup default nightly \
90            && rustup component add rust-src --target ci/switch.json \
91            && cargo build -Z build-std=core,alloc --target ci/switch.json"
92}
93
94if [ -z "$target" ]; then
95    for d in ci/docker/*; do
96        run "${d}"
97    done
98else
99    if [ "$target" != "switch" ]; then
100        run "$target"
101    else
102        build_switch
103    fi
104fi
105