Skip to content

[WIP] Implement binary-only dependencies #3870

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

Closed
wants to merge 3 commits into from
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
17 changes: 10 additions & 7 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl ser::Serialize for Dependency {
name: self.name(),
source: &self.source_id(),
req: self.version_req().to_string(),
kind: self.kind(),
kind: self.kind().clone(),
optional: self.is_optional(),
uses_default_features: self.uses_default_features(),
features: self.features(),
Expand All @@ -71,11 +71,13 @@ impl ser::Serialize for Dependency {
}
}

#[derive(PartialEq, Clone, Debug, Copy)]
#[derive(PartialEq, Clone, Debug)]
pub enum Kind {
Normal,
Development,
Build,
/// Binary-only dependency for a named binary target.
Bin(String),
}

impl ser::Serialize for Kind {
Expand All @@ -84,8 +86,9 @@ impl ser::Serialize for Kind {
{
match *self {
Kind::Normal => None,
Kind::Development => Some("dev"),
Kind::Build => Some("build"),
Kind::Development => Some("dev".to_string()),
Kind::Build => Some("build".to_string()),
Kind::Bin(ref name) => Some(format!("bin:{}", name)),
}.serialize(s)
}
}
Expand Down Expand Up @@ -170,7 +173,7 @@ this warning.
pub fn version_req(&self) -> &VersionReq { &self.req }
pub fn name(&self) -> &str { &self.name }
pub fn source_id(&self) -> &SourceId { &self.source_id }
pub fn kind(&self) -> Kind { self.kind }
pub fn kind(&self) -> &Kind { &self.kind }
pub fn specified_req(&self) -> bool { self.specified_req }

/// If none, this dependency must be built for all platforms.
Expand Down Expand Up @@ -231,7 +234,7 @@ this warning.
/// Returns false if the dependency is only used to build the local package.
pub fn is_transitive(&self) -> bool {
match self.kind {
Kind::Normal | Kind::Build => true,
Kind::Normal | Kind::Build | Kind::Bin(_) => true,
Kind::Development => false,
}
}
Expand Down Expand Up @@ -292,7 +295,7 @@ impl Dependency {
pub fn version_req(&self) -> &VersionReq { self.inner.version_req() }
pub fn name(&self) -> &str { self.inner.name() }
pub fn source_id(&self) -> &SourceId { self.inner.source_id() }
pub fn kind(&self) -> Kind { self.inner.kind() }
pub fn kind(&self) -> &Kind { self.inner.kind() }
pub fn specified_req(&self) -> bool { self.inner.specified_req() }

/// If none, this dependencies must be built for all platforms.
Expand Down
15 changes: 13 additions & 2 deletions src/cargo/ops/cargo_rustc/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,14 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
return false;
}

// If it's a binary-only dependency, make sure we're building the
// binary it applies to.
if let DepKind::Bin(ref name) = *d.kind() {
if unit.target.name() != name {
return false;
}
}

// If we've gotten past all that, then this dependency is
// actually used!
true
Expand Down Expand Up @@ -712,10 +720,13 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
unit.pkg.dependencies().iter().filter(|d| {
d.name() == dep.name()
}).any(|dep| {
match dep.kind() {
match *dep.kind() {
DepKind::Normal => self.dep_platform_activated(dep,
unit.kind),
_ => false,
DepKind::Bin(ref name) => {
unit.target.is_bin() && unit.target.name() == name
}
DepKind::Build | DepKind::Development => false,
}
})
}).map(|dep| {
Expand Down
13 changes: 8 additions & 5 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,14 @@ fn transmit(config: &Config,
features: dep.features().to_vec(),
version_req: dep.version_req().to_string(),
target: dep.platform().map(|s| s.to_string()),
kind: match dep.kind() {
Kind::Normal => "normal",
Kind::Build => "build",
Kind::Development => "dev",
}.to_string(),
kind: match *dep.kind() {
Kind::Normal => "normal".to_string(),
Kind::Build => "build".to_string(),
// Binary-only deps are, at least for the purpose of crates.io display, equivalent
// to dev dependencies.
Kind::Development
| Kind::Bin(_) => "dev".to_string(),
},
}
}).collect::<Vec<NewCrateDependency>>();
let manifest = pkg.manifest();
Expand Down
96 changes: 68 additions & 28 deletions src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::de::{self, Deserialize};
use serde_ignored;

