-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
41 lines (35 loc) · 1.37 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
//! The build.rs file is necessary to unzip the rules.
//! rules.zip are needed so there is a way to get the rules dir into the build since you can't get it from the crate.
extern crate cbindgen;
use std::path::{PathBuf, Path};
use zip::ZipArchive;
use std::env;
use cbindgen::*;
fn main() {
let examples_dir = PathBuf::from("Example");
unzip_rules(&examples_dir);
write_headers(&examples_dir, "mathcat-c.h", Language::C);
write_headers(&examples_dir, "mathcat.h", Language::Cxx);
}
fn unzip_rules(location: &Path) {
let archive = libmathcat::ZIPPED_RULE_FILES;
let archive = std::io::Cursor::new(archive);
let mut zip_archive = ZipArchive::new(archive).unwrap();
zip_archive.extract(location).expect("Zip extraction failed");
}
fn write_headers(location: &Path, name: &str, header_style: cbindgen::Language) {
let config = cbindgen::Config::from_file(
if header_style == Language::Cxx {"cbindgen.toml"} else {"cbindgen-c.toml"}
).expect("couldn't read cbindgen.toml");
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let mut location = PathBuf::from(location);
location.push(name);
cbindgen::Builder::new()
.with_language(header_style)
.with_cpp_compat(true)
.with_config(config)
.with_crate(crate_dir)
.generate()
.expect("Unable to generate bindings")
.write_to_file(location);
}