Skip to content

Commit

Permalink
Add support for (absolut) target JSON paths
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-opp committed May 5, 2018
1 parent 8771abd commit c7edeca
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
16 changes: 10 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,22 @@ impl CompilationMode {
Ok(())
}

/// Returns the condensed target triple (removes any `.json` extension and path components).
fn triple(&self) -> &str {
match *self {
CompilationMode::Cross(ref target) => target.triple(),
CompilationMode::Native(ref triple) => triple,
}
}

/// Returns the original target triple passed to xargo (perhaps with `.json` extension).
fn orig_triple(&self) -> &str {
match *self {
CompilationMode::Cross(ref target) => target.orig_triple(),
CompilationMode::Native(ref triple) => triple,
}
}

fn is_native(&self) -> bool {
match *self {
CompilationMode::Native(_) => true,
Expand Down Expand Up @@ -151,12 +160,7 @@ fn run() -> Result<ExitStatus> {
};

let cmode = if let Some(triple) = args.target() {
if Path::new(triple).is_file() {
bail!(
"Xargo doesn't support files as an argument to --target. \
Use `--target foo` instead of `--target foo.json`."
)
} else if triple == meta.host {
if triple == meta.host {
Some(CompilationMode::Native(meta.host.clone()))
} else {
Target::new(triple, &cd, verbose)?.map(CompilationMode::Cross)
Expand Down
27 changes: 24 additions & 3 deletions src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Sysroot {
#[derive(Debug)]
pub enum Target {
Builtin { triple: String },
Custom { json: PathBuf, triple: String },
Custom { json: PathBuf, triple: String, orig_triple: String },
}

impl Target {
Expand All @@ -121,13 +121,24 @@ impl Target {
if rustc::targets(verbose)?.iter().any(|t| t == &triple) {
Ok(Some(Target::Builtin { triple: triple }))
} else {
let json = PathBuf::from(&triple);
if json.exists() {
let file_stem = match json.file_stem().and_then(|stem| stem.to_str()) {
Some(stem) => stem.into(),
None => {
bail!("target file name {:?} is empty or contains invalid unicode", json)
}
};
return Ok(Some(Target::Custom { json, triple: file_stem, orig_triple: triple }))
}
let mut json = cd.path().join(&triple);
json.set_extension("json");

if json.exists() {
return Ok(Some(Target::Custom {
json: json,
triple: triple,
triple: triple.clone(),
orig_triple: triple,
}));
} else {
if let Some(p) = env::var_os("RUST_TARGET_PATH") {
Expand All @@ -138,7 +149,8 @@ impl Target {
if json.exists() {
return Ok(Some(Target::Custom {
json: json,
triple: triple,
triple: triple.clone(),
orig_triple: triple,
}));
}
}
Expand All @@ -148,13 +160,22 @@ impl Target {
}
}

/// Returns the condensed target triple (removes any `.json` extension and path components).
pub fn triple(&self) -> &str {
match *self {
Target::Builtin { ref triple } => triple,
Target::Custom { ref triple, .. } => triple,
}
}

/// Returns the original target triple passed to xargo (perhaps with `.json` extension).
pub fn orig_triple(&self) -> &str {
match *self {
Target::Builtin { ref triple } => triple,
Target::Custom { ref orig_triple, .. } => orig_triple,
}
}

pub fn hash<H>(&self, hasher: &mut H) -> Result<()>
where
H: Hasher,
Expand Down
2 changes: 1 addition & 1 deletion src/sysroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ version = "0.0.0"
}
cmd.arg("--manifest-path");
cmd.arg(td.join("Cargo.toml"));
cmd.args(&["--target", cmode.triple()]);
cmd.args(&["--target", cmode.orig_triple()]);

if verbose {
cmd.arg("-v");
Expand Down

0 comments on commit c7edeca

Please sign in to comment.