-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
121 lines (107 loc) · 3.92 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
119
120
121
extern crate pkg_config;
use std::fs;
use std::process::{Command, Stdio};
use std::env;
use std::path::Path;
fn main() {
let cargo_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let output_dir = env::var("OUT_DIR").unwrap();
let src = Path::new(&cargo_dir[..]);
let dst = Path::new(&output_dir[..]);
let target = env::var("TARGET").unwrap();
println!("target={}", target);
match target.find("-windows-") {
Some(..) => {
// do not build c-code on windows, use binaries
let prebuilt_dir = env::var("MILAGRO_DIR").unwrap();
println!("cargo:rustc-link-search=native={}", prebuilt_dir);
println!("cargo:rustc-flags=-L {}/lib \
-l libamcl_pairing \
-l libamcl_ecc \
-l libamcl_curve \
-l libamcl_core \
", prebuilt_dir);
println!("cargo:include={}/include", prebuilt_dir);
return;
},
None => {}
}
let libs = vec!["libamcl_pairing", "libamcl_ecc", "libamcl_curve", "libamcl_core"];
let mut found = true;
for l in libs {
println!("searching for {}", l);
match pkg_config::find_library(l) {
Ok(..) => {},
Err(..) => {
println!("pkg-config cannot find {}", l);
let mdata = fs::metadata(
&format!("{}/{}.a",
dst.join("pkg/lib").to_str().unwrap(),
l));
match mdata {
Ok(md) => {
if !md.is_file() {
found = false;
println!("not found");
break;
}
},
Err(..) => {
found = false;
println!("error getting metadata");
break;
}
}
}
}
}
if found {
println!("cargo:rustc-flags=-L {}/lib \
-l amcl_pairing \
-l amcl_ecc \
-l amcl_curve \
-l amcl_core \
", dst.join("pkg").display());
println!("cargo:root={}", dst.join("pkg").display());
println!("cargo:include={}/include", dst.join("pkg").display());
return;
}
let root = src.join("milagro-crypto-c");
let _ = fs::remove_dir_all(&dst.join("pkg"));
let _ = fs::remove_dir_all(&dst.join("build"));
fs::create_dir(&dst.join("build")).unwrap();
run(Command::new("sh")
.arg("-c")
.arg(&format!("cd {} && \
cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/ \
-DBUILD_SHARED_LIBS=OFF \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON {}",
dst.join("build").to_str().unwrap(),
root.as_path().to_str().unwrap()))
.current_dir(&dst.join("build")));
run(Command::new("make")
.arg(&format!("-j{}", env::var("NUM_JOBS").unwrap()))
.current_dir(&dst.join("build")));
run(Command::new("make")
.arg(&format!("-j{}", env::var("NUM_JOBS").unwrap()))
.arg("install")
.arg(&format!("DESTDIR={}", dst.join("pkg").to_str().unwrap()))
.current_dir(&dst.join("build")));
println!("cargo:rustc-flags=-L {}/lib \
-l amcl_pairing \
-l amcl_ecc \
-l amcl_curve \
-l amcl_core", dst.join("pkg").display());
println!("cargo:root={}", dst.join("pkg").display());
println!("cargo:include={}/include", dst.join("pkg").display());
}
fn run(cmd: &mut Command) {
println!("running: {:?}", cmd);
assert!(cmd.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.status()
.unwrap()
.success());
}