Skip to content

Commit 832bba3

Browse files
authored
Try #982: --target x86_64-unknown-linux-gnu
2 parents 557bac5 + 046cc34 commit 832bba3

9 files changed

+245
-3
lines changed

docker/Dockerfile.native

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
FROM ubuntu:20.04
2+
ARG DEBIAN_FRONTEND=noninteractive
3+
4+
COPY common.sh lib.sh /
5+
RUN /common.sh
6+
7+
COPY cmake.sh /
8+
RUN /cmake.sh
9+
10+
COPY xargo.sh /
11+
RUN /xargo.sh
12+
13+
ARG TARGETARCH
14+
ARG TARGETVARIANT
15+
ARG CROSS_TRIPLE
16+
17+
COPY qemu.sh native-qemu.sh /
18+
RUN /native-qemu.sh
19+
20+
COPY dropbear.sh /
21+
RUN /dropbear.sh
22+
23+
COPY linux-image.sh native-linux-image.sh /
24+
RUN /native-linux-image.sh
25+
26+
COPY linux-runner native-linux-runner /
27+
28+
ENV CROSS_TARGETARCH=$TARGETARCH
29+
ENV CROSS_TARGETVARIANT=$TARGETVARIANT
30+
ENV CARGO_TARGET_${CROSS_TRIPLE}_RUNNER="/native-linux-runner"

docker/Dockerfile.x86_64-unknown-linux-gnu

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ RUN /cmake.sh
1010
COPY xargo.sh /
1111
RUN /xargo.sh
1212

13+
RUN apt-get update && apt-get install --assume-yes --no-install-recommends \
14+
g++-x86-64-linux-gnu \
15+
libc6-dev-amd64-cross
16+
17+
COPY deny-debian-packages.sh /
18+
RUN TARGET_ARCH=amd64 /deny-debian-packages.sh \
19+
binutils \
20+
binutils-x86-64-linux-gnu
21+
1322
COPY qemu.sh /
1423
RUN /qemu.sh x86_64 softmmu
1524

@@ -21,4 +30,11 @@ RUN /linux-image.sh x86_64
2130

2231
COPY linux-runner /
2332

24-
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="/linux-runner x86_64"
33+
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc \
34+
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="/linux-runner x86_64" \
35+
CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc \
36+
CXX_x86_64_unknown_linux_gnu=x86_64-linux-gnu-g++ \
37+
BINDGEN_EXTRA_CLANG_ARGS_x86_64_unknown_linux_gnu="--sysroot=/usr/x86_64-linux-gnu" \
38+
QEMU_LD_PREFIX=/usr/x86_64-linux-gnu \
39+
RUST_TEST_THREADS=1 \
40+
PKG_CONFIG_PATH="/usr/lib/x86_64-linux-gnu/pkgconfig/:${PKG_CONFIG_PATH}"

docker/lib.sh

100644100755
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,72 @@ download_gcc() {
8686

8787
download_mirrors "gcc/gcc-${version}" "${filename}" "${GNU_MIRRORS[@]}"
8888
}
89+
90+
docker_to_qemu_arch() {
91+
local arch="${1}"
92+
case "${arch}" in
93+
arm64)
94+
echo "aarch64"
95+
;;
96+
386)
97+
echo "i386"
98+
;;
99+
amd64)
100+
echo "x86_64"
101+
;;
102+
arm|ppc64le|riscv64|s390x)
103+
echo "${arch}"
104+
;;
105+
*)
106+
echo "Unknown Docker image architecture, got \"${arch}\"." >&2
107+
exit 1
108+
;;
109+
esac
110+
}
111+
112+
docker_to_linux_arch() {
113+
# variant may not be provided
114+
local oldstate
115+
oldstate="$(set +o)"
116+
set +u
117+
118+
local arch="${1}"
119+
local variant="${2}"
120+
case "${arch}" in
121+
arm64)
122+
echo "aarch64"
123+
;;
124+
386)
125+
echo "i686"
126+
;;
127+
amd64)
128+
echo "x86_64"
129+
;;
130+
ppc64le)
131+
echo "powerpc64le"
132+
;;
133+
arm)
134+
case "${variant}" in
135+
v6)
136+
echo "arm"
137+
;;
138+
""|v7)
139+
echo "armv7"
140+
;;
141+
*)
142+
echo "Unknown Docker image variant, got \"${variant}\"." >&2
143+
exit 1
144+
;;
145+
esac
146+
;;
147+
riscv64|s390x)
148+
echo "${arch}"
149+
;;
150+
*)
151+
echo "Unknown Docker image architecture, got \"${arch}\"." >&2
152+
exit 1
153+
;;
154+
esac
155+
156+
eval "${oldstate}"
157+
}

docker/native-linux-image.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
set -eo pipefail
5+
6+
# shellcheck disable=SC1091
7+
. lib.sh
8+
9+
main() {
10+
local arch
11+
arch=$(docker_to_linux_arch "${TARGETARCH}" "${TARGETVARIANT}")
12+
/linux-image.sh "${arch}"
13+
rm "${0}"
14+
}
15+
16+
main "${@}"

docker/native-linux-runner

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
# shellcheck disable=SC1091
6+
. lib.sh
7+
8+
main() {
9+
local arch
10+
arch=$(docker_to_linux_arch "${CROSS_TARGETARCH}" "${CROSS_TARGETVARIANT}")
11+
exec /linux-runner "${arch}" "${@}"
12+
}
13+
14+
main "${@}"

