@@ -321,55 +321,69 @@ where
321
321
}
322
322
}
323
323
324
+ /// Returns the effective target, and whether it was explicitly specified on the
325
+ /// clang flags.
326
+ fn find_effective_target ( clang_args : & [ String ] ) -> ( String , bool ) {
327
+ use std:: env;
328
+
329
+ for opt in clang_args {
330
+ if opt. starts_with ( "--target=" ) {
331
+ let mut split = opt. split ( '=' ) ;
332
+ split. next ( ) ;
333
+ return ( split. next ( ) . unwrap ( ) . to_owned ( ) , true ) ;
334
+ }
335
+ }
336
+
337
+ // If we're running from a build script, try to find the cargo target.
338
+ if let Ok ( t) = env:: var ( "TARGET" ) {
339
+ return ( t, false )
340
+ }
341
+
342
+ const HOST_TARGET : & ' static str =
343
+ include_str ! ( concat!( env!( "OUT_DIR" ) , "/host-target.txt" ) ) ;
344
+ ( HOST_TARGET . to_owned ( ) , false )
345
+ }
346
+
324
347
impl < ' ctx > BindgenContext < ' ctx > {
325
348
/// Construct the context for the given `options`.
326
349
pub fn new ( options : BindgenOptions ) -> Self {
327
350
use clang_sys;
328
351
352
+ // TODO(emilio): Use the CXTargetInfo here when available.
353
+ //
354
+ // see: https://reviews.llvm.org/D32389
355
+ let ( effective_target, explicit_target) =
356
+ find_effective_target ( & options. clang_args ) ;
357
+
329
358
let index = clang:: Index :: new ( false , true ) ;
330
359
331
360
let parse_options =
332
361
clang_sys:: CXTranslationUnit_DetailedPreprocessingRecord ;
333
- let translation_unit = clang:: TranslationUnit :: parse (
334
- & index,
335
- "" ,
336
- & options. clang_args ,
337
- & options. input_unsaved_files ,
338
- parse_options,
339
- ) . expect ( "TranslationUnit::parse failed" ) ;
340
362
341
- // TODO(emilio): Use the CXTargetInfo here when available.
342
- //
343
- // see: https://reviews.llvm.org/D32389
344
- let mut effective_target = None ;
345
- for opt in & options. clang_args {
346
- if opt. starts_with ( "--target=" ) {
347
- let mut split = opt. split ( '=' ) ;
348
- split. next ( ) ;
349
- effective_target = Some ( split. next ( ) . unwrap ( ) . to_owned ( ) ) ;
350
- break ;
351
- }
352
- }
353
-
354
- if effective_target. is_none ( ) {
355
- use std:: env;
356
- // If we're running from a build script, try to find the cargo
357
- // target.
358
- effective_target = env:: var ( "TARGET" ) . ok ( ) ;
359
- }
363
+ let translation_unit = {
364
+ let clang_args = if explicit_target {
365
+ Cow :: Borrowed ( & options. clang_args )
366
+ } else {
367
+ let mut args = Vec :: with_capacity ( options. clang_args . len ( ) + 1 ) ;
368
+ args. push ( format ! ( "--target={}" , effective_target) ) ;
369
+ args. extend_from_slice ( & options. clang_args ) ;
370
+ Cow :: Owned ( args)
371
+ } ;
360
372
361
- if effective_target. is_none ( ) {
362
- const HOST_TARGET : & ' static str =
363
- include_str ! ( concat!( env!( "OUT_DIR" ) , "/host-target.txt" ) ) ;
364
- effective_target = Some ( HOST_TARGET . to_owned ( ) ) ;
365
- }
373
+ clang:: TranslationUnit :: parse (
374
+ & index,
375
+ "" ,
376
+ & clang_args,
377
+ & options. input_unsaved_files ,
378
+ parse_options,
379
+ ) . expect ( "TranslationUnit::parse failed" )
380
+ } ;
366
381
367
- // Mac os, iOS and Win32 need __ for mangled symbols but rust will automatically
368
- // prepend the extra _.
382
+ // Mac os, iOS and Win32 need __ for mangled symbols but rust will
383
+ // automatically prepend the extra _.
369
384
//
370
- // We need to make sure that we don't include __ because rust will turn into
371
- // ___.
372
- let effective_target = effective_target. unwrap ( ) ;
385
+ // We need to make sure that we don't include __ because rust will turn
386
+ // into ___.
373
387
let needs_mangling_hack = effective_target. contains ( "darwin" ) ||
374
388
effective_target. contains ( "ios" ) ||
375
389
effective_target == "i686-pc-win32" ;
@@ -882,7 +896,6 @@ impl<'ctx> BindgenContext<'ctx> {
882
896
use syntax:: codemap:: { ExpnInfo , MacroBang , NameAndSpan } ;
883
897
use syntax:: ext:: base;
884
898
use syntax:: parse;
885
- use std:: mem;
886
899
887
900
let cfg = ExpansionConfig :: default ( "xxx" . to_owned ( ) ) ;
888
901
let sess = parse:: ParseSess :: new ( ) ;
0 commit comments