forked from denoland/rusty_v8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
335 lines (296 loc) · 9.71 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
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use std::env;
use std::path::Path;
use std::path::PathBuf;
use std::process::exit;
use std::process::Command;
use which::which;
fn main() {
// Detect if trybuild tests are being compiled.
let is_trybuild = env::var_os("DENO_TRYBUILD").is_some();
// Don't build V8 if "cargo doc" is being run. This is to support docs.rs.
let is_cargo_doc = env::var_os("RUSTDOCFLAGS").is_some();
// Don't build V8 if the rust language server (RLS) is running.
let is_rls = env::var_os("CARGO")
.map(PathBuf::from)
.as_ref()
.and_then(|p| p.file_stem())
.and_then(|f| f.to_str())
.map(|s| s.starts_with("rls"))
.unwrap_or(false);
if !(is_trybuild || is_cargo_doc | is_rls) {
if env::var_os("V8_FROM_SOURCE").is_some() {
build_v8()
} else {
download_static_lib_binaries();
}
}
if !(is_cargo_doc || is_rls) {
print_link_flags()
}
}
fn build_v8() {
env::set_var("DEPOT_TOOLS_WIN_TOOLCHAIN", "0");
// cargo publish doesn't like pyc files.
env::set_var("PYTHONDONTWRITEBYTECODE", "1");
// git submodule update --init --recursive
let libcxx_src = PathBuf::from("buildtools/third_party/libc++/trunk/src");
if !libcxx_src.is_dir() {
eprintln!(
"missing source code. Run 'git submodule update --init --recursive'"
);
exit(1);
}
if need_gn_ninja_download() {
download_ninja_gn_binaries();
}
// On windows, rustc cannot link with a V8 debug build.
let mut gn_args = if cargo_gn::is_debug() && !cfg!(target_os = "windows") {
vec!["is_debug=true".to_string()]
} else {
vec!["is_debug=false".to_string()]
};
if let Some(clang_base_path) = find_compatible_system_clang() {
println!("clang_base_path {}", clang_base_path.display());
gn_args.push(format!("clang_base_path={:?}", clang_base_path));
// TODO: Dedupe this with the one from cc_wrapper()
gn_args.push("treat_warnings_as_errors=false".to_string());
// we can't use chromiums clang plugins with a system clang
gn_args.push("clang_use_chrome_plugins=false".to_string());
} else {
let clang_base_path = clang_download();
gn_args.push(format!("clang_base_path={:?}", clang_base_path));
}
if let Some(p) = env::var_os("SCCACHE") {
cc_wrapper(&mut gn_args, &Path::new(&p));
} else if let Ok(p) = which("sccache") {
cc_wrapper(&mut gn_args, &p);
} else {
println!("cargo:warning=Not using sccache");
}
if let Ok(args) = env::var("GN_ARGS") {
for arg in args.split_whitespace() {
gn_args.push(arg.to_string());
}
}
if env::var("TARGET").unwrap() == "aarch64-unknown-linux-gnu" {
gn_args.push(r#"target_cpu="arm64""#.to_string());
gn_args.push("use_sysroot=true".to_string());
maybe_install_sysroot("arm64");
maybe_install_sysroot("amd64");
};
let gn_root = env::var("CARGO_MANIFEST_DIR").unwrap();
let gn_out = cargo_gn::maybe_gen(&gn_root, gn_args);
assert!(gn_out.exists());
assert!(gn_out.join("args.gn").exists());
cargo_gn::build("rusty_v8", None);
}
fn maybe_install_sysroot(arch: &str) {
let sysroot_path = format!("build/linux/debian_sid_{}-sysroot", arch);
if !PathBuf::from(sysroot_path).is_dir() {
let status = Command::new("python")
.arg("./build/linux/sysroot_scripts/install-sysroot.py")
.arg(format!("--arch={}", arch))
.status()
.unwrap_or_else(|_| panic!("sysroot download failed: {}", arch));
assert!(status.success());
}
}
fn platform() -> &'static str {
#[cfg(target_os = "windows")]
{
"win"
}
#[cfg(target_os = "linux")]
{
"linux64"
}
#[cfg(target_os = "macos")]
{
"mac"
}
}
fn download_ninja_gn_binaries() {
let root = env::current_dir().unwrap();
let out_dir = env::var_os("OUT_DIR").expect(
"The 'OUT_DIR' environment is not set (it should be something like \
'target/debug/rusty_v8-{hash}').",
);
let out_dir_abs = root.join(out_dir);
// This would be target/debug or target/release
let target_dir = out_dir_abs
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap();
let bin_dir = target_dir
.join("ninja_gn_binaries-20200506")
.join(platform());
let gn = bin_dir.join("gn");
let ninja = bin_dir.join("ninja");
#[cfg(windows)]
let gn = gn.with_extension("exe");
#[cfg(windows)]
let ninja = ninja.with_extension("exe");
if !gn.exists() || !ninja.exists() {
let status = Command::new("python")
.arg("./tools/ninja_gn_binaries.py")
.arg("--dir")
.arg(&target_dir)
.status()
.expect("ninja_gn_binaries.py download failed");
assert!(status.success());
}
assert!(gn.exists());
assert!(ninja.exists());
env::set_var("GN", gn);
env::set_var("NINJA", ninja);
}
fn static_lib_url() -> (String, String) {
let base = "https://github.com/denoland/rusty_v8/releases/download";
let version = env::var("CARGO_PKG_VERSION").unwrap();
let target = env::var("TARGET").unwrap();
if cfg!(target_os = "windows") {
// Note: we always use the release build on windows.
let url = format!("{}/v{}/rusty_v8_release_{}.lib", base, version, target);
let static_lib_name = "rusty_v8.lib".to_string();
(url, static_lib_name)
} else {
let profile = env::var("PROFILE").unwrap();
assert!(profile == "release" || profile == "debug");
let url =
format!("{}/v{}/librusty_v8_{}_{}.a", base, version, profile, target);
let static_lib_name = "librusty_v8.a".to_string();
(url, static_lib_name)
}
}
fn download_file(url: String, filename: PathBuf) {
// Try downloading with python first. Python is a V8 build dependency,
// so this saves us from adding a Rust HTTP client dependency.
println!("Downloading {}", url);
let status = Command::new("python")
.arg("./tools/download_file.py")
.arg("--url")
.arg(&url)
.arg("--filename")
.arg(&filename)
.status();
// Python is only a required dependency for `V8_FROM_SOURCE` builds.
// If python is not available, try falling back to curl.
let status = match status {
Ok(status) if status.success() => status,
_ => {
println!("Python downloader failed, trying with curl.");
Command::new("curl")
.arg("-L")
.arg("-s")
.arg("-o")
.arg(&filename)
.arg(&url)
.status()
.unwrap()
}
};
assert!(status.success());
assert!(filename.exists());
}
fn download_static_lib_binaries() {
let (url, static_lib_name) = static_lib_url();
println!("static lib URL: {}", url);
let root = env::current_dir().unwrap();
// target/debug//build/rusty_v8-d9e5a424d4f96994/out/
let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir_abs = root.join(out_dir);
// This would be target/debug or target/release
let target_dir = out_dir_abs
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap();
let obj_dir = target_dir.join("gn_out").join("obj");
std::fs::create_dir_all(&obj_dir).unwrap();
println!("cargo:rustc-link-search={}", obj_dir.display());
let filename = obj_dir.join(static_lib_name);
if filename.exists() {
println!("static lib already exists {}", filename.display());
println!("To re-download this file, it must be manually deleted.");
} else {
download_file(url, filename);
}
}
fn print_link_flags() {
println!("cargo:rustc-link-lib=static=rusty_v8");
if cfg!(target_os = "windows") {
println!("cargo:rustc-link-lib=dylib=winmm");
println!("cargo:rustc-link-lib=dylib=dbghelp");
}
}
fn need_gn_ninja_download() -> bool {
!((which("ninja").is_ok() || env::var_os("NINJA").is_some())
&& env::var_os("GN").is_some())
}
// Chromiums gn arg clang_base_path is currently compatible with:
// * Apples clang and clang from homebrew's llvm@x packages
// * the official binaries from releases.llvm.org
// * unversioned (Linux) packages of clang (if recent enough)
// but unfortunately it doesn't work with version-suffixed packages commonly
// found in Linux packet managers
fn is_compatible_clang_version(clang_path: &Path) -> bool {
if let Ok(o) = Command::new(clang_path).arg("--version").output() {
let _output = String::from_utf8(o.stdout).unwrap();
// TODO check version output to make sure it's supported.
const _MIN_APPLE_CLANG_VER: f32 = 11.0;
const _MIN_LLVM_CLANG_VER: f32 = 8.0;
return true;
}
false
}
fn find_compatible_system_clang() -> Option<PathBuf> {
if let Ok(p) = env::var("CLANG_BASE_PATH") {
let base_path = Path::new(&p);
let clang_path = base_path.join("bin").join("clang");
if is_compatible_clang_version(&clang_path) {
return Some(base_path.to_path_buf());
}
}
println!("using Chromiums clang");
None
}
// Download chromium's clang into OUT_DIR because Cargo will not allow us to
// modify the source directory.
fn clang_download() -> PathBuf {
let root = env::current_dir().unwrap();
// target/debug//build/rusty_v8-d9e5a424d4f96994/out/
let out_dir = env::var_os("OUT_DIR").unwrap();
let clang_base_path = root
.join(out_dir)
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.join("clang");
println!("clang_base_path {}", clang_base_path.display());
let status = Command::new("python")
.arg("./tools/clang/scripts/update.py")
.arg("--output-dir")
.arg(&clang_base_path)
.status()
.expect("clang download failed");
assert!(status.success());
assert!(clang_base_path.exists());
clang_base_path
}
fn cc_wrapper(gn_args: &mut Vec<String>, sccache_path: &Path) {
gn_args.push(format!("cc_wrapper={:?}", sccache_path));
// Disable treat_warnings_as_errors until this sccache bug is fixed:
// https://github.com/mozilla/sccache/issues/264
if cfg!(target_os = "windows") {
gn_args.push("treat_warnings_as_errors=false".to_string());
}
}