Skip to content

Support for custom Cargo profiles, #2 #6676

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 1 commit 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
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ 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))?;

compile_opts.build_config.release = true;
compile_opts.build_config.build_profile = BuildProfile::Release;

let ops = TestOptions {
no_run: args.is_present("no-run"),
Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub fn cli() -> App {
"Build all targets",
)
.arg_release("Build artifacts in release mode, with optimizations")
.arg_profile("Build artifacts with the specified custom profile")
.arg_features()
.arg_target_triple("Build for the target triple")
.arg_target_dir()
Expand Down
6 changes: 5 additions & 1 deletion src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ 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())?;

compile_opts.build_config.release = !args.is_present("debug");
compile_opts.build_config.build_profile = if !args._is_present("debug") {
BuildProfile::Release
} else {
BuildProfile::Dev
};

let krates = args
.values_of("crate")
Expand Down
23 changes: 20 additions & 3 deletions src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,32 @@ use serde::ser;

use crate::util::{CargoResult, CargoResultExt, Config, RustfixDiagnosticServer};

#[derive(Debug, Clone)]
pub enum BuildProfile {
Dev,
Release,
Custom(String),
}

impl BuildProfile {
pub fn dest(&self) -> &str {
match self {
BuildProfile::Dev => "debug",
BuildProfile::Release => "release",
BuildProfile::Custom(name) => &name,
}
}
}

