-
Notifications
You must be signed in to change notification settings - Fork 19
/
build.rs
294 lines (261 loc) · 11.2 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// jkcoxson
extern crate bindgen;
use std::{env, fs::canonicalize, path::PathBuf};
fn main() {
// Tell cargo to invalidate the built crate whenever build files change
println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rerun-if-changed=build.rs");
////////////////////////////
// BINDGEN GENERATION //
////////////////////////////
if cfg!(feature = "pls-generate") {
// Get gnutls path per OS
let gnutls_path = match env::consts::OS {
"linux" => "/usr/include",
"macos" => "/opt/homebrew/include",
"windows" => {
panic!("Generating bindings on Windows is broken, pls remove the pls-generate feature.");
}
_ => panic!("Unsupported OS"),
};
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("wrapper.h")
// Include in clang build
.clang_arg(format!("-I{}", gnutls_path))
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.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!");
}
if cfg!(feature = "vendored") {
// Change current directory to OUT_DIR
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
env::set_current_dir(&out_path).unwrap();
env::set_var("PKG_CONFIG_PATH", out_path.join("lib/pkgconfig"));
// Lib path setup
let lib_path = out_path.join("lib");
if !lib_path.exists() {
// Create lib directory
std::fs::create_dir(&lib_path).unwrap();
}
let mut lib_path = lib_path.canonicalize().unwrap().display().to_string();
// Set the LD_LIBRARY_PATH environment variable to lib_path
env::set_var("LD_LIBRARY_PATH", lib_path.clone());
// Include path setup
let include_path = out_path.join("include");
if !include_path.exists() {
// Create include directory
std::fs::create_dir(&include_path).unwrap();
}
let mut include_path = include_path.canonicalize().unwrap().display().to_string();
// Search for where openssl-src placed my libs
env::set_current_dir("../../").unwrap();
let mut openssl_found = false;
loop {
for path in std::fs::read_dir(".").unwrap() {
let path = path.unwrap().path();
if !path.is_dir() {
continue;
}
if path.to_str().unwrap().contains("openssl-sys") {
let install_path = path.join("out").join("openssl-build").join("install");
if install_path.exists() {
println!(
"cargo:rustc-link-search=native={}",
install_path.join("lib").canonicalize().unwrap().display()
);
include_path = format!(
"{} -I{}",
include_path,
install_path
.join("include")
.canonicalize()
.unwrap()
.display()
);
lib_path = format!(
"{} -L{}",
lib_path,
install_path.join("lib").canonicalize().unwrap().display()
);
// Copy the pkgconfig files to the OUT_DIR
let ssl_pkg_config_path = install_path.join("lib").join("pkgconfig");
// Create the path
std::fs::create_dir_all(&ssl_pkg_config_path).unwrap();
let ssl_pkg_config_path = ssl_pkg_config_path.canonicalize().unwrap();
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap())
.join("lib")
.join("pkgconfig");
std::fs::create_dir_all(&out_path).unwrap();
let out_path = out_path.canonicalize().unwrap();
for path in std::fs::read_dir(&ssl_pkg_config_path).unwrap() {
let path = path.unwrap().path();
if !path.is_file() {
continue;
}
let file_name = path.file_name().unwrap().to_str().unwrap();
std::fs::copy(&path, out_path.join(file_name)).unwrap();
}
openssl_found = true;
}
}
}
if openssl_found {
break;
}
std::thread::sleep(std::time::Duration::from_secs(10));
}
if !openssl_found {
panic!("\nopenssl-src was not found, exiting\n");
}
// Clone the vendored libraries
repo_setup("https://github.com/libimobiledevice/libplist.git");
repo_setup("https://github.com/libimobiledevice/libimobiledevice-glue.git");
repo_setup("https://github.com/libimobiledevice/libusbmuxd.git");
repo_setup("https://github.com/libimobiledevice/libtatsu.git");
repo_setup("https://github.com/libimobiledevice/libimobiledevice.git");
// Remove tools from libimobiledevice's makefile. There's no point to build them, and they cause errors on MacOS.
let mut makefile = std::fs::read_to_string("libimobiledevice/Makefile.in").unwrap();
makefile = makefile.replace("tools", "");
std::fs::write("libimobiledevice/Makefile.in", makefile).unwrap();
let mut c_flags = vec![];
let mut cxx_flags = vec![];
// If building for Windows, set the env var for mbedtls
if env::var("TARGET").unwrap().contains("windows") {
env::set_var("WINDOWS_BUILD", "1");
// Windows needs extra crap
println!("cargo:rustc-link-lib=dylib=iphlpapi");
println!("cargo:rustc-link-lib=dylib=shell32");
println!("cargo:rustc-link-lib=dylib=ole32");
}
if env::var("TARGET").unwrap().contains("apple") {
println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.13");
c_flags.push("-mmacosx-version-min=10.13".to_string());
cxx_flags.push("-mmacosx-version-min=10.13".to_string());
}
c_flags.push(format!("-L{} -I{}", lib_path, include_path));
// Build those bad bois
let mut dst = autotools::Config::new("libplist");
let mut dst = dst.without("cython", None);
for flag in &c_flags {
dst = dst.cflag(flag);
}
for flag in &cxx_flags {
dst = dst.cxxflag(flag);
}
let dst = dst.build();
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
let mut dst = autotools::Config::new("libimobiledevice-glue");
let dst = dst.without("cython", None);
let mut dst = dst.env("PKG_CONFIG_PATH", out_path.join("lib/pkgconfig"));
for flag in &c_flags {
dst = dst.cflag(flag);
}
for flag in &cxx_flags {
dst = dst.cxxflag(flag);
}
let dst = dst.build();
println!("cargo:rustc-link-search=native={}", dst.display());
let mut dst = autotools::Config::new("libusbmuxd");
let dst = dst.without("cython", None);
let mut dst = dst.env("PKG_CONFIG_PATH", out_path.join("lib/pkgconfig"));
for flag in &c_flags {
dst = dst.cflag(flag);
}
for flag in &cxx_flags {
dst = dst.cxxflag(flag);
}
let dst = dst.build();
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
let mut dst = autotools::Config::new("libtatsu");
let dst = dst.without("cython", None);
let mut dst = dst.env("PKG_CONFIG_PATH", out_path.join("lib/pkgconfig"));
for flag in &c_flags {
dst = dst.cflag(flag);
}
for flag in &cxx_flags {
dst = dst.cxxflag(flag);
}
let dst = dst.build();
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
let mut dst = autotools::Config::new("libimobiledevice");
let dst = dst.without("cython", None);
let mut dst = dst.env("PKG_CONFIG_PATH", out_path.join("lib/pkgconfig"));
for flag in &c_flags {
dst = dst.cflag(flag);
}
for flag in &cxx_flags {
dst = dst.cxxflag(flag);
}
let dst = dst.build();
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
} else {
// Check if folder ./override exists
let override_path = PathBuf::from("./override").join(env::var("TARGET").unwrap());
if override_path.exists() {
println!(
"cargo:rustc-link-search={}",
canonicalize(&override_path).unwrap().display()
);
}
println!("cargo:rustc-link-search=/usr/local/lib");
println!("cargo:rustc-link-search=/usr/lib");
println!("cargo:rustc-link-search=/opt/homebrew/lib");
println!("cargo:rustc-link-search=/usr/local/opt/libimobiledevice/lib");
println!("cargo:rustc-link-search=/usr/local/opt/libusbmuxd/lib");
println!("cargo:rustc-link-search=/usr/local/opt/libimobiledevice-glue/lib");
}
let location_determinator = if cfg!(feature = "static") {
"static"
} else {
"dylib"
};
// Link libi* deps
println!(
"cargo:rustc-link-lib={}=imobiledevice-1.0",
location_determinator
);
println!("cargo:rustc-link-lib={}=usbmuxd-2.0", location_determinator);
println!(
"cargo:rustc-link-lib={}=imobiledevice-glue-1.0",
location_determinator
);
println!("cargo:rustc-link-lib={}=plist-2.0", location_determinator);
println!("cargo:rustc-link-lib={location_determinator}=crypto");
println!("cargo:rustc-link-lib={location_determinator}=ssl");
}
fn repo_setup(url: &str) {
let mut cmd = std::process::Command::new("git");
cmd.arg("clone");
cmd.arg("--depth=1");
cmd.arg(url);
cmd.output().unwrap();
env::set_current_dir(url.split('/').last().unwrap().replace(".git", "")).unwrap();
env::set_var("NOCONFIGURE", "1");
let mut cmd = std::process::Command::new("./autogen.sh");
let _ = cmd.output();
env::remove_var("NOCONFIGURE");
env::set_current_dir("..").unwrap();
}