Skip to content

Commit

Permalink
Share target directories amonng metadata libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Sep 20, 2021
1 parent 9d1c142 commit 7204bce
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
35 changes: 20 additions & 15 deletions dylint/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
use anyhow::{anyhow, bail, ensure, Result};
use cargo::{
core::{source::MaybePackage, Dependency, Features, Package, PackageId, Source, SourceId},
util::{self, Config},
util::Config,
};
use cargo_metadata::{Error, Metadata, MetadataCommand};
use dylint_internal::rustup::SanitizeEnvironment;
Expand Down Expand Up @@ -291,7 +291,7 @@ fn package_library_path(
package_root: &Path,
package_id: PackageId,
) -> Result<PathBuf> {
let target_dir = target_dir(metadata, package_root, package_id);
let target_dir = target_dir(metadata, package_root, package_id)?;

if !opts.no_build {
let mut command = dylint_internal::build();
Expand All @@ -308,26 +308,31 @@ fn package_library_path(
Ok(target_dir.join("release"))
}

fn target_dir(metadata: &Metadata, package_root: &Path, package_id: PackageId) -> PathBuf {
metadata
fn target_dir(metadata: &Metadata, package_root: &Path, _package_id: PackageId) -> Result<PathBuf> {
let toolchain = dylint_internal::rustup::active_toolchain(package_root)?;
Ok(metadata
.target_directory
.join("dylint")
.join(pkg_dir(package_root, package_id))
.into()
.join(toolchain)
// .join(pkg_dir(package_root, package_id))
.into())
}

// smoelius: `pkg_dir` and `target_short_hash` are based on functions with the same names in
// https://github.com/rust-lang/cargo/blob/master/src/cargo/core/compiler/context/compilation_files.rs

fn pkg_dir(package_root: &Path, pkg_id: PackageId) -> String {
let name = pkg_id.name();
format!("{}-{}", name, target_short_hash(package_root, pkg_id))
}
#[cfg(any())]
mod disabled {
fn pkg_dir(package_root: &Path, pkg_id: PackageId) -> String {
let name = pkg_id.name();
format!("{}-{}", name, target_short_hash(package_root, pkg_id))
}

const METADATA_VERSION: u8 = 2;
const METADATA_VERSION: u8 = 2;

fn target_short_hash(package_root: &Path, pkg_id: PackageId) -> String {
// smoelius: For now, the package root is the workspace root.
let hashable = pkg_id.stable_hash(package_root);
util::short_hash(&(METADATA_VERSION, hashable))
fn target_short_hash(package_root: &Path, pkg_id: PackageId) -> String {
// smoelius: For now, the package root is the workspace root.
let hashable = pkg_id.stable_hash(package_root);
cargo::util::short_hash(&(METADATA_VERSION, hashable))
}
}
16 changes: 16 additions & 0 deletions internal/src/rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ impl SanitizeEnvironment for crate::Command {
}
}

// smoelius: Consider carefully whether you need to call this function! In most cases, the toolchain
// you want is not the one returned by rustup.
pub fn active_toolchain(path: &Path) -> Result<String> {
let output = Command::new("rustup")
.sanitize_environment()
.current_dir(path)
.args(&["show", "active-toolchain"])
.output()?;
let stdout = std::str::from_utf8(&output.stdout)?;
stdout
.splitn(2, ' ')
.next()
.map(ToOwned::to_owned)
.ok_or_else(|| anyhow!("Could not determine active toolchain"))
}

pub fn toolchain_path(path: &Path) -> Result<PathBuf> {
let output = Command::new("rustup")
.sanitize_environment()
Expand Down

0 comments on commit 7204bce

Please sign in to comment.