/// Configuration information for a rustc build.
#[derive(Debug)]
pub struct BuildConfig {
/// The target arch triple, defaults to host arch
pub requested_target: Option<String>,
/// How many rustc jobs to run in parallel
pub jobs: u32,
/// Whether we are building for release
pub release: bool,
/// Custom profile
pub build_profile: BuildProfile,
/// In what mode we are compiling
pub mode: CompileMode,
/// Whether to print std output in json format (for machine reading)
Expand Down Expand Up @@ -82,7 +99,7 @@ impl BuildConfig {
Ok(BuildConfig {
requested_target: target,
jobs,
release: false,
build_profile: BuildProfile::Dev,
mode,
message_format: MessageFormat::Human,
force_rebuild: false,
Expand Down
6 changes: 1 addition & 5 deletions src/cargo/core/compiler/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
export_dir: Option<PathBuf>,
units: &[Unit<'a>],
) -> CargoResult<()> {
let dest = if self.bcx.build_config.release {
"release"
} else {
"debug"
};
let dest = self.bcx.build_config.build_profile.dest();
let host_layout = Layout::new(self.bcx.ws, None, dest)?;
let target_layout = match self.bcx.build_config.requested_target.as_ref() {
Some(target) => Some(Layout::new(self.bcx.ws, Some(target), dest)?),
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/context/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ fn new_unit<'a>(
bcx.ws.is_member(pkg),
unit_for,
mode,
bcx.build_config.release,
bcx.build_config.build_profile.clone(),
);
Unit {
pkg,
Expand Down
9 changes: 1 addition & 8 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,7 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
)
.env("DEBUG", debug.to_string())
.env("OPT_LEVEL", &unit.profile.opt_level.to_string())
.env(
"PROFILE",
if bcx.build_config.release {
"release"
} else {
"debug"
},
)
.env("PROFILE", bcx.build_config.build_profile.dest())
.env("HOST", &bcx.host_triple())
.env("RUSTC", &bcx.rustc.path)
.env("RUSTDOC", &*bcx.config.rustdoc()?)
Expand Down
9 changes: 5 additions & 4 deletions src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crossbeam_utils::thread::Scope;
use jobserver::{Acquired, HelperThread};
use log::{debug, info, trace};

use crate::core::compiler::{BuildProfile};
use crate::core::profiles::Profile;
use crate::core::{PackageId, Target, TargetKind};
use crate::handle_error;
Expand Down Expand Up @@ -38,7 +39,7 @@ pub struct JobQueue<'a, 'cfg> {
compiled: HashSet<PackageId>,
documented: HashSet<PackageId>,
counts: HashMap<PackageId, usize>,
is_release: bool,
build_profile: BuildProfile,
progress: Progress<'cfg>,
}

Expand Down Expand Up @@ -145,8 +146,8 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
compiled: HashSet::new(),
documented: HashSet::new(),
counts: HashMap::new(),
is_release: bcx.build_config.release,
progress,
build_profile: bcx.build_config.build_profile.clone(),
}
}

Expand Down Expand Up @@ -354,15 +355,15 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
}
self.progress.clear();

let build_type = if self.is_release { "release" } else { "dev" };
let build_type = self.build_profile.dest();
// NOTE: This may be a bit inaccurate, since this may not display the
// profile for what was actually built. Profile overrides can change
// these settings, and in some cases different targets are built with
// different profiles. To be accurate, it would need to collect a
// list of Units built, and maybe display a list of the different
// profiles used. However, to keep it simple and compatible with old
// behavior, we just display what the base profile is.
let profile = cx.bcx.profiles.base_profile(self.is_release);
let profile = cx.bcx.profiles.base_profile(&self.build_profile);
let mut opt_type = String::from(if profile.opt_level.as_str() == "0" {
"unoptimized"
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use self::job_queue::JobQueue;

use self::output_depinfo::output_depinfo;

pub use self::build_config::{BuildConfig, CompileMode, MessageFormat};
pub use self::build_config::{BuildConfig, CompileMode, MessageFormat, BuildProfile};
pub use self::build_context::{BuildContext, FileFlavor, TargetConfig, TargetInfo};
pub use self::compilation::{Compilation, Doctest};
pub use self::context::{Context, Unit};
Expand Down
138 changes: 121 additions & 17 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::collections::HashSet;
use std::collections::BTreeMap;
use std::{cmp, fmt, hash};

use serde::Deserialize;

use crate::core::compiler::CompileMode;
use crate::core::compiler::{BuildProfile, CompileMode};
use crate::core::interning::InternedString;
use crate::core::{Features, PackageId, PackageIdSpec, PackageSet, Shell};
use crate::util::errors::CargoResultExt;
Expand All @@ -19,6 +20,7 @@ pub struct Profiles {
test: ProfileMaker,
bench: ProfileMaker,
doc: ProfileMaker,
custom: BTreeMap<String, ProfileMaker>,
}

impl Profiles {
Expand All @@ -35,35 +37,110 @@ impl Profiles {
let config_profiles = config.profiles()?;
config_profiles.validate(features, warnings)?;

Ok(Profiles {
let mut profile_makers = Profiles {
dev: ProfileMaker {
default: Profile::default_dev(),
toml: profiles.and_then(|p| p.dev.clone()),
config: config_profiles.dev.clone(),
inherits: vec![],
},
release: ProfileMaker {
default: Profile::default_release(),
toml: profiles.and_then(|p| p.release.clone()),
inherits: vec![],
config: config_profiles.release.clone(),
},
test: ProfileMaker {
default: Profile::default_test(),
toml: profiles.and_then(|p| p.test.clone()),
config: None,
inherits: vec![],
},
bench: ProfileMaker {
default: Profile::default_bench(),
toml: profiles.and_then(|p| p.bench.clone()),
config: None,
inherits: vec![],
},
doc: ProfileMaker {
default: Profile::default_doc(),
toml: profiles.and_then(|p| p.doc.clone()),
config: None,
inherits: vec![],
},
})
custom: BTreeMap::new(),
};

if let Some(profiles) = profiles {
match &profiles.custom {
None => {},
Some(customs) => {
profile_makers.process_customs(customs)?;
}
}
}

Ok(profile_makers)
}

pub 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());

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

self.custom.insert(name.as_str().to_owned(), maker);
}

Ok(())
}

fn process_chain_custom(&mut self,
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("release") => {
return Ok(self.release.clone());
}
Some("dev") => {
return Ok(self.dev.clone());
}
Some(custom_name) => {
let custom_name = custom_name.to_owned();
if set.get(&custom_name).is_some() {
return Err(failure::format_err!("Inheritance loop of custom profiles cycles with {}", custom_name));
}

set.insert(custom_name.clone());
match profiles.get(&custom_name) {
None => {
return Err(failure::format_err!("Custom profile {} not found in Cargo.toml", custom_name));
}
Some(parent) => {
self.process_chain_custom(parent, set, result, profiles)
}
}
}
None => {
Err(failure::format_err!("An 'inherits' directive is needed for all custom profiles"))
}
}
}


/// Retrieve the profile for a target.
/// `is_member` is whether or not this package is a member of the
/// workspace.
Expand All @@ -73,14 +150,20 @@ impl Profiles {
is_member: bool,
unit_for: UnitFor,
mode: CompileMode,
release: bool,
build_profile: BuildProfile,
) -> Profile {
let maker = match mode {
CompileMode::Test | CompileMode::Bench => {
if release {
&self.bench
} else {
&self.test
match &build_profile {
BuildProfile::Release => {
&self.bench
}
BuildProfile::Dev => {
&self.test
}
BuildProfile::Custom(name) => {
self.custom.get(name.as_str()).unwrap()
}
}
}
CompileMode::Build
Expand All @@ -91,10 +174,16 @@ impl Profiles {
// `build_unit_profiles` normally ensures that it selects the
// ancestor's profile. However `cargo clean -p` can hit this
// path.
if release {
&self.release
} else {
&self.dev
match &build_profile {
BuildProfile::Release => {
&self.release
}
BuildProfile::Dev => {
&self.dev
}
BuildProfile::Custom(name) => {
self.custom.get(name.as_str()).unwrap()
}
}
}
CompileMode::Doc { .. } => &self.doc,
Expand Down Expand Up @@ -123,11 +212,18 @@ impl Profiles {
/// This returns a generic base profile. This is currently used for the
/// `[Finished]` line. It is not entirely accurate, since it doesn't
/// select for the package that was actually built.
pub fn base_profile(&self, release: bool) -> Profile {
if release {
self.release.get_profile(None, true, UnitFor::new_normal())
} else {
self.dev.get_profile(None, true, UnitFor::new_normal())
pub fn base_profile(&self, build_profile: &BuildProfile) -> Profile {
match &build_profile {
BuildProfile::Release => {
self.release.get_profile(None, true, UnitFor::new_normal())
}
BuildProfile::Dev => {
self.dev.get_profile(None, true, UnitFor::new_normal())
}
BuildProfile::Custom(name) => {
let r = self.custom.get(name.as_str()).unwrap();
r.get_profile(None, true, UnitFor::new_normal())
}
}
}

Expand Down Expand Up @@ -162,6 +258,11 @@ struct ProfileMaker {
default: Profile,
/// The profile from the `Cargo.toml` manifest.
toml: Option<TomlProfile>,

/// Profiles from which we inherit, in the order from which
/// we inherit.
inherits: Vec<TomlProfile>,

/// Profile loaded from `.cargo/config` files.
config: Option<TomlProfile>,
}
Expand All @@ -177,6 +278,9 @@ impl ProfileMaker {
if let Some(ref toml) = self.toml {
merge_toml(pkg_id, is_member, unit_for, &mut profile, toml);
}
for toml in &self.inherits {
merge_toml(pkg_id, is_member, unit_for, &mut profile, toml);
}
if let Some(ref toml) = self.config {
merge_toml(pkg_id, is_member, unit_for, &mut profile, toml);
}
Expand Down
Loading