use core::{SourceId, Profiles, PackageIdSpec, GitReference, WorkspaceConfig};
use core::{Summary, Manifest, Target, Dependency, DependencyInner, PackageId};
use core::{Summary, Manifest, Target, TargetKind, Dependency, DependencyInner, PackageId};
use core::{EitherManifest, VirtualManifest};
use core::dependency::{Kind, Platform};
use core::manifest::{LibKind, Profile, ManifestMetadata};
Expand Down Expand Up @@ -191,6 +191,7 @@ type TomlExampleTarget = TomlTarget;
type TomlTestTarget = TomlTarget;
type TomlBenchTarget = TomlTarget;

#[derive(Debug, Clone)]
pub enum TomlDependency {
Simple(String),
Detailed(DetailedTomlDependency)
Expand Down Expand Up @@ -228,7 +229,7 @@ impl de::Deserialize for TomlDependency {
}
}

#[derive(Deserialize, Clone, Default)]
#[derive(Debug, Deserialize, Clone, Default)]
pub struct DetailedTomlDependency {
version: Option<String>,
path: Option<String>,
Expand Down Expand Up @@ -674,13 +675,13 @@ impl TomlManifest {
let new_build = self.maybe_custom_build(&project.build, &layout.root);

// Get targets
let targets = normalize(&layout.root,
&lib,
&bins,
new_build,
&examples,
&tests,
&benches);
let (targets, target_deps) = normalize(&layout.root,
&lib,
&bins,
new_build,
&examples,
&tests,
&benches);

if targets.is_empty() {
debug!("manifest has no build targets");
Expand Down Expand Up @@ -718,7 +719,7 @@ impl TomlManifest {
None => return Ok(())
};
for (n, v) in dependencies.iter() {
let dep = v.to_dependency(n, cx, kind)?;
let dep = v.to_dependency(n, cx, kind.clone())?;
cx.deps.push(dep);
}

Expand Down Expand Up @@ -747,6 +748,26 @@ impl TomlManifest {
process_dependencies(&mut cx, dev_deps, Some(Kind::Development))?;
}

for (index, (target, deps)) in targets.iter().zip(target_deps.iter()).enumerate() {
if deps.is_some() {
debug!("adding target-specific deps for '{}' (#{})",
target.name(),
index);

match *target.kind() {
TargetKind::Bin => {
process_dependencies(&mut cx,
deps.as_ref(),
Some(Kind::Bin(target.name().to_string())))?;
},
_ => {
bail!("Target-specific dependencies are only supported for binary \
targets.");
}
}
}
}

replace = self.replace(&mut cx)?;
}

Expand Down Expand Up @@ -1093,6 +1114,7 @@ struct TomlTarget {
harness: Option<bool>,
#[serde(rename = "required-features")]
required_features: Option<Vec<String>>,
dependencies: Option<HashMap<String, TomlDependency>>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -1228,13 +1250,15 @@ impl fmt::Debug for PathValue {
}
}

fn normalize(package_root: &Path,
lib: &Option<TomlLibTarget>,
bins: &[TomlBinTarget],
custom_build: Option<PathBuf>,
examples: &[TomlExampleTarget],
tests: &[TomlTestTarget],
benches: &[TomlBenchTarget]) -> Vec<Target> {
fn normalize(
package_root: &Path,
lib: &Option<TomlLibTarget>,
bins: &[TomlBinTarget],
custom_build: Option<PathBuf>,
examples: &[TomlExampleTarget],
tests: &[TomlTestTarget],
benches: &[TomlBenchTarget]
) -> (Vec<Target>, Vec<Option<HashMap<String, TomlDependency>>>) {
fn configure(toml: &TomlTarget, target: &mut Target) {
let t2 = target.clone();
target.set_tested(toml.test.unwrap_or(t2.tested()))
Expand All @@ -1249,7 +1273,9 @@ fn normalize(package_root: &Path,
});
}

let lib_target = |dst: &mut Vec<Target>, l: &TomlLibTarget| {
let lib_target = |dst: &mut Vec<Target>,
dst_deps: &mut Vec<_>,
l: &TomlLibTarget| {
let path = l.path.clone().unwrap_or_else(
|| PathValue(Path::new("src").join(&format!("{}.rs", l.name())))
);
Expand All @@ -1267,9 +1293,12 @@ fn normalize(package_root: &Path,
package_root.join(&path.0));
configure(l, &mut target);
dst.push(target);
dst_deps.push(l.dependencies.clone());
};

let bin_targets = |dst: &mut Vec<Target>, bins: &[TomlBinTarget],
let bin_targets = |dst: &mut Vec<Target>,
dst_deps: &mut Vec<_>,
bins: &[TomlBinTarget],
default: &mut FnMut(&TomlBinTarget) -> PathBuf| {
for bin in bins.iter() {
let path = bin.path.clone().unwrap_or_else(|| {
Expand All @@ -1289,6 +1318,7 @@ fn normalize(package_root: &Path,
bin.required_features.clone());
configure(bin, &mut target);
dst.push(target);
dst_deps.push(bin.dependencies.clone());
}
};

Expand All @@ -1300,6 +1330,7 @@ fn normalize(package_root: &Path,
};

let example_targets = |dst: &mut Vec<Target>,
dst_deps: &mut Vec<_>,
examples: &[TomlExampleTarget],
default: &mut FnMut(&TomlExampleTarget) -> PathBuf| {
for ex in examples.iter() {
Expand All @@ -1321,10 +1352,12 @@ fn normalize(package_root: &Path,
);
configure(ex, &mut target);
dst.push(target);
dst_deps.push(ex.dependencies.clone());
}
};

let test_targets = |dst: &mut Vec<Target>,
let test_targets = |dst: &mut Vec<_>,
dst_deps: &mut Vec<_>,
tests: &[TomlTestTarget],
default: &mut FnMut(&TomlTestTarget) -> PathBuf| {
for test in tests.iter() {
Expand All @@ -1336,10 +1369,12 @@ fn normalize(package_root: &Path,
test.required_features.clone());
configure(test, &mut target);
dst.push(target);
dst_deps.push(test.dependencies.clone());
}
};

let bench_targets = |dst: &mut Vec<Target>,
let bench_targets = |dst: &mut Vec<_>,
dst_deps: &mut Vec<_>,
benches: &[TomlBenchTarget],
default: &mut FnMut(&TomlBenchTarget) -> PathBuf| {
for bench in benches.iter() {
Expand All @@ -1351,47 +1386,52 @@ fn normalize(package_root: &Path,
bench.required_features.clone());
configure(bench, &mut target);
dst.push(target);
dst_deps.push(bench.dependencies.clone());
}
};

let mut ret = Vec::new();
let mut ret_deps = Vec::new();

if let Some(ref lib) = *lib {
lib_target(&mut ret, lib);
bin_targets(&mut ret, bins,
lib_target(&mut ret, &mut ret_deps, lib);
bin_targets(&mut ret, &mut ret_deps, bins,
&mut |bin| Path::new("src").join("bin")
.join(&format!("{}.rs", bin.name())));
} else if bins.len() > 0 {
bin_targets(&mut ret, bins,
bin_targets(&mut ret, &mut ret_deps, bins,
&mut |bin| Path::new("src")
.join(&format!("{}.rs", bin.name())));
}

if let Some(custom_build) = custom_build {
custom_build_target(&mut ret, &custom_build);
ret_deps.push(None); // Can't have build-script-specific dependencies
}

example_targets(&mut ret, examples,
example_targets(&mut ret, &mut ret_deps, examples,
&mut |ex| Path::new("examples")
.join(&format!("{}.rs", ex.name())));

test_targets(&mut ret, tests, &mut |test| {
test_targets(&mut ret, &mut ret_deps, tests, &mut |test| {
if test.name() == "test" {
Path::new("src").join("test.rs")
} else {
Path::new("tests").join(&format!("{}.rs", test.name()))
}
});

bench_targets(&mut ret, benches, &mut |bench| {
bench_targets(&mut ret, &mut ret_deps, benches, &mut |bench| {
if bench.name() == "bench" {
Path::new("src").join("bench.rs")
} else {
Path::new("benches").join(&format!("{}.rs", bench.name()))
}
});

ret
assert_eq!(ret.len(), ret_deps.len());

(ret, ret_deps)
}

fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {
Expand Down
Loading