Skip to content

Commit

Permalink
Merge pull request #9 from 0xPolygonHermez/edu/improve_cargo_zisk
Browse files Browse the repository at this point in the history
Improve cargo-zisk
  • Loading branch information
eduadiez authored Jul 19, 2024
2 parents f2ca151 + 4563f17 commit 240ffad
Show file tree
Hide file tree
Showing 9 changed files with 922 additions and 54 deletions.
540 changes: 508 additions & 32 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
members = [
"cli",
"common",
"examples/fibonacci",
"riscv/riscv2zisk",
"simulator",
"state-machines/main",
"state-machines/mem",
"witness-computation"
"witness-computation",
"ziskos/entrypoint",
]

resolver = "2"
Expand Down
4 changes: 4 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name = "cargo-zisk"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "cargo-zisk"
path = "src/bin/cargo-zisk.rs"

[build-dependencies]
vergen = { version = "8", default-features = false, features = [
"build",
Expand Down
173 changes: 153 additions & 20 deletions cli/src/bin/cargo-zisk.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use cargo_zisk::commands::build_toolchain::BuildToolchainCmd;
use cargo_zisk::commands::install_toolchain::InstallToolchainCmd;
use cargo_zisk::commands::new::NewCmd;
use cargo_zisk::ZISK_VERSION_MESSAGE;
use clap::{Parser, Subcommand};
use std::env;
use std::process::{Command, Stdio};

use std::fs::File;
use std::io::{self, Read, Write};
use std::path::Path;

// Main enum defining cargo subcommands.
#[derive(Parser)]
#[command(name = "cargo", bin_name = "cargo")]
#[command(name = "cargo-zisk", bin_name = "cargo-zisk", version = ZISK_VERSION_MESSAGE)]
pub enum Cargo {
Sdk(ZiskSdk),
Run(ZiskRun),
}

// Structure representing the 'sdk' subcommand of cargo.
Expand All @@ -20,6 +27,28 @@ pub struct ZiskSdk {
pub command: Option<ZiskSdkCommands>,
}

// Structure representing the 'run' subcommand of cargo.
#[derive(clap::Args)]
#[command(author, about, long_about = None, version = ZISK_VERSION_MESSAGE)]
pub struct ZiskRun {
#[clap(long, short = 'F')]
features: Option<String>,
#[clap(long)]
all_features: bool,
#[clap(long)]
release: bool,
#[clap(long)]
no_default_features: bool,
#[clap(long)]
sim: bool,
#[clap(long)]
stats: bool,
#[clap(long)]
gdb: bool,
#[clap(last = true)]
args: Vec<String>,
}

// Enum defining the available subcommands for `ZiskSdk`.
#[derive(Subcommand)]
pub enum ZiskSdkCommands {
Expand All @@ -28,28 +57,132 @@ pub enum ZiskSdkCommands {
New(NewCmd),
}

fn main() -> Result<()> {
// Parse command-line arguments and handle errors if they occur.
let Cargo::Sdk(args) = Cargo::parse();

// Check if a command was provided and execute the corresponding command.
if let Some(command) = args.command {
match command {
ZiskSdkCommands::BuildToolchain(cmd) => {
cmd.run()
.context("Error executing BuildToolchain command")?;
// Implement the run functionality for ZiskRun
impl ZiskRun {
fn run(&self) -> Result<()> {
let mut runner_command: String = String::new();
// Construct the cargo run command
let mut command = Command::new("cargo");
command.args(["+zisk", "run"]);

// Add the feature selection flags
if let Some(features) = &self.features {
command.arg("--features").arg(features);
}
if self.all_features {
command.arg("--all-features");
}
if self.no_default_features {
command.arg("--no-default-features");
}
if self.release {
command.arg("--release");
}
if self.sim {
let mut stats_command = "";
if self.stats {
stats_command = "-s"
}
ZiskSdkCommands::InstallToolchain(cmd) => {
cmd.run()
.context("Error executing BuildToolchain command")?;
runner_command = format!(
"node /home/edu/ziskjs/src/sim/main.js -n 100000000000 -i output/input.bin {} -e",
stats_command
);
} else {
let mut gdb_command = "";
if self.gdb {
gdb_command = "-S";
}
ZiskSdkCommands::New(cmd) => {
cmd.run()
.context("Error executing BuildToolchain command")?;

let input_filename = "output/input.bin";
let output_filename = "output/input_size.bin";

let input_path = Path::new(input_filename);
let metadata = std::fs::metadata(input_path)?;

let file_size = metadata.len();

let size_bytes = file_size.to_le_bytes();
let mut output_file = File::create(output_filename)?;
output_file.write_all(&size_bytes)?;

runner_command = format!(
"
qemu-system-riscv64 \
-cpu rv64 \
-machine virt \
-device loader,file=./output/input_size.bin,addr=0x90000000 \
-device loader,file=./output/input.bin,addr=0x90000008 \
-m 1G \
-s \
{} \
-nographic \
-serial mon:stdio \
-bios none \
-kernel",
gdb_command
);
}


env::set_var("CARGO_TARGET_RISCV64IMA_POLYGON_ZISKOS_ELF_RUNNER", runner_command.to_string());
// Verify the environment variable is set
println!(
"CARGO_TARGET_RISCV64IMA_POLYGON_ZISKOS_ELF_RUNNER: {}",
env::var("CARGO_TARGET_RISCV64IMA_POLYGON_ZISKOS_ELF_RUNNER").unwrap()
);

command.args(["--target", "riscv64ima-polygon-ziskos-elf"]);

// Add any additional arguments passed to the run command
command.args(&self.args);

println!("running {:?}", command);
// Set up the command to inherit the parent's stdout and stderr
command.stdout(Stdio::inherit());
command.stderr(Stdio::inherit());

// Execute the command
let status = command.status().context("Failed to execute cargo run command")?;
if !status.success() {
return Err(anyhow!("Cargo run command failed with status {}", status));
}

Ok(())
}
}

fn main() -> Result<()> {
// Parse command-line arguments and handle errors if they occur.
let cargo_args = Cargo::parse();

match cargo_args {
Cargo::Sdk(args) => {
if let Some(command) = args.command {
execute_sdk_command(command)?;
} else {
println!("No SDK command provided");
}
}
} else {
println!("No command provided");
Cargo::Run(args) => {
args.run().context("Error executing Run command")?;
}
}

Ok(())
}

// Function to handle SDK commands execution
fn execute_sdk_command(command: ZiskSdkCommands) -> Result<()> {
match command {
ZiskSdkCommands::BuildToolchain(cmd) => {
cmd.run().context("Error executing BuildToolchain command")?;
}
ZiskSdkCommands::InstallToolchain(cmd) => {
cmd.run().context("Error executing InstallToolchain command")?;
}
ZiskSdkCommands::New(cmd) => {
cmd.run().context("Error executing New command")?;
}
}
Ok(())
}
2 changes: 1 addition & 1 deletion cli/src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct NewCmd {
name: String,
}

const TEMPLATE_REPOSITORY_URL: &str = "https://github.com/0xPolygonHermez/hellozisk_rust";
const TEMPLATE_REPOSITORY_URL: &str = "https://github.com/0xPolygonHermez/zisk_template";

impl NewCmd {
pub fn run(&self) -> Result<()> {
Expand Down
1 change: 1 addition & 0 deletions examples/fibonacci
Submodule fibonacci added at 4221af
8 changes: 8 additions & 0 deletions ziskos/entrypoint/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "ziskos"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Loading

0 comments on commit 240ffad

Please sign in to comment.