@@ -116,8 +116,8 @@ pub struct Build {
116
116
shared_flag : Option < bool > ,
117
117
static_flag : Option < bool > ,
118
118
warnings_into_errors : bool ,
119
- warnings : bool ,
120
- extra_warnings : bool ,
119
+ warnings : Option < bool > ,
120
+ extra_warnings : Option < bool > ,
121
121
}
122
122
123
123
/// Represents the types of errors that may occur while using cc-rs.
@@ -319,8 +319,8 @@ impl Build {
319
319
cargo_metadata : true ,
320
320
pic : None ,
321
321
static_crt : None ,
322
- warnings : true ,
323
- extra_warnings : true ,
322
+ warnings : None ,
323
+ extra_warnings : None ,
324
324
warnings_into_errors : false ,
325
325
}
326
326
}
@@ -597,8 +597,8 @@ impl Build {
597
597
/// .compile("libfoo.a");
598
598
/// ```
599
599
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) ;
602
602
self
603
603
}
604
604
@@ -620,7 +620,7 @@ impl Build {
620
620
/// .compile("libfoo.a");
621
621
/// ```
622
622
pub fn extra_warnings ( & mut self , warnings : bool ) -> & mut Build {
623
- self . extra_warnings = warnings;
623
+ self . extra_warnings = Some ( warnings) ;
624
624
self
625
625
}
626
626
@@ -1324,12 +1324,17 @@ impl Build {
1324
1324
cmd. args . push ( directory. into ( ) ) ;
1325
1325
}
1326
1326
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 } ) {
1328
1333
let wflags = cmd. family . warnings_flags ( ) . into ( ) ;
1329
1334
cmd. push_cc_arg ( wflags) ;
1330
1335
}
1331
1336
1332
- if self . extra_warnings {
1337
+ if self . extra_warnings . unwrap_or ( if self . has_flags ( ) { false } else { true } ) {
1333
1338
if let Some ( wflags) = cmd. family . extra_warnings_flags ( ) {
1334
1339
cmd. push_cc_arg ( wflags. into ( ) ) ;
1335
1340
}
@@ -1366,6 +1371,12 @@ impl Build {
1366
1371
Ok ( cmd)
1367
1372
}
1368
1373
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
+
1369
1380
fn msvc_macro_assembler ( & self ) -> Result < ( Command , String ) , Error > {
1370
1381
let target = self . get_target ( ) ?;
1371
1382
let tool = if target. contains ( "x86_64" ) {
0 commit comments