diff --git a/src/lib.rs b/src/lib.rs index 9af1793..a60c9ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,6 +91,8 @@ pub struct Config { shared_flag: Option, static_flag: Option, check_file_created: bool, + warnings_into_errors: bool, + warnings: bool, } /// Configuration used to represent an invocation of a C compiler. @@ -151,6 +153,28 @@ impl ToolFamily { ToolFamily::Clang => "-E", } } + + /// What the flags to enable all warnings + fn warnings_flags(&self) -> &'static [&'static str] { + static MSVC_FLAGS: &'static [&'static str] = &["/Wall"]; + static GNU_FLAGS: &'static [&'static str] = &["-Wall", "-Wpedantic", "-Wextra"]; + static CLANG_FLAGS: &'static [&'static str] = &["-Weverything"]; + + match *self { + ToolFamily::Msvc => &MSVC_FLAGS, + ToolFamily::Gnu => &GNU_FLAGS, + ToolFamily::Clang => &CLANG_FLAGS, + } + } + + /// What the flag to turn warning into errors + fn warnings_to_errors_flag(&self) -> &'static str { + match *self { + ToolFamily::Msvc => "/WX", + ToolFamily::Gnu | + ToolFamily::Clang => "-Werror" + } + } } /// Compile a library from the given set of input C files. @@ -206,6 +230,8 @@ impl Config { pic: None, static_crt: None, check_file_created: false, + warnings: true, + warnings_into_errors: false, } } @@ -374,6 +400,45 @@ impl Config { self } + /// Set warnings into errors flag. + /// + /// Disabled by default. + /// + /// # Example + /// + /// ```no_run + /// gcc::Config::new() + /// .file("src/foo.c") + /// .warnings_into_errors(true) + /// .compile("libfoo.a"); + /// ``` + pub fn warnings_into_errors(&mut self, warnings_into_errors: bool) -> &mut Config { + self.warnings_into_errors = warnings_into_errors; + self + } + + /// Set warnings flags. + /// + /// Adds some flags: + /// - "/Wall" for MSVC. + /// - "-Wall", "-Wpedantic", "-Wextra" for GNU. + /// - "-Weverything" for Clang. + /// + /// Enabled by default. + /// + /// # Example + /// + /// ```no_run + /// gcc::Config::new() + /// .file("src/foo.c") + /// .warnings(false) + /// .compile("libfoo.a"); + /// ``` + pub fn warnings(&mut self, warnings: bool) -> &mut Config { + self.warnings = warnings; + self + } + /// Set the standard library to link against when compiling with C++ /// support. /// @@ -534,7 +599,6 @@ impl Config { self } - #[doc(hidden)] pub fn __set_env(&mut self, a: A, b: B) -> &mut Config where A: AsRef, @@ -912,6 +976,17 @@ impl Config { cmd.args.push(format!("{}D{}", lead, key).into()); } } + + if self.warnings { + for flag in cmd.family.warnings_flags().iter() { + cmd.args.push(flag.into()); + } + } + + if self.warnings_into_errors { + cmd.args.push(cmd.family.warnings_to_errors_flag().into()); + } + cmd }