Skip to content

Commit 28dd4d4

Browse files
committed
Improve automatic CMAKE_BUILD_TYPE detection.
The previous algorithm was based in Rust's profile: * `profile={release,bench}` => `CMAKE_BUILD_TYPE=Release`, * otherwise => `CMAKE_BUILD_TYPE=Debug`. The new algorithm takes into account `opt-level` and `debuginfo`: * `opt-level=0` => `CMAKE_BUILD_TYPE=Debug` * `opt-level={1,2,3}` and: * `debug=false` => `CMAKE_BUILD_TYPE=Release` * otherwise => `CMAKE_BUILD_TYPE=RelWithDebInfo` * `opt-level={s,z}` => `CMAKE_BUILD_TYPE=MinSizeRel` The Rust profiles flow into the algorithm when: * determining the `opt-level` in case its value is unknown: `profile=debug` implies `opt-level=0`, and otherwise a non-zero `opt-level` is assumed. Closes #51 .
1 parent a9dd53a commit 28dd4d4

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
@@ -193,10 +193,16 @@ impl Config {
193193
self
194194
}
195195

196-
/// Sets the profile for this compilation.
196+
/// Sets the `CMAKE_BUILD_TYPE=build_type` variable.
197197
///
198-
/// This is automatically scraped from `$PROFILE` which is set for Cargo
199-
/// build scripts so it's not necessary to call this from a build script.
198+
/// By default, this value is automatically inferred from Rust's compilation
199+
/// profile as follows:
200+
///
201+
/// * if `opt-level=0` then `CMAKE_BUILD_TYPE=Debug`,
202+
/// * if `opt-level={1,2,3}` and:
203+
/// * `debug=false` then `CMAKE_BUILD_TYPE=Release`
204+
/// * otherwise `CMAKE_BUILD_TYPE=RelWithDebInfo`
205+
/// * if `opt-level={s,z}` then `CMAKE_BUILD_TYPE=MinSizeRel`
200206
pub fn profile(&mut self, profile: &str) -> &mut Config {
201207
self.profile = Some(profile.to_string());
202208
self
@@ -386,9 +392,68 @@ impl Config {
386392
is_ninja = generator.to_string_lossy().contains("Ninja");
387393
}
388394
let profile = self.profile.clone().unwrap_or_else(|| {
389-
match &getenv_unwrap("PROFILE")[..] {
390-
"bench" | "release" => "Release",
391-
_ => "Debug",
395+
// Automatically set the `CMAKE_BUILD_TYPE` if the user did not
396+
// specify one.
397+
398+
// Determine Rust's profile, optimization level, and debug info:
399+
#[derive(PartialEq)]
400+
enum RustProfile {
401+
Debug,
402+
Release,
403+
}
404+
#[derive(PartialEq, Debug)]
405+
enum OptLevel {
406+
Debug,
407+
Release,
408+
Size,
409+
}
410+
411+
let rust_profile = match &getenv_unwrap("PROFILE")[..] {
412+
"debug" => RustProfile::Debug,
413+
"release" | "bench" => RustProfile::Release,
414+
unknown => {
415+
eprintln!(
416+
"Warning: unknown Rust profile={}; defaulting to a release build.",
417+
unknown
418+
);
419+
RustProfile::Release
420+
}
421+
};
422+
423+
let opt_level = match &getenv_unwrap("OPT_LEVEL")[..] {
424+
"0" => OptLevel::Debug,
425+
"1" | "2" | "3" => OptLevel::Release,
426+
"s" | "z" => OptLevel::Size,
427+
unknown => {
428+
let default_opt_level = match rust_profile {
429+
RustProfile::Debug => OptLevel::Debug,
430+
RustProfile::Release => OptLevel::Release,
431+
};
432+
eprintln!(
433+
"Warning: unknown opt-level={}; defaulting to a {:?} build.",
434+
unknown, default_opt_level
435+
);
436+
default_opt_level
437+
}
438+
};
439+
440+
let debug_info: bool = match &getenv_unwrap("DEBUG")[..] {
441+
"false" => false,
442+
"true" => true,
443+
unknown => {
444+
eprintln!(
445+
"Warning: unknown debug={}; defaulting to `true`.",
446+
unknown
447+
);
448+
true
449+
}
450+
};
451+
452+
match (opt_level, debug_info) {
453+
(OptLevel::Debug, _) => "Debug",
454+
(OptLevel::Release, false) => "Release",
455+
(OptLevel::Release, true) => "RelWithDebInfo",
456+
(OptLevel::Size, _) => "MinSizeRel",
392457
}.to_string()
393458
});
394459
for &(ref k, ref v) in &self.defines {

0 commit comments

Comments
 (0)