@@ -6,7 +6,7 @@ use serde::Deserialize;
6
6
7
7
use crate :: core:: compiler:: { CompileMode , ProfileKind } ;
8
8
use crate :: core:: interning:: InternedString ;
9
- use crate :: core:: { Features , PackageId , PackageIdSpec , PackageSet , Shell } ;
9
+ use crate :: core:: { Feature , Features , PackageId , PackageIdSpec , PackageSet , Shell } ;
10
10
use crate :: util:: errors:: CargoResultExt ;
11
11
use crate :: util:: toml:: { ProfilePackageSpec , StringOrBool , TomlProfile , TomlProfiles , U32OrBool } ;
12
12
use crate :: util:: { closest_msg, CargoResult , Config } ;
@@ -20,6 +20,7 @@ pub struct Profiles {
20
20
incremental : Option < bool > ,
21
21
dir_names : HashMap < String , String > ,
22
22
by_name : HashMap < String , ProfileMaker > ,
23
+ named_profiles_enabled : bool ,
23
24
}
24
25
25
26
impl Profiles {
@@ -40,8 +41,85 @@ impl Profiles {
40
41
None => config. get :: < Option < bool > > ( "build.incremental" ) ?,
41
42
} ;
42
43
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
+
43
120
let mut profile_makers = Profiles {
44
121
incremental,
122
+ named_profiles_enabled : true ,
45
123
dir_names : Self :: predefined_dir_names ( ) ,
46
124
by_name : HashMap :: new ( ) ,
47
125
} ;
@@ -236,8 +314,47 @@ impl Profiles {
236
314
mode : CompileMode ,
237
315
profile_kind : ProfileKind ,
238
316
) -> 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) ,
241
358
Some ( r) => r,
242
359
} ;
243
360
let mut profile = maker. get_profile ( Some ( pkg_id) , is_member, unit_for) ;
@@ -666,6 +783,29 @@ impl Profile {
666
783
}
667
784
}
668
785
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
+
669
809
/// Compares all fields except `name`, which doesn't affect compilation.
670
810
/// This is necessary for `Unit` deduplication for things like "test" and
671
811
/// "dev" which are essentially the same.
0 commit comments