Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent calling rustc_version::version_meta() multiple times #101

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ version = "0.3.3"
[dependencies]
cargo_metadata = "0.11"
clap = "2.33"
lazy_static = "1.4"
regex = "1.3"
rustc-cfg = "0.4"
rustc-demangle = "0.1"
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ impl Context {
// TODO: How will custom profiles impact this?
if path == "debug" || path == "release" {
// Looks like this artifact was built for the host.
rustc_version::version_meta()?.host
rustc::version_meta()?.host.as_str().into()
} else {
// The artifact
path.to_string()
path
}
} else {
unreachable!();
Expand All @@ -76,7 +76,7 @@ impl Context {
/// Get a context structure from a provided target flag, used when cargo
/// was not used to build the binary.
fn from_flag(metadata: Metadata, target_flag: Option<&str>) -> Result<Self> {
let host_target_name = rustc_version::version_meta()?.host;
let host_target_name = rustc::version_meta()?.host.as_str();

// Get the "default" target override in .cargo/config.
let mut config_target_name = None;
Expand All @@ -93,7 +93,7 @@ impl Context {
// Find the actual target.
let target_name = target_flag
.or(config_target_name)
.unwrap_or(&host_target_name);
.unwrap_or(host_target_name);

Self::from_target_name(target_name)
}
Expand Down
35 changes: 34 additions & 1 deletion src/rustc.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
use std::env;
use std::error::Error;
use std::fmt::{self, Debug, Display, Formatter};
use std::path::PathBuf;
use std::process::Command;

use anyhow::Result;
use rustc_version::VersionMeta;

struct RefError<'a, T>(&'a T);

impl<'a, T: Debug> Debug for RefError<'a, T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Debug::fmt(self.0, f)
}
}

impl<'a, T: Display> Display for RefError<'a, T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self.0, f)
}
}

impl<'a, T: Error> Error for RefError<'a, T> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.0.source()
}
}

lazy_static::lazy_static! {
static ref VERSION_META: Result<VersionMeta, rustc_version::Error> = rustc_version::version_meta();
}

pub fn version_meta() -> Result<&'static VersionMeta> {
VERSION_META
.as_ref()
.map_err(|error| RefError(error).into())
}

pub fn sysroot() -> Result<String> {
let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
Expand All @@ -17,7 +50,7 @@ pub fn rustlib() -> Result<PathBuf> {
let mut pathbuf = PathBuf::from(sysroot);
pathbuf.push("lib");
pathbuf.push("rustlib");
pathbuf.push(rustc_version::version_meta()?.host); // TODO: Prevent calling rustc_version::version_meta() multiple times
pathbuf.push(&version_meta()?.host);
pathbuf.push("bin");
Ok(pathbuf)
}