xref: /rust-libc-0.2.174/ci/run-docker.sh (revision 52035e1c)
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 RUST_LIBC_UNSTABLE_GNU_TIME_BITS \
48        --env CARGO_HOME=/cargo \
49        --env CARGO_TARGET_DIR=/checkout/target \
50        --volume "$CARGO_HOME":/cargo \
51        --volume "$(rustc --print sysroot)":/rust:ro \
52        --volume "$PWD":/checkout:ro \
53        --volume "$PWD"/target:/checkout/target \
54        $kvm \
55        --init \
56        --workdir /checkout \
57        "libc-$target" \
58        sh -c "HOME=/tmp PATH=\$PATH:/rust/bin exec ci/run.sh $target"
59}
60
61build_switch() {
62    echo "Building docker container for target switch"
63
64    # use -f so we can use ci/ as build context
65    docker build -t libc-switch -f "ci/docker/switch/Dockerfile" ci/
66    mkdir -p target
67    if [ -w /dev/kvm ]; then
68        kvm="--volume /dev/kvm:/dev/kvm"
69    else
70        kvm=""
71    fi
72
73    cp "$(command -v rustup)" "$(rustc --print sysroot)/bin"
74
75    docker run \
76        --rm \
77        --user "$(id -u)":"$(id -g)" \
78        --env LIBC_CI \
79        --env CARGO_HOME=/cargo \
80        --env CARGO_TARGET_DIR=/checkout/target \
81        --volume "$CARGO_HOME":/cargo \
82        --volume "$(rustc --print sysroot)":/rust:ro \
83        --volume "$(pwd)":/checkout:ro \
84        --volume "$(pwd)"/target:/checkout/target \
85        --volume ~/.rustup:/.rustup:Z \
86        $kvm \
87        --init \
88        --workdir /checkout \
89        libc-switch \
90        sh -c "HOME=/tmp RUSTUP_HOME=/tmp PATH=\$PATH:/rust/bin rustup default nightly \
91            && rustup component add rust-src --target ci/switch.json \
92            && cargo build -Z build-std=core,alloc --target ci/switch.json"
93}
94
95if [ -z "$target" ]; then
96    for d in ci/docker/*; do
97        run "${d}"
98    done
99else
100    if [ "$target" != "switch" ]; then
101        run "$target"
102    else
103        build_switch
104    fi
105fi
106