Skip to content

Commit b209d42

Browse files
author
bors-servo
authored
Auto merge of #953 - emilio:target, r=fitzgen
ir: Pass the target to clang if it wasn't explicitly passed already. Fixes #942 Fixes #947
2 parents d23db77 + 26da344 commit b209d42

File tree

1 file changed

+50
-37
lines changed

1 file changed

+50
-37
lines changed

src/ir/context.rs

+50-37
Original file line numberDiff line numberDiff line change
@@ -321,55 +321,69 @@ where
321321
}
322322
}
323323

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+
324347
impl<'ctx> BindgenContext<'ctx> {
325348
/// Construct the context for the given `options`.
326349
pub fn new(options: BindgenOptions) -> Self {
327350
use clang_sys;
328351

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+
329358
let index = clang::Index::new(false, true);
330359

331360
let parse_options =
332361
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");
340362

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+
};
360372

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+
};
366381

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 _.
369384
//
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 ___.
373387
let needs_mangling_hack = effective_target.contains("darwin") ||
374388
effective_target.contains("ios") ||
375389
effective_target == "i686-pc-win32";
@@ -882,7 +896,6 @@ impl<'ctx> BindgenContext<'ctx> {
882896
use syntax::codemap::{ExpnInfo, MacroBang, NameAndSpan};
883897
use syntax::ext::base;
884898
use syntax::parse;
885-
use std::mem;
886899

887900
let cfg = ExpansionConfig::default("xxx".to_owned());
888901
let sess = parse::ParseSess::new();

0 commit comments

Comments
 (0)