Skip to content

Commit a4da896

Browse files
Merge #817
817: Enable usage of other toolchains in images r=Alexhuszagh a=Emilgardis Makes the following work ```toml # Cross.toml [build] default-target = "x86_64-unknown-linux-musl" [target."x86_64-unknown-linux-musl"] image.name = "alpine:edge" image.toolchain = ["x86_64-unknown-linux-musl"] pre-build = ["apk add --no-cache gcc musl-dev"] ``` ```rust // src/main.rs fn main() { println!("Hello, world!"); } ``` ``` > cross run [..snip] Finished dev [unoptimized + debuginfo] target(s) in 0.24s Running `/target/x86_64-unknown-linux-musl/debug/cross-minimal` Hello, world! ``` and also ```toml [target.x86_64-unknown-linux-gnu] pre-build = [ "apt-get update && apt-get install -y libc6 g++-x86-64-linux-gnu libc6-dev-amd64-cross", ] [target.x86_64-unknown-linux-gnu.env] passthrough = [ "CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc", "CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc", "CXX_x86_64_unknown_linux_gnu=x86_64-linux-gnu-g++", ] [target.x86_64-unknown-linux-gnu.image] name = "ubuntu:20.04" toolchain = ["aarch64-unknown-linux-gnu"] ``` ``` cross build --target x86_64-unknown-linux-gnu -v + cargo metadata --format-version 1 --filter-platform x86_64-unknown-linux-gnu + rustc --print sysroot + /usr/local/bin/docker + /usr/local/bin/docker version '-f "{{ .Server.Os }},{{ .Server.Arch }}"' + rustup --verbose toolchain list + rustup --verbose target list --toolchain stable-aarch64-unknown-linux-gnu + rustup --verbose component list --toolchain stable-aarch64-unknown-linux-gnu + "/Users/emil/workspace/cross-tests/basic" /usr/local/bin/docker build --platform linux/arm64 --label 'org.cross-rs.for-cross-target=x86_64-unknown-linux-gnu' --label 'org.cross-rs.runs-with=aarch64-unknown-linux-gnu' --label 'org.cross-rs.workspace_root=/Users/emil/workspace/cross-tests/basic' --tag cross-custom-basic:x86_64-unknown-linux-gnu-50dfa-pre-build --build-arg 'CROSS_CMD=apt-get update && apt-get install -y libc6 g++-x86-64-linux-gnu libc6-dev-amd64-cross' --build-arg 'CROSS_DEB_ARCH=amd64' --file /Users/emil/workspace/cross-tests/basic/target/x86_64-unknown-linux-gnu/Dockerfile.x86_64-unknown-linux-gnu-custom . [+] Building 10.0s (6/6) FINISHED => [internal] load build definition from Dockerfile.x86_64-unknown-linux-gnu-custom 0.0s => => transferring dockerfile: 166B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/ubuntu:20.04 0.6s => CACHED [1/2] FROM docker.io/library/ubuntu:20.04@sha256:fd92c36d3cb9b1d027c4d2a72c6bf0125da82425fc2ca 0.0s => [2/2] RUN eval "apt-get update && apt-get install -y libc6 g++-x86-64-linux-gnu libc6-dev-amd64-cross 8.8s => exporting to image 0.5s => => exporting layers 0.5s => => writing image sha256:92f3cf51cc16c7b176c9bc09ce28ef83c029881e9c5017589eb8372bf6fff8d3 0.0s => => naming to docker.io/library/cross-custom-basic:x86_64-unknown-linux-gnu-50dfa-pre-build 0.0s + /usr/local/bin/docker run --userns host --platform linux/arm64 -e 'CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc' -e 'CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc' -e 'CXX_x86_64_unknown_linux_gnu=x86_64-linux-gnu-g++' -e 'PKG_CONFIG_ALLOW_CROSS=1' -e 'XARGO_HOME=/xargo' -e 'CARGO_HOME=/cargo' -e 'CARGO_TARGET_DIR=/target' -e 'CROSS_RUNNER=' -e TERM -e 'USER=emil' --rm --user 501:20 -v /Users/emil/.xargo:/xargo:Z -v /Users/emil/.cargo:/cargo:Z -v /cargo/bin -v /Users/emil/workspace/cross-tests/basic:/project:Z -v /Users/emil/.rustup/toolchains/stable-aarch64-unknown-linux-gnu:/rust:Z,ro -v /Users/emil/workspace/cross-tests/basic/target:/target:Z -w /project -i -t cross-custom-basic:x86_64-unknown-linux-gnu-50dfa-pre-build sh -c 'PATH=$PATH:/rust/bin cargo build --target x86_64-unknown-linux-gnu -v' Fresh basic v0.1.0 (/project) Finished dev [unoptimized + debuginfo] target(s) in 0.47s basic on master [?] is 📦 v0.1.0 via 🦀 v1.61.0 took 12s ❯ file target/x86_64-unknown-linux-gnu/debug/basic target/x86_64-unknown-linux-gnu/debug/basic: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=5716ca6c6514e255e9a29eeeeac3d9cb4fb43c0a, for GNU/Linux 3.2.0, with debug_info, not stripped basic on master [?] is 📦 v0.1.0 via 🦀 v1.61.0 ❯ uname -a Darwin Emils-MacBook-Pro.local 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000 arm64 ``` also makes `cross +nightly-2022-01-01` possible. this is a requirement for #751 Co-authored-by: Emil Gardström <[email protected]>
2 parents 86e3f62 + 86a550d commit a4da896

