Browse Source

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
wintest
Alex Crichton 6 years ago
committed by GitHub
parent
commit
cb32f5e583
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      src/lib.rs
  2. 25
      tests/test.rs

29
src/lib.rs

@ -116,8 +116,8 @@ pub struct Build {
shared_flag: Option<bool>, shared_flag: Option<bool>,
static_flag: Option<bool>, static_flag: Option<bool>,
warnings_into_errors: bool, warnings_into_errors: bool,
warnings: bool, warnings: Option<bool>,
extra_warnings: bool, extra_warnings: Option<bool>,
} }
/// Represents the types of errors that may occur while using cc-rs. /// Represents the types of errors that may occur while using cc-rs.
@ -319,8 +319,8 @@ impl Build {
cargo_metadata: true, cargo_metadata: true,
pic: None, pic: None,
static_crt: None, static_crt: None,
warnings: true, warnings: None,
extra_warnings: true, extra_warnings: None,
warnings_into_errors: false, warnings_into_errors: false,
} }
} }
@ -597,8 +597,8 @@ impl Build {
/// .compile("libfoo.a"); /// .compile("libfoo.a");
/// ``` /// ```
pub fn warnings(&mut self, warnings: bool) -> &mut Build { pub fn warnings(&mut self, warnings: bool) -> &mut Build {
self.warnings = warnings; self.warnings = Some(warnings);
self.extra_warnings = warnings; self.extra_warnings = Some(warnings);
self self
} }
@ -620,7 +620,7 @@ impl Build {
/// .compile("libfoo.a"); /// .compile("libfoo.a");
/// ``` /// ```
pub fn extra_warnings(&mut self, warnings: bool) -> &mut Build { pub fn extra_warnings(&mut self, warnings: bool) -> &mut Build {
self.extra_warnings = warnings; self.extra_warnings = Some(warnings);
self self
} }
@ -1324,12 +1324,17 @@ impl Build {
cmd.args.push(directory.into()); 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(); let wflags = cmd.family.warnings_flags().into();
cmd.push_cc_arg(wflags); 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() { if let Some(wflags) = cmd.family.extra_warnings_flags() {
cmd.push_cc_arg(wflags.into()); cmd.push_cc_arg(wflags.into());
} }
@ -1366,6 +1371,12 @@ impl Build {
Ok(cmd) 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> { fn msvc_macro_assembler(&self) -> Result<(Command, String), Error> {
let target = self.get_target()?; let target = self.get_target()?;
let tool = if target.contains("x86_64") { let tool = if target.contains("x86_64") {

25
tests/test.rs

@ -1,6 +1,7 @@
extern crate cc; extern crate cc;
extern crate tempdir; extern crate tempdir;
use std::env;
use support::Test; use support::Test;
mod support; mod support;
@ -110,6 +111,30 @@ fn gnu_warnings_overridable() {
.must_have_in_order("-Wall", "-Wno-missing-field-initializers"); .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] #[test]
fn gnu_x86_64() { fn gnu_x86_64() {
for vendor in &["unknown-linux-gnu", "apple-darwin"] { for vendor in &["unknown-linux-gnu", "apple-darwin"] {

Loading…
Cancel
Save