Skip to content

Commit 1e40e45

Browse files
bors[bot]malbarbo
andcommitted
Merge #217
217: Simplify Target to use only to cases: BuiltIn and Custom r=Dylan-DPC a=malbarbo In the current form, cross needs to known every target its supports. This makes things too restrict. For example, its not possible to use a custom target image for a target that cross does not provides a docker image (like aarch64-unknown-linux-musl, for example). Cross refuses to execute saying it does not support the specified target. Besides that, to add a new docker image the cross code needs to be changed. This changes simplifies the Target enum to have only two variants: BuiltIn and Custom. Cross needs to known if a target is a custom target so it can call xargo, otherwise, all BuitIn targets can be uniformly handled. Co-authored-by: Marco A L Barbosa <[email protected]>
2 parents 65db4ac + 29bc75d commit 1e40e45

File tree

4 files changed

+111
-270
lines changed

4 files changed

+111
-270
lines changed

build.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::env;
22
use std::error::Error;
3-
use std::fs::File;
3+
use std::fs::{read_dir, File};
44
use std::io::Write;
55
use std::path::PathBuf;
66
use std::process::Command;
@@ -22,6 +22,11 @@ fn main() {
2222
.unwrap()
2323
.write_all(commit_info().as_bytes())
2424
.unwrap();
25+
26+
File::create(out_dir.join("docker-images.rs"))
27+
.unwrap()
28+
.write_all(docker_images().as_bytes())
29+
.unwrap();
2530
}
2631

2732
fn commit_info() -> String {
@@ -53,3 +58,19 @@ fn commit_date() -> Result<String, Some> {
5358
Err(Some {})
5459
}
5560
}
61+
62+
fn docker_images() -> String {
63+
let mut images = String::from("[");
64+
let mut dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
65+
dir.push("docker");
66+
for entry in read_dir(dir).unwrap() {
67+
let path = entry.unwrap().path();
68+
if path.is_dir() {
69+
images.push_str("\"");
70+
images.push_str(path.file_name().unwrap().to_str().unwrap());
71+
images.push_str("\", ");
72+
}
73+
}
74+
images.push_str("]");
75+
images
76+
}

src/docker.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use extensions::CommandExt;
1212
use id;
1313
use rustc;
1414

15+
const DOCKER_IMAGES: &[&str] = &include!(concat!(env!("OUT_DIR"), "/docker-images.rs"));
16+
1517
lazy_static! {
1618
/// Retrieve the Docker Daemon version.
1719
///
@@ -145,18 +147,22 @@ pub fn run(target: &Target,
145147
}
146148

147149
fn image(toml: Option<&Toml>, target: &Target) -> Result<String> {
148-
Ok(if let Some(toml) = toml {
149-
toml.image(target)?.map(|s| s.to_owned())
150-
} else {
151-
None
150+
if let Some(toml) = toml {
151+
if let Some(image) = toml.image(target)?.map(|s| s.to_owned()) {
152+
return Ok(image)
152153
}
153-
.unwrap_or_else(|| {
154-
let version = env!("CARGO_PKG_VERSION");
155-
let tag = if version.ends_with("-dev") {
156-
Cow::from("latest")
157-
} else {
158-
Cow::from(format!("v{}", version))
159-
};
160-
format!("japaric/{}:{}", target.triple(), tag)
161-
}))
154+
}
155+
156+
let version = env!("CARGO_PKG_VERSION");
157+
let tag = if version.ends_with("-dev") {
158+
Cow::from("latest")
159+
} else {
160+
Cow::from(format!("v{}", version))
161+
};
162+
let triple = target.triple();
163+
if !DOCKER_IMAGES.contains(&triple) {
164+
bail!("cross does not provide docker image for {} target, \
165+
specify a custom image in Cross.toml", triple);
166+
}
167+
Ok(format!("japaric/{}:{}", target.triple(), tag))
162168
}

0 commit comments

Comments
 (0)