@@ -78,6 +78,7 @@ pub use crate::regex_set::RegexSet;
78
78
79
79
use std:: borrow:: Cow ;
80
80
use std:: env;
81
+ use std:: ffi:: OsStr ;
81
82
use std:: fs:: { File , OpenOptions } ;
82
83
use std:: io:: { self , Write } ;
83
84
use std:: path:: { Path , PathBuf } ;
@@ -95,10 +96,10 @@ pub const DEFAULT_ANON_FIELDS_PREFIX: &str = "__bindgen_anon_";
95
96
const DEFAULT_NON_EXTERN_FNS_SUFFIX : & str = "__extern" ;
96
97
97
98
fn file_is_cpp ( name_file : & str ) -> bool {
98
- name_file. ends_with ( ".hpp" ) ||
99
- name_file. ends_with ( ".hxx" ) ||
100
- name_file. ends_with ( ".hh" ) ||
101
- name_file. ends_with ( ".h++" )
99
+ name_file. ends_with ( ".hpp" )
100
+ || name_file. ends_with ( ".hxx" )
101
+ || name_file. ends_with ( ".hh" )
102
+ || name_file. ends_with ( ".h++" )
102
103
}
103
104
104
105
fn args_are_cpp ( clang_args : & [ String ] ) -> bool {
@@ -276,13 +277,18 @@ pub fn builder() -> Builder {
276
277
Default :: default ( )
277
278
}
278
279
279
- fn get_extra_clang_args ( ) -> Vec < String > {
280
+ fn get_extra_clang_args (
281
+ parse_callbacks : & [ Rc < dyn callbacks:: ParseCallbacks > ] ,
282
+ ) -> Vec < String > {
280
283
// Add any extra arguments from the environment to the clang command line.
281
- let extra_clang_args =
282
- match get_target_dependent_env_var ( "BINDGEN_EXTRA_CLANG_ARGS" ) {
283
- None => return vec ! [ ] ,
284
- Some ( s) => s,
285
- } ;
284
+ let extra_clang_args = match get_target_dependent_env_var (
285
+ parse_callbacks,
286
+ "BINDGEN_EXTRA_CLANG_ARGS" ,
287
+ ) {
288
+ None => return vec ! [ ] ,
289
+ Some ( s) => s,
290
+ } ;
291
+
286
292
// Try to parse it with shell quoting. If we fail, make it one single big argument.
287
293
if let Some ( strings) = shlex:: split ( & extra_clang_args) {
288
294
return strings;
@@ -421,8 +427,8 @@ impl Builder {
421
427
// FIXME(emilio): This is a bit hacky, maybe we should stop re-using the
422
428
// RustFeatures to store the "disable_untagged_union" call, and make it
423
429
// a different flag that we check elsewhere / in generate().
424
- if !rust_features. untagged_union &&
425
- RustFeatures :: from ( * rust_target) . untagged_union
430
+ if !rust_features. untagged_union
431
+ && RustFeatures :: from ( * rust_target) . untagged_union
426
432
{
427
433
output_vector. push ( "--disable-untagged-union" . into ( ) ) ;
428
434
}
@@ -1731,7 +1737,9 @@ impl Builder {
1731
1737
/// Generate the Rust bindings using the options built up thus far.
1732
1738
pub fn generate ( mut self ) -> Result < Bindings , BindgenError > {
1733
1739
// Add any extra arguments from the environment to the clang command line.
1734
- self . options . clang_args . extend ( get_extra_clang_args ( ) ) ;
1740
+ self . options
1741
+ . clang_args
1742
+ . extend ( get_extra_clang_args ( & self . options . parse_callbacks ) ) ;
1735
1743
1736
1744
// Transform input headers to arguments on the clang command line.
1737
1745
self . options . clang_args . extend (
@@ -1814,7 +1822,7 @@ impl Builder {
1814
1822
cmd. arg ( a) ;
1815
1823
}
1816
1824
1817
- for a in get_extra_clang_args ( ) {
1825
+ for a in get_extra_clang_args ( & self . options . parse_callbacks ) {
1818
1826
cmd. arg ( a) ;
1819
1827
}
1820
1828
@@ -2646,8 +2654,8 @@ fn rust_to_clang_target(rust_target: &str) -> String {
2646
2654
rust_target. strip_suffix ( "-espidf" ) . unwrap ( ) . to_owned ( ) ;
2647
2655
clang_target. push_str ( "-elf" ) ;
2648
2656
if clang_target. starts_with ( "riscv32imc-" ) {
2649
- clang_target = "riscv32-" . to_owned ( ) +
2650
- clang_target. strip_prefix ( "riscv32imc-" ) . unwrap ( ) ;
2657
+ clang_target = "riscv32-" . to_owned ( )
2658
+ + clang_target. strip_prefix ( "riscv32imc-" ) . unwrap ( ) ;
2651
2659
}
2652
2660
return clang_target;
2653
2661
}
@@ -2742,8 +2750,8 @@ impl Bindings {
2742
2750
return false ;
2743
2751
}
2744
2752
2745
- if arg. starts_with ( "-I" ) ||
2746
- arg. starts_with ( "--include-directory=" )
2753
+ if arg. starts_with ( "-I" )
2754
+ || arg. starts_with ( "--include-directory=" )
2747
2755
{
2748
2756
return false ;
2749
2757
}
@@ -2770,8 +2778,8 @@ impl Bindings {
2770
2778
debug ! ( "Found clang: {:?}" , clang) ;
2771
2779
2772
2780
// Whether we are working with C or C++ inputs.
2773
- let is_cpp = args_are_cpp ( & options. clang_args ) ||
2774
- options. input_headers . iter ( ) . any ( |h| file_is_cpp ( h) ) ;
2781
+ let is_cpp = args_are_cpp ( & options. clang_args )
2782
+ || options. input_headers . iter ( ) . any ( |h| file_is_cpp ( h) ) ;
2775
2783
2776
2784
let search_paths = if is_cpp {
2777
2785
clang. cpp_search_paths
@@ -3140,22 +3148,38 @@ pub fn clang_version() -> ClangVersion {
3140
3148
}
3141
3149
}
3142
3150
3151
+ fn env_var < K : AsRef < str > + AsRef < OsStr > > (
3152
+ parse_callbacks : & [ Rc < dyn callbacks:: ParseCallbacks > ] ,
3153
+ key : K ,
3154
+ ) -> Result < String , std:: env:: VarError > {
3155
+ for callback in parse_callbacks {
3156
+ callback. read_env_var ( key. as_ref ( ) ) ;
3157
+ }
3158
+ std:: env:: var ( key)
3159
+ }
3160
+
3143
3161
/// Looks for the env var `var_${TARGET}`, and falls back to just `var` when it is not found.
3144
- fn get_target_dependent_env_var ( var : & str ) -> Option < String > {
3145
- if let Ok ( target) = env:: var ( "TARGET" ) {
3146
- if let Ok ( v) = env:: var ( format ! ( "{}_{}" , var, target) ) {
3162
+ fn get_target_dependent_env_var (
3163
+ parse_callbacks : & [ Rc < dyn callbacks:: ParseCallbacks > ] ,
3164
+ var : & str ,
3165
+ ) -> Option < String > {
3166
+ if let Ok ( target) = env_var ( parse_callbacks, "TARGET" ) {
3167
+ if let Ok ( v) = env_var ( parse_callbacks, format ! ( "{}_{}" , var, target) ) {
3147
3168
return Some ( v) ;
3148
3169
}
3149
- if let Ok ( v) = env:: var ( format ! ( "{}_{}" , var, target. replace( '-' , "_" ) ) )
3150
- {
3170
+ if let Ok ( v) = env_var (
3171
+ parse_callbacks,
3172
+ format ! ( "{}_{}" , var, target. replace( '-' , "_" ) ) ,
3173
+ ) {
3151
3174
return Some ( v) ;
3152
3175
}
3153
3176
}
3154
- env:: var ( var) . ok ( )
3177
+
3178
+ env_var ( parse_callbacks, var) . ok ( )
3155
3179
}
3156
3180
3157
3181
/// A ParseCallbacks implementation that will act on file includes by echoing a rerun-if-changed
3158
- /// line
3182
+ /// line and on env variable usage by echoing a rerun-if-env-changed line
3159
3183
///
3160
3184
/// When running inside a `build.rs` script, this can be used to make cargo invalidate the
3161
3185
/// generated bindings whenever any of the files included from the header change:
@@ -3173,6 +3197,10 @@ impl callbacks::ParseCallbacks for CargoCallbacks {
3173
3197
fn include_file ( & self , filename : & str ) {
3174
3198
println ! ( "cargo:rerun-if-changed={}" , filename) ;
3175
3199
}
3200
+
3201
+ fn read_env_var ( & self , key : & str ) {
3202
+ println ! ( "cargo:rerun-if-env-changed={}" , key) ;
3203
+ }
3176
3204
}
3177
3205
3178
3206
/// Test command_line_flag function.
0 commit comments