Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ jobs:
id: build-docker-image
if: steps.prepare-meta.outputs.has-image
timeout-minutes: 120
run: cargo xtask build-docker-image -v "${TARGET}${SUB:+.$SUB}" ${{ matrix.verbose && '-v' || '' }} --platform ${{ matrix.platform }}
run: cargo xtask build-docker-image -v "${TARGET}${SUB:+.$SUB}" ${{ matrix.verbose && '-v' || '' }}${{ matrix.platform && '--platform ' }}${{ join(matrix.platform) || '' }}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems not necessary

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a very neat trick, if platforms are configured they will be used, otherwise it will be empty.

env:
TARGET: ${{ matrix.target }}
SUB: ${{ matrix.sub }}
Expand Down Expand Up @@ -323,7 +323,7 @@ jobs:
github.ref == format('refs/heads/{0}', github.event.repository.default_branch) ||
startsWith(github.ref, 'refs/tags/v')
)
run: cargo xtask build-docker-image -v --push "${TARGET}${SUB:+.$SUB}" --platform ${{ join(matrix.platforms, ',') }}
run: cargo xtask build-docker-image -v --push "${TARGET}${SUB:+.$SUB}" ${{ matrix.verbose && '-v' || '' }}${{ matrix.platform && '--platform ' }}${{ join(matrix.platform) || '' }}
env:
TARGET: ${{ matrix.target }}
SUB: ${{ matrix.sub }}
Expand Down
2 changes: 1 addition & 1 deletion docker/qemu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ build_static_slirp() {

rm -rf "${td}"
}

