about summary refs log tree commit diff
path: root/library/compiler-builtins/ci/run-docker.sh
blob: 4c1fe0fe264450ce06b56f7dc343a5405515813c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash

# Small script to run tests for a target (or all targets) inside all the
# respective docker images.

set -euxo pipefail

host_arch="$(uname -m | sed 's/arm64/aarch64/')"

# Directories and files that do not yet exist need to be created before
# calling docker, otherwise docker will create them but they will be owned
# by root.
mkdir -p target
cargo generate-lockfile
cargo generate-lockfile --manifest-path builtins-test-intrinsics/Cargo.toml

run() {
    local target="$1"

    echo "testing target: $target"

    emulated=""
    target_arch="$(echo "$target" | cut -d'-' -f1)"
    if [ "$target_arch" != "$host_arch" ]; then
        emulated=1
        echo "target is emulated"
    fi

    run_cmd="HOME=/tmp"

    if [ "${GITHUB_ACTIONS:-}" = "true" ]; then
        # Enable Docker image caching on GHA
        build_cmd=("buildx" "build")
        build_args=(
            "--cache-from" "type=local,src=/tmp/.buildx-cache"
            "--cache-to" "type=local,dest=/tmp/.buildx-cache-new"
            # This is the beautiful bash syntax for expanding an array but neither
            # raising an error nor returning an empty string if the array is empty.
            "${build_args[@]:+"${build_args[@]}"}"
            "--load"
        )
    fi

    if [ "$(uname -s)" = "Linux" ] && [ -z "${DOCKER_BASE_IMAGE:-}" ]; then
        # Share the host rustc and target. Do this only on Linux and if the image
        # isn't overridden
        run_args=(
            --user "$(id -u):$(id -g)"
            -e "CARGO_HOME=/cargo"
            -v "${HOME}/.cargo:/cargo"
            -v "$(pwd)/target:/builtins-target"
            -v "$(rustc --print sysroot):/rust:ro"
        )
        run_cmd="$run_cmd PATH=\$PATH:/rust/bin:/cargo/bin"
    else
        # Use rustc provided by a docker image
        docker volume create compiler-builtins-cache
        build_args=(
            "--build-arg"
            "IMAGE=${DOCKER_BASE_IMAGE:-rustlang/rust:nightly}"
        )
        run_args=(-v "compiler-builtins-cache:/builtins-target")
        run_cmd="$run_cmd HOME=/tmp" "USING_CONTAINER_RUSTC=1"
    fi

    if [ -d compiler-rt ]; then
        export RUST_COMPILER_RT_ROOT="/checkout/compiler-rt"
    fi

    run_cmd="$run_cmd ci/run.sh $target"

    docker "${build_cmd[@]:-build}" \
        -t "builtins-$target" \
        "${build_args[@]:-}" \
        "ci/docker/$target"
    docker run \
        --rm \
        -e CI \
        -e CARGO_TARGET_DIR=/builtins-target \
        -e CARGO_TERM_COLOR \
        -e MAY_SKIP_LIBM_CI \
        -e RUSTFLAGS \
        -e RUST_BACKTRACE \
        -e RUST_COMPILER_RT_ROOT \
        -e "EMULATED=$emulated" \
        -v "$(pwd):/checkout:ro" \
        -w /checkout \
        "${run_args[@]:-}" \
        --init \
        "builtins-$target" \
        sh -c "$run_cmd"
}

if [ "${1:-}" = "--help" ] || [ "$#" -gt 1 ]; then
    set +x
    echo "\
    usage: ./ci/run-docker.sh [target]

    you can also set DOCKER_BASE_IMAGE to use something other than the default
    ubuntu:25.04 (or rustlang/rust:nightly).
    "
    exit
fi

if [ -z "${1:-}" ]; then
    for d in ci/docker/*; do
        run $(basename "$d")
    done
else
    run "$1"
fi