blob: a68094e851e6f846e6f6658fa0edb285233c7b35 [file] [log] [blame]
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use crate::sdk::strip_tool_path;
use failure::{bail, format_err, Error, ResultExt};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Duration;
use uname::uname;
#[allow(dead_code)]
pub fn duration_as_milliseconds(duration: &Duration) -> u64 {
let subsec_ms: u64 = u64::from(duration.subsec_nanos()) / 1_000_000;
duration.as_secs() * 1000 + subsec_ms
}
pub fn is_mac() -> bool {
uname().expect("uname failed").sysname == "Darwin"
}
pub fn strip_binary(binary: &Path) -> Result<PathBuf, Error> {
let file_name = binary.file_name().ok_or(format_err!("file_name failed on {:#?}", binary))?;
let new_file_name = file_name.to_string_lossy().into_owned() + "_stripped";
let target_path = binary
.parent()
.expect("failed to get parent directory of binary to strip")
.join(new_file_name);
let strip_result = Command::new(strip_tool_path()?)
.arg("--strip-all")
.arg(binary)
.arg(&target_path)
.status()
.context("strip command failed to start")?;
if !strip_result.success() {
bail!("strip failed with error {:?}", strip_result);
}
Ok(target_path)
}
pub fn target_crate_path(manifest_path: &Option<PathBuf>) -> Result<PathBuf, Error> {
let cwd: PathBuf = if let Some(actual_manifest_path) = manifest_path.as_ref() {
actual_manifest_path.parent().expect("manifest_path parent").to_path_buf()
} else {
std::fs::canonicalize(std::env::current_dir()?)
.context("autotest: canonicalize working directory")?
};
Ok(cwd)
}