Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
10 changes: 5 additions & 5 deletions crates/resolver-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use cargo::core::resolver::{self, ResolveOpts};
use cargo::core::source::{GitReference, SourceId};
use cargo::core::Resolve;
use cargo::core::{Dependency, PackageId, Registry, Summary};
use cargo::util::{CargoResult, Config, Graph, IntoUrl};
use cargo::util::{CargoResult, Config, Graph, IntoUrl, Platform};

use proptest::collection::{btree_map, vec};
use proptest::prelude::*;
Expand Down Expand Up @@ -170,7 +170,7 @@ pub fn resolve_with_config_raw(
let summary = Summary::new(
pkg_id("root"),
deps,
&BTreeMap::<String, Vec<String>>::new(),
&BTreeMap::<String, (Option<Platform>, Vec<String>)>::new(),
None::<String>,
false,
)
Expand Down Expand Up @@ -571,7 +571,7 @@ pub fn pkg_dep<T: ToPkgId>(name: T, dep: Vec<Dependency>) -> Summary {
Summary::new(
name.to_pkgid(),
dep,
&BTreeMap::<String, Vec<String>>::new(),
&BTreeMap::<String, (Option<Platform>, Vec<String>)>::new(),
link,
false,
)
Expand Down Expand Up @@ -599,7 +599,7 @@ pub fn pkg_loc(name: &str, loc: &str) -> Summary {
Summary::new(
pkg_id_loc(name, loc),
Vec::new(),
&BTreeMap::<String, Vec<String>>::new(),
&BTreeMap::<String, (Option<Platform>, Vec<String>)>::new(),
link,
false,
)
Expand All @@ -613,7 +613,7 @@ pub fn remove_dep(sum: &Summary, ind: usize) -> Summary {
Summary::new(
sum.package_id(),
deps,
&BTreeMap::<String, Vec<String>>::new(),
&BTreeMap::<String, (Option<Platform>, Vec<String>)>::new(),
sum.links().map(|a| a.as_str()),
sum.namespaced_features(),
)
Expand Down
20 changes: 13 additions & 7 deletions src/cargo/core/compiler/build_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::core::profiles::Profiles;
use crate::core::{Dependency, Workspace};
use crate::core::{PackageId, PackageSet, Resolve};
use crate::util::errors::CargoResult;
use crate::util::{profile, Cfg, Config, Rustc};
use crate::util::{profile, Cfg, Config, Platform, Rustc};

mod target_info;
pub use self::target_info::{FileFlavor, TargetInfo};
Expand Down Expand Up @@ -95,12 +95,10 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
.is_public_dep(unit.pkg.package_id(), dep.pkg.package_id())
}

/// Whether a dependency should be compiled for the host or target platform,
/// Whether a given platform matches the host or target platform,
/// specified by `Kind`.
pub fn dep_platform_activated(&self, dep: &Dependency, kind: Kind) -> bool {
// If this dependency is only available for certain platforms,
// make sure we're only enabling it for that platform.
let platform = match dep.platform() {
pub fn platform_activated(&self, platform: Option<&Platform>, kind: Kind) -> bool {
let platform = match platform {
Some(p) => p,
None => return true,
};
Expand All @@ -111,7 +109,15 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
platform.matches(name, info.cfg())
}

/// Gets the user-specified linker for a particular host or target.
/// Whether a dependency should be compiled for the host or target platform,
/// specified by `Kind`.
pub fn dep_platform_activated(&self, dep: &Dependency, kind: Kind) -> bool {
// If this dependency is only available for certain platforms,
// make sure we're only enabling it for that platform.
self.platform_activated(dep.platform(), kind)
}

/// Gets the user-specified linker for a particular host or target
pub fn linker(&self, kind: Kind) -> Option<&Path> {
self.target_config(kind).linker.as_ref().map(|s| s.as_ref())
}
Expand Down
14 changes: 14 additions & 0 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct TargetInfo {
pub rustflags: Vec<String>,
/// Extra flags to pass to `rustdoc`, see `env_args`.
pub rustdocflags: Vec<String>,
pub supports_pipelining: Option<bool>,
}

/// Kind of each file generated by a Unit, part of `FileType`.
Expand Down Expand Up @@ -98,6 +99,18 @@ impl TargetInfo {
.args(&rustflags)
.env_remove("RUSTC_LOG");

// NOTE: set this unconditionally to `true` once support for `--json`
// rides to stable.
//
// Also note that we only learn about this functionality for the host
// compiler since the host/target rustc are always the same.
let mut pipelining_test = process.clone();
pipelining_test.args(&["--error-format=json", "--json=artifacts"]);
let supports_pipelining = match kind {
Kind::Host => Some(rustc.cached_output(&pipelining_test).is_ok()),
Kind::Target => None,
};

let target_triple = requested_target
.as_ref()
.map(|s| s.as_str())
Expand Down Expand Up @@ -179,6 +192,7 @@ impl TargetInfo {
"RUSTDOCFLAGS",
)?,
cfg,
supports_pipelining,
})
}

Expand Down
8 changes: 5 additions & 3 deletions src/cargo/core/compiler/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
.config
.get_bool("build.pipelining")?
.map(|t| t.val)
.unwrap_or(false);
.unwrap_or(bcx.host_info.supports_pipelining.unwrap());

Ok(Self {
bcx,
Expand Down Expand Up @@ -205,15 +205,17 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
});
}

let feats = self.bcx.resolve.features(unit.pkg.package_id());
let bcx = self.bcx;
let feats = bcx.resolve.features(unit.pkg.package_id());
if !feats.is_empty() {
self.compilation
.cfgs
.entry(unit.pkg.package_id())
.or_insert_with(|| {
feats
.iter()
.map(|feat| format!("feature=\"{}\"", feat))
.filter(|feat| bcx.platform_activated(feat.1.as_ref(), unit.kind))
.map(|feat| format!("feature=\"{}\"", feat.0))
.collect()
});
}
Expand Down
15 changes: 14 additions & 1 deletion src/cargo/core/compiler/context/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ fn compute_deps<'a, 'cfg, 'tmp>(
return false;
}

// If the dependency is optional, then we're only activating it
// if the corresponding feature was activated
if dep.is_optional() {
// Same for features this dependency is referenced
if let Some(platform) = bcx.resolve.features(id).get(&*dep.name_in_toml()) {
if !bcx.platform_activated(platform.as_ref(), unit.kind) {
return false;
}
} else {
return false;
}
}

// If we've gotten past all that, then this dependency is
// actually used!
true
Expand Down Expand Up @@ -228,7 +241,7 @@ fn compute_deps<'a, 'cfg, 'tmp>(
t.is_bin() &&
// Skip binaries with required features that have not been selected.
t.required_features().unwrap_or(&no_required_features).iter().all(|f| {
bcx.resolve.features(id).contains(f)
bcx.resolve.features(id).contains_key(f) && bcx.platform_activated(bcx.resolve.features(id).get(f).unwrap().as_ref(), unit.kind)
})
})
.map(|t| {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
// Be sure to pass along all enabled features for this package, this is the
// last piece of statically known information that we have.
for feat in bcx.resolve.features(unit.pkg.package_id()).iter() {
cmd.env(&format!("CARGO_FEATURE_{}", super::envify(feat)), "1");
cmd.env(&format!("CARGO_FEATURE_{}", super::envify(feat.0)), "1");
}

let mut cfg_map = HashMap::new();
Expand Down
71 changes: 37 additions & 34 deletions src/cargo/core/compiler/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ impl Layout {
///
/// This function will block if the directory is already locked.
pub fn at(config: &Config, root: Filesystem) -> CargoResult<Layout> {
// If the root directory doesn't already exist go ahead and create it
// here. Use this opportunity to exclude it from backups as well if the
// system supports it since this is a freshly created folder.
if !root.as_path_unlocked().exists() {
root.create_dir()?;
exclude_from_backups(root.as_path_unlocked());
}

// For now we don't do any more finer-grained locking on the artifact
// directory, so just lock the entire thing for the duration of this
// compile.
Expand All @@ -127,42 +135,8 @@ impl Layout {
})
}

#[cfg(not(target_os = "macos"))]
fn exclude_from_backups(&self, _: &Path) {}

#[cfg(target_os = "macos")]
/// Marks files or directories as excluded from Time Machine on macOS
///
/// This is recommended to prevent derived/temporary files from bloating backups.
fn exclude_from_backups(&self, path: &Path) {
use core_foundation::base::TCFType;
use core_foundation::{number, string, url};
use std::ptr;

// For compatibility with 10.7 a string is used instead of global kCFURLIsExcludedFromBackupKey
let is_excluded_key: Result<string::CFString, _> = "NSURLIsExcludedFromBackupKey".parse();
let path = url::CFURL::from_path(path, false);
if let (Some(path), Ok(is_excluded_key)) = (path, is_excluded_key) {
unsafe {
url::CFURLSetResourcePropertyForKey(
path.as_concrete_TypeRef(),
is_excluded_key.as_concrete_TypeRef(),
number::kCFBooleanTrue as *const _,
ptr::null_mut(),
);
}
}
// Errors are ignored, since it's an optional feature and failure
// doesn't prevent Cargo from working
}

/// Makes sure all directories stored in the Layout exist on the filesystem.
pub fn prepare(&mut self) -> io::Result<()> {
if fs::metadata(&self.root).is_err() {
fs::create_dir_all(&self.root)?;
self.exclude_from_backups(&self.root);
}

mkdir(&self.deps)?;
mkdir(&self.native)?;
mkdir(&self.incremental)?;
Expand Down Expand Up @@ -209,3 +183,32 @@ impl Layout {
&self.build
}
}

#[cfg(not(target_os = "macos"))]
fn exclude_from_backups(_: &Path) {}

#[cfg(target_os = "macos")]
/// Marks files or directories as excluded from Time Machine on macOS
///
/// This is recommended to prevent derived/temporary files from bloating backups.
fn exclude_from_backups(path: &Path) {
use core_foundation::base::TCFType;
use core_foundation::{number, string, url};
use std::ptr;

// For compatibility with 10.7 a string is used instead of global kCFURLIsExcludedFromBackupKey
let is_excluded_key: Result<string::CFString, _> = "NSURLIsExcludedFromBackupKey".parse();
let path = url::CFURL::from_path(path, false);
if let (Some(path), Ok(is_excluded_key)) = (path, is_excluded_key) {
unsafe {
url::CFURLSetResourcePropertyForKey(
path.as_concrete_TypeRef(),
is_excluded_key.as_concrete_TypeRef(),
number::kCFBooleanTrue as *const _,
ptr::null_mut(),
);
}
}
// Errors are ignored, since it's an optional feature and failure
// doesn't prevent Cargo from working
}
Loading