-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.rs
72 lines (54 loc) · 1.95 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
extern crate syn;
use std::env;
use std::fs::File;
use std::io::Read;
use std::io::Write;
use std::path::Path;
pub const KDB_SYM_FILE: &'static str = "symbols.rs";
fn parse_fns(fns: &syn::ItemForeignMod) -> Vec<String> {
let mut functions = vec![];
for item in &fns.items {
match item {
syn::ForeignItem::Fn(ref item_fn) => {
let mut sym: String = "_".to_owned();
let ident = item_fn.ident.to_string();
sym.push_str(&ident);
functions.push(sym);
}
_ => (),
}
}
functions
}
fn ksyms(fname: &str) -> Vec<String> {
let mut src = String::new();
let mut file = File::open(&fname).expect("Unable to open file");
file.read_to_string(&mut src).expect("Unable to read file");
let syntax = syn::parse_file(&src).expect("Unable to parse file");
syntax.items.iter().map(|item| match item {
syn::Item::ForeignMod(ref item_fn) => parse_fns(item_fn),
_ => vec![],
})
.flat_map(|x| x)
.collect()
}
fn sym_file() -> std::path::PathBuf {
let out_dir = env::var("OUT_DIR").expect("OUT_DIR environment variable not provided");
Path::new(&out_dir).join(KDB_SYM_FILE)
}
fn main() {
let base = env::current_dir().unwrap();
let src = base.join("src").join("k.rs");
let syms = ksyms(src.to_str().unwrap());
let dest_path = sym_file();
let mut f = File::create(&dest_path)
.expect("Could not create file to store external KDB symbols");
writeln!(f, "#[allow(dead_code)]")
.expect("Could not write to KDB symbol file header");
writeln!(f, "pub const SYMBOLS: [&'static str; {}] = {:?};", syms.len(), syms)
.expect("Could not write to KDB symbol file");
f.flush()
.expect("Could not flush write to KDB symbol");
let dir = base.join("src").join("c");
println!("cargo:rustc-link-search={}", dir.to_str().unwrap());
}