Skip to content

Commit

Permalink
Add Bazel CrateId hash method
Browse files Browse the repository at this point in the history
  • Loading branch information
irajtaghlidi committed Aug 14, 2024
1 parent 2f08d21 commit b057c5a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
2 changes: 1 addition & 1 deletion mbedtls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "mbedtls"
# we decided not to support.
version = "0.12.3"
authors = ["Jethro Beekman <[email protected]>"]
build = "build.rs"
build = "build/build.rs"
edition = "2018"
license = "Apache-2.0 OR GPL-2.0-or-later"
description = """
Expand Down
24 changes: 20 additions & 4 deletions mbedtls/build.rs → mbedtls/build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* option. This file may not be copied, modified, or distributed except
* according to those terms. */

mod crate_id;

use std::collections::{HashMap, HashSet};
use std::env;

Expand All @@ -15,13 +17,27 @@ use rustc_version::Channel;
// If there's a panic in this code block, that means Cargo's way of running the
// build script has changed, and this code should be updated to handle the new
// case.
// Since Bazel does not use Cargo to generate Crate Metadata hash we need to implement a separate one for it.
fn get_compilation_metadata_hash() -> String {
let out_dir: std::path::PathBuf = std::env::var_os("OUT_DIR").unwrap().into();
let mut out_dir_it = out_dir.iter().rev();
assert_eq!(out_dir_it.next().unwrap(), "out");
let crate_ = out_dir_it.next().unwrap().to_string_lossy();
assert!(crate_.starts_with("mbedtls-"));
crate_[8..].to_owned()
let next = out_dir_it.next().unwrap();

if next == "out" {
let crate_ = out_dir_it.next().unwrap().to_string_lossy();
assert!(crate_.starts_with("mbedtls-"));
return crate_[8..].to_owned();
} else if next == "_bs.out_dir" {
let compiler_version = rustc_version::version().expect("Failed to get rustc version").to_string();
let version = env!("CARGO_PKG_VERSION");
let versioned_string = format!("mbedtls_{}", version);
let metadata = vec!["".to_string()];
let stable_crate_id = crate_id::StableCrateId::new(&versioned_string, false, metadata, compiler_version);

return stable_crate_id.to_string();
} else {
panic!("Unexpected directory structure: {:?}", next);
}
}

fn main() {
Expand Down
52 changes: 52 additions & 0 deletions mbedtls/build/crate_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::fmt;

pub struct StableCrateId(u64);

impl StableCrateId {
pub fn new(
crate_name: &str,
is_exe: bool,
mut metadata: Vec<String>,
cfg_version: String,
) -> StableCrateId {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

let mut hasher = DefaultHasher::new();
crate_name.hash(&mut hasher);

metadata.sort();
metadata.dedup();

hasher.write(b"metadata");
for s in &metadata {
hasher.write_usize(s.len());
hasher.write(s.as_bytes());
}

hasher.write(if is_exe { b"exe" } else { b"lib" });

hasher.write(cfg_version.as_bytes());

StableCrateId(hasher.finish() as u64)
}

pub fn as_u64(self) -> u64 {
self.0
}
}

// Implement the LowerHex trait for StableCrateId
impl fmt::LowerHex for StableCrateId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::LowerHex::fmt(&self.0, f)
}
}

impl fmt::Display for StableCrateId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Implement the formatting logic here
// Example: Display the inner value as a hexadecimal string
write!(f, "{:x}", self.0)
}
}

0 comments on commit b057c5a

Please sign in to comment.