Skip to content

Commit cb32f5e

Browse files
authored
Merge pull request #342 from mykmelez/disable-warnings-if-flags-var-in-env
don't enable warnings by default if CFLAGS/CXXFLAGS defined in environment
2 parents bf321de + f48f8ed commit cb32f5e

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

src/lib.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ pub struct Build {
116116
shared_flag: Option<bool>,
117117
static_flag: Option<bool>,
118118
warnings_into_errors: bool,
119-
warnings: bool,
120-
extra_warnings: bool,
119+
warnings: Option<bool>,
120+
extra_warnings: Option<bool>,
121121
}
122122

123123
/// Represents the types of errors that may occur while using cc-rs.
@@ -319,8 +319,8 @@ impl Build {
319319
cargo_metadata: true,
320320
pic: None,
321321
static_crt: None,
322-
warnings: true,
323-
extra_warnings: true,
322+
warnings: None,
323+
extra_warnings: None,
324324
warnings_into_errors: false,
325325
}
326326
}
@@ -597,8 +597,8 @@ impl Build {
597597
/// .compile("libfoo.a");
598598
/// ```
599599
pub fn warnings(&mut self, warnings: bool) -> &mut Build {
600-
self.warnings = warnings;
601-
self.extra_warnings = warnings;
600+
self.warnings = Some(warnings);
601+
self.extra_warnings = Some(warnings);
602602
self
603603
}
604604

@@ -620,7 +620,7 @@ impl Build {
620620
/// .compile("libfoo.a");
621621
/// ```
622622
pub fn extra_warnings(&mut self, warnings: bool) -> &mut Build {
623-
self.extra_warnings = warnings;
623+
self.extra_warnings = Some(warnings);
624624
self
625625
}
626626

@@ -1324,12 +1324,17 @@ impl Build {
13241324
cmd.args.push(directory.into());
13251325
}
13261326

1327-
if self.warnings {
1327+
// If warnings and/or extra_warnings haven't been explicitly set,
1328+
// then we set them only if the environment doesn't already have
1329+
// CFLAGS/CXXFLAGS, since those variables presumably already contain
1330+
// the desired set of warnings flags.
1331+
1332+
if self.warnings.unwrap_or(if self.has_flags() { false } else { true }) {
13281333
let wflags = cmd.family.warnings_flags().into();
13291334
cmd.push_cc_arg(wflags);
13301335
}
13311336

1332-
if self.extra_warnings {
1337+
if self.extra_warnings.unwrap_or(if self.has_flags() { false } else { true }) {
13331338
if let Some(wflags) = cmd.family.extra_warnings_flags() {
13341339
cmd.push_cc_arg(wflags.into());
13351340
}
@@ -1366,6 +1371,12 @@ impl Build {
13661371
Ok(cmd)
13671372
}
13681373

1374+
fn has_flags(&self) -> bool {
1375+
let flags_env_var_name = if self.cpp { "CXXFLAGS" } else { "CFLAGS" };
1376+
let flags_env_var_value = self.get_var(flags_env_var_name);
1377+
if let Ok(_) = flags_env_var_value { true } else { false }
1378+
}
1379+
13691380
fn msvc_macro_assembler(&self) -> Result<(Command, String), Error> {
13701381
let target = self.get_target()?;
13711382
let tool = if target.contains("x86_64") {

tests/test.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extern crate cc;
22
extern crate tempdir;
33

4+
use std::env;
45
use support::Test;
56

67
mod support;
@@ -110,6 +111,30 @@ fn gnu_warnings_overridable() {
110111
.must_have_in_order("-Wall", "-Wno-missing-field-initializers");
111112
}
112113

114+
#[test]
115+
fn gnu_no_warnings_if_cflags() {
116+
env::set_var("CFLAGS", "-Wflag-does-not-exist");
117+
let test = Test::gnu();
118+
test.gcc()
119+
.file("foo.c")
120+
.compile("foo");
121+
122+
test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
123+
env::set_var("CFLAGS", "");
124+
}
125+
126+
#[test]
127+
fn gnu_no_warnings_if_cxxflags() {
128+
env::set_var("CXXFLAGS", "-Wflag-does-not-exist");
129+
let test = Test::gnu();
130+
test.gcc()
131+
.file("foo.c")
132+
.compile("foo");
133+
134+
test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
135+
env::set_var("CXXFLAGS", "");
136+
}
137+
113138
#[test]
114139
fn gnu_x86_64() {
115140
for vendor in &["unknown-linux-gnu", "apple-darwin"] {

0 commit comments

Comments
 (0)