forked from MagicalBitcoin/libtor-sys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
366 lines (321 loc) · 13.5 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
extern crate autotools;
extern crate cc;
extern crate fs_extra;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use fs_extra::dir::{copy, remove, CopyOptions};
pub struct Artifacts {
pub root: PathBuf,
pub include_dir: PathBuf,
pub lib_dir: PathBuf,
pub libs: Vec<String>,
}
impl Artifacts {
pub fn print_cargo_metadata(&self) {
println!("cargo:rustc-link-search=native={}", self.lib_dir.display());
for lib in self.libs.iter() {
println!("cargo:rustc-link-lib=static={}", lib);
}
println!("cargo:include={}", self.include_dir.display());
println!("cargo:lib={}", self.lib_dir.display());
}
}
pub fn autoreconf(path: &PathBuf) -> Result<(), Vec<u8>> {
match Command::new("autoreconf")
.current_dir(path)
.args(&["--force", "--install"])
.output()
{
Ok(output) => {
if !output.status.success() {
Err(output.stderr)
} else {
Ok(())
}
}
Err(e) => Err(format!("{:?}", e).as_bytes().to_vec()),
}
}
fn build_libevent() -> Artifacts {
// TODO: cmake on windows
let target = env::var("TARGET").unwrap();
let host = env::var("HOST").unwrap();
let mut cc = cc::Build::new();
cc.target(&target).host(&host);
let compiler = cc.get_compiler();
let root = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR expected")).join("libevent");
fs::create_dir_all(&root).expect("Cannot create `libevent` in `OUT_DIR`");
let original_src = Path::new(env!("CARGO_MANIFEST_DIR")).join("libevent-src");
let path = Path::new(root.to_str().unwrap()).join("libevent-src");
if path.exists() {
remove(&path).expect("Unable to remove libevent's src folder");
}
copy(original_src, &root, &CopyOptions::new()).expect("Unable to copy libevent's src folder");
if let Err(e) = autoreconf(&path) {
println!(
"cargo:warning=Failed to run `autoreconf`: {:?}",
String::from_utf8(e)
);
}
let mut config = autotools::Config::new(path.clone());
let mut config = autotools::Config::new(path.clone());
config.out_dir(&root);
if target.contains("aarch64-apple-ios") {
config
.env("CC", "cc")
.env("CFLAGS", "-arch arm64 -mios-version-min=7.0.0 -fno-common -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.1.sdk -O3 -O2 -fPIC -g -fno-omit-frame-pointer -miphoneos-version-min=7.0");
} else if target.contains("x86_64-apple-ios") {
config.env("CC", "cc")
.env("CFLAGS", "-arch x86_64 -mios-version-min=7.0.0 -fno-common -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.1.sdk -O3 -O2 -fPIC -g -fno-omit-frame-pointer -miphoneos-version-min=7.0");
} else {
config.env("CC", compiler.path());
}
config
.host(&target)
.enable_static()
.disable_shared()
.with("pic", None)
.disable("samples", None)
.disable("openssl", None)
.disable("libevent-regress", None)
.disable("debug-mode", None)
.disable("dependency-tracking", None);
let libevent = config.build();
let mut libs = vec!["event".to_string()];
if !target.contains("windows") {
// Windows targets don't build event_pthreads
libs.push("event_pthreads".to_string());
}
let artifacts = Artifacts {
lib_dir: libevent.join("lib"),
include_dir: root.join("include"),
libs, // TODO: on windows re-add the `lib` prefix
root,
};
artifacts.print_cargo_metadata();
artifacts
}
fn build_tor(libevent: Artifacts) {
let target = env::var("TARGET").expect("TARGET expected");
let host = env::var("HOST").expect("HOST expected");
let mut cc = cc::Build::new();
cc.target(&target).host(&host);
let compiler = cc.get_compiler();
/* for (key, value) in std::env::vars() {
println!("{}: {}", key, value);
}
return; */
// TODO https://github.com/arlolra/tor/blob/master/INSTALL#L32
let openssl_dir = env::var("DEP_OPENSSL_ROOT").ok().map(PathBuf::from);
let original_src = Path::new(env!("CARGO_MANIFEST_DIR")).join("tor-src");
let root = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR expected"));
let path = PathBuf::from(root.to_str().unwrap()).join("tor-src");
if path.exists() {
remove(&path).expect("Unable to remove Tor's src folder");
}
copy(original_src, &root, &CopyOptions::new()).expect("Unable to copy Tor's src folder");
if let Err(e) = autoreconf(&path) {
println!(
"cargo:warning=Failed to run `autoreconf`: {:?}",
String::from_utf8(e)
);
}
let mut config = autotools::Config::new(path.clone());
if target.contains("aarch64-apple-ios") {
config
.env("CC", "cc")
.env("CFLAGS", "-arch arm64 -mios-version-min=7.0.0 -fno-common -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.1.sdk -O3 -O2 -fPIC -g -fno-omit-frame-pointer -miphoneos-version-min=7.0");
} else if target.contains("x86_64-apple-ios") {
config.env("CC", "cc")
.env("CFLAGS", "-arch x86_64 -mios-version-min=7.0.0 -fno-common -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.1.sdk -O3 -O2 -fPIC -g -fno-omit-frame-pointer -miphoneos-version-min=7.0");
} else {
config.env("CC", compiler.path());
}
config
.with("libevent-dir", libevent.root.to_str())
.cflag(format!("-I{}", libevent.include_dir.display()))
.enable("pic", None)
//.enable("static-tor", None)
.enable("static-libevent", None)
.enable("static-zlib", None)
.disable("system-torrc", None)
.disable("asciidoc", None)
.disable("systemd", None)
.disable("zstd", None)
.disable("lzma", None)
.disable("largefile", None)
.disable("unittests", None)
.disable("tool-name-check", None)
.disable("manpage", None)
.disable("html-manual", None)
.disable("module-dirauth", None)
.disable("module-relay", None)
.disable("module-dircache", None)
.disable("seccomp", None)
.disable("rust", None);
if target.contains("windows") {
// On Windows targets the configure script needs some extra libs so it properly detects OpenSSL
config.env("LIBS", "-lcrypt32 -liphlpapi -lws2_32 -lgdi32");
}
if let Some(dir) = &openssl_dir {
config
.with("openssl-dir", dir.to_str())
.enable("static-openssl", None);
}
if target.contains("android") {
// Apparently zlib is already there on Android https://github.com/rust-lang/libz-sys/blob/master/build.rs#L42
let sysroot_lib = format!("{}/usr/lib", env::var("SYSROOT").expect("SYSROOT expected"));
// provides stdin and stderr
cc::Build::new()
.file("fake-stdio/stdio.c")
.compile("libfakestdio.a");
config
.enable("android", None)
.env(
"LDFLAGS",
format!(
"-L{} -L{}",
sysroot_lib,
env::var("OUT_DIR").expect("OUT_DIR expected")
),
)
.with("zlib-dir", Some(&sysroot_lib));
println!("cargo:rustc-link-search=native={}", sysroot_lib);
} else {
let mut zlib_dir = PathBuf::from(env::var("DEP_Z_ROOT").expect("DEP_Z_ROOT expected"));
let zlib_include_dir = zlib_dir.join("include");
zlib_dir.push("build");
config
.with("zlib-dir", zlib_dir.to_str())
.cflag(format!("-I{}", zlib_include_dir.display()));
println!("cargo:rustc-link-search=native={}", zlib_dir.display());
}
let tor = config.build();
if let Some(dir) = &openssl_dir {
println!(
"cargo:rustc-link-search=native={}",
dir.join("lib/").display()
);
}
println!(
"cargo:rustc-link-search=native={}",
tor.join("build/src/core").display()
);
println!(
"cargo:rustc-link-search=native={}",
tor.join("build/src/lib").display()
);
println!(
"cargo:rustc-link-search=native={}",
tor.join("build/src/trunnel").display()
);
println!(
"cargo:rustc-link-search=native={}",
tor.join("build/src/ext/ed25519/ref10").display()
);
println!(
"cargo:rustc-link-search=native={}",
tor.join("build/src/ext/ed25519/donna").display()
);
println!(
"cargo:rustc-link-search=native={}",
tor.join("build/src/ext/keccak-tiny").display()
);
println!("cargo:rustc-link-lib=static={}", "event");
if !target.contains("windows") {
// Windows targets don't build event_pthreads
println!("cargo:rustc-link-lib=static={}", "event_pthreads");
}
if openssl_dir.is_some() {
println!("cargo:rustc-link-lib=static={}", "crypto");
println!("cargo:rustc-link-lib=static={}", "ssl");
} else {
println!("cargo:rustc-link-lib={}", "crypto");
println!("cargo:rustc-link-lib={}", "ssl");
}
println!("cargo:rustc-link-lib=static={}", "z");
println!("cargo:rustc-link-lib=static={}", "curve25519_donna");
println!("cargo:rustc-link-lib=static={}", "ed25519_donna");
println!("cargo:rustc-link-lib=static={}", "ed25519_ref10");
println!("cargo:rustc-link-lib=static={}", "tor-confmgt");
println!("cargo:rustc-link-lib=static={}", "tor-app");
println!("cargo:rustc-link-lib=static={}", "keccak-tiny");
println!("cargo:rustc-link-lib=static={}", "or-trunnel");
println!("cargo:rustc-link-lib=static={}", "tor-intmath");
println!("cargo:rustc-link-lib=static={}", "tor-lock");
println!("cargo:rustc-link-lib=static={}", "tor-malloc");
println!("cargo:rustc-link-lib=static={}", "tor-math");
println!("cargo:rustc-link-lib=static={}", "tor-memarea");
println!("cargo:rustc-link-lib=static={}", "tor-meminfo");
println!("cargo:rustc-link-lib=static={}", "tor-osinfo");
println!("cargo:rustc-link-lib=static={}", "tor-process");
println!("cargo:rustc-link-lib=static={}", "tor-sandbox");
println!("cargo:rustc-link-lib=static={}", "tor-smartlist-core");
println!("cargo:rustc-link-lib=static={}", "tor-string");
println!("cargo:rustc-link-lib=static={}", "tor-term");
println!("cargo:rustc-link-lib=static={}", "tor-time");
println!("cargo:rustc-link-lib=static={}", "tor-thread");
println!("cargo:rustc-link-lib=static={}", "tor-wallclock");
println!("cargo:rustc-link-lib=static={}", "tor-log");
println!("cargo:rustc-link-lib=static={}", "tor-tls");
println!("cargo:rustc-link-lib=static={}", "tor-compress");
println!("cargo:rustc-link-lib=static={}", "tor-container");
println!("cargo:rustc-link-lib=static={}", "tor-crypt-ops");
println!("cargo:rustc-link-lib=static={}", "tor-ctime");
println!("cargo:rustc-link-lib=static={}", "tor-encoding");
println!("cargo:rustc-link-lib=static={}", "tor-net");
println!("cargo:rustc-link-lib=static={}", "tor-err");
println!("cargo:rustc-link-lib=static={}", "tor-evloop");
println!("cargo:rustc-link-lib=static={}", "tor-fdio");
println!("cargo:rustc-link-lib=static={}", "tor-fs");
println!("cargo:rustc-link-lib=static={}", "tor-geoip");
println!("cargo:rustc-link-lib=static={}", "tor-version");
println!("cargo:rustc-link-lib=static={}", "tor-buf");
println!("cargo:rustc-link-lib=static={}", "tor-pubsub");
println!("cargo:rustc-link-lib=static={}", "tor-dispatch");
println!("cargo:rustc-link-lib=static={}", "tor-trace");
println!("cargo:rustc-link-lib=static={}", "tor-llharden");
if target.contains("windows") {
// println!("cargo:rustc-link-search=native=/usr/i686-w64-mingw32/lib");
// Add the CC's library paths
let output = Command::new(format!("{}", compiler.path().display()))
.arg("-print-search-dirs")
.output()
.expect("CC doesn't accept -print-search-dirs");
let output = std::str::from_utf8(&output.stdout).expect("Invalid output");
let lines = output.lines().filter_map(|line| {
if line.starts_with("libraries: =") {
Some(line.replacen("libraries: =", "", 1))
} else {
None
}
});
for line in lines {
for path in line.split(':') {
println!("cargo:rustc-link-search=native={}", path);
}
}
println!("cargo:rustc-link-lib=static={}", "crypt32");
println!("cargo:rustc-link-lib=static={}", "iphlpapi");
println!("cargo:rustc-link-lib=static={}", "ws2_32");
println!("cargo:rustc-link-lib=static={}", "gdi32");
println!("cargo:rustc-link-lib=static={}", "shell32");
println!("cargo:rustc-link-lib=static={}", "ssp");
}
fs::create_dir_all(tor.join("include")).unwrap();
fs::copy(
path.join("src/feature/api/tor_api.h"),
tor.join("include/tor_api.h"),
)
.unwrap();
println!("cargo:include={}/include", tor.to_str().unwrap());
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=tor-src");
println!("cargo:rerun-if-changed=libevent-src");
}
fn main() {
let libevent = build_libevent();
build_tor(libevent);
}