Skip to content

Commit eb60028

Browse files
authored
Merge pull request #52 from gnzlbg/build-opts
Improve automatic CMAKE_BUILD_TYPE detection.
2 parents 5e049c8 + 28dd4d4 commit eb60028

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

src/lib.rs

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,16 @@ impl Config {
197197
self
198198
}
199199

200-
/// Sets the profile for this compilation.
200+
/// Sets the `CMAKE_BUILD_TYPE=build_type` variable.
201201
///
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`
204210
pub fn profile(&mut self, profile: &str) -> &mut Config {
205211
self.profile = Some(profile.to_string());
206212
self
@@ -403,9 +409,68 @@ impl Config {
403409
is_ninja = generator.to_string_lossy().contains("Ninja");
404410
}
405411
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",
409474
}.to_string()
410475
});
411476
for &(ref k, ref v) in &self.defines {

0 commit comments

Comments
 (0)