forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
152 lines (134 loc) · 5.22 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
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
// Run "cargo build -vv" if you want to see gn output.
#![deny(warnings)]
use std::env;
use std::path::{self, Path, PathBuf};
use std::process::Command;
fn main() {
let gn_mode = if cfg!(target_os = "windows") {
// On Windows, we need to link with a release build of libdeno, because
// rust always uses the release CRT.
// TODO(piscisaureus): make linking with debug libdeno possible.
String::from("release")
} else {
// Cargo sets PROFILE to either "debug" or "release", which conveniently
// matches the build modes we support.
env::var("PROFILE").unwrap()
};
// Equivalent to target arch != host arch
let is_different_target_arch =
env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() != env::var("HOST")
.unwrap()
.as_str()
.split("-")
.collect::<Vec<&str>>()[0];
// If we are using the same target as the host's default
// "rustup target list" should show your default target
let is_default_target =
env::var("TARGET").unwrap() == env::var("HOST").unwrap();
let cwd = env::current_dir().unwrap();
// If not using host default target the output folder will change
// target/release will become target/$TARGET/release
// Gn should also be using this output directory as well
// most things will work with gn using the default
// output directory but some tests depend on artifacts
// being in a specific directory relative to the main build output
let gn_out_path = cwd.join(format!(
"target/{}",
match is_default_target {
true => gn_mode.clone(),
false => format!("{}/{}", env::var("TARGET").unwrap(), gn_mode.clone()),
}
));
let gn_out_dir = normalize_path(&gn_out_path);
// Tell Cargo when to re-run this file. We do this first, so these directives
// can take effect even if something goes wrong later in the build process.
println!("cargo:rerun-if-env-changed=DENO_BUILD_PATH");
// TODO: this is obviously not appropriate here.
println!("cargo:rerun-if-env-changed=APPVEYOR_REPO_COMMIT");
// Detect if we're being invoked by the rust language server (RLS).
// Unfortunately we can't detect whether we're being run by `cargo check`.
let check_only = 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);
// This helps Rust source files locate the snapshot, source map etc.
println!("cargo:rustc-env=GN_OUT_DIR={}", gn_out_dir);
let gn_target;
if check_only {
// When RLS is running "cargo check" to analyze the source code, we're not
// trying to build a working executable, rather we're just compiling all
// rust code. Therefore, make ninja build only 'msg_generated.rs'.
gn_target = "msg_rs";
// Enable the 'check_only' feature, which enables some workarounds in the
// rust source code to compile successfully without a bundle and snapshot
println!("cargo:rustc-cfg=feature=\"check-only\"");
} else {
// "Full" (non-RLS) build.
if is_different_target_arch {
gn_target = "deno_deps_cross";
} else {
gn_target = "deno_deps";
}
// Link with libdeno.a/.lib, which includes V8.
println!("cargo:rustc-link-search=native={}/obj/libdeno", gn_out_dir);
if cfg!(target_os = "windows") {
println!("cargo:rustc-link-lib=static=libdeno");
} else {
println!("cargo:rustc-link-lib=static=deno");
}
// Link the system libraries that libdeno and V8 depend on.
if cfg!(any(target_os = "macos", target_os = "freebsd")) {
println!("cargo:rustc-link-lib=dylib=c++");
} else if cfg!(target_os = "windows") {
for lib in vec!["dbghelp", "shlwapi", "winmm", "ws2_32"] {
println!("cargo:rustc-link-lib={}", lib);
}
}
}
// If target_arch != host_arch disable snapshots.
// v8 snapshots seem to not be compatible with binaries
// other than the ones used to gernerate them,
// so for non native architecture builds we don't
// have an easy way to generate these snapshots.
// We can't run any binary capable of generating
// compatible snapshots without emulating the
// target architecture.
if is_different_target_arch {
// no-snapshot-init is not related to v8_use_snapshots
println!("cargo:rustc-cfg=feature=\"no-snapshot-init\"");
}
if !gn_out_path.join("build.ninja").exists() {
let status = Command::new("python")
.env("DENO_BUILD_PATH", &gn_out_dir)
.env("DENO_BUILD_MODE", &gn_mode)
.arg("./tools/setup.py")
.status()
.expect("setup.py failed");
assert!(status.success());
}
let status = Command::new("python")
.env("DENO_BUILD_PATH", &gn_out_dir)
.env("DENO_BUILD_MODE", &gn_mode)
.arg("./tools/build.py")
.arg(gn_target)
.arg("-v")
.status()
.expect("build.py failed");
assert!(status.success());
}
// Utility function to make a path absolute, normalizing it to use forward
// slashes only. The returned value is an owned String, otherwise panics.
fn normalize_path<T: AsRef<Path>>(path: T) -> String {
path
.as_ref()
.to_str()
.unwrap()
.to_owned()
.chars()
.map(|c| if path::is_separator(c) { '/' } else { c })
.collect()
}