36 files changed

+2261
-589
lines changed

.changes/817.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"description": "Images can now specify a certain toolchain via `target.{target}.image.toolchain`",
4+
"breaking": true,
5+
"type": "changed"
6+
},
7+
{
8+
"description": "made `cross +channel` parsing more compliant to parsing a toolchain",
9+
"type": "fixed"
10+
}
11+
]

.github/workflows/ci.yml

+29-7
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,14 @@ jobs:
333333

334334
# we should always have an artifact from a previous build.
335335
remote:
336-
needs: [shellcheck, test, check]
336+
needs: [test, check]
337337
runs-on: ubuntu-latest
338338
if: github.actor == 'bors[bot]'
339339
steps:
340340
- uses: actions/checkout@v3
341341
- uses: ./.github/actions/setup-rust
342342

343343
- name: LLVM instrument coverage
344-
id: remote-cov
345344
uses: ./.github/actions/cargo-llvm-cov
346345
with:
347346
name: integration-remote
@@ -353,15 +352,14 @@ jobs:
353352
shell: bash
354353

355354
bisect:
356-
needs: [shellcheck, test, check]
355+
needs: [test, check]
357356
runs-on: ubuntu-latest
358357
if: github.actor == 'bors[bot]'
359358
steps:
360359
- uses: actions/checkout@v3
361360
- uses: ./.github/actions/setup-rust
362361

363362
- name: LLVM instrument coverage
364-
id: bisect-cov
365363
uses: ./.github/actions/cargo-llvm-cov
366364
with:
367365
name: integration-bisect
@@ -372,16 +370,40 @@ jobs:
372370
run: ./ci/test-bisect.sh
373371
shell: bash
374372

373+
foreign:
374+
needs: [test, check]
375+
runs-on: ubuntu-latest
376+
if: github.actor == 'bors[bot]'
377+
steps:
378+
- uses: actions/checkout@v3
379+
- uses: ./.github/actions/setup-rust
380+
381+
- name: LLVM instrument coverage
382+
uses: ./.github/actions/cargo-llvm-cov
383+
with:
384+
name: integration-bisect
385+
- name: Set up QEMU
386+
uses: docker/setup-qemu-action@v2
387+
with:
388+
platforms: arm64
389+
- name: Set up docker buildx
390+
uses: docker/setup-buildx-action@v2
391+
id: buildx
392+
with:
393+
install: true
394+
- name: Run Foreign toolchain test
395+
run: ./ci/test-foreign-toolchain.sh
396+
shell: bash
397+
375398
docker-in-docker:
376-
needs: [shellcheck, test, check]
399+
needs: [test, check]
377400
runs-on: ubuntu-latest
378401
if: github.actor == 'bors[bot]'
379402
steps:
380403
- uses: actions/checkout@v3
381404
- uses: ./.github/actions/setup-rust
382405

383406
- name: LLVM instrument coverage
384-
id: docker-in-docker-cov
385407
uses: ./.github/actions/cargo-llvm-cov
386408
with:
387409
name: integration-docker-in-docker
@@ -405,7 +427,7 @@ jobs:
405427
github-token: ${{ secrets.GITHUB_TOKEN }}
406428

