Skip to content

add ability to optimize dependencies in dev #2380

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
36 changes: 34 additions & 2 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::default::Default;
use std::collections::HashMap;
use std::fmt;
use std::path::{PathBuf, Path};

Expand All @@ -20,7 +21,7 @@ pub struct Manifest {
include: Vec<String>,
metadata: ManifestMetadata,
profiles: Profiles,
publish: bool
publish: bool,
}

/// General metadata about a package which is just blindly uploaded to the
Expand Down Expand Up @@ -100,7 +101,7 @@ impl Encodable for TargetKind {
}
}

#[derive(RustcEncodable, RustcDecodable, Clone, PartialEq, Eq, Debug, Hash)]
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct Profile {
pub opt_level: u32,
pub lto: bool,
Expand All @@ -113,6 +114,12 @@ pub struct Profile {
pub test: bool,
pub doc: bool,
pub run_custom_build: bool,
pub deps_profile: Option<ProfileId>,
}

#[derive(Clone, Hash, PartialEq, Eq, Debug)]
pub struct ProfileId {
name: String
}

#[derive(Default, Clone, Debug)]
Expand Down Expand Up @@ -466,6 +473,7 @@ impl Default for Profile {
test: false,
doc: false,
run_custom_build: false,
deps_profile: None,
}
}
}
Expand All @@ -484,3 +492,27 @@ impl fmt::Display for Profile {

}
}

impl Profiles {
pub fn name_to_id(name: &str) -> Option<ProfileId> {
let all_profiles = ["dev", "release", "test", "bench", "doc"];
if all_profiles.iter().any(|&profile| profile == name) {
Some(ProfileId { name: name.to_owned() })
} else {
None
}
}

pub fn by_id(&self, id: &ProfileId) -> &Profile {
let key: &str = &id.name;
self.all_profiles()[key]
}

fn all_profiles(&self) -> HashMap<&str, &Profile> {
[("dev", &self.dev),
("release", &self.release),
("test", &self.test),
("bench", &self.bench),
("doc", &self.doc)].iter().map(|&x| x).collect()
}
}
2 changes: 1 addition & 1 deletion src/cargo/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub use self::dependency::{Dependency, DependencyInner};
pub use self::manifest::{Manifest, Target, TargetKind, Profile, LibKind, Profiles};
pub use self::manifest::{Manifest, Target, TargetKind, Profile, ProfileId, LibKind, Profiles};
pub use self::package::{Package, PackageSet};
pub use self::package_id::{PackageId, Metadata};
pub use self::package_id_spec::PackageIdSpec;
Expand Down
4 changes: 4 additions & 0 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ impl Package {
pub fn generate_metadata(&self) -> Metadata {
self.package_id().generate_metadata(self.root())
}

pub fn is_local(&self) -> bool {
self.manifest.summary().source_id().is_path()
}
}

impl fmt::Display for Package {
Expand Down
3 changes: 3 additions & 0 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ pub fn compile_pkg<'a>(root_package: &Package,
let mut build_config = try!(scrape_build_config(config, jobs, target));
build_config.exec_engine = exec_engine.clone();
build_config.release = release;
build_config.deps_profile = if release { &profiles.release } else { &profiles.dev }
.deps_profile.clone();

if let CompileMode::Doc { deps } = mode {
build_config.doc_all = deps;
}
Expand Down
19 changes: 10 additions & 9 deletions src/cargo/ops/cargo_rustc/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
Unit {
pkg: pkg,
target: t,
profile: self.lib_profile(id),
profile: self.lib_profile(pkg),
kind: unit.kind.for_target(t),
}
})
Expand Down Expand Up @@ -404,7 +404,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
Unit {
pkg: unit.pkg,
target: t,
profile: self.lib_profile(id),
profile: self.lib_profile(unit.pkg),
kind: unit.kind.for_target(t),
}
}));
Expand Down Expand Up @@ -477,7 +477,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
ret.push(Unit {
pkg: dep,
target: lib,
profile: self.lib_profile(dep.package_id()),
profile: self.lib_profile(dep),
kind: unit.kind.for_target(lib),
});
if self.build_config.doc_all {
Expand Down Expand Up @@ -523,7 +523,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
Unit {
pkg: unit.pkg,
target: t,
profile: self.lib_profile(unit.pkg.package_id()),
profile: self.lib_profile(unit.pkg),
kind: unit.kind.for_target(t),
}
})
Expand Down Expand Up @@ -576,12 +576,13 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
self.build_config.requested_target.as_ref().map(|s| &s[..])
}

pub fn lib_profile(&self, _pkg: &PackageId) -> &'a Profile {
if self.build_config.release {
&self.profiles.release
} else {
&self.profiles.dev
pub fn lib_profile(&self, pkg: &Package) -> &'a Profile {
if !pkg.is_local() {
if let Some(ref id) = self.build_config.deps_profile {
return self.profiles.by_id(id)
}
}
if self.build_config.release { &self.profiles.release } else { &self.profiles.dev }
}

