Skip to content

Commit

Permalink
Simplify parse_tu
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwu committed Sep 3, 2018
1 parent 83e3616 commit 652dbfb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
42 changes: 24 additions & 18 deletions rustkit_bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,15 +1118,18 @@ pub fn bind_framework(
include_path.push(&format!("{}.h", framework_name));
let sdk_path_str = sdk_path.to_str().unwrap();
let idx = walker::Index::new().unwrap();
let tu =
idx.parse_tu(&[
"-ObjC",
"-fobjc-arc",
"-fno-objc-exceptions",
"-fobjc-abi-version=2",
&format!("-F{}/System/Library/Frameworks", sdk_path_str),
&format!("-I{}/usr/include", sdk_path_str),
], &include_path).unwrap();
let framework_include = format!("-F{}/System/Library/Frameworks", sdk_path_str);
let system_include_path = format!("-I{}/usr/include", sdk_path_str);
let args = vec![
"-ObjC",
"-fobjc-arc",
"-fno-objc-exceptions",
"-fobjc-abi-version=2",
&framework_include,
&system_include_path,
include_path.to_str().unwrap(),
];
let tu = idx.parse_tu(&args).unwrap();
let mut out_path = out_dir.to_owned();
out_path.push(&format!("{}.rs", framework_name));
bind_tu(&tu, &framework_path, Some(framework_name), &out_path)
Expand All @@ -1143,15 +1146,18 @@ pub fn bind_file(

let sdk_path_str = sdk_path.to_str().unwrap();
let idx = walker::Index::new().unwrap();
let tu =
idx.parse_tu(&[
"-ObjC",
"-fobjc-arc",
"-fno-objc-exceptions",
"-fobjc-abi-version=2",
&format!("-F{}/System/Library/Frameworks", sdk_path_str),
&format!("-I{}/usr/include", sdk_path_str),
], &header_path).unwrap();
let framework_include = format!("-F{}/System/Library/Frameworks", sdk_path_str);
let system_include_path = format!("-I{}/usr/include", sdk_path_str);
let args = vec![
"-ObjC",
"-fobjc-arc",
"-fno-objc-exceptions",
"-fobjc-abi-version=2",
&framework_include,
&system_include_path,
header_path.to_str().unwrap(),
];
let tu = idx.parse_tu(&args).unwrap();
let mut out_path = out_dir.to_owned();
out_path.push(&format!("{}.rs", header_path.file_stem().unwrap().to_str().unwrap()));
bind_tu(&tu, &header_path, None, &out_path);
Expand Down
7 changes: 3 additions & 4 deletions rustkit_bindgen/src/walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// except according to those terms.

use std::ptr;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::marker::PhantomData;
use std::mem;
use std::ffi::{CStr, CString};
Expand Down Expand Up @@ -1467,16 +1467,15 @@ impl Index {
})
}

pub fn parse_tu(&self, args: &[&str], p: &Path) ->
pub fn parse_tu(&self, args: &[&str]) ->
Option<TranslationUnit> {
let cstrargs: Vec<_> = args.iter().map(|s| CString::new(s.as_bytes()).unwrap()).collect();
let cargs: Vec<_> = cstrargs.iter().map(|s| s.as_bytes().as_ptr()).collect();
let file = CString::new(p.to_str()?.as_bytes()).unwrap();
let mut tu: CXTranslationUnit = ptr::null_mut();
let ret = unsafe {
clang_parseTranslationUnit2(
self.idx,
file.as_bytes().as_ptr() as *const _,
ptr::null(),
cargs.as_ptr() as _, cargs.len() as i32,
ptr::null_mut(), 0,
CXTranslationUnit_IncludeAttributedTypes |
Expand Down

0 comments on commit 652dbfb

Please sign in to comment.