2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-10-31 14:11:10 -04:00
|
|
|
|
|
|
|
// Run "cargo build -vv" if you want to see gn output.
|
2018-11-01 01:06:55 -04:00
|
|
|
|
|
|
|
#![deny(warnings)]
|
2018-10-31 14:11:10 -04:00
|
|
|
|
|
|
|
use std::env;
|
2018-11-01 01:06:55 -04:00
|
|
|
use std::path::{self, Path, PathBuf};
|
2018-10-31 14:11:10 -04:00
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
fn main() {
|
2018-12-13 16:19:17 -05:00
|
|
|
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()
|
2018-11-01 01:06:55 -04:00
|
|
|
};
|
|
|
|
|
2018-12-20 11:53:56 -05:00
|
|
|
let cwd = env::current_dir().unwrap();
|
|
|
|
let gn_out_path = cwd.join(format!("target/{}", gn_mode));
|
|
|
|
let gn_out_dir = normalize_path(&gn_out_path);
|
2018-11-01 01:06:55 -04:00
|
|
|
|
2018-12-13 16:19:17 -05:00
|
|
|
// 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.
|
2018-11-01 01:06:55 -04:00
|
|
|
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");
|
2018-10-31 14:11:10 -04:00
|
|
|
|
2018-10-31 21:29:39 -04:00
|
|
|
// 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);
|
|
|
|
|
2018-12-13 16:19:17 -05:00
|
|
|
// This helps Rust source files locate the snapshot, source map etc.
|
|
|
|
println!("cargo:rustc-env=GN_OUT_DIR={}", gn_out_dir);
|
|
|
|
|
|
|
|
let gn_target;
|
2018-10-31 21:29:39 -04:00
|
|
|
|
2018-12-18 22:09:34 -05:00
|
|
|
if check_only {
|
2018-12-13 16:19:17 -05:00
|
|
|
// 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
|
2018-12-18 22:09:34 -05:00
|
|
|
println!("cargo:rustc-cfg=feature=\"check-only\"");
|
2018-12-13 16:19:17 -05:00
|
|
|
} else {
|
|
|
|
// "Full" (non-RLS) build.
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 22:09:34 -05:00
|
|
|
}
|
|
|
|
|
2018-12-20 11:53:56 -05:00
|
|
|
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());
|
|
|
|
}
|
2018-10-31 14:11:10 -04:00
|
|
|
|
|
|
|
let status = Command::new("python")
|
2018-11-01 01:06:55 -04:00
|
|
|
.env("DENO_BUILD_PATH", &gn_out_dir)
|
2018-12-13 16:19:17 -05:00
|
|
|
.env("DENO_BUILD_MODE", &gn_mode)
|
2018-10-31 14:11:10 -04:00
|
|
|
.arg("./tools/build.py")
|
2018-10-31 21:29:39 -04:00
|
|
|
.arg(gn_target)
|
2018-10-31 14:11:10 -04:00
|
|
|
.arg("-v")
|
|
|
|
.status()
|
|
|
|
.expect("build.py failed");
|
|
|
|
assert!(status.success());
|
|
|
|
}
|
2018-11-01 01:06:55 -04:00
|
|
|
|
|
|
|
// Utility function to make a path absolute, normalizing it to use forward
|
|
|
|
// slashes only. The returned value is an owned String, otherwise panics.
|
2018-12-13 16:19:17 -05:00
|
|
|
fn normalize_path<T: AsRef<Path>>(path: T) -> String {
|
2018-11-12 15:05:13 -05:00
|
|
|
path
|
2018-12-13 16:19:17 -05:00
|
|
|
.as_ref()
|
2018-11-01 01:06:55 -04:00
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.to_owned()
|
|
|
|
.chars()
|
|
|
|
.map(|c| if path::is_separator(c) { '/' } else { c })
|
|
|
|
.collect()
|
|
|
|
}
|