Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fallback on value package.rust-src if XARGO_RUST_SRC doesn't exist #288

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,18 @@ fn run(cargo_mode: XargoMode) -> Result<Option<ExitStatus>> {
// We can't build sysroot with stable or beta due to unstable features
let sysroot = rustc::sysroot(verbose)?;
let src = match meta.channel {
Channel::Dev => rustc::Src::from_env().ok_or(
Channel::Dev => if let Some(env) = rustc::Src::from_env() {
Some(env)
} else {
xargo::toml_src(&root)?
}.ok_or(
"The XARGO_RUST_SRC env variable must be set and point to the \
Rust source directory when working with the 'dev' channel",
)?,
Channel::Nightly => if let Some(src) = rustc::Src::from_env() {
src
} else if let Some(src) = xargo::toml_src(&root)? {
src
} else {
sysroot.src()?
},
Expand Down
6 changes: 6 additions & 0 deletions src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ impl Sysroot {
}
}

impl From<PathBuf> for Src {
fn from(path: PathBuf) -> Self {
Self { path }
jam1garner marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[derive(Debug)]
pub enum Target {
Builtin { triple: String },
Expand Down
24 changes: 24 additions & 0 deletions src/xargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use errors::*;
use extensions::CommandExt;
use flock::{FileLock, Filesystem};
use {cargo, util};
use rustc::Src;

pub fn run(
args: &Args,
Expand Down Expand Up @@ -118,6 +119,11 @@ impl Toml {
pub fn patch(&self) -> Option<&Value> {
self.table.lookup("patch")
}

/// Returns the `rust-src` part of Xargo.toml
pub fn package(&self) -> Option<&Value> {
self.table.lookup("package")
}
}

/// Returns the closest directory containing a 'Xargo.toml' and the parsed
Expand All @@ -130,3 +136,21 @@ pub fn toml(root: &Root) -> Result<(Option<&Path>, Option<Toml>)> {
Ok((None, None))
}
}

/// Returns the closest directory containing a 'Xargo.toml' and the parsed
/// content of this 'Xargo.toml'
pub fn toml_src(root: &Root) -> Result<Option<Src>> {
Ok(if let Some(toml) = toml(root)?.1 {
if let Some(Value::Table(table)) = toml.package() {
if let Some(src) = table.get("rust-src").map(Value::as_str).flatten() {
env::current_dir().map(|cur_dir| Src::from(cur_dir.join(src))).ok()
} else {
None
}
} else {
None
}
} else {
None
})
}