Skip to content

Commit 5444bab

Browse files
committed
Add a way to get the target triple, and do our best guess at it until we have proper clang support.
1 parent daf1935 commit 5444bab

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
mod codegen {
22
extern crate quasi_codegen;
33
use std::env;
4+
use std::fs::File;
5+
use std::io::Write;
46
use std::path::{Path, PathBuf};
57

68
pub fn main() {
@@ -13,6 +15,10 @@ mod codegen {
1315
println!("cargo:rerun-if-changed=src/codegen/error.rs");
1416
println!("cargo:rerun-if-changed=src/codegen/helpers.rs");
1517
println!("cargo:rerun-if-changed=src/codegen/struct_layout.rs");
18+
19+
let mut dst =
20+
File::create(Path::new(&out_dir).join("host-target.txt")).unwrap();
21+
dst.write_all(env::var("TARGET").unwrap().as_bytes()).unwrap();
1622
}
1723
}
1824

src/ir/context.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ pub struct BindgenContext<'ctx> {
139139
/// The active replacements collected from replaces="xxx" annotations.
140140
replacements: HashMap<Vec<String>, ItemId>,
141141

142+
/// The target string bindgen was able to deduce from the input.
143+
effective_target: String,
144+
142145
collected_typerefs: bool,
143146

144147
/// Dummy structures for code generation.
@@ -232,6 +235,32 @@ impl<'ctx> BindgenContext<'ctx> {
232235
parse_options)
233236
.expect("TranslationUnit::parse failed");
234237

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+
235264
let root_module = Self::build_root_module(ItemId(0));
236265
let mut me = BindgenContext {
237266
items: Default::default(),
@@ -244,6 +273,7 @@ impl<'ctx> BindgenContext<'ctx> {
244273
currently_parsed_types: vec![],
245274
parsed_macros: Default::default(),
246275
replacements: Default::default(),
276+
effective_target: effective_target.unwrap(),
247277
collected_typerefs: false,
248278
gen_ctx: None,
249279
span: DUMMY_SP,
@@ -764,6 +794,11 @@ impl<'ctx> BindgenContext<'ctx> {
764794
Item::new(id, None, None, id, ItemKind::Module(module))
765795
}
766796

797+
/// Returns the target triple bindgen is running over.
798+
pub fn target(&self) -> &str {
799+
&self.effective_target
800+
}
801+
767802
/// Get the root module.
768803
pub fn root_module(&self) -> ItemId {
769804
self.root_module

0 commit comments

Comments
 (0)