Skip to content

Commit 31b235a

Browse files
Emilgardisreneleonhardt
authored andcommitted
cherry-pick improvements from cross-rs#1635
1 parent bc4b107 commit 31b235a

File tree

9 files changed

+75
-15
lines changed

9 files changed

+75
-15
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ jobs:
203203
id: build-docker-image
204204
if: steps.prepare-meta.outputs.has-image
205205
timeout-minutes: 120
206-
run: cargo xtask build-docker-image -v "${TARGET}${SUB:+.$SUB}" ${{ matrix.verbose && '-v' || '' }} --platform ${{ matrix.platform }}
206+
run: cargo xtask build-docker-image -v "${TARGET}${SUB:+.$SUB}" ${{ matrix.verbose && '-v' || '' }}${{ matrix.platform && '--platform ' }}${{ join(matrix.platform) || '' }}
207207
env:
208208
TARGET: ${{ matrix.target }}
209209
SUB: ${{ matrix.sub }}
@@ -323,7 +323,7 @@ jobs:
323323
github.ref == format('refs/heads/{0}', github.event.repository.default_branch) ||
324324
startsWith(github.ref, 'refs/tags/v')
325325
)
326-
run: cargo xtask build-docker-image -v --push "${TARGET}${SUB:+.$SUB}" --platform ${{ join(matrix.platforms, ',') }}
326+
run: cargo xtask build-docker-image -v --push "${TARGET}${SUB:+.$SUB}" ${{ matrix.verbose && '-v' || '' }}${{ matrix.platform && '--platform ' }}${{ join(matrix.platform) || '' }}
327327
env:
328328
TARGET: ${{ matrix.target }}
329329
SUB: ${{ matrix.sub }}

