diff --git a/src/lib.rs b/src/lib.rs index 177aabe..4961fa5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,8 +116,8 @@ pub struct Build { shared_flag: Option, static_flag: Option, warnings_into_errors: bool, - warnings: bool, - extra_warnings: bool, + warnings: Option, + extra_warnings: Option, } /// Represents the types of errors that may occur while using cc-rs. @@ -319,8 +319,8 @@ impl Build { cargo_metadata: true, pic: None, static_crt: None, - warnings: true, - extra_warnings: true, + warnings: None, + extra_warnings: None, warnings_into_errors: false, } } @@ -597,8 +597,8 @@ impl Build { /// .compile("libfoo.a"); /// ``` pub fn warnings(&mut self, warnings: bool) -> &mut Build { - self.warnings = warnings; - self.extra_warnings = warnings; + self.warnings = Some(warnings); + self.extra_warnings = Some(warnings); self } @@ -620,7 +620,7 @@ impl Build { /// .compile("libfoo.a"); /// ``` pub fn extra_warnings(&mut self, warnings: bool) -> &mut Build { - self.extra_warnings = warnings; + self.extra_warnings = Some(warnings); self } @@ -1324,12 +1324,17 @@ impl Build { cmd.args.push(directory.into()); } - if self.warnings { + // If warnings and/or extra_warnings haven't been explicitly set, + // then we set them only if the environment doesn't already have + // CFLAGS/CXXFLAGS, since those variables presumably already contain + // the desired set of warnings flags. + + if self.warnings.unwrap_or(if self.has_flags() { false } else { true }) { let wflags = cmd.family.warnings_flags().into(); cmd.push_cc_arg(wflags); } - if self.extra_warnings { + if self.extra_warnings.unwrap_or(if self.has_flags() { false } else { true }) { if let Some(wflags) = cmd.family.extra_warnings_flags() { cmd.push_cc_arg(wflags.into()); } @@ -1366,6 +1371,12 @@ impl Build { Ok(cmd) } + fn has_flags(&self) -> bool { + let flags_env_var_name = if self.cpp { "CXXFLAGS" } else { "CFLAGS" }; + let flags_env_var_value = self.get_var(flags_env_var_name); + if let Ok(_) = flags_env_var_value { true } else { false } + } + fn msvc_macro_assembler(&self) -> Result<(Command, String), Error> { let target = self.get_target()?; let tool = if target.contains("x86_64") { diff --git a/tests/test.rs b/tests/test.rs index e6c4e8e..820072f 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,6 +1,7 @@ extern crate cc; extern crate tempdir; +use std::env; use support::Test; mod support; @@ -110,6 +111,30 @@ fn gnu_warnings_overridable() { .must_have_in_order("-Wall", "-Wno-missing-field-initializers"); } +#[test] +fn gnu_no_warnings_if_cflags() { + env::set_var("CFLAGS", "-Wflag-does-not-exist"); + let test = Test::gnu(); + test.gcc() + .file("foo.c") + .compile("foo"); + + test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra"); + env::set_var("CFLAGS", ""); +} + +#[test] +fn gnu_no_warnings_if_cxxflags() { + env::set_var("CXXFLAGS", "-Wflag-does-not-exist"); + let test = Test::gnu(); + test.gcc() + .file("foo.c") + .compile("foo"); + + test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra"); + env::set_var("CXXFLAGS", ""); +} + #[test] fn gnu_x86_64() { for vendor in &["unknown-linux-gnu", "apple-darwin"] {