Skip to content

Commit 376593e

Browse files
committed
Add dockerfile for cross.
This uses the installs Docker from the official sources using the latest Ubuntu image stable, and installs the latest stable Rust version internally. Cross is installed from the latest git (locked), which when tagged will work well with our releases. It exports the environment variable `CROSS_CONTAINER_IN_CONTAINER` so everything should work as expected.
1 parent 1b986f3 commit 376593e

File tree

8 files changed

+109
-7
lines changed

8 files changed

+109
-7
lines changed

.github/workflows/ci.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ jobs:
207207
- { target: thumbv7em-none-eabi, os: ubuntu-latest, std: 1 }
208208
- { target: thumbv7em-none-eabihf, os: ubuntu-latest, std: 1 }
209209
- { target: thumbv7m-none-eabi, os: ubuntu-latest, std: 1 }
210+
- { target: cross, os: ubuntu-latest }
210211
211212
build:
212213
name: target (${{ matrix.pretty }},${{ matrix.os }})
@@ -283,7 +284,7 @@ jobs:
283284
IMAGE: ${{ steps.build-docker-image.outputs.image }}
284285
shell: bash
285286
- name: Test Image
286-
if: steps.prepare-meta.outputs.has-image
287+
if: steps.prepare-meta.outputs.has-image && steps.prepare-meta.outputs.test-variant == 'default'
287288
run: ./ci/test.sh
288289
env:
289290
TARGET: ${{ matrix.target }}
@@ -300,6 +301,13 @@ jobs:
300301
target: ${{ matrix.target }}
301302
image: ${{ steps.build-docker-image.outputs.image }}
302303

304+
- name: Test Cross Image
305+
if: steps.prepare-meta.outputs.has-image && steps.prepare-meta.outputs.test-variant == 'cross'
306+
run: ./ci/test-cross-image.sh
307+
env:
308+
TARGET: 'aarch64-unknown-linux-gnu'
309+
shell: bash
310+
303311
- name: Login to GitHub Container Registry
304312
if: steps.prepare-meta.outputs.has-image
305313
uses: docker/login-action@v1

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1414
- #900 - add the option to skip copying build artifacts back to host when using remote cross via `CROSS_REMOTE_SKIP_BUILD_ARTIFACTS`.
1515
- #891 - support custom user namespace overrides by setting the `CROSS_CONTAINER_USER_NAMESPACE` environment variable.
1616
- #890 - support rootless docker via the `CROSS_ROOTLESS_CONTAINER_ENGINE` environment variable.
17+
- #878 - added an image `ghcr.io/cross-rs/cross` containing cross.
1718

1819
### Changed
1920

ci/test-cross-image.sh

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
# shellcheck disable=SC2086
3+
4+
set -x
5+
set -eo pipefail
6+
7+
if [[ -z "${TARGET}" ]]; then
8+
export TARGET="aarch64-unknown-linux-gnu"
9+
fi
10+
if [[ -z "${CROSS_TARGET_CROSS_IMAGE}" ]]; then
11+
CROSS_TARGET_CROSS_IMAGE="ghcr.io/cross-rs/cross:main"
12+
fi
13+
14+
main() {
15+
docker run --rm -e TARGET \
16+
-v /var/run/docker.sock:/var/run/docker.sock \
17+
"${CROSS_TARGET_CROSS_IMAGE}" sh -c '
18+
#!/usr/bin/env sh
19+
td="$(mktemp -d)"
20+
git clone --depth 1 https://github.com/cross-rs/rust-cpp-hello-word "${td}"
21+
cd "${td}"
22+
cross run --target "${TARGET}"
23+
'
24+
}
25+
26+
main "${@}"

