-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.rs
118 lines (102 loc) · 3.85 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
extern crate bindgen;
extern crate cc;
use std::env;
use std::process::Command;
use rand::Rng;
use std::fs::{remove_file, File};
use std::io::Write;
use std::path::PathBuf;
fn add_def(v: &mut Vec<(String, String)>, key: &str, val: &str) {
v.push((key.to_owned(), val.to_owned()));
}
fn main() {
let mut defines = Vec::new();
for i in &[
"size_t",
"unsigned int",
"unsigned long",
"unsigned long long",
] {
let def_name = format!("SIZEOF_{}", i.to_uppercase().replace(" ", "_"));
defines.push((def_name, check_native_size(i)));
}
add_def(&mut defines, "SECONDARY_DJW", "1");
add_def(&mut defines, "SECONDARY_FGK", "1");
add_def(&mut defines, "EXTERNAL_COMPRESSION", "0");
add_def(&mut defines, "XD3_USE_LARGEFILE64", "1");
#[cfg(windows)]
add_def(&mut defines, "XD3_WIN32", "1");
add_def(&mut defines, "SHELL_TESTS", "0");
#[cfg(feature = "lzma")]
{
add_def(&mut defines, "SECONDARY_LZMA", "1");
pkg_config::Config::new().probe("liblzma").unwrap();
}
{
let mut builder = cc::Build::new();
builder.include("xdelta3/xdelta3");
for (key, val) in &defines {
builder.define(&key, Some(val.as_str()));
}
builder
.file("xdelta3/xdelta3/xdelta3.c")
.warnings(false)
.compile("xdelta3");
}
{
let mut builder = bindgen::Builder::default();
for (key, val) in &defines {
builder = builder.clang_arg(format!("-D{}={}", key, val));
}
let bindings = builder
.header("xdelta3/xdelta3/xdelta3.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.whitelist_function("xd3_.*")
.whitelist_type("xd3_.*")
.rustified_enum("xd3_.*")
.generate()
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
}
fn check_native_size(name: &str) -> String {
let builder = cc::Build::new();
let out_dir = env::var("OUT_DIR").unwrap();
let compiler = builder.get_compiler();
let mut compile = Command::new(compiler.path().as_os_str());
let test_code = format!("#include <stdint.h>\n#include <stdio.h>\nint main() {{printf(\"%lu\", sizeof({})); return 0;}}\n", name);
// didn't use tempfile since tempfile was having issues on Windows
let mut rng = rand::thread_rng();
let test_binary_fn = format!("{}/test-{}", out_dir, rng.gen::<i32>());
#[cfg(windows)]
let test_binary_fn = format!("{}.exe", test_binary_fn);
let test_source_fn = format!("{}/test-{:x}.c", out_dir, rng.gen::<i32>());
let mut test_source = File::create(&test_source_fn).expect("Error creating test compile files");
compile.args(compiler.args()).current_dir(out_dir);
if compiler.is_like_msvc() {
compile.args(&[&test_source_fn, &format!("/Fe{}", test_binary_fn)]);
} else {
compile.args(&[&test_source_fn, "-o", &test_binary_fn]);
}
test_source
.write_all(test_code.as_bytes())
.expect("Error writing test compile files");
drop(test_source); // close the source file, otherwise there will be problems on Windows
for &(ref a, ref b) in compiler.env().iter() {
compile.env(a, b);
}
compile.output().expect("Error compiling test source");
remove_file(test_source_fn).ok();
compile = Command::new(&test_binary_fn);
let output = compile
.output()
.expect("Error executing test binary")
.stdout;
let output = String::from_utf8(output).expect("Error converting Unicode sequence");
remove_file(test_binary_fn).ok();
return output;
}