Skip to content

Commit a64df2c

Browse files
Allow getting final build profile from Config (#105)
This allows the user to get the default one if not set explicitly Ref: mgeier/libflac-sys#5
1 parent 3d60fcb commit a64df2c

File tree

1 file changed

+72
-63
lines changed

1 file changed

+72
-63
lines changed

src/lib.rs

Lines changed: 72 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,77 @@ pub fn build<P: AsRef<Path>>(path: P) -> PathBuf {
100100
}
101101

102102
impl Config {
103+
/// Return explicitly set profile or infer `CMAKE_BUILD_TYPE` from Rust's compilation profile.
104+
///
105+
/// * if `opt-level=0` then `CMAKE_BUILD_TYPE=Debug`,
106+
/// * if `opt-level={1,2,3}` and:
107+
/// * `debug=false` then `CMAKE_BUILD_TYPE=Release`
108+
/// * otherwise `CMAKE_BUILD_TYPE=RelWithDebInfo`
109+
/// * if `opt-level={s,z}` then `CMAKE_BUILD_TYPE=MinSizeRel`
110+
pub fn get_profile(&self) -> &str {
111+
if let Some(profile) = self.profile.as_ref() {
112+
profile
113+
} else {
114+
// Determine Rust's profile, optimization level, and debug info:
115+
#[derive(PartialEq)]
116+
enum RustProfile {
117+
Debug,
118+
Release,
119+
}
120+
#[derive(PartialEq, Debug)]
121+
enum OptLevel {
122+
Debug,
123+
Release,
124+
Size,
125+
}
126+
127+
let rust_profile = match &getenv_unwrap("PROFILE")[..] {
128+
"debug" => RustProfile::Debug,
129+
"release" | "bench" => RustProfile::Release,
130+
unknown => {
131+
eprintln!(
132+
"Warning: unknown Rust profile={}; defaulting to a release build.",
133+
unknown
134+
);
135+
RustProfile::Release
136+
}
137+
};
138+
139+
let opt_level = match &getenv_unwrap("OPT_LEVEL")[..] {
140+
"0" => OptLevel::Debug,
141+
"1" | "2" | "3" => OptLevel::Release,
142+
"s" | "z" => OptLevel::Size,
143+
unknown => {
144+
let default_opt_level = match rust_profile {
145+
RustProfile::Debug => OptLevel::Debug,
146+
RustProfile::Release => OptLevel::Release,
147+
};
148+
eprintln!(
149+
"Warning: unknown opt-level={}; defaulting to a {:?} build.",
150+
unknown, default_opt_level
151+
);
152+
default_opt_level
153+
}
154+
};
155+
156+
let debug_info: bool = match &getenv_unwrap("DEBUG")[..] {
157+
"false" => false,
158+
"true" => true,
159+
unknown => {
160+
eprintln!("Warning: unknown debug={}; defaulting to `true`.", unknown);
161+
true
162+
}
163+
};
164+
165+
match (opt_level, debug_info) {
166+
(OptLevel::Debug, _) => "Debug",
167+
(OptLevel::Release, false) => "Release",
168+
(OptLevel::Release, true) => "RelWithDebInfo",
169+
(OptLevel::Size, _) => "MinSizeRel",
170+
}
171+
}
172+
}
173+
103174
/// Creates a new blank set of configuration to build the project specified
104175
/// at the path `path`.
105176
pub fn new<P: AsRef<Path>>(path: P) -> Config {
@@ -495,69 +566,7 @@ impl Config {
495566
if let Some(ref generator) = self.generator {
496567
cmd.arg("-G").arg(generator);
497568
}
498-
let profile = self.profile.clone().unwrap_or_else(|| {
499-
// Automatically set the `CMAKE_BUILD_TYPE` if the user did not
500-
// specify one.
501-
502-
// Determine Rust's profile, optimization level, and debug info:
503-
#[derive(PartialEq)]
504-
enum RustProfile {
505-
Debug,
506-
Release,
507-
}
508-
#[derive(PartialEq, Debug)]
509-
enum OptLevel {
510-
Debug,
511-
Release,
512-
Size,
513-
}
514-
515-
let rust_profile = match &getenv_unwrap("PROFILE")[..] {
516-
"debug" => RustProfile::Debug,
517-
"release" | "bench" => RustProfile::Release,
518-
unknown => {
519-
eprintln!(
520-
"Warning: unknown Rust profile={}; defaulting to a release build.",
521-
unknown
522-
);
523-
RustProfile::Release
524-
}
525-
};
526-
527-
let opt_level = match &getenv_unwrap("OPT_LEVEL")[..] {
528-
"0" => OptLevel::Debug,
529-
"1" | "2" | "3" => OptLevel::Release,
530-
"s" | "z" => OptLevel::Size,
531-
unknown => {
532-
let default_opt_level = match rust_profile {
533-
RustProfile::Debug => OptLevel::Debug,
534-
RustProfile::Release => OptLevel::Release,
535-
};
536-
eprintln!(
537-
"Warning: unknown opt-level={}; defaulting to a {:?} build.",
538-
unknown, default_opt_level
539-
);
540-
default_opt_level
541-
}
542-
};
543-
544-
let debug_info: bool = match &getenv_unwrap("DEBUG")[..] {
545-
"false" => false,
546-
"true" => true,
547-
unknown => {
548-
eprintln!("Warning: unknown debug={}; defaulting to `true`.", unknown);
549-
true
550-
}
551-
};
552-
553-
match (opt_level, debug_info) {
554-
(OptLevel::Debug, _) => "Debug",
555-
(OptLevel::Release, false) => "Release",
556-
(OptLevel::Release, true) => "RelWithDebInfo",
557-
(OptLevel::Size, _) => "MinSizeRel",
558-
}
559-
.to_string()
560-
});
569+
let profile = self.get_profile();
561570
for &(ref k, ref v) in &self.defines {
562571
let mut os = OsString::from("-D");
563572
os.push(k);

0 commit comments

Comments
 (0)