@@ -139,6 +139,9 @@ pub struct BindgenContext<'ctx> {
139
139
/// The active replacements collected from replaces="xxx" annotations.
140
140
replacements : HashMap < Vec < String > , ItemId > ,
141
141
142
+ /// The target string bindgen was able to deduce from the input.
143
+ effective_target : String ,
144
+
142
145
collected_typerefs : bool ,
143
146
144
147
/// Dummy structures for code generation.
@@ -232,6 +235,32 @@ impl<'ctx> BindgenContext<'ctx> {
232
235
parse_options)
233
236
. expect ( "TranslationUnit::parse failed" ) ;
234
237
238
+ // TODO(emilio): Use the CXTargetInfo here when available.
239
+ //
240
+ // see: https://reviews.llvm.org/D32389
241
+ let mut effective_target = None ;
242
+ for opt in & options. clang_args {
243
+ if opt. starts_with ( "--target=" ) {
244
+ let mut split = opt. split ( '=' ) ;
245
+ split. next ( ) ;
246
+ effective_target = Some ( split. next ( ) . unwrap ( ) . to_owned ( ) ) ;
247
+ break ;
248
+ }
249
+ }
250
+
251
+ if effective_target. is_none ( ) {
252
+ use std:: env;
253
+ // If we're running from a build script, try to find the cargo
254
+ // target.
255
+ effective_target = env:: var ( "TARGET" ) . ok ( ) ;
256
+ }
257
+
258
+ if effective_target. is_none ( ) {
259
+ const HOST_TARGET : & ' static str =
260
+ include_str ! ( concat!( env!( "OUT_DIR" ) , "/host-target.txt" ) ) ;
261
+ effective_target = Some ( HOST_TARGET . to_owned ( ) ) ;
262
+ }
263
+
235
264
let root_module = Self :: build_root_module ( ItemId ( 0 ) ) ;
236
265
let mut me = BindgenContext {
237
266
items : Default :: default ( ) ,
@@ -244,6 +273,7 @@ impl<'ctx> BindgenContext<'ctx> {
244
273
currently_parsed_types : vec ! [ ] ,
245
274
parsed_macros : Default :: default ( ) ,
246
275
replacements : Default :: default ( ) ,
276
+ effective_target : effective_target. unwrap ( ) ,
247
277
collected_typerefs : false ,
248
278
gen_ctx : None ,
249
279
span : DUMMY_SP ,
@@ -764,6 +794,11 @@ impl<'ctx> BindgenContext<'ctx> {
764
794
Item :: new ( id, None , None , id, ItemKind :: Module ( module) )
765
795
}
766
796
797
+ /// Returns the target triple bindgen is running over.
798
+ pub fn target ( & self ) -> & str {
799
+ & self . effective_target
800
+ }
801
+
767
802
/// Get the root module.
768
803
pub fn root_module ( & self ) -> ItemId {
769
804
self . root_module
0 commit comments