# passed arg 1: $1 = arch, so for example "arm64" or "aarch64"
main() {
local version=5.1.0

Expand Down
2 changes: 1 addition & 1 deletion src/docker/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl From<String> for ImageReference {
/// The architecture/platform to use in the image
///
/// <https://github.com/containerd/containerd/blob/release/1.6/platforms/platforms.go#L63>
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize)]
#[serde(try_from = "String")]
pub struct ImagePlatform {
/// CPU architecture, x86_64, aarch64 etc
Expand Down
2 changes: 1 addition & 1 deletion src/docker/provided_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{ImagePlatform, ProvidedImage};
pub static PROVIDED_IMAGES: &[ProvidedImage] = &[
ProvidedImage {
name: "x86_64-unknown-linux-gnu",
platforms: &[ImagePlatform::X86_64_UNKNOWN_LINUX_GNU],
platforms: &[ImagePlatform::X86_64_UNKNOWN_LINUX_GNU, ImagePlatform::AARCH64_UNKNOWN_LINUX_GNU],
sub: None
},
ProvidedImage {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub use self::rustc::{TargetList, VersionMetaExt};
pub const CROSS_LABEL_DOMAIN: &str = "org.cross-rs";

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Hash, PartialOrd, Ord)]
#[serde(from = "&str", into = "String")]
#[serde(rename_all = "snake_case")]
pub enum TargetTriple {
Expand Down
4 changes: 4 additions & 0 deletions targets.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ std = true
run = true
runners = "native qemu-user qemu-system"
deploy = true
platforms = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu"]

[[target]]
target = "x86_64-unknown-linux-musl"
Expand Down Expand Up @@ -402,13 +403,15 @@ os = "ubuntu-latest"
cpp = true
std = true
run = true
platforms = ["x86_64-unknown-linux-gnu"]

[[target]]
target = "i686-pc-windows-gnu"
os = "ubuntu-latest"
cpp = true
std = true
run = true
platforms = ["x86_64-unknown-linux-gnu"]

[[target]]
# Disabled for now, see https://github.com/rust-lang/rust/issues/98216 & https://github.com/cross-rs/cross/issues/634
Expand Down Expand Up @@ -545,3 +548,4 @@ dylib = true
std = true
run = true
runners = "qemu-user qemu-system"
platforms = ["x86_64-unknown-linux-gnu"]
31 changes: 29 additions & 2 deletions xtask/src/build_docker_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ pub fn build_docker_image(
.contents_first(true)
.into_iter()
.filter_map(|e| e.ok().filter(|f| f.file_type().is_file()))
// don't add native
.filter(|f| {
!f.file_name()
.to_string_lossy()
.starts_with("Dockerfile.native")
})
.filter_map(|f| {
f.file_name()
.to_string_lossy()
Expand All @@ -156,6 +162,17 @@ pub fn build_docker_image(
.collect();
}
}

// grab platform from the targets.toml file, if it exists.
targets.iter_mut().for_each(|t| {
if let Some(m) = get_matrix()
.iter()
.find(|m| m.target == t.name && m.sub == t.sub)
{
t.platform = m.platforms.clone();
}
});

let gha = std::env::var("GITHUB_ACTIONS").is_ok();
let mut progress = progress.map(|x| x.parse().unwrap());
if gha {
Expand All @@ -176,9 +193,19 @@ pub fn build_docker_image(
};

let mut results = vec![];
for (target, dockerfile) in targets.iter() {
for (platform, (target, dockerfile)) in targets
.iter()
.flat_map(|(t, d)| {
t.platform
.as_deref()
.unwrap_or(&platforms)
.iter()
.map(move |p| (p, (t, d)))
})
.filter(|(p, _)| platforms.contains(p))
{
if gha && targets.len() > 1 {
gha_print("::group::Build {target}");
gha_print(&format!("::group::Build {target} for {}", platform.target));
} else {
msg_info.note(format_args!(
"Build {target} for {}",
Expand Down
26 changes: 23 additions & 3 deletions xtask/src/ci/target_matrix.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::process::Command;

use crate::util::{get_matrix, gha_output, gha_print, CiTarget, ImageTarget};
use clap::builder::{BoolishValueParser, PossibleValuesParser};
use clap::Parser;
use cross::docker::ImagePlatform;
use cross::{shell::Verbosity, CommandExt};
use cross::{docker::ImagePlatform, shell::Verbosity, CommandExt};
use serde::{Deserialize, Serialize};

use crate::util::{get_matrix, gha_output, gha_print, CiTarget, ImageTarget};

#[derive(Parser, Debug)]
pub struct TargetMatrix {
/// check is being run as part of a weekly check
Expand Down Expand Up @@ -64,6 +64,7 @@ impl TargetMatrix {
runners: vec![],
none: false,
has_image: true,
platform: vec![],
verbose: false,
tests: vec!["all".to_owned()],
},
Expand Down Expand Up @@ -341,6 +342,8 @@ struct TargetMatrixArgs {
none: bool,
#[clap(long)]
has_image: bool,
#[clap(long, num_args = 0..)]
platform: Vec<ImagePlatform>,
#[clap(long, short)]
verbose: bool,
#[clap(long, value_parser = PossibleValuesParser::new(&[
Expand Down Expand Up @@ -370,6 +373,7 @@ impl Default for TargetMatrixArgs {
runners: Vec::new(),
none: false,
has_image: false,
platform: Vec::new(),
verbose: false,
tests: vec!["all".to_owned()],
}
Expand Down Expand Up @@ -423,6 +427,22 @@ impl TargetMatrixArgs {
.any(|runner| m.runners.as_deref().unwrap_or_default().contains(runner))
});
}
if !self.platform.is_empty() {
matrix.retain(|t| {
t.platforms()
.iter()
.any(|platform| self.platform.contains(platform))
});
matrix.iter_mut().for_each(|t| {
t.platforms = Some(
t.platforms()
.iter()
.filter(|&p| self.platform.contains(p))
.cloned()
.collect(),
)
});
}
}

fn tests(&self) -> Result<serde_json::Value, serde_json::Error> {
Expand Down
16 changes: 12 additions & 4 deletions xtask/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;

use cross::shell::MessageInfo;
use cross::{docker, CommandExt, ToUtf8};
use cross::{docker::ImagePlatform, shell::MessageInfo};

use cross::docker::ImagePlatform;
use once_cell::sync::{Lazy, OnceCell};
use serde::Deserialize;

Expand Down Expand Up @@ -47,9 +46,9 @@ pub struct CiTarget {
/// if `true` publish the generated binaries for cross
#[serde(default)]
pub deploy: Option<bool>,
/// the platform to build this image for, defaults to `["linux/amd64"]`, takes multiple
/// the platform to build this image for, defaults to whatever is needed, takes multiple
#[serde(skip_serializing_if = "Option::is_none")]
platforms: Option<Vec<ImagePlatform>>,
pub platforms: Option<Vec<ImagePlatform>>,
/// if `true` signal that this target requires `-Zbuild-std`
#[serde(skip_serializing_if = "Option::is_none")]
pub build_std: Option<bool>,
Expand Down Expand Up @@ -83,6 +82,8 @@ impl CiTarget {
crate::ImageTarget {
name: self.target.clone(),
sub: self.sub.clone(),
// XXX: This does not align with platforms() by design, as we want to be able to know if the field was set or not.
platform: self.platforms.clone(),
}
}

Expand Down Expand Up @@ -163,6 +164,7 @@ pub fn pull_image(
pub struct ImageTarget {
pub name: String,
pub sub: Option<String>,
pub platform: Option<Vec<ImagePlatform>>,
}

impl ImageTarget {
Expand Down Expand Up @@ -236,13 +238,15 @@ impl std::str::FromStr for ImageTarget {
return Ok(ImageTarget {
name: target.to_string(),
sub: Some(sub.to_string()),
platform: None,
});
}
}

Ok(ImageTarget {
name: s.to_string(),
sub: None,
platform: None,
})
}
}
Expand Down Expand Up @@ -383,27 +387,31 @@ mod tests {
ImageTarget {
name: "x86_64-unknown-linux-gnu".to_owned(),
sub: None,
platform: None,
},
"x86_64-unknown-linux-gnu".parse().unwrap()
);
assert_eq!(
ImageTarget {
name: "x86_64-unknown-linux-gnu".to_owned(),
sub: Some("centos".to_owned()),
platform: None,
},
"x86_64-unknown-linux-gnu.centos".parse().unwrap()
);
assert_eq!(
ImageTarget {
name: "thumbv8m.main-none-eabihf".to_owned(),
sub: None,
platform: None,
},
"thumbv8m.main-none-eabihf".parse().unwrap()
);
assert_eq!(
ImageTarget {
name: "thumbv8m.main-unknown-linux-gnueabihf".to_owned(),
sub: Some("alpine".to_owned()),
platform: None,
},
"thumbv8m.main-unknown-linux-gnueabihf.alpine"
.parse()
Expand Down