Skip to content

Commit

Permalink
Set execute default output format per target
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr committed Feb 1, 2025
1 parent df3c9df commit 68db115
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
13 changes: 11 additions & 2 deletions extensions/scarb-execute/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub struct RunArgs {
pub arguments: ProgramArguments,

/// Desired execution output, either default Standard or CairoPie
#[arg(long, default_value = "standard")]
pub output: OutputFormat,
#[arg(long)]
pub output: Option<OutputFormat>,

/// Execution target.
#[arg(long, default_value = "standalone")]
Expand Down Expand Up @@ -93,6 +93,12 @@ pub enum OutputFormat {
Standard,
}
impl OutputFormat {
pub fn default_for_target(target: ExecutionTarget) -> OutputFormat {
match target {
ExecutionTarget::Bootloader => OutputFormat::CairoPie,
ExecutionTarget::Standalone => OutputFormat::Standard,
}
}
pub fn is_standard(&self) -> bool {
matches!(self, OutputFormat::Standard)
}
Expand All @@ -111,4 +117,7 @@ impl ExecutionTarget {
pub fn is_standalone(&self) -> bool {
matches!(self, ExecutionTarget::Standalone)
}
pub fn is_bootloader(&self) -> bool {
matches!(self, ExecutionTarget::Bootloader)
}
}
19 changes: 15 additions & 4 deletions extensions/scarb-execute/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::args::OutputFormat;
use crate::output::{ExecutionOutput, ExecutionResources, ExecutionSummary, PanicReason};
use anyhow::{bail, ensure, Context, Result};
use bincode::enc::write::Writer;
Expand Down Expand Up @@ -36,10 +37,20 @@ pub fn execute(
args: &args::ExecutionArgs,
ui: &Ui,
) -> Result<usize, anyhow::Error> {
let output = args
.run
.output
.as_ref()
.cloned()
.unwrap_or_else(|| OutputFormat::default_for_target(args.run.target.clone()));
ensure!(
!(args.run.output.is_cairo_pie() && args.run.target.is_standalone()),
!(output.is_cairo_pie() && args.run.target.is_standalone()),
"Cairo pie output format is not supported for standalone execution target"
);
ensure!(
!(output.is_standard() && args.run.target.is_bootloader()),
"Standard output format is not supported for bootloader execution target"
);

if !args.no_build {
let filter = PackagesFilter::generate_for::<Metadata>(vec![package.clone()].iter());
Expand Down Expand Up @@ -122,8 +133,8 @@ pub fn execute(
layout: LayoutName::all_cairo,
proof_mode: args.run.target.is_standalone(),
secure_run: None,
relocate_mem: args.run.output.is_standard(),
trace_enabled: args.run.output.is_standard(),
relocate_mem: output.is_standard(),
trace_enabled: output.is_standard(),
..Default::default()
};

Expand All @@ -149,7 +160,7 @@ pub fn execute(

let (execution_output_dir, execution_id) = incremental_create_output_dir(&output_dir)?;

if args.run.output.is_cairo_pie() {
if output.is_cairo_pie() {
let output_value = runner.get_cairo_pie()?;
let output_file_path = execution_output_dir.join("cairo_pie.zip");
ui.print(Status::new(
Expand Down
39 changes: 21 additions & 18 deletions extensions/scarb-execute/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,38 +97,41 @@ fn can_execute_bootloader_target() {
[..]Compiling hello v0.1.0 ([..]Scarb.toml)
[..]Finished `dev` profile target(s) in [..]
[..]Executing hello
Saving output to: target/execute/hello/execution1
Saving output to: target/execute/hello/execution1/cairo_pie.zip
"#});

t.child("target/execute/hello/execution1/air_private_input.json")
.assert_is_json::<serde_json::Value>();
t.child("target/execute/hello/execution1/air_public_input.json")
.assert_is_json::<serde_json::Value>();
t.child("target/execute/hello/execution1/memory.bin")
.assert(predicates::path::exists().and(is_file_empty().not()));
t.child("target/execute/hello/execution1/trace.bin")
.assert(predicates::path::exists().and(is_file_empty().not()));
t.child("target/execute/hello/execution1/cairo_pie.zip")
.assert(predicates::path::exists());
}

#[test]
fn can_produce_cairo_pie_output() {
fn cannot_produce_trace_file_for_bootloader_target() {
let t = build_executable_project();
Scarb::quick_snapbox()
.arg("execute")
.arg("--target=bootloader")
.arg("--output=cairo-pie")
.arg("--output=standard")
.current_dir(&t)
.assert()
.success()
.failure()
.stdout_matches(indoc! {r#"
[..]Compiling hello v0.1.0 ([..]Scarb.toml)
[..]Finished `dev` profile target(s) in [..]
[..]Executing hello
Saving output to: target/execute/hello/execution1/cairo_pie.zip
error: Standard output format is not supported for bootloader execution target
"#});
}

t.child("target/execute/hello/execution1/cairo_pie.zip")
.assert(predicates::path::exists());
#[test]
fn cannot_produce_cairo_pie_for_standalone_target() {
let t = build_executable_project();
Scarb::quick_snapbox()
.arg("execute")
.arg("--target=standalone")
.arg("--output=cairo-pie")
.current_dir(&t)
.assert()
.failure()
.stdout_matches(indoc! {r#"
error: Cairo pie output format is not supported for standalone execution target
"#});
}

#[test]
Expand Down

0 comments on commit 68db115

Please sign in to comment.