Skip to content

Commit

Permalink
Rename overrides to package in profiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Oct 10, 2019
1 parent aa4820b commit fcfe0b8
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 69 deletions.
22 changes: 11 additions & 11 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,12 @@ impl Profiles {
}
}

/// An object used for handling the profile override hierarchy.
/// An object used for handling the profile hierarchy.
///
/// The precedence of profiles are (first one wins):
/// - Profiles in `.cargo/config` files (using same order as below).
/// - [profile.dev.overrides.name] -- a named package.
/// - [profile.dev.overrides."*"] -- this cannot apply to workspace members.
/// - [profile.dev.package.name] -- a named package.
/// - [profile.dev.package."*"] -- this cannot apply to workspace members.
/// - [profile.dev.build-override] -- this can only apply to `build.rs` scripts
/// and their dependencies.
/// - [profile.dev]
Expand Down Expand Up @@ -511,8 +511,8 @@ impl ProfileMaker {
Some(ref toml) => toml,
None => return Ok(()),
};
let overrides = match toml.overrides {
Some(ref overrides) => overrides,
let overrides = match toml.package.as_ref().or(toml.overrides.as_ref()) {
Some(overrides) => overrides,
None => return Ok(()),
};
// Verify that a package doesn't match multiple spec overrides.
Expand Down Expand Up @@ -543,8 +543,8 @@ impl ProfileMaker {
.collect::<Vec<_>>()
.join(", ");
failure::bail!(
"multiple profile overrides in profile `{}` match package `{}`\n\
found profile override specs: {}",
"multiple package overrides in profile `{}` match package `{}`\n\
found package specs: {}",
self.default.name,
pkg_id,
specs
Expand Down Expand Up @@ -581,12 +581,12 @@ impl ProfileMaker {
let suggestion =
closest_msg(&spec.name(), packages.package_ids(), |p| p.name().as_str());
shell.warn(format!(
"profile override spec `{}` did not match any packages{}",
"package profile spec `{}` did not match any packages{}",
spec, suggestion
))?;
} else {
shell.warn(format!(
"version or URL in profile override spec `{}` does not \
"version or URL in package profile spec `{}` does not \
match any of the packages: {}",
spec,
name_matches.join(", ")
Expand All @@ -609,7 +609,7 @@ fn merge_toml_overrides(
merge_profile(profile, build_override);
}
}
if let Some(ref overrides) = toml.overrides {
if let Some(overrides) = toml.package.as_ref().or(toml.overrides.as_ref()) {
if !is_member {
if let Some(all) = overrides.get(&ProfilePackageSpec::All) {
merge_profile(profile, all);
Expand All @@ -634,7 +634,7 @@ fn merge_toml_overrides(
// no additional matches.
assert!(
matches.next().is_none(),
"package `{}` matched multiple profile overrides",
"package `{}` matched multiple package profile overrides",
pkg_id
);
}
Expand Down
37 changes: 29 additions & 8 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,10 @@ pub struct TomlProfile {
pub panic: Option<String>,
pub overflow_checks: Option<bool>,
pub incremental: Option<bool>,
// `overrides` has been renamed to `package`, this should be removed when
// stabilized.
pub overrides: Option<BTreeMap<ProfilePackageSpec, TomlProfile>>,
pub package: Option<BTreeMap<ProfilePackageSpec, TomlProfile>>,
pub build_override: Option<Box<TomlProfile>>,
pub dir_name: Option<String>,
pub inherits: Option<String>,
Expand Down Expand Up @@ -457,12 +460,23 @@ impl TomlProfile {
) -> CargoResult<()> {
if let Some(ref profile) = self.build_override {
features.require(Feature::profile_overrides())?;
profile.validate_override()?;
profile.validate_override("build-override")?;
}
if let Some(ref override_map) = self.overrides {
warnings.push(
"profile key `overrides` has been renamed to `package`, \
please update the manifest to the new key name"
.to_string(),
);
features.require(Feature::profile_overrides())?;
for profile in override_map.values() {
profile.validate_override()?;
profile.validate_override("package")?;
}
}
if let Some(ref packages) = self.package {
features.require(Feature::profile_overrides())?;
for profile in packages.values() {
profile.validate_override("package")?;
}
}

Expand Down Expand Up @@ -555,18 +569,21 @@ impl TomlProfile {
Ok(())
}

fn validate_override(&self) -> CargoResult<()> {
if self.overrides.is_some() || self.build_override.is_some() {
bail!("Profile overrides cannot be nested.");
fn validate_override(&self, which: &str) -> CargoResult<()> {
if self.overrides.is_some() || self.package.is_some() {
bail!("package-specific profiles cannot be nested");
}
if self.build_override.is_some() {
bail!("build-override profiles cannot be nested");
}
if self.panic.is_some() {
bail!("`panic` may not be specified in a profile override.")
bail!("`panic` may not be specified in a `{}` profile", which)
}
if self.lto.is_some() {
bail!("`lto` may not be specified in a profile override.")
bail!("`lto` may not be specified in a `{}` profile", which)
}
if self.rpath.is_some() {
bail!("`rpath` may not be specified in a profile override.")
bail!("`rpath` may not be specified in a `{}` profile", which)
}
Ok(())
}
Expand Down Expand Up @@ -612,6 +629,10 @@ impl TomlProfile {
self.overrides = Some(v.clone());
}

if let Some(v) = &profile.package {
self.package = Some(v.clone());
}

if let Some(v) = &profile.build_override {
self.build_override = Some(v.clone());
}
Expand Down
10 changes: 5 additions & 5 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ index each time.
* Original Issue: [#6477](https://github.com/rust-lang/cargo/pull/6477)
* Cache usage meta tracking issue: [#7150](https://github.com/rust-lang/cargo/issues/7150)

The `-Z mtime-on-use` flag is an experiment to have Cargo update the mtime of
used files to make it easier for tools like cargo-sweep to detect which files
The `-Z mtime-on-use` flag is an experiment to have Cargo update the mtime of
used files to make it easier for tools like cargo-sweep to detect which files
are stale. For many workflows this needs to be set on *all* invocations of cargo.
To make this more practical setting the `unstable.mtime_on_use` flag in `.cargo/config`
or the corresponding ENV variable will apply the `-Z mtime-on-use` to all
or the corresponding ENV variable will apply the `-Z mtime-on-use` to all
invocations of nightly cargo. (the config flag is ignored by stable)

### avoid-dev-deps
Expand Down Expand Up @@ -104,12 +104,12 @@ opt-level = 0
debug = true

# the `image` crate will be compiled with -Copt-level=3
[profile.dev.overrides.image]
[profile.dev.package.image]
opt-level = 3

# All dependencies (but not this crate itself or any workspace member)
# will be compiled with -Copt-level=2 . This includes build dependencies.
[profile.dev.overrides."*"]
[profile.dev.package."*"]
opt-level = 2

# Build scripts or proc-macros and their dependencies will be compiled with
Expand Down
14 changes: 7 additions & 7 deletions tests/testsuite/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ incremental = true
[profile.dev.build-override]
opt-level = 1
[profile.dev.overrides.bar]
[profile.dev.package.bar]
codegen-units = 9
[profile.no-lto]
Expand All @@ -315,26 +315,26 @@ lto = false
let config = new_config(&[
("CARGO_PROFILE_DEV_CODEGEN_UNITS", "5"),
("CARGO_PROFILE_DEV_BUILD_OVERRIDE_CODEGEN_UNITS", "11"),
("CARGO_PROFILE_DEV_OVERRIDES_env_CODEGEN_UNITS", "13"),
("CARGO_PROFILE_DEV_OVERRIDES_bar_OPT_LEVEL", "2"),
("CARGO_PROFILE_DEV_PACKAGE_env_CODEGEN_UNITS", "13"),
("CARGO_PROFILE_DEV_PACKAGE_bar_OPT_LEVEL", "2"),
]);

// TODO: don't use actual `tomlprofile`.
let p: toml::TomlProfile = config.get("profile.dev").unwrap();
let mut overrides = collections::BTreeMap::new();
let mut packages = collections::BTreeMap::new();
let key = toml::ProfilePackageSpec::Spec(::cargo::core::PackageIdSpec::parse("bar").unwrap());
let o_profile = toml::TomlProfile {
opt_level: Some(toml::TomlOptLevel("2".to_string())),
codegen_units: Some(9),
..Default::default()
};
overrides.insert(key, o_profile);
packages.insert(key, o_profile);
let key = toml::ProfilePackageSpec::Spec(::cargo::core::PackageIdSpec::parse("env").unwrap());
let o_profile = toml::TomlProfile {
codegen_units: Some(13),
..Default::default()
};
overrides.insert(key, o_profile);
packages.insert(key, o_profile);

assert_eq!(
p,
Expand All @@ -348,7 +348,7 @@ lto = false
panic: Some("abort".to_string()),
overflow_checks: Some(true),
incremental: Some(true),
overrides: Some(overrides),
package: Some(packages),
build_override: Some(Box::new(toml::TomlProfile {
opt_level: Some(toml::TomlOptLevel("1".to_string())),
codegen_units: Some(11),
Expand Down
22 changes: 11 additions & 11 deletions tests/testsuite/profile_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn profile_config_validate_warnings() {
[profile.dev.build-override]
bad-key-bo = true
[profile.dev.overrides.bar]
[profile.dev.package.bar]
bad-key-bar = true
"#,
)
Expand All @@ -66,7 +66,7 @@ fn profile_config_validate_warnings() {
[WARNING] unused key `profile.asdf` in config file `[..].cargo/config`
[WARNING] unused key `profile.test` in config file `[..].cargo/config`
[WARNING] unused key `profile.dev.bad-key` in config file `[..].cargo/config`
[WARNING] unused key `profile.dev.overrides.bar.bad-key-bar` in config file `[..].cargo/config`
[WARNING] unused key `profile.dev.package.bar.bad-key-bar` in config file `[..].cargo/config`
[WARNING] unused key `profile.dev.build-override.bad-key-bo` in config file `[..].cargo/config`
[COMPILING] foo [..]
[FINISHED] [..]
Expand Down Expand Up @@ -127,7 +127,7 @@ fn profile_config_validate_errors() {
.file(
".cargo/config",
r#"
[profile.dev.overrides.foo]
[profile.dev.package.foo]
panic = "abort"
"#,
)
Expand All @@ -141,7 +141,7 @@ fn profile_config_validate_errors() {
[ERROR] config profile `profile.dev` is not valid
Caused by:
`panic` may not be specified in a profile override.
`panic` may not be specified in a `package` profile
",
)
.run();
Expand Down Expand Up @@ -194,10 +194,10 @@ fn profile_config_override_spec_multiple() {
.file(
".cargo/config",
r#"
[profile.dev.overrides.bar]
[profile.dev.package.bar]
opt-level = 3
[profile.dev.overrides."bar:0.5.0"]
[profile.dev.package."bar:0.5.0"]
opt-level = 3
"#,
)
Expand All @@ -222,8 +222,8 @@ fn profile_config_override_spec_multiple() {
.with_status(101)
.with_stderr(
"\
[ERROR] multiple profile overrides in profile `dev` match package `bar v0.5.0 ([..])`
found profile override specs: bar, bar:0.5.0",
[ERROR] multiple package overrides in profile `dev` match package `bar v0.5.0 ([..])`
found package specs: bar, bar:0.5.0",
)
.run();
}
Expand Down Expand Up @@ -291,7 +291,7 @@ fn profile_config_override_precedence() {
[profile.dev]
codegen-units = 2
[profile.dev.overrides.bar]
[profile.dev.package.bar]
opt-level = 3
"#,
)
Expand All @@ -310,7 +310,7 @@ fn profile_config_override_precedence() {
.file(
".cargo/config",
r#"
[profile.dev.overrides.bar]
[profile.dev.package.bar]
opt-level = 2
"#,
)
Expand Down Expand Up @@ -346,7 +346,7 @@ fn profile_config_no_warn_unknown_override() {
.file(
".cargo/config",
r#"
[profile.dev.overrides.bar]
[profile.dev.package.bar]
codegen-units = 4
"#,
)
Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/profile_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,16 @@ fn overrides_with_custom() {
[profile.dev]
codegen-units = 7
[profile.dev.overrides.xxx]
[profile.dev.package.xxx]
codegen-units = 5
[profile.dev.overrides.yyy]
[profile.dev.package.yyy]
codegen-units = 3
[profile.other]
inherits = "dev"
codegen-units = 2
[profile.other.overrides.yyy]
[profile.other.package.yyy]
codegen-units = 6
"#,
)
Expand Down
Loading

0 comments on commit fcfe0b8

Please sign in to comment.