From 290d73970a178dca1622535e654584d06e6a1290 Mon Sep 17 00:00:00 2001 From: Alexander Bulaev Date: Sat, 24 Sep 2016 16:00:08 +0300 Subject: [PATCH] Fix build with opt-levels s/z (issue #102) --- src/lib.rs | 15 ++++++++++++--- tests/test.rs | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3b542ff..078fe07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,7 +74,7 @@ pub struct Config { target: Option, host: Option, out_dir: Option, - opt_level: Option, + opt_level: Option, debug: Option, env: Vec<(OsString, OsString)>, compiler: Option, @@ -250,7 +250,16 @@ impl Config { /// This option is automatically scraped from the `OPT_LEVEL` environment /// variable by build scripts, so it's not required to call this function. pub fn opt_level(&mut self, opt_level: u32) -> &mut Config { - self.opt_level = Some(opt_level); + self.opt_level = Some(opt_level.to_string()); + self + } + + /// Configures the optimization level of the generated object files. + /// + /// This option is automatically scraped from the `OPT_LEVEL` environment + /// variable by build scripts, so it's not required to call this function. + pub fn opt_level_str(&mut self, opt_level: &str) -> &mut Config { + self.opt_level = Some(opt_level.to_string()); self } @@ -796,7 +805,7 @@ impl Config { } fn get_opt_level(&self) -> String { - self.opt_level.map(|s| s.to_string()).unwrap_or_else(|| { + self.opt_level.as_ref().cloned().unwrap_or_else(|| { self.getenv_unwrap("OPT_LEVEL") }) } diff --git a/tests/test.rs b/tests/test.rs index b1d6c8d..1b6a0bd 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -31,6 +31,20 @@ fn gnu_opt_level_1() { .must_not_have("-O2"); } +#[test] +fn gnu_opt_level_s() { + let test = Test::gnu(); + test.gcc() + .opt_level_str("s") + .file("foo.c").compile("libfoo.a"); + + test.cmd(0).must_have("-Os") + .must_not_have("-O1") + .must_not_have("-O2") + .must_not_have("-O3") + .must_not_have("-Oz"); +} + #[test] fn gnu_debug() { let test = Test::gnu();