docker/qemu.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ build_static_slirp() {
131131

132132
rm -rf "${td}"
133133
}
134-
134+
# passed arg 1: $1 = arch, so for example "arm64" or "aarch64"
135135
main() {
136136
local version=5.1.0
137137

src/docker/image.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl From<String> for ImageReference {
162162
/// The architecture/platform to use in the image
163163
///
164164
/// <https://github.com/containerd/containerd/blob/release/1.6/platforms/platforms.go#L63>
165-
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize)]
165+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize)]
166166
#[serde(try_from = "String")]
167167
pub struct ImagePlatform {
168168
/// CPU architecture, x86_64, aarch64 etc

src/docker/provided_images.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{ImagePlatform, ProvidedImage};
55
pub static PROVIDED_IMAGES: &[ProvidedImage] = &[
66
ProvidedImage {
77
name: "x86_64-unknown-linux-gnu",
8-
platforms: &[ImagePlatform::X86_64_UNKNOWN_LINUX_GNU],
8+
platforms: &[ImagePlatform::X86_64_UNKNOWN_LINUX_GNU, ImagePlatform::AARCH64_UNKNOWN_LINUX_GNU],
99
sub: None
1010
},
1111
ProvidedImage {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub use self::rustc::{TargetList, VersionMetaExt};
7272
pub const CROSS_LABEL_DOMAIN: &str = "org.cross-rs";
7373

7474
#[allow(non_camel_case_types)]
75-
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Hash)]
75+
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Hash, PartialOrd, Ord)]
7676
#[serde(from = "&str", into = "String")]
7777
#[serde(rename_all = "snake_case")]
7878
pub enum TargetTriple {

targets.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ deploy = true
1111

1212
[[target]]
1313
target = "x86_64-unknown-linux-gnu"
14+
platforms = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu"]
1415
os = "ubuntu-latest"
1516
cpp = true
1617
dylib = true
@@ -47,6 +48,7 @@ runners = "native qemu-user qemu-system"
4748

4849
[[target]]
4950
target = "aarch64-unknown-linux-gnu"
51+
platforms = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu"]
5052
os = "ubuntu-latest"
5153
cpp = true
5254
dylib = true
@@ -399,13 +401,15 @@ run = true
399401
[[target]]
400402
target = "x86_64-pc-windows-gnu"
401403
os = "ubuntu-latest"
404+
platforms = ["x86_64-unknown-linux-gnu"]
402405
cpp = true
403406
std = true
404407
run = true
405408

406409
[[target]]
407410
target = "i686-pc-windows-gnu"
408411
os = "ubuntu-latest"
412+
platforms = ["x86_64-unknown-linux-gnu"]
409413
cpp = true
410414
std = true
411415
run = true
@@ -539,6 +543,7 @@ special = true
539543
[[target]]
540544
target = "aarch64-unknown-linux-gnu"
541545
sub = "centos"
546+
platforms = ["x86_64-unknown-linux-gnu"]
542547
os = "ubuntu-latest"
543548
cpp = true
544549
dylib = true

xtask/src/build_docker_image.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ pub fn build_docker_image(
146146
.contents_first(true)
147147
.into_iter()
148148
.filter_map(|e| e.ok().filter(|f| f.file_type().is_file()))
149+
// don't add native
150+
.filter(|f| {
151+
!f.file_name()
152+
.to_string_lossy()
153+
.starts_with("Dockerfile.native")
154+
})
149155
.filter_map(|f| {
150156
f.file_name()
151157
.to_string_lossy()
@@ -156,6 +162,17 @@ pub fn build_docker_image(
156162
.collect();
157163
}
158164
}
165+
166+
// grab platform from the targets.toml file, if it exists.
167+
targets.iter_mut().for_each(|t| {
168+
if let Some(m) = get_matrix()
169+
.iter()
170+
.find(|m| m.target == t.name && m.sub == t.sub)
171+
{
172+
t.platform = m.platforms.clone();
173+
}
174+
});
175+
159176
let gha = std::env::var("GITHUB_ACTIONS").is_ok();
160177
let mut progress = progress.map(|x| x.parse().unwrap());
161178
if gha {
@@ -176,9 +193,19 @@ pub fn build_docker_image(
176193
};
177194

178195
let mut results = vec![];
179-
for (target, dockerfile) in targets.iter() {
196+
for (platform, (target, dockerfile)) in targets
197+
.iter()
198+
.flat_map(|(t, d)| {
199+
t.platform
200+
.as_deref()
201+
.unwrap_or(&platforms)
202+
.iter()
203+
.map(move |p| (p, (t, d)))
204+
})
205+
.filter(|(p, _)| platforms.contains(p))
206+
{
180207
if gha && targets.len() > 1 {
181-
gha_print("::group::Build {target}");
208+
gha_print(&format!("::group::Build {target} for {}", platform.target));
182209
} else {
183210
msg_info.note(format_args!(
184211
"Build {target} for {}",

xtask/src/ci/target_matrix.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::process::Command;
22

3-
use crate::util::{get_matrix, gha_output, gha_print, CiTarget, ImageTarget};
43
use clap::builder::{BoolishValueParser, PossibleValuesParser};
54
use clap::Parser;
6-
use cross::docker::ImagePlatform;
7-
use cross::{shell::Verbosity, CommandExt};
5+
use cross::{docker::ImagePlatform, shell::Verbosity, CommandExt};
86
use serde::{Deserialize, Serialize};
97

8+
use crate::util::{get_matrix, gha_output, gha_print, CiTarget, ImageTarget};
9+
1010
#[derive(Parser, Debug)]
1111
pub struct TargetMatrix {
1212
/// check is being run as part of a weekly check
@@ -64,6 +64,7 @@ impl TargetMatrix {
6464
runners: vec![],
6565
none: false,
6666
has_image: true,
67+
platform: vec![],
6768
verbose: false,
6869
tests: vec!["all".to_owned()],
6970
},
@@ -341,6 +342,8 @@ struct TargetMatrixArgs {
341342
none: bool,
342343
#[clap(long)]
343344
has_image: bool,
345+
#[clap(long, num_args = 0..)]
346+
platform: Vec<ImagePlatform>,
344347
#[clap(long, short)]
345348
verbose: bool,
346349
#[clap(long, value_parser = PossibleValuesParser::new(&[
@@ -370,6 +373,7 @@ impl Default for TargetMatrixArgs {
370373
runners: Vec::new(),
371374
none: false,
372375
has_image: false,
376+
platform: Vec::new(),
373377
verbose: false,
374378
tests: vec!["all".to_owned()],
375379
}
@@ -423,6 +427,22 @@ impl TargetMatrixArgs {
423427
.any(|runner| m.runners.as_deref().unwrap_or_default().contains(runner))
424428
});
425429
}
430+
if !self.platform.is_empty() {
431+
matrix.retain(|t| {
432+
t.platforms()
433+
.iter()
434+
.any(|platform| self.platform.contains(platform))
435+
});
436+
matrix.iter_mut().for_each(|t| {
437+
t.platforms = Some(
438+
t.platforms()
439+
.iter()
440+
.filter(|&p| self.platform.contains(p))
441+
.cloned()
442+
.collect(),
443+
)
444+
});
445+
}
426446
}
427447

428448
fn tests(&self) -> Result<serde_json::Value, serde_json::Error> {

xtask/src/util.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use std::io::Write;
44
use std::path::{Path, PathBuf};
55
use std::process::Command;
66

7-
use cross::shell::MessageInfo;
87
use cross::{docker, CommandExt, ToUtf8};
8+
use cross::{docker::ImagePlatform, shell::MessageInfo};
99

10-
use cross::docker::ImagePlatform;
1110
use once_cell::sync::{Lazy, OnceCell};
1211
use serde::Deserialize;
1312

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

@@ -163,6 +164,7 @@ pub fn pull_image(
163164
pub struct ImageTarget {
164165
pub name: String,
165166
pub sub: Option<String>,
167+
pub platform: Option<Vec<ImagePlatform>>,
166168
}
167169

168170
impl ImageTarget {
@@ -236,13 +238,15 @@ impl std::str::FromStr for ImageTarget {
236238
return Ok(ImageTarget {
237239
name: target.to_string(),
238240
sub: Some(sub.to_string()),
241+
platform: None,
239242
});
240243
}
241244
}
242245

243246
Ok(ImageTarget {
244247
name: s.to_string(),
245248
sub: None,
249+
platform: None,
246250
})
247251
}
248252
}
@@ -383,27 +387,31 @@ mod tests {
383387
ImageTarget {
384388
name: "x86_64-unknown-linux-gnu".to_owned(),
385389
sub: None,
390+
platform: None,
386391
},
387392
"x86_64-unknown-linux-gnu".parse().unwrap()
388393
);
389394
assert_eq!(
390395
ImageTarget {
391396
name: "x86_64-unknown-linux-gnu".to_owned(),
392397
sub: Some("centos".to_owned()),
398+
platform: None,
393399
},
394400
"x86_64-unknown-linux-gnu.centos".parse().unwrap()
395401
);
396402
assert_eq!(
397403
ImageTarget {
398404
name: "thumbv8m.main-none-eabihf".to_owned(),
399405
sub: None,
406+
platform: None,
400407
},
401408
"thumbv8m.main-none-eabihf".parse().unwrap()
402409
);
403410
assert_eq!(
404411
ImageTarget {
405412
name: "thumbv8m.main-unknown-linux-gnueabihf".to_owned(),
406413
sub: Some("alpine".to_owned()),
414+
platform: None,
407415
},
408416
"thumbv8m.main-unknown-linux-gnueabihf.alpine"
409417
.parse()

0 commit comments

Comments
 (0)