docker/Dockerfile.cross

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM ubuntu:20.04 as rust
2+
ARG DEBIAN_FRONTEND=noninteractive
3+
COPY docker/lib.sh docker/cross.sh /
4+
COPY ./ /project
5+
RUN /cross.sh /project
6+
7+
# we build our images in 2 steps, to ensure we have a compact
8+
# image, since we want to add our current subdirectory
9+
FROM ubuntu:20.04
10+
COPY --from=rust /root/.cargo /root/.cargo
11+
COPY --from=rust /root/.rustup /root/.rustup
12+
13+
# need some basic devtools, and requirements for docker
14+
RUN apt-get update && apt-get install --assume-yes --no-install-recommends \
15+
ca-certificates \
16+
curl \
17+
git
18+
19+
RUN curl -fsSL https://get.docker.com | sh
20+
21+
ENV CROSS_CONTAINER_IN_CONTAINER=1 \
22+
PATH=/root/.cargo/bin:$PATH

docker/cross.sh

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
# shellcheck disable=SC1090,SC1091
3+
4+
set -x
5+
set -euo pipefail
6+
7+
. lib.sh
8+
9+
main() {
10+
local project_dir="${1}"
11+
12+
install_packages ca-certificates curl gcc libc6-dev
13+
14+
cd "${project_dir}"
15+
curl --proto "=https" --tlsv1.2 --retry 3 -sSfL https://sh.rustup.rs | sh -s -- -y
16+
source "${HOME}"/.cargo/env
17+
cargo install --path . --locked
18+
19+
purge_packages
20+
21+
rm -rf "${0}"
22+
}
23+
24+
main "${@}"

xtask/src/build_docker_image.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ pub fn build_docker_image(
144144
}
145145
}
146146
let gha = std::env::var("GITHUB_ACTIONS").is_ok();
147-
let docker_root = metadata.workspace_root.join("docker");
147+
let root = metadata.workspace_root;
148+
let docker_root = root.join("docker");
148149
let cross_toolchains_root = docker_root.join("cross-toolchains").join("docker");
149150
let targets = targets
150151
.into_iter()
@@ -237,7 +238,11 @@ pub fn build_docker_image(
237238
docker_build.args(&["--build-arg", arg]);
238239
}
239240

240-
docker_build.arg(".");
241+
if target.needs_workspace_root_context() {
242+
docker_build.arg(&root);
243+
} else {
244+
docker_build.arg(".");
245+
}
241246

242247
if !dry_run && (force || !push || gha) {
243248
let result = docker_build.run(msg_info, false);

xtask/src/ci.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ pub fn ci(args: CiJob, metadata: CargoMetadata) -> cross::Result<()> {
4040
// Set labels
4141
let mut labels = vec![];
4242

43-
labels.push(format!(
44-
"org.opencontainers.image.title=cross (for {})",
45-
target.triplet
46-
));
43+
let image_title = match target.triplet.as_ref() {
44+
"cross" => target.triplet.to_string(),
45+
_ => format!("cross (for {})", target.triplet),
46+
};
47+
labels.push(format!("org.opencontainers.image.title={image_title}"));
4748
labels.push(format!(
4849
"org.opencontainers.image.licenses={}",
4950
cross_meta.license.as_deref().unwrap_or_default()
@@ -69,6 +70,11 @@ pub fn ci(args: CiJob, metadata: CargoMetadata) -> cross::Result<()> {
6970
if target.has_ci_image() {
7071
gha_output("has-image", "true")
7172
}
73+
if target.is_default_test_image() {
74+
gha_output("test-variant", "default")
75+
} else {
76+
gha_output("test-variant", &target.triplet)
77+
}
7278
}
7379
CiJob::Check { ref_type, ref_name } => {
7480
let version = semver::Version::parse(&cross_meta.version)?;

xtask/src/util.rs

+10
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ impl ImageTarget {
129129
.iter()
130130
.any(|m| m.builds_image() && m.target == self.triplet && m.sub == self.sub)
131131
}
132+
133+
/// Determine if this target uses the default test script
134+
pub fn is_default_test_image(&self) -> bool {
135+
self.triplet != "cross"
136+
}
137+
138+
/// Determine if this target needs to interact with the project root.
139+
pub fn needs_workspace_root_context(&self) -> bool {
140+
self.triplet == "cross"
141+
}
132142
}
133143

134144
impl std::str::FromStr for ImageTarget {

0 commit comments

Comments
 (0)