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