Skip to content

Commit

Permalink
named-profiles: formatting fixes from 'cargo fmt'
Browse files Browse the repository at this point in the history
  • Loading branch information
da-x committed Jun 20, 2019
1 parent 2f81adb commit 29b7e90
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 102 deletions.
16 changes: 11 additions & 5 deletions src/bin/cargo/commands/bench.rs
Expand Up @@ -73,12 +73,18 @@ Compilation can be customized with the `bench` profile in the manifest.

pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;
let mut compile_opts = args.compile_options(config, CompileMode::Bench, Some(&ws),
ProfileChecking::Checked)?;
let mut compile_opts = args.compile_options(
config,
CompileMode::Bench,
Some(&ws),
ProfileChecking::Checked,
)?;

compile_opts.build_config.profile_kind =
args.get_profile_kind(config, ProfileKind::Custom("bench".to_owned()),
ProfileChecking::Checked)?;
compile_opts.build_config.profile_kind = args.get_profile_kind(
config,
ProfileKind::Custom("bench".to_owned()),
ProfileChecking::Checked,
)?;

let ops = TestOptions {
no_run: args.is_present("no-run"),
Expand Down
8 changes: 6 additions & 2 deletions src/bin/cargo/commands/build.rs
Expand Up @@ -56,8 +56,12 @@ the --release flag will use the `release` profile instead.

pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;
let mut compile_opts = args.compile_options(config, CompileMode::Build, Some(&ws),
ProfileChecking::Checked)?;
let mut compile_opts = args.compile_options(
config,
CompileMode::Build,
Some(&ws),
ProfileChecking::Checked,
)?;

compile_opts.export_dir = args.value_of_path("out-dir", config);
if compile_opts.export_dir.is_some() {
Expand Down
3 changes: 2 additions & 1 deletion src/bin/cargo/commands/clippy.rs
Expand Up @@ -61,7 +61,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;

let mode = CompileMode::Check { test: false };
let mut compile_opts = args.compile_options(config, mode, Some(&ws), ProfileChecking::Checked)?;
let mut compile_opts =
args.compile_options(config, mode, Some(&ws), ProfileChecking::Checked)?;

if !config.cli_unstable().unstable_options {
return Err(failure::format_err!(
Expand Down
3 changes: 2 additions & 1 deletion src/bin/cargo/commands/doc.rs
Expand Up @@ -52,7 +52,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let mode = CompileMode::Doc {
deps: !args.is_present("no-deps"),
};
let mut compile_opts = args.compile_options(config, mode, Some(&ws), ProfileChecking::Checked)?;
let mut compile_opts =
args.compile_options(config, mode, Some(&ws), ProfileChecking::Checked)?;
compile_opts.local_rustdoc_args = if args.is_present("document-private-items") {
Some(vec!["--document-private-items".to_string()])
} else {
Expand Down
14 changes: 9 additions & 5 deletions src/bin/cargo/commands/install.rs
Expand Up @@ -116,11 +116,15 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
}

let workspace = args.workspace(config).ok();
let mut compile_opts = args.compile_options(config, CompileMode::Build, workspace.as_ref(),
ProfileChecking::Checked)?;

compile_opts.build_config.profile_kind = args.get_profile_kind(config, ProfileKind::Release,
ProfileChecking::Checked)?;
let mut compile_opts = args.compile_options(
config,
CompileMode::Build,
workspace.as_ref(),
ProfileChecking::Checked,
)?;

compile_opts.build_config.profile_kind =
args.get_profile_kind(config, ProfileKind::Release, ProfileChecking::Checked)?;

let krates = args
.values_of("crate")
Expand Down
8 changes: 6 additions & 2 deletions src/bin/cargo/commands/run.rs
Expand Up @@ -41,8 +41,12 @@ run. If you're passing arguments to both Cargo and the binary, the ones after
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;

let mut compile_opts = args.compile_options(config, CompileMode::Build, Some(&ws),
ProfileChecking::Checked)?;
let mut compile_opts = args.compile_options(
config,
CompileMode::Build,
Some(&ws),
ProfileChecking::Checked,
)?;

if !args.is_present("example") && !args.is_present("bin") {
let default_runs: Vec<_> = compile_opts
Expand Down
6 changes: 5 additions & 1 deletion src/bin/cargo/commands/rustc.rs
Expand Up @@ -64,7 +64,11 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
}
};
let mut compile_opts = args.compile_options_for_single_package(
config, mode, Some(&ws), ProfileChecking::Unchecked)?;
config,
mode,
Some(&ws),
ProfileChecking::Unchecked,
)?;
let target_args = values(args, "args");
compile_opts.target_rustc_args = if target_args.is_empty() {
None
Expand Down
18 changes: 12 additions & 6 deletions src/bin/cargo/commands/test.rs
Expand Up @@ -100,12 +100,18 @@ To get the list of all options available for the test binaries use this:
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;

let mut compile_opts = args.compile_options(config, CompileMode::Test, Some(&ws),
ProfileChecking::Checked)?;

compile_opts.build_config.profile_kind =
args.get_profile_kind(config, ProfileKind::Custom("test".to_owned()),
ProfileChecking::Checked)?;
let mut compile_opts = args.compile_options(
config,
CompileMode::Test,
Some(&ws),
ProfileChecking::Checked,
)?;

compile_opts.build_config.profile_kind = args.get_profile_kind(
config,
ProfileKind::Custom("test".to_owned()),
ProfileChecking::Checked,
)?;

// `TESTNAME` is actually an argument of the test binary, but it's
// important, so we explicitly mention it and reconfigure.
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/job_queue.rs
Expand Up @@ -15,8 +15,8 @@ use super::job::{
Job,
};
use super::{BuildContext, BuildPlan, CompileMode, Context, Unit};
use crate::core::compiler::ProfileKind;
use crate::core::{PackageId, TargetKind};
use crate::core::compiler::{ProfileKind};
use crate::handle_error;
use crate::util;
use crate::util::diagnostic_server::{self, DiagnosticPrinter};
Expand Down
129 changes: 70 additions & 59 deletions src/cargo/core/profiles.rs
@@ -1,12 +1,12 @@
use std::collections::HashSet;
use std::collections::BTreeMap;
use std::collections::HashSet;
use std::{cmp, env, fmt, hash};

use serde::Deserialize;

use crate::core::compiler::{ProfileKind, CompileMode};
use crate::core::compiler::{CompileMode, ProfileKind};
use crate::core::interning::InternedString;
use crate::core::{Features, Feature, PackageId, PackageIdSpec, PackageSet, Shell};
use crate::core::{Feature, Features, PackageId, PackageIdSpec, PackageSet, Shell};
use crate::util::errors::CargoResultExt;
use crate::util::lev_distance::lev_distance;
use crate::util::toml::{ProfilePackageSpec, StringOrBool, TomlProfile, TomlProfiles, U32OrBool};
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Profiles {
features.require(Feature::named_profiles())?;
break;
}
},
}
_ => {
features.require(Feature::named_profiles())?;
break;
Expand All @@ -75,13 +75,15 @@ impl Profiles {
use std::collections::btree_map::Entry;
for (predef_name, mut predef_prof) in Self::predefined_profiles().into_iter() {
match profiles.entry(predef_name.to_owned()) {
Entry::Vacant(vac) => { vac.insert(predef_prof); },
Entry::Vacant(vac) => {
vac.insert(predef_prof);
}
Entry::Occupied(mut oc) => {
// Override predefined with the user-provided Toml.
let r = oc.get_mut();
predef_prof.merge(r);
*r = predef_prof;
},
}
}
}

Expand All @@ -99,53 +101,68 @@ impl Profiles {
dir_names
}

fn add_root_profiles(profile_makers: &mut Profiles,
profiles: Option<&TomlProfiles>, config_profiles: &ConfigProfiles)
{
fn add_root_profiles(
profile_makers: &mut Profiles,
profiles: Option<&TomlProfiles>,
config_profiles: &ConfigProfiles,
) {
let profile_name = "dev";
profile_makers.by_name.insert(profile_name.to_owned(), ProfileMaker {
default: Profile::default_dev(),
toml: profiles.and_then(|p| p.get(profile_name).cloned()),
config: config_profiles.dev.clone(),
inherits: vec![],
});
profile_makers.by_name.insert(
profile_name.to_owned(),
ProfileMaker {
default: Profile::default_dev(),
toml: profiles.and_then(|p| p.get(profile_name).cloned()),
config: config_profiles.dev.clone(),
inherits: vec![],
},
);

let profile_name = "release";
profile_makers.by_name.insert(profile_name.to_owned(), ProfileMaker {
default: Profile::default_release(),
toml: profiles.and_then(|p| p.get(profile_name).cloned()),
config: config_profiles.release.clone(),
inherits: vec![],
});
profile_makers.by_name.insert(
profile_name.to_owned(),
ProfileMaker {
default: Profile::default_release(),
toml: profiles.and_then(|p| p.get(profile_name).cloned()),
config: config_profiles.release.clone(),
inherits: vec![],
},
);
}

fn predefined_profiles() -> Vec<(&'static str, TomlProfile)> {
vec![
("bench", TomlProfile {
inherits: Some(String::from("release")),
..TomlProfile::default()
}),
("test", TomlProfile {
inherits: Some(String::from("dev")),
..TomlProfile::default()
}),
("check", TomlProfile {
inherits: Some(String::from("dev")),
..TomlProfile::default()
}),
(
"bench",
TomlProfile {
inherits: Some(String::from("release")),
..TomlProfile::default()
},
),
(
"test",
TomlProfile {
inherits: Some(String::from("dev")),
..TomlProfile::default()
},
),
(
"check",
TomlProfile {
inherits: Some(String::from("dev")),
..TomlProfile::default()
},
),
]
}

fn process_customs(&mut self, profiles: &BTreeMap<String, TomlProfile>)
-> CargoResult<()>
{
fn process_customs(&mut self, profiles: &BTreeMap<String, TomlProfile>) -> CargoResult<()> {
for (name, profile) in profiles {
let mut set = HashSet::new();
let mut result = Vec::new();

set.insert(name.as_str().to_owned());
match &profile.dir_name {
None => {},
None => {}
Some(dir_name) => {
self.dir_names.insert(name.clone(), dir_name.to_owned());
}
Expand All @@ -157,9 +174,7 @@ impl Profiles {
_ => {}
};

let mut maker = self.process_chain(name,
&profile, &mut set,
&mut result, profiles)?;
let mut maker = self.process_chain(name, &profile, &mut set, &mut result, profiles)?;
result.reverse();
maker.inherits = result;

Expand All @@ -169,17 +184,17 @@ impl Profiles {
Ok(())
}

fn process_chain(&mut self,
name: &String,
profile: &TomlProfile,
set: &mut HashSet<String>,
result: &mut Vec<TomlProfile>,
profiles: &BTreeMap<String, TomlProfile>)
-> CargoResult<ProfileMaker>
{
fn process_chain(
&mut self,
name: &String,
profile: &TomlProfile,
set: &mut HashSet<String>,
result: &mut Vec<TomlProfile>,
profiles: &BTreeMap<String, TomlProfile>,
) -> CargoResult<ProfileMaker> {
result.push(profile.clone());
match profile.inherits.as_ref().map(|x| x.as_str()) {
Some(name@"dev") | Some(name@"release") => {
Some(name @ "dev") | Some(name @ "release") => {
// These are the root profiles
return Ok(self.by_name.get(name).unwrap().clone());
}
Expand All @@ -194,22 +209,20 @@ impl Profiles {
None => {
failure::bail!("Profile {} not found in Cargo.toml", name);
}
Some(parent) => {
self.process_chain(&name, parent, set, result, profiles)
}
Some(parent) => self.process_chain(&name, parent, set, result, profiles),
}
}
None => {
failure::bail!(
"An 'inherits' directive is needed for all \
profiles that are not 'dev' or 'release. Here \
it is missing from {}",
name);
profiles that are not 'dev' or 'release. Here \
it is missing from {}",
name
);
}
}
}


/// Retrieves the profile for a target.
/// `is_member` is whether or not this package is a member of the
/// workspace.
Expand Down Expand Up @@ -267,9 +280,7 @@ impl Profiles {
pub fn base_profile(&self, profile_kind: &ProfileKind) -> CargoResult<Profile> {
match self.by_name.get(profile_kind.name()) {
None => failure::bail!("Profile {} undefined", profile_kind.name()),
Some(r) => {
Ok(r.get_profile(None, true, UnitFor::new_normal()))
}
Some(r) => Ok(r.get_profile(None, true, UnitFor::new_normal())),
}
}

Expand Down

0 comments on commit 29b7e90

Please sign in to comment.