Skip to content

Specify metadata location via env #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025
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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions guest-examples/sum-balance-percent/build.rs
Original file line number Diff line number Diff line change
@@ -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(&current_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");
}
}
3 changes: 1 addition & 2 deletions guest-examples/sum-balance-percent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AccountId>) -> Balance {
fn sum_balance(asset: AssetId, accounts: alloc::vec::Vec<AccountId>) -> Balance {
let mut sum_balance = 0;
for account in accounts {
sum_balance += balance(asset, account);
Expand Down
4 changes: 3 additions & 1 deletion guest-examples/sum-balance/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions pvq-program-metadata-gen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Expand Down
40 changes: 20 additions & 20 deletions pvq-program-metadata-gen/src/bin/pvq-program-metadata-gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions pvq-program-metadata-gen/src/metadata_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<proc_macro2::TokenStream> {
pub fn metadata_gen_src(source: &str, pkg_name: &str, output_dir: &str) -> syn::Result<proc_macro2::TokenStream> {
// Parse the source code
let mut syntax = syn::parse_file(source)?;

Expand Down Expand Up @@ -102,8 +102,8 @@ pub fn metadata_gen_src(source: &str, output_dir: &str) -> syn::Result<proc_macr
let encoded = parity_scale_codec::Encode::encode(&metadata);
let json = serde_json::to_string(&metadata).expect("Failed to serialize metadata to JSON");

let bin_path = Path::new(#output_dir).join("metadata.bin");
let json_path = Path::new(#output_dir).join("metadata.json");
let bin_path = Path::new(#output_dir).join(format!("{}-metadata.bin", #pkg_name));
let json_path = Path::new(#output_dir).join(format!("{}-metadata.json", #pkg_name));

// Write the binary format
std::fs::write(bin_path, &encoded).expect("Failed to write binary metadata");
Expand Down