-
Notifications
You must be signed in to change notification settings - Fork 10
/
build.rs
315 lines (264 loc) · 9.77 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
extern crate autotools;
extern crate cc;
extern crate libtor_src;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
macro_rules! std_env_expected {
($name:expr) => {{
std::env::var($name).expect(&format!("{} expected", $name))
}};
}
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 = std_env_expected!("TARGET");
let host = std_env_expected!("HOST");
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("missing OUT_DIR")).join("libevent");
fs::create_dir_all(&root).expect("Cannot write to `OUT_DIR`");
let path = libtor_src::get_libevent_dir();
// 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());
config
.out_dir(&root)
.config_option("host", Some(&host))
.env("CC", compiler.path())
.env("CFLAGS", compiler.cflags_env())
.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") {
// Statically link libssp-0.dll (part of mingw), otherwise it would have to be shipped
// together with the executable
println!("cargo:rustc-link-lib=static=ssp");
} else {
// 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,
root,
};
artifacts.print_cargo_metadata();
artifacts
}
fn build_tor(libevent: Artifacts) {
let target = std_env_expected!("TARGET");
let host = std_env_expected!("HOST");
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;
let openssl_dir = env::var("DEP_OPENSSL_ROOT").ok().map(PathBuf::from);
let lzma_dir = env::var("DEP_LZMA_ROOT").ok().map(PathBuf::from);
let zstd_dir = env::var("DEP_ZSTD_ROOT").ok().map(PathBuf::from);
let path = libtor_src::get_tor_dir();
// if let Err(e) = autoreconf(&path) {
// println!(
// "cargo:warning=Failed to run `autoreconf`: {:?}",
// String::from_utf8(e)
// );
// }
// lzma and zstd are enabled by default, but it doesn't fail if it can't find it
let mut config = autotools::Config::new(path.clone());
config
.config_option("host", Some(&host))
.env("CC", compiler.path())
.with("libevent-dir", libevent.root.to_str())
.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("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("libscrypt", None)
.disable("rust", None);
let mut cflags = String::new();
cflags += &format!(" {}", compiler.cflags_env().into_string().unwrap());
if !cfg!(feature = "with-lzma") {
config.disable("lzma", None);
}
if !cfg!(feature = "with-zstd") {
config.disable("zstd", 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 let Some(dir) = &lzma_dir {
let lzma_include = std_env_expected!("DEP_LZMA_INCLUDE");
config.env("LZMA_CFLAGS", format!("-I{}", lzma_include));
config.env("LZMA_LIBS", dir.join("liblzma.a").to_str().unwrap());
println!("cargo:rustc-link-lib=static={}", "lzma");
}
if let Some(dir) = &zstd_dir {
let lzma_include = std_env_expected!("DEP_ZSTD_INCLUDE");
config.env("ZSTD_CFLAGS", format!("-I{}", lzma_include));
config.env("ZSTD_LIBS", dir.join("libzstd.a").to_str().unwrap());
println!("cargo:rustc-link-lib=static={}", "zstd");
}
if target.contains("android") {
// zlib is part of the `sysroot` on android. Use `clang` to get the full path so that we
// can link with it.
let output = compiler
.to_command()
.args(&["--print-file-name", "libz.a"])
.output()
.expect("Failed to run `clang`");
if !output.status.success() {
panic!("`clang` did not complete successfully");
}
let libz_path =
std::str::from_utf8(&output.stdout).expect("Invalid path for `libz.a` returned");
let libz_path = PathBuf::from(libz_path);
let sysroot_lib = libz_path
.parent()
.expect("Invalid path for `libz.a` returned")
.to_str()
.unwrap();
config
.enable("android", None)
.with("zlib-dir", Some(&sysroot_lib));
println!("cargo:rustc-link-search=native={}", sysroot_lib);
} else {
let mut zlib_dir = PathBuf::from(std_env_expected!("DEP_Z_ROOT"));
let zlib_include_dir = zlib_dir.join("include");
cflags += &format!(" -I{}", zlib_include_dir.display());
zlib_dir.push("lib");
config.with("zlib-dir", zlib_dir.to_str());
// .env("CFLAGS", format!("-I{}", zlib_include_dir.display()));
println!("cargo:rustc-link-search=native={}", zlib_dir.display());
}
let tor = config.env("CFLAGS", cflags).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/").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");
}
println!("cargo:rustc-link-lib=static={}", "z");
println!("cargo:rustc-link-lib=static={}", "tor");
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");
}
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={}", "crypt32");
println!("cargo:rustc-link-lib={}", "iphlpapi");
println!("cargo:rustc-link-lib={}", "ws2_32");
println!("cargo:rustc-link-lib={}", "gdi32");
println!("cargo:rustc-link-lib={}", "shell32");
println!("cargo:rustc-link-lib={}", "ssp");
println!("cargo:rustc-link-lib={}", "shlwapi");
}
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");
}
fn main() {
let libevent = build_libevent();
build_tor(libevent);
}