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
2 changes: 2 additions & 0 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub struct Profile {
pub test: bool,
pub doc: bool,
pub run_custom_build: bool,
pub sysroot: Option<PathBuf>,
}

#[derive(Default, Clone, Debug)]
Expand Down Expand Up @@ -466,6 +467,7 @@ impl Default for Profile {
test: false,
doc: false,
run_custom_build: false,
sysroot: None,
}
}
}
Expand Down
30 changes: 28 additions & 2 deletions src/cargo/ops/cargo_rustc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io::prelude::*;
use std::path::{self, PathBuf};
use std::path::{self, Path, PathBuf};
use std::sync::Arc;

use core::{Package, PackageId, PackageSet, Target, Resolve};
Expand Down Expand Up @@ -446,10 +446,32 @@ fn build_base_args(cx: &Context,
cmd: &mut CommandPrototype,
unit: &Unit,
crate_types: &[&str]) {
// Check if the sysroot is applicable for this Unit
// For the explanation of this logic see Context::rustflags_args
fn applicable_sysroot<'a>(sysroot: &'a Option<PathBuf>,
cx: &Context,
unit: &Unit) -> Option<&'a Path> {
sysroot.as_ref().and_then(|sysroot| {
let compiling_with_target = cx.build_config.requested_target.is_some();
let is_target_kind = unit.kind == Kind::Target;
let use_sysroot = match (compiling_with_target, is_target_kind) {
(false, _) => true,
(true, true) => true,
(true, false) => false,
};

if use_sysroot {
Some(&**sysroot)
} else {
None
}
})
}

let Profile {
opt_level, lto, codegen_units, ref rustc_args, debuginfo,
debug_assertions, rpath, test, doc: _doc, run_custom_build,
rustdoc_args: _,
ref sysroot, rustdoc_args: _,
} = *unit.profile;
assert!(!run_custom_build);

Expand All @@ -458,6 +480,10 @@ fn build_base_args(cx: &Context,

cmd.arg(&root_path(cx, unit));

if let Some(sysroot) = applicable_sysroot(sysroot, cx, unit) {
cmd.arg("--sysroot").arg(sysroot.display().to_string());
}

cmd.arg("--crate-name").arg(&unit.target.crate_name());

for crate_type in crate_types.iter() {
Expand Down
6 changes: 5 additions & 1 deletion src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ pub struct TomlProfile {
debug: Option<bool>,
debug_assertions: Option<bool>,
rpath: Option<bool>,
sysroot: Option<String>,
}

#[derive(RustcDecodable)]
Expand Down Expand Up @@ -973,7 +974,7 @@ fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {

fn merge(profile: Profile, toml: Option<&TomlProfile>) -> Profile {
let &TomlProfile {
opt_level, lto, codegen_units, debug, debug_assertions, rpath
opt_level, lto, codegen_units, debug, debug_assertions, rpath, ref sysroot
} = match toml {
Some(toml) => toml,
None => return profile,
Expand All @@ -990,6 +991,9 @@ fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {
test: profile.test,
doc: profile.doc,
run_custom_build: profile.run_custom_build,
// FIXME if 'sysroot' is a relative path it should be relative to the root of the cargo
// project (???)
sysroot: sysroot.as_ref().map(|s| PathBuf::from(s)),
}
}
}