@@ -79,12 +79,12 @@ use crate::parse::{ClangItemParser, ParseError};
79
79
use crate :: regex_set:: RegexSet ;
80
80
81
81
use std:: borrow:: Cow ;
82
+ use std:: env;
82
83
use std:: fs:: { File , OpenOptions } ;
83
84
use std:: io:: { self , Write } ;
84
85
use std:: path:: { Path , PathBuf } ;
85
86
use std:: process:: { Command , Stdio } ;
86
87
use std:: rc:: Rc ;
87
- use std:: { env, iter} ;
88
88
89
89
// Some convenient typedefs for a fast hash map and hash set.
90
90
type HashMap < K , V > = :: rustc_hash:: FxHashMap < K , V > ;
@@ -222,12 +222,9 @@ impl Default for CodegenConfig {
222
222
/// End-users of the crate may need to set the `BINDGEN_EXTRA_CLANG_ARGS` environment variable to
223
223
/// add additional arguments. For example, to build against a different sysroot a user could set
224
224
/// `BINDGEN_EXTRA_CLANG_ARGS` to `--sysroot=/path/to/sysroot`.
225
- #[ derive( Debug , Default ) ]
225
+ #[ derive( Debug , Default , Clone ) ]
226
226
pub struct Builder {
227
227
options : BindgenOptions ,
228
- input_headers : Vec < String > ,
229
- // Tuples of unsaved file contents of the form (name, contents).
230
- input_header_contents : Vec < ( String , String ) > ,
231
228
}
232
229
233
230
/// Construct a new [`Builder`](./struct.Builder.html).
@@ -254,7 +251,7 @@ impl Builder {
254
251
pub fn command_line_flags ( & self ) -> Vec < String > {
255
252
let mut output_vector: Vec < String > = Vec :: new ( ) ;
256
253
257
- if let Some ( header) = self . input_headers . last ( ) . cloned ( ) {
254
+ if let Some ( header) = self . options . input_headers . last ( ) . cloned ( ) {
258
255
// Positional argument 'header'
259
256
output_vector. push ( header) ;
260
257
}
@@ -627,13 +624,13 @@ impl Builder {
627
624
output_vector. extend ( self . options . clang_args . iter ( ) . cloned ( ) ) ;
628
625
}
629
626
630
- if self . input_headers . len ( ) > 1 {
631
- // To pass more than one header, we need to pass all but the last
632
- // header via the `-include` clang arg
633
- for header in & self . input_headers [ ..self . input_headers . len ( ) - 1 ] {
634
- output_vector . push ( "-include" . to_string ( ) ) ;
635
- output_vector. push ( header . clone ( ) ) ;
636
- }
627
+ // To pass more than one header, we need to pass all but the last
628
+ // header via the `-include` clang arg
629
+ for header in & self . options . input_headers
630
+ [ ..self . options . input_headers . len ( ) . saturating_sub ( 1 ) ]
631
+ {
632
+ output_vector. push ( "-include" . to_string ( ) ) ;
633
+ output_vector . push ( header . clone ( ) ) ;
637
634
}
638
635
639
636
output_vector
@@ -662,7 +659,7 @@ impl Builder {
662
659
/// .unwrap();
663
660
/// ```
664
661
pub fn header < T : Into < String > > ( mut self , header : T ) -> Builder {
665
- self . input_headers . push ( header. into ( ) ) ;
662
+ self . options . input_headers . push ( header. into ( ) ) ;
666
663
self
667
664
}
668
665
@@ -691,7 +688,8 @@ impl Builder {
691
688
. to_str ( )
692
689
. expect ( "Cannot convert current directory name to string" )
693
690
. to_owned ( ) ;
694
- self . input_header_contents
691
+ self . options
692
+ . input_header_contents
695
693
. push ( ( absolute_path, contents. into ( ) ) ) ;
696
694
self
697
695
}
@@ -1566,20 +1564,18 @@ impl Builder {
1566
1564
self . options . clang_args . extend ( get_extra_clang_args ( ) ) ;
1567
1565
1568
1566
// Transform input headers to arguments on the clang command line.
1569
- self . options . input_header = self . input_headers . pop ( ) ;
1570
- self . options . extra_input_headers = self . input_headers ;
1571
1567
self . options . clang_args . extend (
1572
- self . options . extra_input_headers . iter ( ) . flat_map ( |header| {
1573
- iter :: once ( "-include" . into ( ) )
1574
- . chain ( iter:: once ( header . to_string ( ) ) )
1575
- } ) ,
1568
+ self . options . input_headers
1569
+ [ .. self . options . input_headers . len ( ) . saturating_sub ( 1 ) ]
1570
+ . iter ( )
1571
+ . flat_map ( |header| [ "-include" . into ( ) , header . to_string ( ) ] ) ,
1576
1572
) ;
1577
1573
1578
- let input_unsaved_files = self
1579
- . input_header_contents
1580
- . into_iter ( )
1581
- . map ( |( name, contents) | clang:: UnsavedFile :: new ( & name, & contents) )
1582
- . collect :: < Vec < _ > > ( ) ;
1574
+ let input_unsaved_files =
1575
+ std :: mem :: take ( & mut self . options . input_header_contents )
1576
+ . into_iter ( )
1577
+ . map ( |( name, contents) | clang:: UnsavedFile :: new ( name, contents) )
1578
+ . collect :: < Vec < _ > > ( ) ;
1583
1579
1584
1580
Bindings :: generate ( self . options , input_unsaved_files)
1585
1581
}
@@ -1606,7 +1602,7 @@ impl Builder {
1606
1602
let mut is_cpp = args_are_cpp ( & self . options . clang_args ) ;
1607
1603
1608
1604
// For each input header, add `#include "$header"`.
1609
- for header in & self . input_headers {
1605
+ for header in & self . options . input_headers {
1610
1606
is_cpp |= file_is_cpp ( header) ;
1611
1607
1612
1608
wrapper_contents. push_str ( "#include \" " ) ;
@@ -1616,7 +1612,7 @@ impl Builder {
1616
1612
1617
1613
// For each input header content, add a prefix line of `#line 0 "$name"`
1618
1614
// followed by the contents.
1619
- for & ( ref name, ref contents) in & self . input_header_contents {
1615
+ for & ( ref name, ref contents) in & self . options . input_header_contents {
1620
1616
is_cpp |= file_is_cpp ( name) ;
1621
1617
1622
1618
wrapper_contents. push_str ( "#line 0 \" " ) ;
@@ -1970,11 +1966,11 @@ struct BindgenOptions {
1970
1966
/// The set of arguments to pass straight through to Clang.
1971
1967
clang_args : Vec < String > ,
1972
1968
1973
- /// The input header file .
1974
- input_header : Option < String > ,
1969
+ /// The input header files .
1970
+ input_headers : Vec < String > ,
1975
1971
1976
- /// Any additional input header files .
1977
- extra_input_headers : Vec < String > ,
1972
+ /// Tuples of unsaved file contents of the form (name, contents) .
1973
+ input_header_contents : Vec < ( String , String ) > ,
1978
1974
1979
1975
/// A user-provided visitor to allow customizing different kinds of
1980
1976
/// situations.
@@ -2224,8 +2220,8 @@ impl Default for BindgenOptions {
2224
2220
raw_lines : vec ! [ ] ,
2225
2221
module_lines : HashMap :: default ( ) ,
2226
2222
clang_args : vec ! [ ] ,
2227
- input_header : None ,
2228
- extra_input_headers : vec ! [ ] ,
2223
+ input_headers : vec ! [ ] ,
2224
+ input_header_contents : Default :: default ( ) ,
2229
2225
parse_callbacks : None ,
2230
2226
codegen_config : CodegenConfig :: all ( ) ,
2231
2227
conservative_inline_namespaces : false ,
@@ -2470,7 +2466,7 @@ impl Bindings {
2470
2466
2471
2467
// Whether we are working with C or C++ inputs.
2472
2468
let is_cpp = args_are_cpp ( & options. clang_args ) ||
2473
- options. input_header . as_deref ( ) . map_or ( false , file_is_cpp) ;
2469
+ options. input_headers . iter ( ) . any ( |h| file_is_cpp ( h ) ) ;
2474
2470
2475
2471
let search_paths = if is_cpp {
2476
2472
clang. cpp_search_paths
@@ -2501,7 +2497,7 @@ impl Bindings {
2501
2497
true
2502
2498
}
2503
2499
2504
- if let Some ( h) = options. input_header . as_ref ( ) {
2500
+ if let Some ( h) = options. input_headers . last ( ) {
2505
2501
let path = Path :: new ( h) ;
2506
2502
if let Ok ( md) = std:: fs:: metadata ( path) {
2507
2503
if md. is_dir ( ) {
@@ -2512,14 +2508,15 @@ impl Bindings {
2512
2508
path. into ( ) ,
2513
2509
) ) ;
2514
2510
}
2515
- options. clang_args . push ( h. clone ( ) )
2511
+ let h = h. clone ( ) ;
2512
+ options. clang_args . push ( h) ;
2516
2513
} else {
2517
2514
return Err ( BindgenError :: NotExist ( path. into ( ) ) ) ;
2518
2515
}
2519
2516
}
2520
2517
2521
2518
for ( idx, f) in input_unsaved_files. iter ( ) . enumerate ( ) {
2522
- if idx != 0 || options. input_header . is_some ( ) {
2519
+ if idx != 0 || ! options. input_headers . is_empty ( ) {
2523
2520
options. clang_args . push ( "-include" . to_owned ( ) ) ;
2524
2521
}
2525
2522
options. clang_args . push ( f. name . to_str ( ) . unwrap ( ) . to_owned ( ) )
0 commit comments