docker/native-qemu.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
set -euo pipefail
5+
6+
# shellcheck disable=SC1091
7+
. lib.sh
8+
9+
main() {
10+
local arch
11+
arch=$(docker_to_qemu_arch "${TARGETARCH}")
12+
/qemu.sh "${arch}" softmmu
13+
rm "${0}"
14+
}
15+
16+
main "${@}"

xtask/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[package]
2+
build = "src/build.rs"
23
documentation = "https://github.com/cross-rs/cross"
34
license = "MIT OR Apache-2.0"
45
name = "xtask"

xtask/src/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let target = std::env::var("TARGET").expect("rustc must set the TARGET environment variable");
3+
println!("cargo:rustc-cfg=cross_triple=\"{target}\"",);
4+
println!("cargo:rustc-env=CROSS_TRIPLE=\"{target}\"",);
5+
}

xtask/src/build_docker_image.rs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::path::Path;
33

44
use crate::util::{cargo_metadata, gha_error, gha_output, gha_print};
55
use clap::Args;
6+
use cross::docker::Architecture;
67
use cross::docker::ImagePlatform;
78
use cross::shell::MessageInfo;
89
use cross::{docker, CommandExt, ToUtf8};
@@ -165,6 +166,7 @@ pub fn build_docker_image(
165166
platform
166167
};
167168

169+
let native_dockerfile = docker_root.join("Dockerfile.native").to_utf8()?.to_string();
168170
let mut results = vec![];
169171
for (platform, (target, dockerfile)) in targets
170172
.iter()
@@ -179,7 +181,24 @@ pub fn build_docker_image(
179181
docker_build.args(&["buildx", "build"]);
180182
docker_build.current_dir(&docker_root);
181183

182-
docker_build.args(&["--platform", &platform.docker_platform()]);
184+
// add our platform, and determine if we need to use a native docker image
185+
let docker_platform = platform.docker_platform();
186+
let mut dockerfile = dockerfile.clone();
187+
docker_build.args(&["--platform", &docker_platform]);
188+
docker_build.args(&[
189+
"--build-arg",
190+
&format!("CROSS_TRIPLE={}", env!("CROSS_TRIPLE")),
191+
]);
192+
if target.sub.is_none()
193+
&& is_native(
194+
engine.arch.as_ref(),
195+
docker_platform.as_str(),
196+
target,
197+
msg_info,
198+
)?
199+
{
200+
dockerfile = native_dockerfile.clone();
201+
}
183202

184203
if push {
185204
docker_build.arg("--push");
@@ -259,7 +278,7 @@ pub fn build_docker_image(
259278
),
260279
]);
261280

262-
docker_build.args(&["-f", dockerfile]);
281+
docker_build.args(&["-f", &dockerfile]);
263282

264283
if gha || progress == "plain" {
265284
docker_build.args(&["--progress", "plain"]);
@@ -321,6 +340,62 @@ pub fn build_docker_image(
321340
Ok(())
322341
}
323342

343+
fn is_armv6() -> bool {
344+
cfg!(any(
345+
cross_triple = "arm-unknown-linux-gnueabi",
346+
cross_triple = "arm-unknown-linux-musleabi"
347+
))
348+
}
349+
350+
fn is_armv7() -> bool {
351+
cfg!(any(
352+
cross_triple = "armv7-unknown-linux-gnueabihf",
353+
cross_triple = "armv7-unknown-linux-musleabihf"
354+
))
355+
}
356+
357+
fn is_native(
358+
arch: Option<&Architecture>,
359+
platform: &str,
360+
target: &crate::ImageTarget,
361+
msg_info: &mut MessageInfo,
362+
) -> cross::Result<bool> {
363+
Ok(match target.sub {
364+
Some(_) => false,
365+
None => match (arch, platform, target.name.as_str()) {
366+
(Some(Architecture::I386), "linux/386", "i686-unknown-linux-gnu")
367+
| (Some(Architecture::Amd64), "linux/amd64", "x86_64-unknown-linux-gnu")
368+
| (
369+
Some(Architecture::Arm64),
370+
"linux/arm64" | "linux/arm64/v8",
371+
"aarch64-unknown-linux-gnu",
372+
)
373+
| (Some(Architecture::Ppc64Le), "linux/ppc64le", "powerpc64le-unknown-linux-gnu")
374+
| (Some(Architecture::Riscv64), "linux/riscv64", "riscv64gc-unknown-linux-gnu")
375+
| (Some(Architecture::S390x), "linux/s390x", "s390x-unknown-linux-gnu") => true,
376+
(Some(Architecture::Arm), "linux/arm/v6", "arm-unknown-linux-gnueabi")
377+
if is_armv6() =>
378+
{
379+
note_host_target_detection(msg_info)?;
380+
true
381+
}
382+
(
383+
Some(Architecture::Arm),
384+
"linux/arm" | "linux/arm/v7",
385+
"armv7-unknown-linux-gnueabihf",
386+
) if is_armv7() => {
387+
note_host_target_detection(msg_info)?;
388+
true
389+
}
390+
_ => false,
391+
},
392+
})
393+
}
394+
395+
fn note_host_target_detection(msg_info: &mut MessageInfo) -> cross::Result<()> {
396+
msg_info.note("using the rust target triple to determine the host triple to determine if the docker platform is native. this may fail if cross-compiling xtask.")
397+
}
398+
324399
pub fn determine_image_name(
325400
target: &crate::ImageTarget,
326401
repository: &str,

0 commit comments

Comments
 (0)