-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[package] | ||
name = "package-loader" | ||
description = "The implementaton of a package builder and loader for efficient runtime package loading." | ||
version.workspace = true | ||
edition.workspace = true | ||
build = "build.rs" | ||
|
||
[dependencies] | ||
radix-engine-common = { workspace = true } | ||
radix-engine-queries = { workspace = true } | ||
|
||
lazy_static = { verison = "1.4.0", optional = true } | ||
getrandom = { version = "0.2.12", features = ["js"] } | ||
|
||
[build-dependencies] | ||
walkdir = { version = "2.3.3", optional = true } | ||
cargo_toml = { version = "0.18.0", optional = true } | ||
|
||
scrypto-unit = { workspace = true, optional = true } | ||
radix-engine-interface = { workspace = true, optional = true } | ||
|
||
[features] | ||
default = ["build-time-blueprints"] | ||
build-time-blueprints = [ | ||
"dep:lazy_static", | ||
"dep:walkdir", | ||
"dep:cargo_toml", | ||
"dep:radix-engine-interface", | ||
"dep:scrypto-unit", | ||
] | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
fn main() { | ||
build_blueprints(); | ||
} | ||
|
||
#[cfg(not(feature = "build-time-blueprints"))] | ||
fn build_blueprints() {} | ||
|
||
#[cfg(feature = "build-time-blueprints")] | ||
fn build_blueprints() { | ||
use std::env; | ||
use std::path::PathBuf; | ||
|
||
use cargo_toml::{Manifest, Package}; | ||
use radix_engine_interface::prelude::*; | ||
|
||
let manifest_dir = PathBuf::from_str(env!("CARGO_MANIFEST_DIR")).unwrap(); | ||
let blueprints_dir = manifest_dir.parent().unwrap(); | ||
println!("cargo:rerun-if-changed=\"{:?}\"", blueprints_dir); | ||
|
||
let mut packages = HashMap::new(); | ||
for entry in walkdir::WalkDir::new(blueprints_dir) { | ||
let Ok(entry) = entry else { | ||
continue; | ||
}; | ||
let path = entry.path(); | ||
if !path | ||
.file_name() | ||
.map_or(false, |file_name| file_name == "Cargo.toml") | ||
{ | ||
continue; | ||
} | ||
|
||
let manifest = Manifest::from_path(path).unwrap(); | ||
if !manifest | ||
.dependencies | ||
.into_iter() | ||
.any(|(name, _)| name == "scrypto") | ||
{ | ||
continue; | ||
} | ||
|
||
let Some(Package { name, .. }) = manifest.package else { | ||
continue; | ||
}; | ||
|
||
let (code, definition) = scrypto_unit::Compile::compile_with_env_vars( | ||
path.parent().unwrap(), | ||
btreemap! { | ||
"RUSTFLAGS".to_owned() => "".to_owned(), | ||
"CARGO_ENCODED_RUSTFLAGS".to_owned() => "".to_owned(), | ||
"LLVM_PROFILE_FILE".to_owned() => "".to_owned() | ||
}, | ||
); | ||
packages.insert(name, (code, definition)); | ||
} | ||
|
||
let out_dir = | ||
PathBuf::from_str(env::var("OUT_DIR").unwrap().as_str()).unwrap(); | ||
let compilation_path = out_dir.join("compiled_packages.bin"); | ||
|
||
let encoded_packages = scrypto_encode(&packages).unwrap(); | ||
std::fs::write(compilation_path, encoded_packages).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#[cfg(feature = "build-time-blueprints")] | ||
#[allow(unused, clippy::module_inception)] | ||
mod package_loader { | ||
use radix_engine_common::prelude::*; | ||
use radix_engine_queries::typed_substate_layout::*; | ||
|
||
const PACKAGES_BINARY: &[u8] = | ||
include_bytes!(concat!(env!("OUT_DIR"), "/compiled_packages.bin")); | ||
|
||
lazy_static::lazy_static! { | ||
static ref PACKAGES: HashMap<String, (Vec<u8>, PackageDefinition)> = { | ||
scrypto_decode(PACKAGES_BINARY).unwrap() | ||
}; | ||
} | ||
|
||
pub struct PackageLoader; | ||
impl PackageLoader { | ||
pub fn get(name: &str) -> (Vec<u8>, PackageDefinition) { | ||
if let Some(rtn) = PACKAGES.get(name) { | ||
rtn.clone() | ||
} else { | ||
panic!("Package \"{}\" not found. Are you sure that this package is: a) in the blueprints folder, b) that this is the same as the package name in the Cargo.toml file?", name) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "build-time-blueprints"))] | ||
#[allow(unused, clippy::module_inception)] | ||
mod package_loader { | ||
use radix_engine_common::prelude::*; | ||
use radix_engine_queries::typed_substate_layout::*; | ||
use std::path::PathBuf; | ||
|
||
pub struct PackageLoader; | ||
impl PackageLoader { | ||
pub fn get(name: &str) -> (Vec<u8>, PackageDefinition) { | ||
let package_dir = PathBuf::from_str(env!("CARGO_MANIFEST_DIR")) | ||
.unwrap() | ||
.parent() | ||
.unwrap() | ||
.join(name); | ||
scrypto_unit::Compile::compile(package_dir) | ||
} | ||
} | ||
} | ||
|
||
pub use package_loader::PackageLoader; |