2020-07-22 19:51:19 +03:00
|
|
|
use std::ffi::{OsStr, OsString};
|
|
|
|
use std::fmt::Display;
|
2015-11-19 15:20:12 -08:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-03-07 15:24:36 -08:00
|
|
|
use std::process::{Command, Stdio};
|
2018-03-31 20:32:40 -06:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
2018-03-25 23:20:56 +09:00
|
|
|
use std::{env, fs};
|
2017-02-01 00:27:51 +03:00
|
|
|
|
|
|
|
/// A helper macro to `unwrap` a result except also print out details like:
|
|
|
|
///
|
|
|
|
/// * The file/line of the panic
|
|
|
|
/// * The expression that failed
|
|
|
|
/// * The error itself
|
|
|
|
///
|
|
|
|
/// This is currently used judiciously throughout the build system rather than
|
|
|
|
/// using a `Result` with `try!`, but this may change one day...
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! t {
|
2018-03-25 23:20:56 +09:00
|
|
|
($e:expr) => {
|
|
|
|
match $e {
|
|
|
|
Ok(e) => e,
|
|
|
|
Err(e) => panic!("{} failed with {}", stringify!($e), e),
|
|
|
|
}
|
|
|
|
};
|
2019-10-17 16:57:46 +08:00
|
|
|
// it can show extra info in the second parameter
|
|
|
|
($e:expr, $extra:expr) => {
|
|
|
|
match $e {
|
|
|
|
Ok(e) => e,
|
|
|
|
Err(e) => panic!("{} failed with {} ({:?})", stringify!($e), e, $extra),
|
|
|
|
}
|
|
|
|
};
|
2017-02-01 00:27:51 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 19:51:19 +03:00
|
|
|
/// Reads an environment variable and adds it to dependencies.
|
|
|
|
/// Supposed to be used for all variables except those set for build scripts by cargo
|
2020-11-05 14:33:23 +01:00
|
|
|
/// <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts>
|
2020-07-22 19:51:19 +03:00
|
|
|
pub fn tracked_env_var_os<K: AsRef<OsStr> + Display>(key: K) -> Option<OsString> {
|
|
|
|
println!("cargo:rerun-if-env-changed={}", key);
|
|
|
|
env::var_os(key)
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:47:57 -07:00
|
|
|
// Because Cargo adds the compiler's dylib path to our library search path, llvm-config may
|
|
|
|
// break: the dylib path for the compiler, as of this writing, contains a copy of the LLVM
|
|
|
|
// shared library, which means that when our freshly built llvm-config goes to load it's
|
|
|
|
// associated LLVM, it actually loads the compiler's LLVM. In particular when building the first
|
|
|
|
// compiler (i.e., in stage 0) that's a problem, as the compiler's LLVM is likely different from
|
|
|
|
// the one we want to use. As such, we restore the environment to what bootstrap saw. This isn't
|
|
|
|
// perfect -- we might actually want to see something from Cargo's added library paths -- but
|
|
|
|
// for now it works.
|
|
|
|
pub fn restore_library_path() {
|
2020-07-22 19:51:19 +03:00
|
|
|
let key = tracked_env_var_os("REAL_LIBRARY_PATH_VAR").expect("REAL_LIBRARY_PATH_VAR");
|
|
|
|
if let Some(env) = tracked_env_var_os("REAL_LIBRARY_PATH") {
|
2019-01-21 17:47:57 -07:00
|
|
|
env::set_var(&key, &env);
|
|
|
|
} else {
|
|
|
|
env::remove_var(&key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:08:49 +02:00
|
|
|
/// Run the command, printing what we are running.
|
|
|
|
pub fn run_verbose(cmd: &mut Command) {
|
2015-11-19 15:20:12 -08:00
|
|
|
println!("running: {:?}", cmd);
|
2019-08-01 20:13:47 +02:00
|
|
|
run(cmd);
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
|
|
|
|
2019-08-01 20:13:47 +02:00
|
|
|
pub fn run(cmd: &mut Command) {
|
|
|
|
if !try_run(cmd) {
|
2017-06-02 09:27:44 -07:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:13:47 +02:00
|
|
|
pub fn try_run(cmd: &mut Command) -> bool {
|
2015-11-19 15:20:12 -08:00
|
|
|
let status = match cmd.status() {
|
|
|
|
Ok(status) => status,
|
2019-12-22 17:42:04 -05:00
|
|
|
Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}", cmd, e)),
|
2015-11-19 15:20:12 -08:00
|
|
|
};
|
2017-12-07 05:06:48 +08:00
|
|
|
if !status.success() {
|
2018-03-25 23:20:56 +09:00
|
|
|
println!(
|
|
|
|
"\n\ncommand did not execute successfully: {:?}\n\
|
|
|
|
expected success, got: {}\n\n",
|
|
|
|
cmd, status
|
|
|
|
);
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
2017-12-07 05:06:48 +08:00
|
|
|
status.success()
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
|
|
|
|
2017-12-07 05:06:48 +08:00
|
|
|
pub fn run_suppressed(cmd: &mut Command) {
|
|
|
|
if !try_run_suppressed(cmd) {
|
2017-06-02 09:27:44 -07:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:06:48 +08:00
|
|
|
pub fn try_run_suppressed(cmd: &mut Command) -> bool {
|
2017-02-15 15:57:06 -08:00
|
|
|
let output = match cmd.output() {
|
|
|
|
Ok(status) => status,
|
2019-12-22 17:42:04 -05:00
|
|
|
Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}", cmd, e)),
|
2017-02-15 15:57:06 -08:00
|
|
|
};
|
2017-12-07 05:06:48 +08:00
|
|
|
if !output.status.success() {
|
2018-03-25 23:20:56 +09:00
|
|
|
println!(
|
|
|
|
"\n\ncommand did not execute successfully: {:?}\n\
|
|
|
|
expected success, got: {}\n\n\
|
|
|
|
stdout ----\n{}\n\
|
|
|
|
stderr ----\n{}\n\n",
|
|
|
|
cmd,
|
|
|
|
output.status,
|
|
|
|
String::from_utf8_lossy(&output.stdout),
|
|
|
|
String::from_utf8_lossy(&output.stderr)
|
|
|
|
);
|
2017-12-07 05:06:48 +08:00
|
|
|
}
|
|
|
|
output.status.success()
|
2017-02-15 15:57:06 -08:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:30:14 +02:00
|
|
|
pub fn gnu_target(target: &str) -> &str {
|
2015-11-19 15:20:12 -08:00
|
|
|
match target {
|
2018-10-09 14:30:14 +02:00
|
|
|
"i686-pc-windows-msvc" => "i686-pc-win32",
|
|
|
|
"x86_64-pc-windows-msvc" => "x86_64-pc-win32",
|
|
|
|
"i686-pc-windows-gnu" => "i686-w64-mingw32",
|
|
|
|
"x86_64-pc-windows-gnu" => "x86_64-w64-mingw32",
|
|
|
|
s => s,
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 20:09:23 +01:00
|
|
|
pub fn make(host: &str) -> PathBuf {
|
2019-12-22 17:42:04 -05:00
|
|
|
if host.contains("dragonfly")
|
|
|
|
|| host.contains("freebsd")
|
|
|
|
|| host.contains("netbsd")
|
|
|
|
|| host.contains("openbsd")
|
2018-03-25 23:20:56 +09:00
|
|
|
{
|
2016-12-17 20:09:23 +01:00
|
|
|
PathBuf::from("gmake")
|
|
|
|
} else {
|
|
|
|
PathBuf::from("make")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:20:12 -08:00
|
|
|
pub fn output(cmd: &mut Command) -> String {
|
|
|
|
let output = match cmd.stderr(Stdio::inherit()).output() {
|
|
|
|
Ok(status) => status,
|
2019-12-22 17:42:04 -05:00
|
|
|
Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}", cmd, e)),
|
2015-11-19 15:20:12 -08:00
|
|
|
};
|
|
|
|
if !output.status.success() {
|
2018-03-25 23:20:56 +09:00
|
|
|
panic!(
|
|
|
|
"command did not execute successfully: {:?}\n\
|
|
|
|
expected success, got: {}",
|
|
|
|
cmd, output.status
|
|
|
|
);
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
|
|
|
String::from_utf8(output.stdout).unwrap()
|
|
|
|
}
|
|
|
|
|
2017-01-27 01:57:30 +03:00
|
|
|
pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut stack = dir
|
|
|
|
.read_dir()
|
2018-03-25 23:20:56 +09:00
|
|
|
.unwrap()
|
|
|
|
.map(|e| e.unwrap())
|
|
|
|
.filter(|e| &*e.file_name() != ".git")
|
|
|
|
.collect::<Vec<_>>();
|
2017-01-27 01:57:30 +03:00
|
|
|
while let Some(entry) = stack.pop() {
|
|
|
|
let path = entry.path();
|
|
|
|
if entry.file_type().unwrap().is_dir() {
|
|
|
|
stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
|
|
|
|
} else {
|
|
|
|
println!("cargo:rerun-if-changed={}", path.display());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 00:27:51 +03:00
|
|
|
/// Returns the last-modified time for `path`, or zero if it doesn't exist.
|
2018-03-31 20:32:40 -06:00
|
|
|
pub fn mtime(path: &Path) -> SystemTime {
|
2019-12-22 17:42:04 -05:00
|
|
|
fs::metadata(path).and_then(|f| f.modified()).unwrap_or(UNIX_EPOCH)
|
2017-02-01 00:27:51 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if `dst` is up to date given that the file or files in `src`
|
2017-02-01 00:27:51 +03:00
|
|
|
/// are used to generate it.
|
|
|
|
///
|
|
|
|
/// Uses last-modified time checks to verify this.
|
|
|
|
pub fn up_to_date(src: &Path, dst: &Path) -> bool {
|
2017-12-01 14:28:14 +05:00
|
|
|
if !dst.exists() {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-01 00:27:51 +03:00
|
|
|
let threshold = mtime(dst);
|
|
|
|
let meta = match fs::metadata(src) {
|
|
|
|
Ok(meta) => meta,
|
|
|
|
Err(e) => panic!("source {:?} failed to get metadata: {}", src, e),
|
|
|
|
};
|
|
|
|
if meta.is_dir() {
|
2018-03-31 20:32:40 -06:00
|
|
|
dir_up_to_date(src, threshold)
|
2017-02-01 00:27:51 +03:00
|
|
|
} else {
|
2018-03-31 20:32:40 -06:00
|
|
|
meta.modified().unwrap_or(UNIX_EPOCH) <= threshold
|
2017-02-01 00:27:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 20:32:40 -06:00
|
|
|
fn dir_up_to_date(src: &Path, threshold: SystemTime) -> bool {
|
2017-02-01 00:27:51 +03:00
|
|
|
t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
|
|
|
|
let meta = t!(e.metadata());
|
|
|
|
if meta.is_dir() {
|
|
|
|
dir_up_to_date(&e.path(), threshold)
|
|
|
|
} else {
|
2018-03-31 20:32:40 -06:00
|
|
|
meta.modified().unwrap_or(UNIX_EPOCH) < threshold
|
2017-02-01 00:27:51 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:20:12 -08:00
|
|
|
fn fail(s: &str) -> ! {
|
|
|
|
println!("\n\n{}\n\n", s);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|