-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
89 lines (73 loc) · 2.75 KB
/
build.rs
File metadata and controls
89 lines (73 loc) · 2.75 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! 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 std::env;
use cbindgen::*;
#[cfg(target_os = "windows")]
extern crate embed_resource;
#[cfg(target_os = "windows")]
use std::fs;
fn embed_description() {
#[cfg(target_os = "windows")] {
let _name: &str = option_env!("CARGO_PKG_NAME").unwrap_or("");
let _version_major: &str = option_env!("CARGO_PKG_VERSION_MAJOR").unwrap_or("0");
let _version_minor: &str = option_env!("CARGO_PKG_VERSION_MINOR").unwrap_or("0");
let _version_patch: &str = option_env!("CARGO_PKG_VERSION_PATCH").unwrap_or("0");
let _authors: &str = option_env!("CARGO_PKG_AUTHORS").unwrap_or("");
let _description: &str = option_env!("CARGO_PKG_DESCRIPTION").unwrap_or("");
let _license: &str = option_env!("CARGO_PKG_LICENSE").unwrap_or("");
let data = format!("1 VERSIONINFO
FILEVERSION {_version_major},{_version_minor},{_version_patch}
BEGIN
BLOCK \"StringFileInfo\"
BEGIN
BLOCK \"040904b0\"
BEGIN
VALUE \"FileDescription\", \"{_description}\"
VALUE \"ProductName\", \"{_name}\"
VALUE \"ProductVersion\", \"{_version_major}.{_version_minor}.{_version_patch}\"
END
END
BLOCK \"VarFileInfo\"
BEGIN
VALUE \"Translation\", 0x409, 1200
END
END");
fs::write("version.rc", data).expect("Unable to write file");
embed_resource::compile("version.rc", embed_resource::NONE);
}
}
#[cfg(feature = "include-zip")]
use zip::ZipArchive;
#[cfg(feature = "include-zip")]
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 main() {
let examples_dir = PathBuf::from("Example");
#[cfg(feature = "include-zip")]
unzip_rules(&examples_dir);
write_headers(&examples_dir, "mathcat-c.h", Language::C);
write_headers(&examples_dir, "mathcat.h", Language::Cxx);
embed_description();
}
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);
}