File tree 3 files changed +46
-0
lines changed
3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ quote = { version = "0.6", default-features = false }
55
55
regex = " 1.0"
56
56
which = " >=1.0, <3.0"
57
57
hashbrown = " 0.1"
58
+ shlex = " 0.1"
58
59
# New validation in 0.3.6 breaks bindgen-integration:
59
60
# https://github.com/alexcrichton/proc-macro2/commit/489c642.
60
61
proc-macro2 = { version = " 0.4" , default-features = false }
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ extern crate peeking_take_while;
31
31
extern crate quote;
32
32
extern crate proc_macro2;
33
33
extern crate regex;
34
+ extern crate shlex;
34
35
extern crate which;
35
36
36
37
#[ cfg( feature = "logging" ) ]
@@ -1168,6 +1169,17 @@ impl Builder {
1168
1169
1169
1170
/// Generate the Rust bindings using the options built up thus far.
1170
1171
pub fn generate ( mut self ) -> Result < Bindings , ( ) > {
1172
+ // Add any extra arguments from the environment to the clang command line.
1173
+ if let Some ( extra_clang_args) = std:: env:: var ( "BINDGEN_EXTRA_CLANG_ARGS" ) . ok ( ) {
1174
+ // Try to parse it with shell quoting. If we fail, make it one single big argument.
1175
+ if let Some ( strings) = shlex:: split ( & extra_clang_args) {
1176
+ self . options . clang_args . extend ( strings) ;
1177
+ } else {
1178
+ self . options . clang_args . push ( extra_clang_args) ;
1179
+ } ;
1180
+ }
1181
+
1182
+ // Transform input headers to arguments on the clang command line.
1171
1183
self . options . input_header = self . input_headers . pop ( ) ;
1172
1184
self . options . clang_args . extend (
1173
1185
self . input_headers
Original file line number Diff line number Diff line change @@ -309,6 +309,39 @@ macro_rules! test_header {
309
309
// This file is generated by build.rs
310
310
include ! ( concat!( env!( "OUT_DIR" ) , "/tests.rs" ) ) ;
311
311
312
+ #[ test]
313
+ fn test_clang_env_args ( ) {
314
+ std:: env:: set_var ( "BINDGEN_EXTRA_CLANG_ARGS" , "-D_ENV_ONE=1 -D_ENV_TWO=\" 2 -DNOT_THREE=1\" " ) ;
315
+ let actual = builder ( )
316
+ . header_contents ( "test.hpp" ,
317
+ "#ifdef _ENV_ONE\n extern const int x[] = { 42 };\n #endif\n \
318
+ #ifdef _ENV_TWO\n extern const int y[] = { 42 };\n #endif\n \
319
+ #ifdef NOT_THREE\n extern const int z[] = { 42 };\n #endif\n ")
320
+ . generate ( )
321
+ . unwrap ( )
322
+ . to_string ( ) ;
323
+
324
+ let ( actual, stderr) = rustfmt ( actual) ;
325
+ println ! ( "{}" , stderr) ;
326
+
327
+ let ( expected, _) = rustfmt ( "/* automatically generated by rust-bindgen */
328
+
329
+ extern \" C\" {
330
+ #[link_name = \" \\ u{1}x\" ]
331
+ pub static mut x: [::std::os::raw::c_int; 1usize];
332
+ }
333
+ extern \" C\" {
334
+ #[link_name = \" \\ u{1}y\" ]
335
+ pub static mut y: [::std::os::raw::c_int; 1usize];
336
+ }
337
+ " . to_string ( ) ) ;
338
+
339
+ assert_eq ! (
340
+ expected,
341
+ actual
342
+ ) ;
343
+ }
344
+
312
345
#[ test]
313
346
fn test_header_contents ( ) {
314
347
let actual = builder ( )
You can’t perform that action at this time.
0 commit comments