pub fn build_script_profile(&self, _pkg: &PackageId) -> &'a Profile {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_rustc/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>)
// environment variables. Note that the profile-related environment
// variables are not set with this the build script's profile but rather the
// package's library profile.
let profile = cx.lib_profile(unit.pkg.package_id());
let profile = cx.lib_profile(unit.pkg);
let to_exec = to_exec.into_os_string();
let mut p = try!(super::process(CommandType::Host(to_exec), unit.pkg, cx));
p.env("OUT_DIR", &build_output)
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/ops/cargo_rustc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::{self, PathBuf};
use std::sync::Arc;

use core::{SourceMap, Package, PackageId, PackageSet, Target, Resolve};
use core::{Profile, Profiles};
use core::{Profile, ProfileId, Profiles};
use util::{self, CargoResult, human};
use util::{Config, internal, ChainError, profile, join_paths};

Expand Down Expand Up @@ -42,6 +42,7 @@ pub struct BuildConfig {
pub exec_engine: Option<Arc<Box<ExecEngine>>>,
pub release: bool,
pub doc_all: bool,
pub deps_profile: Option<ProfileId>,
}

#[derive(Clone, Default)]
Expand Down Expand Up @@ -448,7 +449,7 @@ fn build_base_args(cx: &Context,
let Profile {
opt_level, lto, codegen_units, ref rustc_args, debuginfo,
debug_assertions, rpath, test, doc: _doc, run_custom_build,
rustdoc_args: _,
rustdoc_args: _, deps_profile: _,
} = *unit.profile;
assert!(!run_custom_build);

Expand Down
52 changes: 32 additions & 20 deletions 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>,
dependencies_profile: Option<String>,
}

#[derive(RustcDecodable)]
Expand Down Expand Up @@ -562,7 +563,7 @@ impl TomlManifest {
repository: project.repository.clone(),
keywords: project.keywords.clone().unwrap_or(Vec::new()),
};
let profiles = build_profiles(&self.profile);
let profiles = try!(build_profiles(&self.profile));
let publish = project.publish.unwrap_or(true);
let mut manifest = Manifest::new(summary,
targets,
Expand Down Expand Up @@ -957,30 +958,40 @@ fn normalize(lib: &Option<TomlLibTarget>,
ret
}

fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {
fn build_profiles(profiles: &Option<TomlProfiles>) -> CargoResult<Profiles> {
let profiles = profiles.as_ref();
return Profiles {
release: merge(Profile::default_release(),
profiles.and_then(|p| p.release.as_ref())),
dev: merge(Profile::default_dev(),
profiles.and_then(|p| p.dev.as_ref())),
test: merge(Profile::default_test(),
profiles.and_then(|p| p.test.as_ref())),
bench: merge(Profile::default_bench(),
profiles.and_then(|p| p.bench.as_ref())),
doc: merge(Profile::default_doc(),
profiles.and_then(|p| p.doc.as_ref())),
return Ok(Profiles {
release: try!(merge(Profile::default_release(),
profiles.and_then(|p| p.release.as_ref()))),
dev: try!(merge(Profile::default_dev(),
profiles.and_then(|p| p.dev.as_ref()))),
test: try!(merge(Profile::default_test(),
profiles.and_then(|p| p.test.as_ref()))),
bench: try!(merge(Profile::default_bench(),
profiles.and_then(|p| p.bench.as_ref()))),
doc: try!(merge(Profile::default_doc(),
profiles.and_then(|p| p.doc.as_ref()))),
custom_build: Profile::default_custom_build(),
};
});

fn merge(profile: Profile, toml: Option<&TomlProfile>) -> Profile {
fn merge(profile: Profile, toml: Option<&TomlProfile>) -> CargoResult<Profile> {
let &TomlProfile {
opt_level, lto, codegen_units, debug, debug_assertions, rpath
opt_level, lto, codegen_units, debug,
debug_assertions, rpath, ref dependencies_profile
} = match toml {
Some(toml) => toml,
None => return profile,
None => return Ok(profile),
};
let deps_profile = match *dependencies_profile {
None => None,
Some(ref name) => if let Some(id) = Profiles::name_to_id(name) {
Some(id)
} else {
bail!("no such profile: {}", name)
}
};
Profile {

Ok(Profile {
opt_level: opt_level.unwrap_or(profile.opt_level),
lto: lto.unwrap_or(profile.lto),
codegen_units: codegen_units,
Expand All @@ -992,6 +1003,7 @@ fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {
test: profile.test,
doc: profile.doc,
run_custom_build: profile.run_custom_build,
}
}
deps_profile: deps_profile,
})
};
}
Loading