Skip to content

Commit e634ee5

Browse files
committed
named-profiles: add backward compatibility if feature is disabled
The effects over the profile used by targets are made conditional in this commit, using the old scheme if the `named-profiles` feature is disabled. This also affects the `profile_targets` tests, which now have two modes - stable, and nightly with the feature enabled.
1 parent 04804ea commit e634ee5

File tree

2 files changed

+228
-61
lines changed

2 files changed

+228
-61
lines changed

src/cargo/core/profiles.rs

+143-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::Deserialize;
66

77
use crate::core::compiler::{CompileMode, ProfileKind};
88
use crate::core::interning::InternedString;
9-
use crate::core::{Features, PackageId, PackageIdSpec, PackageSet, Shell};
9+
use crate::core::{Feature, Features, PackageId, PackageIdSpec, PackageSet, Shell};
1010
use crate::util::errors::CargoResultExt;
1111
use crate::util::toml::{ProfilePackageSpec, StringOrBool, TomlProfile, TomlProfiles, U32OrBool};
1212
use crate::util::{closest_msg, CargoResult, Config};
@@ -20,6 +20,7 @@ pub struct Profiles {
2020
incremental: Option<bool>,
2121
dir_names: HashMap<String, String>,
2222
by_name: HashMap<String, ProfileMaker>,
23+
named_profiles_enabled: bool,
2324
}
2425

2526
impl Profiles {
@@ -40,8 +41,85 @@ impl Profiles {
4041
None => config.get::<Option<bool>>("build.incremental")?,
4142
};
4243

44+
if !features.is_enabled(Feature::named_profiles()) {
45+
let mut profile_makers = Profiles {
46+
incremental,
47+
named_profiles_enabled: false,
48+
dir_names: Self::predefined_dir_names(),
49+
by_name: HashMap::new(),
50+
};
51+
52+
profile_makers.by_name.insert(
53+
"dev".to_owned(),
54+
ProfileMaker {
55+
toml: profiles.and_then(|p| p.get("dev").cloned()),
56+
inherits: vec![],
57+
config: config_profiles.dev.clone(),
58+
default: Profile::default_dev(),
59+
},
60+
);
61+
profile_makers
62+
.dir_names
63+
.insert("dev".to_owned(), "debug".to_owned());
64+
65+
profile_makers.by_name.insert(
66+
"release".to_owned(),
67+
ProfileMaker {
68+
toml: profiles.and_then(|p| p.get("release").cloned()),
69+
inherits: vec![],
70+
config: config_profiles.release.clone(),
71+
default: Profile::default_release(),
72+
},
73+
);
74+
profile_makers
75+
.dir_names
76+
.insert("release".to_owned(), "release".to_owned());
77+
78+
profile_makers.by_name.insert(
79+
"test".to_owned(),
80+
ProfileMaker {
81+
toml: profiles.and_then(|p| p.get("test").cloned()),
82+
inherits: vec![],
83+
config: None,
84+
default: Profile::default_test(),
85+
},
86+
);
87+
profile_makers
88+
.dir_names
89+
.insert("test".to_owned(), "debug".to_owned());
90+
91+
profile_makers.by_name.insert(
92+
"bench".to_owned(),
93+
ProfileMaker {
94+
toml: profiles.and_then(|p| p.get("bench").cloned()),
95+
inherits: vec![],
96+
config: None,
97+
default: Profile::default_bench(),
98+
},
99+
);
100+
profile_makers
101+
.dir_names
102+
.insert("bench".to_owned(), "release".to_owned());
103+
104+
profile_makers.by_name.insert(
105+
"doc".to_owned(),
106+
ProfileMaker {
107+
toml: profiles.and_then(|p| p.get("doc").cloned()),
108+
inherits: vec![],
109+
config: None,
110+
default: Profile::default_doc(),
111+
},
112+
);
113+
profile_makers
114+
.dir_names
115+
.insert("doc".to_owned(), "debug".to_owned());
116+
117+
return Ok(profile_makers);
118+
}
119+
43120
let mut profile_makers = Profiles {
44121
incremental,
122+
named_profiles_enabled: true,
45123
dir_names: Self::predefined_dir_names(),
46124
by_name: HashMap::new(),
47125
};
@@ -236,8 +314,47 @@ impl Profiles {
236314
mode: CompileMode,
237315
profile_kind: ProfileKind,
238316
) -> Profile {
239-
let maker = match self.by_name.get(profile_kind.name()) {
240-
None => panic!("Profile {} undefined", profile_kind.name()),
317+
let profile_name = if !self.named_profiles_enabled {
318+
// With the feature disabled, we degrade `--profile` back to the
319+
// `--release` and `--debug` predicates, and convert back from
320+
// ProfileKind::Custom instantiation.
321+
322+
let release = match profile_kind {
323+
ProfileKind::Release => true,
324+
ProfileKind::Custom(ref s) if s == "bench" => true,
325+
ProfileKind::Custom(ref s) if s == "test" => false,
326+
_ => false,
327+
};
328+
329+
match mode {
330+
CompileMode::Test | CompileMode::Bench => {
331+
if release {
332+
"bench"
333+
} else {
334+
"test"
335+
}
336+
}
337+
CompileMode::Build
338+
| CompileMode::Check { .. }
339+
| CompileMode::Doctest
340+
| CompileMode::RunCustomBuild => {
341+
// Note: `RunCustomBuild` doesn't normally use this code path.
342+
// `build_unit_profiles` normally ensures that it selects the
343+
// ancestor's profile. However, `cargo clean -p` can hit this
344+
// path.
345+
if release {
346+
"release"
347+
} else {
348+
"dev"
349+
}
350+
}
351+
CompileMode::Doc { .. } => "doc",
352+
}
353+
} else {
354+
profile_kind.name()
355+
};
356+
let maker = match self.by_name.get(profile_name) {
357+
None => panic!("Profile {} undefined", profile_name),
241358
Some(r) => r,
242359
};
243360
let mut profile = maker.get_profile(Some(pkg_id), is_member, unit_for);
@@ -666,6 +783,29 @@ impl Profile {
666783
}
667784
}
668785

786+
// NOTE: Remove the following three once `named_profiles` is default:
787+
788+
fn default_test() -> Profile {
789+
Profile {
790+
name: "test",
791+
..Profile::default_dev()
792+
}
793+
}
794+
795+
fn default_bench() -> Profile {
796+
Profile {
797+
name: "bench",
798+
..Profile::default_release()
799+
}
800+
}
801+
802+
fn default_doc() -> Profile {
803+
Profile {
804+
name: "doc",
805+
..Profile::default_dev()
806+
}
807+
}
808+
669809
/// Compares all fields except `name`, which doesn't affect compilation.
670810
/// This is necessary for `Unit` deduplication for things like "test" and
671811
/// "dev" which are essentially the same.

0 commit comments

Comments
 (0)