Skip to content

Commit 8ce7108

Browse files
committed
Add warnings control
1 parent 88ac58e commit 8ce7108

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

src/lib.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ pub struct Config {
9191
shared_flag: Option<bool>,
9292
static_flag: Option<bool>,
9393
check_file_created: bool,
94+
warnings_into_errors: bool,
95+
warnings: bool,
9496
}
9597

9698
/// Configuration used to represent an invocation of a C compiler.
@@ -151,6 +153,28 @@ impl ToolFamily {
151153
ToolFamily::Clang => "-E",
152154
}
153155
}
156+
157+
/// What the flags to enable all warnings
158+
fn warnings_flags(&self) -> &'static [&'static str] {
159+
static MSVC_FLAGS: &'static [&'static str] = &["/Wall"];
160+
static GNU_FLAGS: &'static [&'static str] = &["-Wall", "-Wpedantic", "-Wextra"];
161+
static CLANG_FLAGS: &'static [&'static str] = &["-Weverything"];
162+
163+
match *self {
164+
ToolFamily::Msvc => &MSVC_FLAGS,
165+
ToolFamily::Gnu => &GNU_FLAGS,
166+
ToolFamily::Clang => &CLANG_FLAGS,
167+
}
168+
}
169+
170+
/// What the flag to turn warning into errors
171+
fn warnings_to_errors_flag(&self) -> &'static str {
172+
match *self {
173+
ToolFamily::Msvc => "/WX",
174+
ToolFamily::Gnu |
175+
ToolFamily::Clang => "-Werror"
176+
}
177+
}
154178
}
155179

156180
/// Compile a library from the given set of input C files.
@@ -206,6 +230,8 @@ impl Config {
206230
pic: None,
207231
static_crt: None,
208232
check_file_created: false,
233+
warnings: true,
234+
warnings_into_errors: false,
209235
}
210236
}
211237

@@ -374,6 +400,45 @@ impl Config {
374400
self
375401
}
376402

403+
/// Set warnings into errors flag.
404+
///
405+
/// Disabled by default.
406+
///
407+
/// # Example
408+
///
409+
/// ```no_run
410+
/// gcc::Config::new()
411+
/// .file("src/foo.c")
412+
/// .warnings_into_errors(true)
413+
/// .compile("libfoo.a");
414+
/// ```
415+
pub fn warnings_into_errors(&mut self, warnings_into_errors: bool) -> &mut Config {
416+
self.warnings_into_errors = warnings_into_errors;
417+
self
418+
}
419+
420+
/// Set warnings flags.
421+
///
422+
/// Adds some flags:
423+
/// - "/Wall" for MSVC.
424+
/// - "-Wall", "-Wpedantic", "-Wextra" for GNU.
425+
/// - "-Weverything" for Clang.
426+
///
427+
/// Enabled by default.
428+
///
429+
/// # Example
430+
///
431+
/// ```no_run
432+
/// gcc::Config::new()
433+
/// .file("src/foo.c")
434+
/// .warnings(false)
435+
/// .compile("libfoo.a");
436+
/// ```
437+
pub fn warnings(&mut self, warnings: bool) -> &mut Config {
438+
self.warnings = warnings;
439+
self
440+
}
441+
377442
/// Set the standard library to link against when compiling with C++
378443
/// support.
379444
///
@@ -534,7 +599,6 @@ impl Config {
534599
self
535600
}
536601

537-
538602
#[doc(hidden)]
539603
pub fn __set_env<A, B>(&mut self, a: A, b: B) -> &mut Config
540604
where A: AsRef<OsStr>,
@@ -912,6 +976,17 @@ impl Config {
912976
cmd.args.push(format!("{}D{}", lead, key).into());
913977
}
914978
}
979+
980+
if self.warnings {
981+
for flag in cmd.family.warnings_flags().iter() {
982+
cmd.args.push(flag.into());
983+
}
984+
}
985+
986+
if self.warnings_into_errors {
987+
cmd.args.push(cmd.family.warnings_to_errors_flag().into());
988+
}
989+
915990
cmd
916991
}
917992

0 commit comments

Comments
 (0)