Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ guests: $(GUEST_TARGETS)
.PHONY: dummy-guests
dummy-guests: $(DUMMY_GUEST_TARGETS)

# Build a specific guest example. Optional FEATURES variable can be used to enable cargo features.
# Usage: make guest-swap-info FEATURES="asset-hub"
guest-%:
mkdir -p output
cd guest-examples; METADATA_OUTPUT_DIR=$(shell pwd)/output cargo build --release --bin $* -p $*
cd guest-examples; METADATA_OUTPUT_DIR=$(shell pwd)/output cargo build --release --bin $* -p $* $(if $(FEATURES),--features $(FEATURES))
polkatool link --run-only-if-newer -s guest-examples/target/riscv32emac-unknown-none-polkavm/release/$* -o output/$*.polkavm

dummy-guest-%:
Expand Down
2 changes: 1 addition & 1 deletion guest-examples/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build]
target = "../vendor/polkavm/crates/polkavm-linker/riscv32emac-unknown-none-polkavm.json"

rustflags = ["-D", "warnings"]
rustflags = ["-D", "warnings", "--cfg", "substrate_runtime"]

[unstable]
build-std = ["core", "alloc"]
6 changes: 5 additions & 1 deletion guest-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ scale-info = { version = "2.11.3", default-features = false, features = [
pvq-program = { path = "../pvq-program", default-features = false }
pvq-program-metadata-gen = { path = "../pvq-program-metadata-gen" }
polkavm-derive = { path = "../vendor/polkavm/crates/polkavm-derive" }
acala-primitives = { git = "https://github.com/AcalaNetwork/Acala", branch = "master", default-features = false }
xcm = { version = "16.2.0", default-features = false, package = "staging-xcm" }
sp-io = { version = "40.0.1", default-features = false, features = [
"disable_allocator",
"disable_panic_handler",
] }
cfg-if = "1.0"
30 changes: 6 additions & 24 deletions guest-examples/sum-balance/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,12 @@
#[pvq_program::program]
mod sum_balance {

cfg_if::cfg_if! {
if #[cfg(feature = "option_version_1")] {
/// Represents a unique identifier for an account.
type AccountId = [u8; 64];
/// Represents a unique identifier for an asset.
type AssetId = u64;
/// Represents the balance of an asset.
type Balance = u128;
} else if #[cfg(feature = "option_version_2")] {
/// Represents a unique identifier for an account.
type AccountId = [u8; 32];
/// Represents a unique identifier for an asset.
type AssetId = u32;
/// Represents the balance of an asset.
type Balance = u64;
} else {
/// Represents a unique identifier for an account.
type AccountId = [u8; 32];
/// Represents a unique identifier for an asset.
type AssetId = u32;
/// Represents the balance of an asset.
type Balance = u64;
}
}
/// Represents a unique identifier for an account.
type AccountId = [u8; 32];
/// Represents a unique identifier for an asset.
type AssetId = u32;
/// Represents the balance of an asset.
type Balance = u64;

/// Get the balance of a given asset for a specific account.
///
Expand Down
8 changes: 6 additions & 2 deletions guest-examples/swap-info/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use std::process::Command;

fn main() {
// Tell Cargo to rerun this build script if the source file changes
// println!("cargo:rerun-if-changed=src/main.rs");
println!("cargo:rerun-if-changed=src/main.rs");
let current_dir = env::current_dir().expect("Failed to get current directory");
// Determine the output directory for the metadata
let output_dir = PathBuf::from(env::var("METADATA_OUTPUT_DIR").expect("METADATA_OUTPUT_DIR is not set"));
let output_dir = PathBuf::from(env::var("METADATA_OUTPUT_DIR").expect("METADATA_OUTPUT_DIR is not set"))
.canonicalize()
.expect("Failed to canonicalize output directory");

// Build and run the command
let status = Command::new("pvq-program-metadata-gen")
Expand All @@ -16,6 +18,8 @@ fn main() {
.arg("--output-dir")
.arg(&output_dir)
.env("RUST_LOG", "info")
.stdout(std::process::Stdio::inherit())
.stderr(std::process::Stdio::inherit())
.status()
.expect("Failed to execute pvq-program-metadata-gen");

Expand Down
2 changes: 1 addition & 1 deletion pvq-program-metadata-gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pvq-program-metadata-gen"
description = "PVQ program metadata generation"
version = "0.2.0"
version = "0.5.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
59 changes: 46 additions & 13 deletions pvq-program-metadata-gen/src/bin/pvq-program-metadata-gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ struct Args {

#[arg(short, long)]
output_dir: PathBuf,

#[arg(short, long)]
manifest_path: Option<PathBuf>,

/// Target triple to build for (optional)
#[arg(long)]
target: Option<String>,
}

fn main() {
Expand All @@ -25,24 +32,41 @@ fn main() {
// Logging arguments
info!("Generating metadata for program at: {}", args.crate_path.display());
info!("Output dir: {}", args.output_dir.display());
if let Some(ref manifest_path) = args.manifest_path {
info!("Manifest path: {}", manifest_path.display());
} else {
info!("Using default manifest (no manifest path provided)");
}

// Create a temp crate for the metadata generation
let temp_dir = tempfile::tempdir().expect("Failed to create temp directory");
let temp_crate_path = temp_dir.path();
fs::create_dir_all(temp_crate_path).expect("Failed to create `temp_crate` directory");
info!("Temp crate path: {}", temp_crate_path.display());

// Extract features section from the original manifest
let original_manifest_content =
std::fs::read_to_string(args.crate_path.join("Cargo.toml")).expect("Failed to read original Cargo.toml");
let optional_features = pvq_program_metadata_gen::extract_features(&original_manifest_content)
.expect("Failed to extract features section from the original Cargo.toml");
debug!("Features section: {:?}", optional_features);
// Read or create the manifest content
let (manifest_content, optional_features) = if let Some(ref manifest_path) = args.manifest_path {
// Read the manifest from the provided manifest path
let content = std::fs::read_to_string(manifest_path)
.unwrap_or_else(|_| panic!("Failed to read manifest file: {}", manifest_path.display()));
debug!("Manifest content: {}", content);

// Extract features section from the manifest for active features determination
let features = pvq_program_metadata_gen::extract_features(&content)
.expect("Failed to extract features section from the manifest");
debug!("Features section: {:?}", features);

// Create Cargo.toml with features from the original crate
let manifest = pvq_program_metadata_gen::create_manifest(optional_features.as_ref());
debug!("Manifest: {}", manifest);
std::fs::write(temp_crate_path.join("Cargo.toml"), manifest).expect("Failed to write Cargo.toml");
(content, features)
} else {
// Use the default manifest from create_manifest
let content = pvq_program_metadata_gen::create_manifest(None);
debug!("Generated default manifest content: {}", content);
(content, None)
};

// Copy the manifest to temp directory
std::fs::write(temp_crate_path.join("Cargo.toml"), &manifest_content)
.expect("Failed to write Cargo.toml to temp directory");

// Add active features to the cargo command
let active_features = pvq_program_metadata_gen::get_active_features(optional_features.as_ref())
Expand All @@ -66,11 +90,20 @@ fn main() {
fs::write(temp_crate_path.join("src/main.rs"), metadata_gen_src.to_string())
.expect("Failed to write metadata generator source code");

// Compile and run the metadata generator in one step
let mut cargo_cmd = Command::new("cargo");
cargo_cmd.current_dir(temp_crate_path).args(["run"]);
for feature in active_features {
cargo_cmd.arg("--features").arg(feature);

// Add target if specified
if let Some(ref target) = args.target {
info!("Using explicit target: {}", target);
cargo_cmd.arg("--target").arg(target);
}

if !active_features.is_empty() {
cargo_cmd.arg("--features");
for feature in active_features {
cargo_cmd.arg(feature);
}
}
info!("Compiling and running metadata generator...");
let status = cargo_cmd.status().expect("Failed to run metadata generator");
Expand Down
Loading