mirror of
https://github.com/fluencelabs/wasmer
synced 2025-03-31 06:51:04 +00:00
generate the hash at compile time
This commit is contained in:
parent
874b613f56
commit
8e5f250ed0
@ -46,3 +46,5 @@ field-offset = "0.1.1"
|
|||||||
[features]
|
[features]
|
||||||
debug = []
|
debug = []
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
blake2b_simd = "0.4.1"
|
26
lib/runtime-core/build.rs
Normal file
26
lib/runtime-core/build.rs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
use blake2b_simd::blake2bp;
|
||||||
|
use std::{fs, io::Write, path::PathBuf};
|
||||||
|
|
||||||
|
const WASMER_VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut state = blake2bp::State::new();
|
||||||
|
state.update(WASMER_VERSION.as_bytes());
|
||||||
|
|
||||||
|
let hasher = state.finalize();
|
||||||
|
let hash_string = hasher.to_hex().as_str().to_owned();
|
||||||
|
|
||||||
|
let crate_dir = env!("CARGO_MANIFEST_DIR");
|
||||||
|
let wasmer_version_hash_file = {
|
||||||
|
let mut path = PathBuf::from(&crate_dir);
|
||||||
|
path.push("wasmer_version_hash.txt");
|
||||||
|
path
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut f_out = fs::File::create(wasmer_version_hash_file)
|
||||||
|
.expect("Could not create file for wasmer hash value");
|
||||||
|
|
||||||
|
f_out
|
||||||
|
.write_all(hash_string.as_bytes())
|
||||||
|
.expect("Could not write to file for wasmer hash value");
|
||||||
|
}
|
@ -208,13 +208,5 @@ pub trait Cache {
|
|||||||
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), Self::StoreError>;
|
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), Self::StoreError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const WASMER_VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
/// A unique ID generated from the version of Wasmer for use with cache versioning
|
||||||
|
pub const WASMER_VERSION_HASH: &'static str = include_str!("../wasmer_version_hash.txt");
|
||||||
/// Returns a unique ID generated from the version of Wasmer for use with cache versioning
|
|
||||||
pub fn cache_versioned_sub_directory() -> String {
|
|
||||||
let mut state = blake2bp::State::new();
|
|
||||||
state.update(WASMER_VERSION.as_bytes());
|
|
||||||
|
|
||||||
let hasher = state.finalize();
|
|
||||||
hasher.to_hex().as_str().to_owned()
|
|
||||||
}
|
|
||||||
|
1
lib/runtime-core/wasmer_version_hash.txt
Normal file
1
lib/runtime-core/wasmer_version_hash.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
beb6542931f7f7c823b8d1804b12639fd794711af1f90263c5ed7d338bf4429d1cbe69a531f18144f208c33c49ca01315ffa2b23c170e4c979b1114ffcfbba54
|
@ -7,7 +7,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use wasmer_runtime_core::cache::Error as CacheError;
|
use wasmer_runtime_core::cache::Error as CacheError;
|
||||||
pub use wasmer_runtime_core::cache::{cache_versioned_sub_directory, Artifact, Cache, WasmHash};
|
pub use wasmer_runtime_core::cache::{Artifact, Cache, WasmHash, WASMER_VERSION_HASH};
|
||||||
|
|
||||||
/// Representation of a directory that contains compiled wasm artifacts.
|
/// Representation of a directory that contains compiled wasm artifacts.
|
||||||
///
|
///
|
||||||
@ -36,7 +36,6 @@ pub use wasmer_runtime_core::cache::{cache_versioned_sub_directory, Artifact, Ca
|
|||||||
/// ```
|
/// ```
|
||||||
pub struct FileSystemCache {
|
pub struct FileSystemCache {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
versioned_sub_directory: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileSystemCache {
|
impl FileSystemCache {
|
||||||
@ -48,16 +47,12 @@ impl FileSystemCache {
|
|||||||
/// stored in this cache haven't been corrupted or tampered with.
|
/// stored in this cache haven't been corrupted or tampered with.
|
||||||
pub unsafe fn new<P: Into<PathBuf>>(path: P) -> io::Result<Self> {
|
pub unsafe fn new<P: Into<PathBuf>>(path: P) -> io::Result<Self> {
|
||||||
let path: PathBuf = path.into();
|
let path: PathBuf = path.into();
|
||||||
let versioned_sub_directory = cache_versioned_sub_directory();
|
|
||||||
|
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
let metadata = path.metadata()?;
|
let metadata = path.metadata()?;
|
||||||
if metadata.is_dir() {
|
if metadata.is_dir() {
|
||||||
if !metadata.permissions().readonly() {
|
if !metadata.permissions().readonly() {
|
||||||
Ok(Self {
|
Ok(Self { path })
|
||||||
path,
|
|
||||||
versioned_sub_directory,
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
// This directory is readonly.
|
// This directory is readonly.
|
||||||
Err(io::Error::new(
|
Err(io::Error::new(
|
||||||
@ -78,10 +73,7 @@ impl FileSystemCache {
|
|||||||
} else {
|
} else {
|
||||||
// Create the directory and any parent directories if they don't yet exist.
|
// Create the directory and any parent directories if they don't yet exist.
|
||||||
create_dir_all(&path)?;
|
create_dir_all(&path)?;
|
||||||
Ok(Self {
|
Ok(Self { path })
|
||||||
path,
|
|
||||||
versioned_sub_directory,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -93,7 +85,7 @@ impl Cache for FileSystemCache {
|
|||||||
fn load(&self, key: WasmHash) -> Result<Module, CacheError> {
|
fn load(&self, key: WasmHash) -> Result<Module, CacheError> {
|
||||||
let filename = key.encode();
|
let filename = key.encode();
|
||||||
let mut new_path_buf = self.path.clone();
|
let mut new_path_buf = self.path.clone();
|
||||||
new_path_buf.push(&self.versioned_sub_directory);
|
new_path_buf.push(WASMER_VERSION_HASH);
|
||||||
new_path_buf.push(filename);
|
new_path_buf.push(filename);
|
||||||
let file = File::open(new_path_buf)?;
|
let file = File::open(new_path_buf)?;
|
||||||
let mmap = unsafe { Mmap::map(&file)? };
|
let mmap = unsafe { Mmap::map(&file)? };
|
||||||
@ -105,7 +97,7 @@ impl Cache for FileSystemCache {
|
|||||||
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), CacheError> {
|
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), CacheError> {
|
||||||
let filename = key.encode();
|
let filename = key.encode();
|
||||||
let mut new_path_buf = self.path.clone();
|
let mut new_path_buf = self.path.clone();
|
||||||
new_path_buf.push(&self.versioned_sub_directory);
|
new_path_buf.push(WASMER_VERSION_HASH);
|
||||||
new_path_buf.push(filename);
|
new_path_buf.push(filename);
|
||||||
|
|
||||||
let serialized_cache = module.cache()?;
|
let serialized_cache = module.cache()?;
|
||||||
|
@ -12,9 +12,7 @@ use structopt::StructOpt;
|
|||||||
use wasmer::webassembly::InstanceABI;
|
use wasmer::webassembly::InstanceABI;
|
||||||
use wasmer::*;
|
use wasmer::*;
|
||||||
use wasmer_emscripten;
|
use wasmer_emscripten;
|
||||||
use wasmer_runtime::cache::{
|
use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH};
|
||||||
cache_versioned_sub_directory, Cache as BaseCache, FileSystemCache, WasmHash,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, StructOpt)]
|
#[derive(Debug, StructOpt)]
|
||||||
#[structopt(name = "wasmer", about = "Wasm execution runtime.")]
|
#[structopt(name = "wasmer", about = "Wasm execution runtime.")]
|
||||||
@ -73,7 +71,7 @@ fn get_cache_dir() -> PathBuf {
|
|||||||
Err(_) => {
|
Err(_) => {
|
||||||
// We use a temporal directory for saving cache files
|
// We use a temporal directory for saving cache files
|
||||||
let mut temp_dir = env::temp_dir();
|
let mut temp_dir = env::temp_dir();
|
||||||
temp_dir.push(cache_versioned_sub_directory());
|
temp_dir.push(WASMER_VERSION_HASH);
|
||||||
temp_dir.push("wasmer");
|
temp_dir.push("wasmer");
|
||||||
temp_dir
|
temp_dir
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user