Skip to content

Commit

Permalink
Add concept of native Dockerfile.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexhuszagh committed Jul 28, 2022
1 parent 557bac5 commit 6f1bf13
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
27 changes: 27 additions & 0 deletions docker/Dockerfile.native
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM ubuntu:20.04
ARG DEBIAN_FRONTEND=noninteractive

COPY common.sh lib.sh /
RUN /common.sh

COPY cmake.sh /
RUN /cmake.sh

COPY xargo.sh /
RUN /xargo.sh

ARG TARGET_ARCH
ARG QEMU_ARCH=$TARGET_ARCH

COPY qemu.sh /
RUN /qemu.sh $QEMU_ARCH softmmu

COPY dropbear.sh /
RUN /dropbear.sh

COPY linux-image.sh /
RUN /linux-image.sh $TARGET_ARCH

COPY linux-runner /

ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="/linux-runner $TARGET_ARCH"
1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[package]
build = "src/build.rs"
documentation = "https://github.com/cross-rs/cross"
license = "MIT OR Apache-2.0"
name = "xtask"
Expand Down
6 changes: 6 additions & 0 deletions xtask/src/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
println!(
"cargo:rustc-cfg=cross_triple=\"{}\"",
std::env::var("TARGET").expect("rustc must set the TARGET environment variable")
);
}
77 changes: 75 additions & 2 deletions xtask/src/build_docker_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::Path;

use crate::util::{cargo_metadata, gha_error, gha_output, gha_print};
use clap::Args;
use cross::docker::Architecture;
use cross::docker::ImagePlatform;
use cross::shell::MessageInfo;
use cross::{docker, CommandExt, ToUtf8};
Expand Down Expand Up @@ -165,6 +166,7 @@ pub fn build_docker_image(
platform
};

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

docker_build.args(&["--platform", &platform.docker_platform()]);
// add our platform, and determine if we need to use a native docker image
let docker_platform = platform.docker_platform();
let mut dockerfile = dockerfile.clone();
docker_build.args(&["--platform", &docker_platform]);
if target.sub.is_none() {
match (
engine.arch.as_ref(),
docker_platform.as_str(),
target.name.as_str(),
) {
(Some(Architecture::I386), "linux/386", "i686-unknown-linux-gnu") => {
dockerfile = native_dockerfile.clone();
docker_build.args(&["--build-arg", "TARGET_ARCH=i686"]);
docker_build.args(&["--build-arg", "QEMU_ARCH=i386"]);
}
(Some(Architecture::Amd64), "linux/amd64", "x86_64-unknown-linux-gnu") => {
dockerfile = native_dockerfile.clone();
docker_build.args(&["--build-arg", "TARGET_ARCH=x86_64"]);
}
(Some(Architecture::Arm), "linux/arm/v6", "arm-unknown-linux-gnueabi")
if is_armv6() =>
{
note_host_target_detection(msg_info)?;
dockerfile = native_dockerfile.clone();
docker_build.args(&["--build-arg", "TARGET_ARCH=arm"]);
}
(
Some(Architecture::Arm),
"linux/arm" | "linux/arm/v7",
"armv7-unknown-linux-gnueabihf",
) if is_armv7() => {
note_host_target_detection(msg_info)?;
dockerfile = native_dockerfile.clone();
docker_build.args(&["--build-arg", "TARGET_ARCH=armv7"]);
docker_build.args(&["--build-arg", "QEMU_ARCH=arm"]);
}
(
Some(Architecture::Arm64),
"linux/arm64" | "linux/arm64/v8",
"aarch64-unknown-linux-gnu",
) => {
dockerfile = native_dockerfile.clone();
docker_build.args(&["--build-arg", "TARGET_ARCH=aarch64"]);
}
(Some(Architecture::Ppc64Le), "linux/ppc64le", "powerpc64le-unknown-linux-gnu") => {
dockerfile = native_dockerfile.clone();
docker_build.args(&["--build-arg", "TARGET_ARCH=powerpc64le"]);
docker_build.args(&["--build-arg", "QEMU_ARCH=ppc64le"]);
}
(Some(Architecture::Riscv64), "linux/riscv64", "riscv64gc-unknown-linux-gnu") => {
dockerfile = native_dockerfile.clone();
docker_build.args(&["--build-arg", "TARGET_ARCH=riscv64"]);
}
(Some(Architecture::S390x), "linux/s390x", "s390x-unknown-linux-gnu") => {
dockerfile = native_dockerfile.clone();
docker_build.args(&["--build-arg", "TARGET_ARCH=s390x"]);
}
_ => (),
}
}

if push {
docker_build.arg("--push");
Expand Down Expand Up @@ -259,7 +320,7 @@ pub fn build_docker_image(
),
]);

docker_build.args(&["-f", dockerfile]);
docker_build.args(&["-f", &dockerfile]);

if gha || progress == "plain" {
docker_build.args(&["--progress", "plain"]);
Expand Down Expand Up @@ -321,6 +382,18 @@ pub fn build_docker_image(
Ok(())
}

fn is_armv6() -> bool {
cfg!(any(cross_triple = "arm-unknown-linux-gnueabi", cross_triple = "arm-unknown-linux-musleabi"))
}

fn is_armv7() -> bool {
cfg!(any(cross_triple = "armv7-unknown-linux-gnueabihf", cross_triple = "armv7-unknown-linux-musleabihf"))
}

fn note_host_target_detection(msg_info: &mut MessageInfo) -> cross::Result<()> {
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.")
}

pub fn determine_image_name(
target: &crate::ImageTarget,
repository: &str,
Expand Down

0 comments on commit 6f1bf13

Please sign in to comment.