@@ -91,6 +91,8 @@ pub struct Config {
91
91
shared_flag : Option < bool > ,
92
92
static_flag : Option < bool > ,
93
93
check_file_created : bool ,
94
+ warnings_into_errors : bool ,
95
+ warnings : bool ,
94
96
}
95
97
96
98
/// Configuration used to represent an invocation of a C compiler.
@@ -151,6 +153,28 @@ impl ToolFamily {
151
153
ToolFamily :: Clang => "-E" ,
152
154
}
153
155
}
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
+ }
154
178
}
155
179
156
180
/// Compile a library from the given set of input C files.
@@ -206,6 +230,8 @@ impl Config {
206
230
pic : None ,
207
231
static_crt : None ,
208
232
check_file_created : false ,
233
+ warnings : true ,
234
+ warnings_into_errors : false ,
209
235
}
210
236
}
211
237
@@ -374,6 +400,45 @@ impl Config {
374
400
self
375
401
}
376
402
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
+
377
442
/// Set the standard library to link against when compiling with C++
378
443
/// support.
379
444
///
@@ -534,7 +599,6 @@ impl Config {
534
599
self
535
600
}
536
601
537
-
538
602
#[ doc( hidden) ]
539
603
pub fn __set_env < A , B > ( & mut self , a : A , b : B ) -> & mut Config
540
604
where A : AsRef < OsStr > ,
@@ -912,6 +976,17 @@ impl Config {
912
976
cmd. args . push ( format ! ( "{}D{}" , lead, key) . into ( ) ) ;
913
977
}
914
978
}
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
+
915
990
cmd
916
991
}
917
992
0 commit comments