From 786cb64db50f2ad57e6d9face3ccfe66621e57eb Mon Sep 17 00:00:00 2001 From: indirection42 Date: Wed, 30 Apr 2025 11:40:42 +0800 Subject: [PATCH 1/2] Specify metadata location via env --- Makefile | 2 +- guest-examples/sum-balance-percent/build.rs | 25 ++++++++++++ .../sum-balance-percent/src/main.rs | 3 +- guest-examples/sum-balance/build.rs | 4 +- pvq-program-metadata-gen/README.md | 4 +- .../src/bin/pvq-program-metadata-gen.rs | 40 +++++++++---------- pvq-program-metadata-gen/src/metadata_gen.rs | 6 +-- 7 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 guest-examples/sum-balance-percent/build.rs diff --git a/Makefile b/Makefile index 90dca01..2abd420 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ guests: $(GUEST_TARGETS) dummy-guests: $(DUMMY_GUEST_TARGETS) guest-%: - cd guest-examples; cargo build -q --release --bin guest-$* -p guest-$* + cd guest-examples; METADATA_OUTPUT_DIR=$(realpath output) cargo build -q --release --bin guest-$* -p guest-$* mkdir -p output polkatool link --run-only-if-newer -s guest-examples/target/riscv32emac-unknown-none-polkavm/release/guest-$* -o output/guest-$*.polkavm diff --git a/guest-examples/sum-balance-percent/build.rs b/guest-examples/sum-balance-percent/build.rs new file mode 100644 index 0000000..592da70 --- /dev/null +++ b/guest-examples/sum-balance-percent/build.rs @@ -0,0 +1,25 @@ +use std::env; +use std::path::PathBuf; +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"); + 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")); + + // Build and run the command + let status = Command::new("pvq-program-metadata-gen") + .arg("--crate-path") + .arg(¤t_dir) + .arg("--output-dir") + .arg(&output_dir) + .env("RUST_LOG", "info") + .status() + .expect("Failed to execute pvq-program-metadata-gen"); + + if !status.success() { + panic!("Failed to generate program metadata"); + } +} diff --git a/guest-examples/sum-balance-percent/src/main.rs b/guest-examples/sum-balance-percent/src/main.rs index 562fc0b..3468a24 100644 --- a/guest-examples/sum-balance-percent/src/main.rs +++ b/guest-examples/sum-balance-percent/src/main.rs @@ -6,14 +6,13 @@ mod sum_balance_percent { type AssetId = u32; type AccountId = [u8; 32]; type Balance = u64; - use alloc::vec::Vec; #[program::extension_fn(extension_id = 4071833530116166512u64, fn_index = 1)] fn balance(asset: AssetId, who: AccountId) -> Balance {} #[program::extension_fn(extension_id = 4071833530116166512u64, fn_index = 0)] fn total_supply(asset: AssetId) -> Balance {} #[program::entrypoint] - fn sum_balance(asset: AssetId, accounts: Vec) -> Balance { + fn sum_balance(asset: AssetId, accounts: alloc::vec::Vec) -> Balance { let mut sum_balance = 0; for account in accounts { sum_balance += balance(asset, account); diff --git a/guest-examples/sum-balance/build.rs b/guest-examples/sum-balance/build.rs index 8622dfd..00031b3 100644 --- a/guest-examples/sum-balance/build.rs +++ b/guest-examples/sum-balance/build.rs @@ -7,7 +7,9 @@ fn main() { 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("OUT_DIR").expect("OUT_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") diff --git a/pvq-program-metadata-gen/README.md b/pvq-program-metadata-gen/README.md index adb8cf2..6b23f6c 100644 --- a/pvq-program-metadata-gen/README.md +++ b/pvq-program-metadata-gen/README.md @@ -21,7 +21,7 @@ pvq-program-metadata-gen --crate-path /path/to/your/crate --output-dir /path/to/ ### Arguments - `--crate-path, -c`: Path to the crate directory containing a PVQ program -- `--output-dir, -o`: Output directory for the metadata file, typically `OUT_DIR` specified in your `build.rs` file +- `--output-dir, -o`: Output directory for the metadata file, typically `METADATA_OUTPUT_DIR` environment variable read by `build.rs` ## Integration with Build Scripts @@ -36,7 +36,7 @@ fn main() { // Tell Cargo to rerun this build script if the source file changes 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("OUT_DIR").expect("OUT_DIR is not set")); + let output_dir = PathBuf::from(env::var("METADATA_OUTPUT_DIR").expect("METADATA_OUTPUT_DIR is not set")); // Build and run the command let status = Command::new("pvq-program-metadata-gen") diff --git a/pvq-program-metadata-gen/src/bin/pvq-program-metadata-gen.rs b/pvq-program-metadata-gen/src/bin/pvq-program-metadata-gen.rs index f7e642c..e8f12a6 100644 --- a/pvq-program-metadata-gen/src/bin/pvq-program-metadata-gen.rs +++ b/pvq-program-metadata-gen/src/bin/pvq-program-metadata-gen.rs @@ -32,13 +32,32 @@ fn main() { 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); + + // 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"); + + // Add active features to the cargo command + let active_features = pvq_program_metadata_gen::get_active_features(optional_features.as_ref()) + .expect("Failed to get active features"); + debug!("Active features: {:?}", active_features); + // Read the program source let source = fs::read_to_string(args.crate_path.join("src/main.rs")) .expect("Failed to read pvq program source file, expected `src/main.rs`"); + let pkg_name = std::env::var("CARGO_PKG_NAME").expect("CARGO_PKG_NAME is not set"); + // Generate the metadata generator source codes let metadata_gen_src = - pvq_program_metadata_gen::metadata_gen_src(&source, args.output_dir.to_string_lossy().as_ref()) + pvq_program_metadata_gen::metadata_gen_src(&source, &pkg_name, args.output_dir.to_string_lossy().as_ref()) .expect("Failed to generate metadata generator source code"); debug!("Metadata generator source code: {}", metadata_gen_src); @@ -47,31 +66,12 @@ fn main() { fs::write(temp_crate_path.join("src/main.rs"), metadata_gen_src.to_string()) .expect("Failed to write metadata generator source code"); - // 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); - - // 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"); - // 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"]); - - // Add active features to the cargo command - let active_features = pvq_program_metadata_gen::get_active_features(optional_features.as_ref()) - .expect("Failed to get active features"); - debug!("Active features: {:?}", active_features); for feature in active_features { cargo_cmd.arg("--features").arg(feature); } - - // Compile and run the metadata generator in one step info!("Compiling and running metadata generator..."); let status = cargo_cmd.status().expect("Failed to run metadata generator"); if !status.success() { diff --git a/pvq-program-metadata-gen/src/metadata_gen.rs b/pvq-program-metadata-gen/src/metadata_gen.rs index 02837d7..0e00607 100644 --- a/pvq-program-metadata-gen/src/metadata_gen.rs +++ b/pvq-program-metadata-gen/src/metadata_gen.rs @@ -4,7 +4,7 @@ use syn::spanned::Spanned; type ExtensionId = u64; type FnIndex = u8; -pub fn metadata_gen_src(source: &str, output_dir: &str) -> syn::Result { +pub fn metadata_gen_src(source: &str, pkg_name: &str, output_dir: &str) -> syn::Result { // Parse the source code let mut syntax = syn::parse_file(source)?; @@ -102,8 +102,8 @@ pub fn metadata_gen_src(source: &str, output_dir: &str) -> syn::Result Date: Wed, 30 Apr 2025 11:59:37 +0800 Subject: [PATCH 2/2] clippy fix --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 2abd420..6cdcf85 100644 --- a/Makefile +++ b/Makefile @@ -40,9 +40,9 @@ check: check-wasm SKIP_WASM_BUILD= cargo check cd pvq-program/examples; cargo check -clippy: pvq-program-metadata-gen +clippy: SKIP_WASM_BUILD= cargo clippy -- -D warnings - cd guest-examples; cargo clippy --all + cd guest-examples; METADATA_OUTPUT_DIR=$(realpath output) cargo clippy --all test: SKIP_WASM_BUILD= cargo test