-
Notifications
You must be signed in to change notification settings - Fork 71
/
build.rs
65 lines (57 loc) · 1.86 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
extern crate cc;
use std::{env, fs, process};
use std::error::Error;
use std::path::PathBuf;
fn main() {
let k = "LZ4_SYS_DELEGATE_LINKING";
println!("cargo:rerun-if-env-changed={}", k);
if env::var(k).is_ok() {
return;
}
match run() {
Ok(()) => (),
Err(err) => {
eprintln!("{}", err);
process::exit(1);
}
}
}
fn run() -> Result<(), Box<dyn Error>> {
let mut compiler = cc::Build::new();
compiler
.file("liblz4/lib/lz4.c")
.file("liblz4/lib/lz4frame.c")
.file("liblz4/lib/lz4hc.c")
.file("liblz4/lib/xxhash.c")
// We always compile the C with optimization, because otherwise it is 20x slower.
.opt_level(3);
match env::var("TARGET")
.map_err(|err| format!("reading TARGET environment variable: {}", err))?
.as_str()
{
"i686-pc-windows-gnu" => {
compiler
.flag("-fno-tree-vectorize");
},
_ => {}
}
compiler.compile("liblz4.a");
let src = env::current_dir()?.join("liblz4").join("lib");
let dst = PathBuf::from(env::var_os("OUT_DIR").ok_or("missing OUT_DIR environment variable")?);
let include = dst.join("include");
fs::create_dir_all(&include)
.map_err(|err| format!("creating directory {}: {}", include.display(), err))?;
for e in fs::read_dir(&src)? {
let e = e?;
let utf8_file_name = e.file_name().into_string()
.map_err(|_| format!("unable to convert file name {:?} to UTF-8", e.file_name()))?;
if utf8_file_name.ends_with(".h") {
let from = e.path();
let to = include.join(e.file_name());
fs::copy(&from, &to)
.map_err(|err| format!("copying {} to {}: {}", from.display(), to.display(), err))?;
}
}
println!("cargo:root={}", dst.display());
Ok(())
}