Skip to content

Commit faf45a2

Browse files
add optional metadata manifest path (#101)
1 parent 16e427f commit faf45a2

File tree

8 files changed

+69
-44
lines changed

8 files changed

+69
-44
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ guests: $(GUEST_TARGETS)
1212
.PHONY: dummy-guests
1313
dummy-guests: $(DUMMY_GUEST_TARGETS)
1414

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

2022
dummy-guest-%:

guest-examples/.cargo/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[build]
22
target = "../vendor/polkavm/crates/polkavm-linker/riscv32emac-unknown-none-polkavm.json"
33

4-
rustflags = ["-D", "warnings"]
4+
rustflags = ["-D", "warnings", "--cfg", "substrate_runtime"]
55

66
[unstable]
77
build-std = ["core", "alloc"]

guest-examples/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ scale-info = { version = "2.11.3", default-features = false, features = [
1313
pvq-program = { path = "../pvq-program", default-features = false }
1414
pvq-program-metadata-gen = { path = "../pvq-program-metadata-gen" }
1515
polkavm-derive = { path = "../vendor/polkavm/crates/polkavm-derive" }
16-
acala-primitives = { git = "https://github.com/AcalaNetwork/Acala", branch = "master", default-features = false }
16+
xcm = { version = "16.2.0", default-features = false, package = "staging-xcm" }
17+
sp-io = { version = "40.0.1", default-features = false, features = [
18+
"disable_allocator",
19+
"disable_panic_handler",
20+
] }
1721
cfg-if = "1.0"

guest-examples/sum-balance/src/main.rs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,12 @@
66
#[pvq_program::program]
77
mod sum_balance {
88

9-
cfg_if::cfg_if! {
10-
if #[cfg(feature = "option_version_1")] {
11-
/// Represents a unique identifier for an account.
12-
type AccountId = [u8; 64];
13-
/// Represents a unique identifier for an asset.
14-
type AssetId = u64;
15-
/// Represents the balance of an asset.
16-
type Balance = u128;
17-
} else if #[cfg(feature = "option_version_2")] {
18-
/// Represents a unique identifier for an account.
19-
type AccountId = [u8; 32];
20-
/// Represents a unique identifier for an asset.
21-
type AssetId = u32;
22-
/// Represents the balance of an asset.
23-
type Balance = u64;
24-
} else {
25-
/// Represents a unique identifier for an account.
26-
type AccountId = [u8; 32];
27-
/// Represents a unique identifier for an asset.
28-
type AssetId = u32;
29-
/// Represents the balance of an asset.
30-
type Balance = u64;
31-
}
32-
}
9+
/// Represents a unique identifier for an account.
10+
type AccountId = [u8; 32];
11+
/// Represents a unique identifier for an asset.
12+
type AssetId = u32;
13+
/// Represents the balance of an asset.
14+
type Balance = u64;
3315

3416
/// Get the balance of a given asset for a specific account.
3517
///

guest-examples/swap-info/build.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ use std::process::Command;
44

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

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

pvq-program-metadata-gen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "pvq-program-metadata-gen"
33
description = "PVQ program metadata generation"
4-
version = "0.2.0"
4+
version = "0.5.0"
55
authors.workspace = true
66
edition.workspace = true
77
license.workspace = true

pvq-program-metadata-gen/src/bin/pvq-program-metadata-gen.rs

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ struct Args {
1313

1414
#[arg(short, long)]
1515
output_dir: PathBuf,
16+
17+
#[arg(short, long)]
18+
manifest_path: Option<PathBuf>,
19+
20+
/// Target triple to build for (optional)
21+
#[arg(long)]
22+
target: Option<String>,
1623
}
1724

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

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

35-
// Extract features section from the original manifest
36-
let original_manifest_content =
37-
std::fs::read_to_string(args.crate_path.join("Cargo.toml")).expect("Failed to read original Cargo.toml");
38-
let optional_features = pvq_program_metadata_gen::extract_features(&original_manifest_content)
39-
.expect("Failed to extract features section from the original Cargo.toml");
40-
debug!("Features section: {:?}", optional_features);
47+
// Read or create the manifest content
48+
let (manifest_content, optional_features) = if let Some(ref manifest_path) = args.manifest_path {
49+
// Read the manifest from the provided manifest path
50+
let content = std::fs::read_to_string(manifest_path)
51+
.unwrap_or_else(|_| panic!("Failed to read manifest file: {}", manifest_path.display()));
52+
debug!("Manifest content: {}", content);
53+
54+
// Extract features section from the manifest for active features determination
55+
let features = pvq_program_metadata_gen::extract_features(&content)
56+
.expect("Failed to extract features section from the manifest");
57+
debug!("Features section: {:?}", features);
4158

42-
// Create Cargo.toml with features from the original crate
43-
let manifest = pvq_program_metadata_gen::create_manifest(optional_features.as_ref());
44-
debug!("Manifest: {}", manifest);
45-
std::fs::write(temp_crate_path.join("Cargo.toml"), manifest).expect("Failed to write Cargo.toml");
59+
(content, features)
60+
} else {
61+
// Use the default manifest from create_manifest
62+
let content = pvq_program_metadata_gen::create_manifest(None);
63+
debug!("Generated default manifest content: {}", content);
64+
(content, None)
65+
};
66+
67+
// Copy the manifest to temp directory
68+
std::fs::write(temp_crate_path.join("Cargo.toml"), &manifest_content)
69+
.expect("Failed to write Cargo.toml to temp directory");
4670

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

69-
// Compile and run the metadata generator in one step
7093
let mut cargo_cmd = Command::new("cargo");
7194
cargo_cmd.current_dir(temp_crate_path).args(["run"]);
72-
for feature in active_features {
73-
cargo_cmd.arg("--features").arg(feature);
95+
96+
// Add target if specified
97+
if let Some(ref target) = args.target {
98+
info!("Using explicit target: {}", target);
99+
cargo_cmd.arg("--target").arg(target);
100+
}
101+
102+
if !active_features.is_empty() {
103+
cargo_cmd.arg("--features");
104+
for feature in active_features {
105+
cargo_cmd.arg(feature);
106+
}
74107
}
75108
info!("Compiling and running metadata generator...");
76109
let status = cargo_cmd.status().expect("Failed to run metadata generator");

0 commit comments

Comments
 (0)