Skip to content

Commit

Permalink
Refactoring: move hex and hasher modules from util module to util crate
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Cameron <nrc@ncameron.org>
  • Loading branch information
nrc committed Nov 28, 2022
1 parent 1382b44 commit 6c9d20f
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ path = "src/cargo/lib.rs"
[dependencies]
bytesize = "1.0"
cargo-platform = { path = "crates/cargo-platform", version = "0.1.2" }
cargo-util = { path = "crates/cargo-util", version = "0.2.3" }
cargo-util = { path = "crates/cargo-util", version = "0.2.4" }
crates-io = { path = "crates/crates-io", version = "0.35.0" }
curl = { version = "0.4.44", features = ["http2"] }
curl-sys = "0.4.59"
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-util"
version = "0.2.3"
version = "0.2.4"
edition = "2021"
license = "MIT OR Apache-2.0"
homepage = "https://github.com/rust-lang/cargo"
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions crates/cargo-util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
//! Miscellaneous support code used by Cargo.

pub use self::hasher::StableHasher;
pub use self::hex::{hash_u64, short_hash, to_hex};
pub use self::read2::read2;
pub use process_builder::ProcessBuilder;
pub use process_error::{exit_status_to_string, is_simple_exit_code, ProcessError};
pub use sha256::Sha256;

mod hasher;
pub mod hex;
pub mod paths;
mod process_builder;
mod process_error;
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/core/compiler/compile_kind.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::core::Target;
use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;
use crate::util::{Config, StableHasher};
use crate::util::Config;
use anyhow::Context as _;
use cargo_util::StableHasher;
use serde::Serialize;
use std::collections::BTreeSet;
use std::fs;
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/core/compiler/context/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use cargo_util::{short_hash, StableHasher};
use lazycell::LazyCell;
use log::debug;

use super::{BuildContext, CompileKind, Context, FileFlavor, Layout};
use crate::core::compiler::{CompileMode, CompileTarget, CrateType, FileType, Unit};
use crate::core::{Target, TargetKind, Workspace};
use crate::util::{self, CargoResult, StableHasher};
use crate::util::CargoResult;

/// This is a generic version number that can be changed to make
/// backwards-incompatible changes to any file structures in the output
Expand Down Expand Up @@ -185,7 +186,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
/// Used for the metadata when `metadata` returns `None`.
pub fn target_short_hash(&self, unit: &Unit) -> String {
let hashable = unit.pkg.package_id().stable_hash(self.ws.root());
util::short_hash(&(METADATA_VERSION, hashable))
short_hash(&(METADATA_VERSION, hashable))
}

/// Returns the directory where the artifacts for the given unit are
Expand Down
37 changes: 15 additions & 22 deletions src/cargo/core/compiler/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ use std::sync::{Arc, Mutex};
use std::time::SystemTime;

use anyhow::{bail, format_err, Context as _};
use cargo_util::{paths, ProcessBuilder};
use cargo_util::{hash_u64, paths, to_hex, ProcessBuilder, StableHasher};
use filetime::FileTime;
use log::{debug, info};
use serde::de;
Expand All @@ -331,10 +331,9 @@ use serde::{Deserialize, Serialize};

use crate::core::compiler::unit_graph::UnitDep;
use crate::core::Package;
use crate::util;
use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;
use crate::util::{internal, path_args, profile, StableHasher};
use crate::util::{internal, path_args, profile};
use crate::CARGO_ENV;

