2016-05-02 15:16:15 -07:00
|
|
|
//! Various utility functions used throughout rustbuild.
|
|
|
|
//!
|
|
|
|
//! Simple things like testing the various filesystem operations here and there,
|
|
|
|
//! not a lot of interesting happenings here unfortunately.
|
|
|
|
|
2015-11-19 15:20:12 -08:00
|
|
|
use std::env;
|
2018-03-27 16:06:47 +02:00
|
|
|
use std::fs;
|
2019-07-15 09:18:32 -07:00
|
|
|
use std::io;
|
2016-07-05 21:58:20 -07:00
|
|
|
use std::path::{Path, PathBuf};
|
2015-11-19 15:20:12 -08:00
|
|
|
use std::process::Command;
|
2019-12-22 17:42:04 -05:00
|
|
|
use std::str;
|
2019-07-15 09:18:32 -07:00
|
|
|
use std::time::Instant;
|
2015-11-19 15:20:12 -08:00
|
|
|
|
2019-05-09 12:03:13 -04:00
|
|
|
use build_helper::t;
|
|
|
|
|
2018-12-07 13:21:05 +01:00
|
|
|
use crate::builder::Builder;
|
2019-12-09 09:46:55 +01:00
|
|
|
use crate::cache::Interned;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::config::Config;
|
2017-02-03 17:12:58 -08:00
|
|
|
|
2016-05-02 15:16:15 -07:00
|
|
|
/// Returns the `name` as the filename of a static library for `target`.
|
2015-11-19 15:20:12 -08:00
|
|
|
pub fn staticlib(name: &str, target: &str) -> String {
|
2019-12-22 17:42:04 -05:00
|
|
|
if target.contains("windows") { format!("{}.lib", name) } else { format!("lib{}.a", name) }
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given an executable called `name`, return the filename for the
|
|
|
|
/// executable for a particular target.
|
|
|
|
pub fn exe(name: &str, target: &str) -> String {
|
2019-12-22 17:42:04 -05:00
|
|
|
if target.contains("windows") { format!("{}.exe", name) } else { name.to_string() }
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if the file name given looks like a dynamic library.
|
2015-11-19 15:20:12 -08:00
|
|
|
pub fn is_dylib(name: &str) -> bool {
|
|
|
|
name.ends_with(".dylib") || name.ends_with(".so") || name.ends_with(".dll")
|
|
|
|
}
|
|
|
|
|
2016-05-02 15:16:15 -07:00
|
|
|
/// Returns the corresponding relative library directory that the compiler's
|
|
|
|
/// dylibs will be found in.
|
2015-11-19 15:20:12 -08:00
|
|
|
pub fn libdir(target: &str) -> &'static str {
|
2019-12-22 17:42:04 -05:00
|
|
|
if target.contains("windows") { "bin" } else { "lib" }
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
|
|
|
|
2016-05-02 15:16:15 -07:00
|
|
|
/// Adds a list of lookup paths to `cmd`'s dynamic library lookup path.
|
2020-03-18 13:49:53 -07:00
|
|
|
pub fn add_dylib_path(path: Vec<PathBuf>, cmd: &mut Command) {
|
2015-11-19 15:20:12 -08:00
|
|
|
let mut list = dylib_path();
|
|
|
|
for path in path {
|
|
|
|
list.insert(0, path);
|
|
|
|
}
|
|
|
|
cmd.env(dylib_path_var(), t!(env::join_paths(list)));
|
|
|
|
}
|
|
|
|
|
2016-07-05 21:58:20 -07:00
|
|
|
/// Returns the environment variable which the dynamic library lookup path
|
|
|
|
/// resides in for this platform.
|
|
|
|
pub fn dylib_path_var() -> &'static str {
|
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
"PATH"
|
|
|
|
} else if cfg!(target_os = "macos") {
|
|
|
|
"DYLD_LIBRARY_PATH"
|
2017-04-24 14:21:36 +00:00
|
|
|
} else if cfg!(target_os = "haiku") {
|
|
|
|
"LIBRARY_PATH"
|
2016-07-05 21:58:20 -07:00
|
|
|
} else {
|
|
|
|
"LD_LIBRARY_PATH"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses the `dylib_path_var()` environment variable, returning a list of
|
|
|
|
/// paths that are members of this lookup path.
|
|
|
|
pub fn dylib_path() -> Vec<PathBuf> {
|
2019-01-21 17:47:57 -07:00
|
|
|
let var = match env::var_os(dylib_path_var()) {
|
|
|
|
Some(v) => v,
|
|
|
|
None => return vec![],
|
|
|
|
};
|
|
|
|
env::split_paths(&var).collect()
|
2016-07-05 21:58:20 -07:00
|
|
|
}
|
2016-10-31 21:00:32 +01:00
|
|
|
|
2020-03-18 14:07:04 -07:00
|
|
|
/// Adds a list of lookup paths to `cmd`'s link library lookup path.
|
|
|
|
pub fn add_link_lib_path(path: Vec<PathBuf>, cmd: &mut Command) {
|
|
|
|
let mut list = link_lib_path();
|
|
|
|
for path in path {
|
|
|
|
list.insert(0, path);
|
|
|
|
}
|
|
|
|
cmd.env(link_lib_path_var(), t!(env::join_paths(list)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the environment variable which the link library lookup path
|
|
|
|
/// resides in for this platform.
|
|
|
|
fn link_lib_path_var() -> &'static str {
|
|
|
|
if cfg!(target_env = "msvc") { "LIB" } else { "LIBRARY_PATH" }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses the `link_lib_path_var()` environment variable, returning a list of
|
|
|
|
/// paths that are members of this lookup path.
|
|
|
|
fn link_lib_path() -> Vec<PathBuf> {
|
|
|
|
let var = match env::var_os(link_lib_path_var()) {
|
|
|
|
Some(v) => v,
|
|
|
|
None => return vec![],
|
|
|
|
};
|
|
|
|
env::split_paths(&var).collect()
|
|
|
|
}
|
|
|
|
|
2016-10-31 21:00:32 +01:00
|
|
|
/// `push` all components to `buf`. On windows, append `.exe` to the last component.
|
|
|
|
pub fn push_exe_path(mut buf: PathBuf, components: &[&str]) -> PathBuf {
|
|
|
|
let (&file, components) = components.split_last().expect("at least one component required");
|
|
|
|
let mut file = file.to_owned();
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
file.push_str(".exe");
|
|
|
|
}
|
|
|
|
|
2018-07-26 17:11:10 +02:00
|
|
|
buf.extend(components);
|
2016-10-31 21:00:32 +01:00
|
|
|
buf.push(file);
|
|
|
|
|
|
|
|
buf
|
|
|
|
}
|
2016-11-16 12:31:19 -08:00
|
|
|
|
2018-03-28 17:25:09 +02:00
|
|
|
pub struct TimeIt(bool, Instant);
|
2016-11-16 12:31:19 -08:00
|
|
|
|
|
|
|
/// Returns an RAII structure that prints out how long it took to drop.
|
2019-02-25 19:30:32 +09:00
|
|
|
pub fn timeit(builder: &Builder<'_>) -> TimeIt {
|
2018-04-14 17:27:57 -06:00
|
|
|
TimeIt(builder.config.dry_run, Instant::now())
|
2016-11-16 12:31:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TimeIt {
|
|
|
|
fn drop(&mut self) {
|
2018-03-28 17:25:09 +02:00
|
|
|
let time = self.1.elapsed();
|
|
|
|
if !self.0 {
|
2020-02-03 20:13:30 +01:00
|
|
|
println!("\tfinished in {}.{:03}", time.as_secs(), time.subsec_millis());
|
2018-03-28 17:25:09 +02:00
|
|
|
}
|
2016-11-16 12:31:19 -08:00
|
|
|
}
|
|
|
|
}
|
2017-03-01 15:34:54 -08:00
|
|
|
|
|
|
|
/// Symlinks two directories, using junctions on Windows and normal symlinks on
|
|
|
|
/// Unix.
|
2018-03-27 16:06:47 +02:00
|
|
|
pub fn symlink_dir(config: &Config, src: &Path, dest: &Path) -> io::Result<()> {
|
2019-12-22 17:42:04 -05:00
|
|
|
if config.dry_run {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2017-03-01 15:34:54 -08:00
|
|
|
let _ = fs::remove_dir(dest);
|
|
|
|
return symlink_dir_inner(src, dest);
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn symlink_dir_inner(src: &Path, dest: &Path) -> io::Result<()> {
|
|
|
|
use std::os::unix::fs;
|
|
|
|
fs::symlink(src, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creating a directory junction on windows involves dealing with reparse
|
|
|
|
// points and the DeviceIoControl function, and this code is a skeleton of
|
|
|
|
// what can be found here:
|
|
|
|
//
|
|
|
|
// http://www.flexhex.com/docs/articles/hard-links.phtml
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn symlink_dir_inner(target: &Path, junction: &Path) -> io::Result<()> {
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::os::windows::ffi::OsStrExt;
|
2019-12-22 17:42:04 -05:00
|
|
|
use std::ptr;
|
2017-03-01 15:34:54 -08:00
|
|
|
|
2020-01-04 16:46:47 -05:00
|
|
|
use winapi::shared::minwindef::{DWORD, WORD};
|
|
|
|
use winapi::um::fileapi::{CreateFileW, OPEN_EXISTING};
|
|
|
|
use winapi::um::handleapi::CloseHandle;
|
|
|
|
use winapi::um::ioapiset::DeviceIoControl;
|
|
|
|
use winapi::um::winbase::{FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT};
|
|
|
|
use winapi::um::winioctl::FSCTL_SET_REPARSE_POINT;
|
|
|
|
use winapi::um::winnt::{
|
|
|
|
FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_WRITE,
|
|
|
|
IO_REPARSE_TAG_MOUNT_POINT, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, WCHAR,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
2017-03-01 15:34:54 -08:00
|
|
|
#[repr(C)]
|
|
|
|
struct REPARSE_MOUNTPOINT_DATA_BUFFER {
|
|
|
|
ReparseTag: DWORD,
|
|
|
|
ReparseDataLength: DWORD,
|
|
|
|
Reserved: WORD,
|
|
|
|
ReparseTargetLength: WORD,
|
|
|
|
ReparseTargetMaximumLength: WORD,
|
|
|
|
Reserved1: WORD,
|
|
|
|
ReparseTarget: WCHAR,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_u16s<S: AsRef<OsStr>>(s: S) -> io::Result<Vec<u16>> {
|
|
|
|
Ok(s.as_ref().encode_wide().chain(Some(0)).collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're using low-level APIs to create the junction, and these are more
|
|
|
|
// picky about paths. For example, forward slashes cannot be used as a
|
|
|
|
// path separator, so we should try to canonicalize the path first.
|
2018-12-01 15:48:55 -06:00
|
|
|
let target = fs::canonicalize(target)?;
|
2017-03-01 15:34:54 -08:00
|
|
|
|
2018-12-01 15:48:55 -06:00
|
|
|
fs::create_dir(junction)?;
|
2017-03-01 15:34:54 -08:00
|
|
|
|
2018-12-01 15:48:55 -06:00
|
|
|
let path = to_u16s(junction)?;
|
2017-03-01 15:34:54 -08:00
|
|
|
|
|
|
|
unsafe {
|
2019-12-22 17:42:04 -05:00
|
|
|
let h = CreateFileW(
|
|
|
|
path.as_ptr(),
|
|
|
|
GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
|
|
|
ptr::null_mut(),
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
|
|
|
|
ptr::null_mut(),
|
|
|
|
);
|
2017-03-01 15:34:54 -08:00
|
|
|
|
2020-01-04 16:46:47 -05:00
|
|
|
let mut data = [0u8; MAXIMUM_REPARSE_DATA_BUFFER_SIZE as usize];
|
2019-12-22 17:42:04 -05:00
|
|
|
let db = data.as_mut_ptr() as *mut REPARSE_MOUNTPOINT_DATA_BUFFER;
|
2018-01-01 17:53:47 -08:00
|
|
|
let buf = &mut (*db).ReparseTarget as *mut u16;
|
2017-03-01 15:34:54 -08:00
|
|
|
let mut i = 0;
|
|
|
|
// FIXME: this conversion is very hacky
|
|
|
|
let v = br"\??\";
|
|
|
|
let v = v.iter().map(|x| *x as u16);
|
|
|
|
for c in v.chain(target.as_os_str().encode_wide().skip(4)) {
|
|
|
|
*buf.offset(i) = c;
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
*buf.offset(i) = 0;
|
|
|
|
i += 1;
|
|
|
|
(*db).ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
|
|
|
|
(*db).ReparseTargetMaximumLength = (i * 2) as WORD;
|
|
|
|
(*db).ReparseTargetLength = ((i - 1) * 2) as WORD;
|
2019-12-22 17:42:04 -05:00
|
|
|
(*db).ReparseDataLength = (*db).ReparseTargetLength as DWORD + 12;
|
2017-03-01 15:34:54 -08:00
|
|
|
|
|
|
|
let mut ret = 0;
|
2019-12-22 17:42:04 -05:00
|
|
|
let res = DeviceIoControl(
|
|
|
|
h as *mut _,
|
|
|
|
FSCTL_SET_REPARSE_POINT,
|
|
|
|
data.as_ptr() as *mut _,
|
|
|
|
(*db).ReparseDataLength + 8,
|
|
|
|
ptr::null_mut(),
|
|
|
|
0,
|
|
|
|
&mut ret,
|
|
|
|
ptr::null_mut(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let out = if res == 0 { Err(io::Error::last_os_error()) } else { Ok(()) };
|
2018-03-28 22:43:23 +01:00
|
|
|
CloseHandle(h);
|
|
|
|
out
|
2017-03-01 15:34:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 00:33:20 +08:00
|
|
|
|
|
|
|
/// The CI environment rustbuild is running in. This mainly affects how the logs
|
|
|
|
/// are printed.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum CiEnv {
|
|
|
|
/// Not a CI environment.
|
|
|
|
None,
|
2019-05-13 15:19:29 +02:00
|
|
|
/// The Azure Pipelines environment, for Linux (including Docker), Windows, and macOS builds.
|
2019-05-06 11:15:52 -07:00
|
|
|
AzurePipelines,
|
2019-11-11 14:31:32 +01:00
|
|
|
/// The GitHub Actions environment, for Linux (including Docker), Windows and macOS builds.
|
|
|
|
GitHubActions,
|
2017-05-18 00:33:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CiEnv {
|
|
|
|
/// Obtains the current CI environment.
|
|
|
|
pub fn current() -> CiEnv {
|
2019-12-21 17:04:41 +00:00
|
|
|
if env::var("TF_BUILD").map_or(false, |e| e == "True") {
|
2019-05-06 11:15:52 -07:00
|
|
|
CiEnv::AzurePipelines
|
2019-12-21 17:04:41 +00:00
|
|
|
} else if env::var("GITHUB_ACTIONS").map_or(false, |e| e == "true") {
|
2019-11-11 14:31:32 +01:00
|
|
|
CiEnv::GitHubActions
|
2017-05-18 00:33:20 +08:00
|
|
|
} else {
|
|
|
|
CiEnv::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If in a CI environment, forces the command to run with colors.
|
|
|
|
pub fn force_coloring_in_ci(self, cmd: &mut Command) {
|
|
|
|
if self != CiEnv::None {
|
|
|
|
// Due to use of stamp/docker, the output stream of rustbuild is not
|
|
|
|
// a TTY in CI, so coloring is by-default turned off.
|
|
|
|
// The explicit `TERM=xterm` environment is needed for
|
|
|
|
// `--color always` to actually work. This env var was lost when
|
|
|
|
// compiling through the Makefile. Very strange.
|
|
|
|
cmd.env("TERM", "xterm").args(&["--color", "always"]);
|
|
|
|
}
|
|
|
|
}
|
2017-06-27 09:05:40 -06:00
|
|
|
}
|
2019-05-27 15:09:26 +02:00
|
|
|
|
|
|
|
pub fn forcing_clang_based_tests() -> bool {
|
|
|
|
if let Some(var) = env::var_os("RUSTBUILD_FORCE_CLANG_BASED_TESTS") {
|
|
|
|
match &var.to_string_lossy().to_lowercase()[..] {
|
|
|
|
"1" | "yes" | "on" => true,
|
|
|
|
"0" | "no" | "off" => false,
|
|
|
|
other => {
|
|
|
|
// Let's make sure typos don't go unnoticed
|
2019-12-22 17:42:04 -05:00
|
|
|
panic!(
|
|
|
|
"Unrecognized option '{}' set in \
|
|
|
|
RUSTBUILD_FORCE_CLANG_BASED_TESTS",
|
|
|
|
other
|
|
|
|
)
|
2019-05-27 15:09:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2019-12-09 09:46:55 +01:00
|
|
|
|
|
|
|
pub fn use_host_linker(target: &Interned<String>) -> bool {
|
|
|
|
// FIXME: this information should be gotten by checking the linker flavor
|
|
|
|
// of the rustc target
|
2019-12-22 17:42:04 -05:00
|
|
|
!(target.contains("emscripten")
|
|
|
|
|| target.contains("wasm32")
|
|
|
|
|| target.contains("nvptx")
|
|
|
|
|| target.contains("fortanix")
|
|
|
|
|| target.contains("fuchsia"))
|
2019-12-09 09:46:55 +01:00
|
|
|
}
|