@@ -197,10 +197,16 @@ impl Config {
197
197
self
198
198
}
199
199
200
- /// Sets the profile for this compilation .
200
+ /// Sets the `CMAKE_BUILD_TYPE=build_type` variable .
201
201
///
202
- /// This is automatically scraped from `$PROFILE` which is set for Cargo
203
- /// build scripts so it's not necessary to call this from a build script.
202
+ /// By default, this value is automatically inferred from Rust's compilation
203
+ /// profile as follows:
204
+ ///
205
+ /// * if `opt-level=0` then `CMAKE_BUILD_TYPE=Debug`,
206
+ /// * if `opt-level={1,2,3}` and:
207
+ /// * `debug=false` then `CMAKE_BUILD_TYPE=Release`
208
+ /// * otherwise `CMAKE_BUILD_TYPE=RelWithDebInfo`
209
+ /// * if `opt-level={s,z}` then `CMAKE_BUILD_TYPE=MinSizeRel`
204
210
pub fn profile ( & mut self , profile : & str ) -> & mut Config {
205
211
self . profile = Some ( profile. to_string ( ) ) ;
206
212
self
@@ -403,9 +409,68 @@ impl Config {
403
409
is_ninja = generator. to_string_lossy ( ) . contains ( "Ninja" ) ;
404
410
}
405
411
let profile = self . profile . clone ( ) . unwrap_or_else ( || {
406
- match & getenv_unwrap ( "PROFILE" ) [ ..] {
407
- "bench" | "release" => "Release" ,
408
- _ => "Debug" ,
412
+ // Automatically set the `CMAKE_BUILD_TYPE` if the user did not
413
+ // specify one.
414
+
415
+ // Determine Rust's profile, optimization level, and debug info:
416
+ #[ derive( PartialEq ) ]
417
+ enum RustProfile {
418
+ Debug ,
419
+ Release ,
420
+ }
421
+ #[ derive( PartialEq , Debug ) ]
422
+ enum OptLevel {
423
+ Debug ,
424
+ Release ,
425
+ Size ,
426
+ }
427
+
428
+ let rust_profile = match & getenv_unwrap ( "PROFILE" ) [ ..] {
429
+ "debug" => RustProfile :: Debug ,
430
+ "release" | "bench" => RustProfile :: Release ,
431
+ unknown => {
432
+ eprintln ! (
433
+ "Warning: unknown Rust profile={}; defaulting to a release build." ,
434
+ unknown
435
+ ) ;
436
+ RustProfile :: Release
437
+ }
438
+ } ;
439
+
440
+ let opt_level = match & getenv_unwrap ( "OPT_LEVEL" ) [ ..] {
441
+ "0" => OptLevel :: Debug ,
442
+ "1" | "2" | "3" => OptLevel :: Release ,
443
+ "s" | "z" => OptLevel :: Size ,
444
+ unknown => {
445
+ let default_opt_level = match rust_profile {
446
+ RustProfile :: Debug => OptLevel :: Debug ,
447
+ RustProfile :: Release => OptLevel :: Release ,
448
+ } ;
449
+ eprintln ! (
450
+ "Warning: unknown opt-level={}; defaulting to a {:?} build." ,
451
+ unknown, default_opt_level
452
+ ) ;
453
+ default_opt_level
454
+ }
455
+ } ;
456
+
457
+ let debug_info: bool = match & getenv_unwrap ( "DEBUG" ) [ ..] {
458
+ "false" => false ,
459
+ "true" => true ,
460
+ unknown => {
461
+ eprintln ! (
462
+ "Warning: unknown debug={}; defaulting to `true`." ,
463
+ unknown
464
+ ) ;
465
+ true
466
+ }
467
+ } ;
468
+
469
+ match ( opt_level, debug_info) {
470
+ ( OptLevel :: Debug , _) => "Debug" ,
471
+ ( OptLevel :: Release , false ) => "Release" ,
472
+ ( OptLevel :: Release , true ) => "RelWithDebInfo" ,
473
+ ( OptLevel :: Size , _) => "MinSizeRel" ,
409
474
} . to_string ( )
410
475
} ) ;
411
476
for & ( ref k, ref v) in & self . defines {
0 commit comments