use super::custom_build::BuildDeps;
Expand Down Expand Up @@ -812,7 +811,7 @@ impl Fingerprint {
if let Some(s) = *self.memoized_hash.lock().unwrap() {
return s;
}
let ret = util::hash_u64(self);
let ret = hash_u64(self);
*self.memoized_hash.lock().unwrap() = Some(ret);
ret
}
Expand Down Expand Up @@ -1160,9 +1159,9 @@ impl DepFingerprint {
// `path` then we just hash the name, but otherwise we hash the full
// id as it won't change when the directory is renamed.
let pkg_id = if dep.unit.pkg.package_id().source_id().is_path() {
util::hash_u64(dep.unit.pkg.package_id().name())
hash_u64(dep.unit.pkg.package_id().name())
} else {
util::hash_u64(dep.unit.pkg.package_id())
hash_u64(dep.unit.pkg.package_id())
};

Ok(DepFingerprint {
Expand Down Expand Up @@ -1309,15 +1308,15 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
}
.to_vec();

let profile_hash = util::hash_u64((
let profile_hash = hash_u64((
&unit.profile,
unit.mode,
cx.bcx.extra_args_for(unit),
cx.lto[unit],
));
// Include metadata since it is exposed as environment variables.
let m = unit.pkg.manifest().metadata();
let metadata = util::hash_u64((&m.authors, &m.description, &m.homepage, &m.repository));
let metadata = hash_u64((&m.authors, &m.description, &m.homepage, &m.repository));
let mut config = StableHasher::new();
if let Some(linker) = cx.bcx.linker(unit.kind) {
linker.hash(&mut config);
Expand All @@ -1332,12 +1331,12 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
}
let compile_kind = unit.kind.fingerprint_hash();
Ok(Fingerprint {
rustc: util::hash_u64(&cx.bcx.rustc().verbose_version),
target: util::hash_u64(&unit.target),
rustc: hash_u64(&cx.bcx.rustc().verbose_version),
target: hash_u64(&unit.target),
profile: profile_hash,
// Note that .0 is hashed here, not .1 which is the cwd. That doesn't
// actually affect the output artifact so there's no need to hash it.
path: util::hash_u64(path_args(cx.bcx.ws, unit).0),
path: hash_u64(path_args(cx.bcx.ws, unit).0),
features: format!("{:?}", unit.features),
deps,
local: Mutex::new(local),
Expand Down Expand Up @@ -1402,7 +1401,7 @@ See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-change

Ok(Fingerprint {
local: Mutex::new(local),
rustc: util::hash_u64(&cx.bcx.rustc().verbose_version),
rustc: hash_u64(&cx.bcx.rustc().verbose_version),
deps,
outputs: if overridden { Vec::new() } else { vec![output] },

Expand Down Expand Up @@ -1532,10 +1531,7 @@ fn build_script_override_fingerprint(
let metadata = cx.get_run_build_script_metadata(unit);
// Returns None if it is not overridden.
let output = build_script_outputs.get(metadata)?;
let s = format!(
"overridden build state with hash: {}",
util::hash_u64(output)
);
let s = format!("overridden build state with hash: {}", hash_u64(output));
Some(LocalFingerprint::Precalculated(s))
}

Expand Down Expand Up @@ -1586,7 +1582,7 @@ fn write_fingerprint(loc: &Path, fingerprint: &Fingerprint) -> CargoResult<()> {
// as we can use the full hash.
let hash = fingerprint.hash_u64();
debug!("write fingerprint ({:x}) : {}", hash, loc.display());
paths::write(loc, util::to_hex(hash).as_bytes())?;
paths::write(loc, to_hex(hash).as_bytes())?;

let json = serde_json::to_string(fingerprint).unwrap();
if cfg!(debug_assertions) {
Expand Down Expand Up @@ -1637,7 +1633,7 @@ fn compare_old_fingerprint(

let new_hash = new_fingerprint.hash_u64();

if util::to_hex(new_hash) == old_fingerprint_short && new_fingerprint.fs_status.up_to_date() {
if to_hex(new_hash) == old_fingerprint_short && new_fingerprint.fs_status.up_to_date() {
return Ok(());
}

Expand All @@ -1646,10 +1642,7 @@ fn compare_old_fingerprint(
.with_context(|| internal("failed to deserialize json"))?;
// Fingerprint can be empty after a failed rebuild (see comment in prepare_target).
if !old_fingerprint_short.is_empty() {
debug_assert_eq!(
util::to_hex(old_fingerprint.hash_u64()),
old_fingerprint_short
);
debug_assert_eq!(to_hex(old_fingerprint.hash_u64()), old_fingerprint_short);
}
let result = new_fingerprint.compare(&old_fingerprint);
assert!(result.is_err());
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use std::thread::{self, Scope};
use std::time::Duration;

use anyhow::{format_err, Context as _};
use cargo_util::ProcessBuilder;
use cargo_util::{hash_u64, ProcessBuilder};
use jobserver::{Acquired, Client, HelperThread};
use log::{debug, trace};
use semver::Version;
Expand Down Expand Up @@ -312,7 +312,7 @@ impl<'cfg> DiagDedupe<'cfg> {
/// Returns `true` if the message was emitted, or `false` if it was
/// suppressed for being a duplicate.
fn emit_diag(&self, diag: &str) -> CargoResult<bool> {
let h = util::hash_u64(diag);
let h = hash_u64(diag);
if !self.seen.borrow_mut().insert(h) {
return Ok(false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/unit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::core::compiler::{unit_dependencies::IsArtifact, CompileKind, CompileMode, CrateType};
use crate::core::manifest::{Target, TargetKind};
use crate::core::{profiles::Profile, Package};
use crate::util::hex::short_hash;
use crate::util::interning::InternedString;
use crate::util::Config;
use cargo_util::hex::short_hash;
use std::cell::RefCell;
use std::collections::HashSet;
use std::fmt;
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use crate::core::{Edition, Feature, Features, WorkspaceConfig};
use crate::util::errors::*;
use crate::util::interning::InternedString;
use crate::util::toml::{TomlManifest, TomlProfiles};
use crate::util::{short_hash, Config, Filesystem};
use crate::util::{Config, Filesystem};
use cargo_util::hex::short_hash;

pub enum EitherManifest {
Real(Manifest),
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/source/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ impl Ord for SourceKind {
fn test_cratesio_hash() {
let config = Config::default().unwrap();
let crates_io = SourceId::crates_io(&config).unwrap();
assert_eq!(crate::util::hex::short_hash(&crates_io), "1ecc6299db9ec823");
assert_eq!(cargo_util::hex::short_hash(&crates_io), "1ecc6299db9ec823");
}

/// A `Display`able view into a `SourceId` that will write it as a url
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/ops/cargo_compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ use crate::ops::resolve::WorkspaceResolve;
use crate::util::config::Config;
use crate::util::interning::InternedString;
use crate::util::restricted_names::is_glob_pattern;
use crate::util::{closest_msg, profile, CargoResult, StableHasher};
use crate::util::{closest_msg, profile, CargoResult};
use cargo_util::StableHasher;

mod compile_filter;
pub use compile_filter::{CompileFilter, FilterRule, LibRule};
Expand Down
10 changes: 5 additions & 5 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
use crate::sources::PathSource;
use crate::util::errors::CargoResult;
use crate::util::toml::TomlManifest;
use crate::util::{self, human_readable_bytes, restricted_names, Config, FileLock};
use crate::util::{human_readable_bytes, restricted_names, Config, FileLock};
use crate::{drop_println, ops};
use anyhow::Context as _;
use cargo_util::paths;
use cargo_util::{hex, paths};
use flate2::read::GzDecoder;
use flate2::{Compression, GzBuilder};
use log::debug;
Expand Down Expand Up @@ -885,13 +885,13 @@ fn hash_all(path: &Path) -> CargoResult<HashMap<PathBuf, u64>> {
let file_type = entry.file_type();
if file_type.is_file() {
let file = File::open(entry.path())?;
let hash = util::hex::hash_u64_file(&file)?;
let hash = hex::hash_u64_file(&file)?;
result.insert(entry.path().to_path_buf(), hash);
} else if file_type.is_symlink() {
let hash = util::hex::hash_u64(&fs::read_link(entry.path())?);
let hash = hex::hash_u64(&fs::read_link(entry.path())?);
result.insert(entry.path().to_path_buf(), hash);
} else if file_type.is_dir() {
let hash = util::hex::hash_u64(&());
let hash = hex::hash_u64(&());
result.insert(entry.path().to_path_buf(), hash);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/sources/git/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::core::{Dependency, Package, PackageId, Summary};
use crate::sources::git::utils::GitRemote;
use crate::sources::PathSource;
use crate::util::errors::CargoResult;
use crate::util::hex::short_hash;
use crate::util::Config;
use anyhow::Context;
use cargo_util::hex::short_hash;
use cargo_util::paths::exclude_from_backups_and_indexing;
use log::trace;
use std::fmt::{self, Debug, Formatter};
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/sources/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ use std::path::{Path, PathBuf};
use std::task::Poll;

use anyhow::Context as _;
use cargo_util::hex;
use cargo_util::paths::exclude_from_backups_and_indexing;
use flate2::read::GzDecoder;
use log::debug;
Expand All @@ -178,7 +179,6 @@ use crate::core::dependency::{DepKind, Dependency};
use crate::core::source::MaybePackage;
use crate::core::{Package, PackageId, QueryKind, Source, SourceId, Summary};
use crate::sources::PathSource;
use crate::util::hex;
use crate::util::interning::InternedString;
use crate::util::into_url::IntoUrl;
use crate::util::network::PollExt;
Expand Down
4 changes: 0 additions & 4 deletions src/cargo/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ pub use self::errors::CliError;
pub use self::errors::{internal, CargoResult, CliResult};
pub use self::flock::{FileLock, Filesystem};
pub use self::graph::Graph;
pub use self::hasher::StableHasher;
pub use self::hex::{hash_u64, short_hash, to_hex};
pub use self::into_url::IntoUrl;
pub use self::into_url_with_base::IntoUrlWithBase;
pub(crate) use self::io::LimitErrorReader;
Expand Down Expand Up @@ -40,8 +38,6 @@ pub mod diagnostic_server;
pub mod errors;
mod flock;
pub mod graph;
mod hasher;
pub mod hex;
pub mod important_paths;
pub mod interning;
pub mod into_url;
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/util/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use std::path::{Path, PathBuf};
use std::sync::Mutex;

use anyhow::Context as _;
use cargo_util::{paths, ProcessBuilder, ProcessError};
use cargo_util::{paths, ProcessBuilder, ProcessError, StableHasher};
use log::{debug, info, warn};
use serde::{Deserialize, Serialize};

use crate::util::interning::InternedString;
use crate::util::{profile, CargoResult, StableHasher};
use crate::util::{profile, CargoResult};

/// Information on the `rustc` executable
#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2593,7 +2593,7 @@ fn package_lock_inside_package_is_overwritten() {
p.cargo("build").run();

let id = SourceId::for_registry(registry.index_url()).unwrap();
let hash = cargo::util::hex::short_hash(&id);
let hash = cargo_util::hex::short_hash(&id);
let ok = cargo_home()
.join("registry")
.join("src")
Expand Down Expand Up @@ -2631,7 +2631,7 @@ fn package_lock_as_a_symlink_inside_package_is_overwritten() {
p.cargo("build").run();

let id = SourceId::for_registry(registry.index_url()).unwrap();
let hash = cargo::util::hex::short_hash(&id);
let hash = cargo_util::hex::short_hash(&id);
let pkg_root = cargo_home()
.join("registry")
.join("src")
Expand Down

0 comments on commit 6c9d20f

Please sign in to comment.