407429
conclusion:
408-
needs: [shellcheck, fmt, clippy, test, generate-matrix, build, publish, check, remote, bisect, docker-in-docker]
430+
needs: [shellcheck, fmt, clippy, test, generate-matrix, build, publish, check, remote, bisect, docker-in-docker, foreign]
409431
if: always()
410432
runs-on: ubuntu-latest
411433
steps:

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ version = "0.2.4"
1010
edition = "2021"
1111
include = [
1212
"src/**/*",
13-
"docker/Dockerfile.*",
14-
"docker/*.sh",
1513
"docs/*.md",
1614
"Cargo.toml",
1715
"Cargo.lock",

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ One of these container engines is required. If both are installed, `cross` will
4646
default to `docker`.
4747

4848
- [Docker]. Note that on Linux non-sudo users need to be in the `docker` group.
49-
Read the official [post-installation steps][post]. Requires version 1.24 or later.
49+
Read the official [post-installation steps][post]. Requires version 20.10 (API 1.40) or later.
5050

5151
[post]: https://docs.docker.com/install/linux/linux-postinstall/
5252

53-
- [Podman]. Requires version 1.6.3 or later.
53+
- [Podman]. Requires version 3.4.0 or later.
5454

5555
## Installation
5656

ci/shared.sh

+27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
#!/usr/bin/env bash
22

3+
ci_dir=$(dirname "${BASH_SOURCE[0]}")
4+
ci_dir=$(realpath "${ci_dir}")
5+
PROJECT_HOME=$(dirname "${ci_dir}")
6+
export PROJECT_HOME
7+
CARGO_TMP_DIR="${PROJECT_HOME}/target/tmp"
8+
export CARGO_TMP_DIR
9+
10+
if [[ -n "${CROSS_CONTAINER_ENGINE}" ]]; then
11+
CROSS_ENGINE="${CROSS_CONTAINER_ENGINE}"
12+
elif command -v docker >/dev/null 2>&1; then
13+
CROSS_ENGINE=docker
14+
else
15+
CROSS_ENGINE=podman
16+
fi
17+
export CROSS_ENGINE
18+
319
function retry {
420
local tries="${TRIES-5}"
521
local timeout="${TIMEOUT-1}"
@@ -21,3 +37,14 @@ function retry {
2137

2238
return ${exit_code}
2339
}
40+
41+
function mkcargotemp {
42+
local td=
43+
td="$CARGO_TMP_DIR"/$(mktemp -u "${@}" | xargs basename)
44+
mkdir -p "$td"
45+
echo '# Cargo.toml
46+
[workspace]
47+
members = ["'"$(basename "$td")"'"]
48+
' > "$CARGO_TMP_DIR"/Cargo.toml
49+
echo "$td"
50+
}

ci/test-bisect.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fi
1313
ci_dir=$(dirname "${BASH_SOURCE[0]}")
1414
ci_dir=$(realpath "${ci_dir}")
1515
. "${ci_dir}"/shared.sh
16-
project_home=$(dirname "${ci_dir}")
16+
1717

1818
main() {
1919
local td=
@@ -22,7 +22,7 @@ main() {
2222
retry cargo fetch
2323
cargo build
2424
cargo install cargo-bisect-rustc --debug
25-
export CROSS="${project_home}/target/debug/cross"
25+
export CROSS="${PROJECT_HOME}/target/debug/cross"
2626

2727
td="$(mktemp -d)"
2828
git clone --depth 1 https://github.com/cross-rs/rust-cpp-hello-word "${td}"

ci/test-cross-image.sh

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
# shellcheck disable=SC2086
2+
# shellcheck disable=SC2086,SC1091,SC1090
33

44
set -x
55
set -eo pipefail
@@ -20,14 +20,17 @@ if [[ -z "${CROSS_TARGET_CROSS_IMAGE}" ]]; then
2020
CROSS_TARGET_CROSS_IMAGE="ghcr.io/cross-rs/cross:main"
2121
fi
2222

23+
ci_dir=$(dirname "${BASH_SOURCE[0]}")
24+
ci_dir=$(realpath "${ci_dir}")
25+
. "${ci_dir}"/shared.sh
2326

2427
main() {
2528

2629
docker run --rm -e TARGET -e "CROSS_TARGET_${TARGET_UPPER//-/_}_IMAGE" \
2730
-v /var/run/docker.sock:/var/run/docker.sock \
2831
"${CROSS_TARGET_CROSS_IMAGE}" sh -c '
2932
#!/usr/bin/env sh
30-
td="$(mktemp -d)"
33+
td="$(mkcargotemp -d)"
3134
git clone --depth 1 https://github.com/cross-rs/rust-cpp-hello-word "${td}"
3235
cd "${td}"
3336
cross run --target "${TARGET}"

ci/test-docker-in-docker.sh

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
# shellcheck disable=SC1004
2+
# shellcheck disable=SC1004,SC1091,SC1090
33

44
# test to see that running docker-in-docker works
55

@@ -18,17 +18,17 @@ if [[ "${IMAGE}" ]]; then
1818
export "CROSS_TARGET_${TARGET_UPPER//-/_}_IMAGE"="${IMAGE}"
1919
fi
2020

21-
source=$(dirname "${BASH_SOURCE[0]}")
22-
source=$(realpath "${source}")
23-
home=$(dirname "${source}")
21+
ci_dir=$(dirname "${BASH_SOURCE[0]}")
22+
ci_dir=$(realpath "${ci_dir}")
23+
. "${ci_dir}"/shared.sh
2424

2525
main() {
26-
docker run -v "${home}":"${home}" -w "${home}" \
26+
docker run -v "${PROJECT_HOME}":"${PROJECT_HOME}" -w "${PROJECT_HOME}" \
2727
--rm -e TARGET -e RUSTFLAGS -e RUST_TEST_THREADS \
2828
-e LLVM_PROFILE_FILE -e CARGO_INCREMENTAL \
2929
-e "CROSS_TARGET_${TARGET_UPPER//-/_}_IMAGE" \
3030
-v /var/run/docker.sock:/var/run/docker.sock \
31-
docker:18.09-dind sh -c '
31+
docker:20.10-dind sh -c '
3232
#!/usr/bin/env sh
3333
set -x
3434
set -euo pipefail

ci/test-foreign-toolchain.sh

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env bash
2+
# shellcheck disable=SC1091,SC1090
3+
4+
# test to see that foreign toolchains work
5+
6+
set -x
7+
set -eo pipefail
8+
9+
ci_dir=$(dirname "${BASH_SOURCE[0]}")
10+
ci_dir=$(realpath "${ci_dir}")
11+
. "${ci_dir}"/shared.sh
12+
13+
main() {
14+
local td=
15+
16+
retry cargo fetch
17+
cargo build
18+
export CROSS="${PROJECT_HOME}/target/debug/cross"
19+
20+
td="$(mkcargotemp -d)"
21+
22+
pushd "${td}"
23+
cargo init --bin --name foreign_toolchain
24+
# shellcheck disable=SC2016
25+
echo '# Cross.toml
26+
[build]
27+
default-target = "x86_64-unknown-linux-musl"
28+
29+
[target."x86_64-unknown-linux-musl"]
30+
image.name = "alpine:edge"
31+
image.toolchain = ["x86_64-unknown-linux-musl"]
32+
pre-build = ["apk add --no-cache gcc musl-dev"]' >"${CARGO_TMP_DIR}"/Cross.toml
33+
34+
"$CROSS" run -v
35+
36+
local tmp_basename
37+
tmp_basename=$(basename "${CARGO_TMP_DIR}")
38+
"${CROSS_ENGINE}" images --format '{{.Repository}}:{{.Tag}}' --filter 'label=org.cross-rs.for-cross-target' | grep "cross-custom-${tmp_basename}" | xargs -t "${CROSS_ENGINE}" rmi
39+
40+
echo '# Cross.toml
41+
[build]
42+
default-target = "x86_64-unknown-linux-gnu"
43+
44+
[target.x86_64-unknown-linux-gnu]
45+
pre-build = [
46+
"apt-get update && apt-get install -y libc6 g++-x86-64-linux-gnu libc6-dev-amd64-cross",
47+
]
48+
49+
[target.x86_64-unknown-linux-gnu.env]
50+
passthrough = [
51+
"CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc",
52+
"CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER=/qemu-runner x86_64",
53+
"CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc",
54+
"CXX_x86_64_unknown_linux_gnu=x86_64-linux-gnu-g++",
55+
]
56+
57+
[target.x86_64-unknown-linux-gnu.image]
58+
name = "ubuntu:20.04"
59+
toolchain = ["aarch64-unknown-linux-gnu"]
60+
' >"${CARGO_TMP_DIR}"/Cross.toml
61+
62+
"$CROSS" build -v
63+
64+
"${CROSS_ENGINE}" images --format '{{.Repository}}:{{.Tag}}' --filter 'label=org.cross-rs.for-cross-target' | grep "cross-custom-${tmp_basename}" | xargs "${CROSS_ENGINE}" rmi
65+
66+
popd
67+
68+
rm -rf "${td}"
69+
}
70+
71+
main

ci/test-remote.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ fi
1414
ci_dir=$(dirname "${BASH_SOURCE[0]}")
1515
ci_dir=$(realpath "${ci_dir}")
1616
. "${ci_dir}"/shared.sh
17-
project_home=$(dirname "${ci_dir}")
1817

1918
main() {
2019
local err=
2120

2221
retry cargo fetch
2322
cargo build
24-
export CROSS="${project_home}/target/debug/cross"
25-
export CROSS_UTIL="${project_home}/target/debug/cross-util"
23+
export CROSS="${PROJECT_HOME}/target/debug/cross"
24+
export CROSS_UTIL="${PROJECT_HOME}/target/debug/cross-util"
2625

2726
# if the create volume fails, ensure it exists.
2827
if ! err=$("${CROSS_UTIL}" volumes create 2>&1 >/dev/null); then
@@ -40,7 +39,7 @@ main() {
4039

4140
cross_test_cpp() {
4241
local td=
43-
td="$(mktemp -d)"
42+
td="$(mkcargotemp -d)"
4443

4544
git clone --depth 1 https://github.com/cross-rs/rust-cpp-hello-word "${td}"
4645

0 commit comments

Comments
 (0)