diff --git a/Cargo.lock b/Cargo.lock index da43bb8..db2ac2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2967,7 +2967,7 @@ dependencies = [ [[package]] name = "pvq-program-metadata-gen" -version = "0.2.0" +version = "0.5.0" dependencies = [ "clap", "parity-scale-codec", diff --git a/Makefile b/Makefile index d0b72d0..d16021d 100644 --- a/Makefile +++ b/Makefile @@ -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-%: diff --git a/guest-examples/.cargo/config.toml b/guest-examples/.cargo/config.toml index c4e725a..34d0439 100644 --- a/guest-examples/.cargo/config.toml +++ b/guest-examples/.cargo/config.toml @@ -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"] diff --git a/guest-examples/Cargo.toml b/guest-examples/Cargo.toml index efd93ab..583c6cd 100644 --- a/guest-examples/Cargo.toml +++ b/guest-examples/Cargo.toml @@ -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" diff --git a/guest-examples/sum-balance/src/main.rs b/guest-examples/sum-balance/src/main.rs index 737c1a0..e343df9 100644 --- a/guest-examples/sum-balance/src/main.rs +++ b/guest-examples/sum-balance/src/main.rs @@ -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. /// diff --git a/guest-examples/swap-info/build.rs b/guest-examples/swap-info/build.rs index 0b48870..078f8e4 100644 --- a/guest-examples/swap-info/build.rs +++ b/guest-examples/swap-info/build.rs @@ -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") @@ -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"); diff --git a/pvq-program-metadata-gen/Cargo.toml b/pvq-program-metadata-gen/Cargo.toml index 88c1201..83e0824 100644 --- a/pvq-program-metadata-gen/Cargo.toml +++ b/pvq-program-metadata-gen/Cargo.toml @@ -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 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 e8f12a6..f06aec0 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 @@ -13,6 +13,13 @@ struct Args { #[arg(short, long)] output_dir: PathBuf, + + #[arg(short, long)] + manifest_path: Option, + + /// Target triple to build for (optional) + #[arg(long)] + target: Option, } fn main() { @@ -25,6 +32,11 @@ 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"); @@ -32,17 +44,29 @@ 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); + // 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()) @@ -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");