Skip to content
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

[DRAFT] add casm json target #1287

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scarb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ repository.workspace = true
[dependencies]
anyhow.workspace = true
async-trait.workspace = true
cairo-lang-casm.workspace = true
cairo-lang-compiler.workspace = true
cairo-lang-defs.workspace = true
cairo-lang-diagnostics.workspace = true
Expand Down
83 changes: 59 additions & 24 deletions scarb/src/compiler/compilers/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use anyhow::{Context, Result};
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_casm::hints::Hint;
use cairo_lang_sierra::program::VersionedProgram;
use cairo_lang_sierra_to_casm::compiler::SierraToCasmConfig;
use cairo_lang_sierra_to_casm::metadata::{calc_metadata, calc_metadata_ap_change_only};
use cairo_lang_utils::bigint::BigIntAsHex;
use serde::{Deserialize, Serialize};
use tracing::{debug, trace_span};

Expand All @@ -20,14 +22,16 @@ struct Props {
pub sierra: bool,
pub casm: bool,
pub sierra_text: bool,
pub casm_text: bool,
}

impl Default for Props {
fn default() -> Self {
Self {
sierra: true,
casm: false,
casm: true,
tohrnii marked this conversation as resolved.
Show resolved Hide resolved
sierra_text: false,
casm_text: false,
}
}
}
Expand All @@ -44,9 +48,9 @@ impl Compiler for LibCompiler {
ws: &Workspace<'_>,
) -> Result<()> {
let props: Props = unit.target().props()?;
if !props.sierra && !props.casm && !props.sierra_text {
if !props.sierra && !props.casm && !props.sierra_text && !props.casm_text {
ws.config().ui().warn(
"Sierra, textual Sierra and CASM lib targets have been disabled, \
"Sierra, textual Sierra, CASM and textual CASM lib targets have been disabled, \
Scarb will not produce anything",
);
}
Expand Down Expand Up @@ -89,32 +93,50 @@ impl Compiler for LibCompiler {
&sierra_program,
)?;
}
let program = sierra_program.into_v1().unwrap().program;
let metadata = {
let _ = trace_span!("casm_calc_metadata").enter();

if unit.compiler_config.enable_gas {
debug!("calculating Sierra variables");
calc_metadata(&program, Default::default())
} else {
debug!("calculating Sierra variables with no gas validation");
calc_metadata_ap_change_only(&program)
}
.context("failed calculating Sierra variables")?
};

if props.casm {
let program = sierra_program.into_v1().unwrap().program;

let metadata = {
let _ = trace_span!("casm_calc_metadata").enter();

if unit.compiler_config.enable_gas {
debug!("calculating Sierra variables");
calc_metadata(&program, Default::default())
} else {
debug!("calculating Sierra variables with no gas validation");
calc_metadata_ap_change_only(&program)
}
.context("failed calculating Sierra variables")?
let cairo_program = {
let _ = trace_span!("compile_casm").enter();
let sierra_to_casm = SierraToCasmConfig {
gas_usage_check: unit.compiler_config.enable_gas,
max_bytecode_size: usize::MAX,
};
cairo_lang_sierra_to_casm::compiler::compile(&program, &metadata, sierra_to_casm)?
};

let cairo_program = {
let _ = trace_span!("compile_casm").enter();
let sierra_to_casm = SierraToCasmConfig {
gas_usage_check: unit.compiler_config.enable_gas,
max_bytecode_size: usize::MAX,
};
cairo_lang_sierra_to_casm::compiler::compile(&program, &metadata, sierra_to_casm)?
if props.casm {
let assembled_cairo_program = cairo_program.assemble();
let bytecode = assembled_cairo_program.bytecode.iter().map(|x| BigIntAsHex{ value: x.to_owned() }).collect::<Vec<BigIntAsHex>>();
let hints = assembled_cairo_program.hints;
let casm_program = SerializedCasm {
bytecode,
hints,
};
write_json(
format!("{}.casm.json", unit.target().name).as_str(),
"output file",
&target_dir,
ws,
&casm_program,
)
.with_context(|| {
format!("failed to serialize CASM program {}", unit.target().name)
})?;
}

if props.casm_text {
write_string(
format!("{}.casm", unit.target().name).as_str(),
"output file",
Expand All @@ -127,3 +149,16 @@ impl Compiler for LibCompiler {
Ok(())
}
}

/// Represents a contract in the Starknet network.
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SerializedCasm {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right it should ideally go there but I wasn't sure if the cairo team wanted to make this a part of that repo based on some discussion in related issues. I'll open a PR there.

// #[serde(serialize_with = "serialize_big_uint", deserialize_with = "deserialize_big_uint")]
// pub prime: BigUint,
// pub compiler_version: String,
pub bytecode: Vec<BigIntAsHex>,
// #[serde(skip_serializing_if = "skip_if_none")]
// pub bytecode_segment_lengths: Option<NestedIntList>,
pub hints: Vec<(usize, Vec<Hint>)>,
// pub entry_points_by_type: CasmContractEntryPoints,
}
1 change: 1 addition & 0 deletions scarb/src/core/manifest/toml_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ pub struct TomlLibTargetParams {
pub sierra: Option<bool>,
pub casm: Option<bool>,
pub sierra_text: Option<bool>,
pub casm_text: Option<bool>,
}

pub type TomlExternalTargetParams = BTreeMap<SmolStr, toml::Value>;
Expand Down
17 changes: 11 additions & 6 deletions scarb/tests/build_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn compile_with_custom_lib_target() {
sierra = false
casm = true
sierra-text = true
casm-text = true
"#,
)
.unwrap();
Expand All @@ -104,15 +105,19 @@ fn compile_with_custom_lib_target() {
[..] Compiling hello v0.1.0 ([..])
[..] Finished release target(s) in [..]
"#});

t.child("target/dev/not_hello.casm")

t.child("target/dev/not_hello.sierra.json")
.assert(predicates::path::exists().not());
t.child("target/dev/not_hello.casm.json")
.assert(predicates::str::is_empty().not());
t.child("target/dev/not_hello.sierra")
.assert(predicates::str::is_empty().not());
t.child("target/dev/not_hello.sierra.json")
.assert(predicates::path::exists().not());
t.child("target/dev/not_hello.casm")
.assert(predicates::str::is_empty().not());
t.child("target/dev/hello.sierra.json")
.assert(predicates::path::exists().not());
t.child("target/dev/hello.casm.json")
.assert(predicates::path::exists().not());
t.child("target/dev/hello.casm")
.assert(predicates::path::exists().not());
t.child("target/dev/hello.sierra")
Expand Down Expand Up @@ -156,10 +161,10 @@ fn compile_with_named_default_lib_target() {
.assert(predicates::path::exists().not());
t.child("target/dev/hello.sierra.json")
.assert(predicates::path::exists().not());
t.child("target/dev/hello.casm")
.assert(predicates::path::exists().not());
t.child("target/dev/hello.sierra")
.assert(predicates::path::exists().not());
t.child("target/dev/hello.casm")
.assert(predicates::path::exists().not());
}

#[test]
Expand Down
Loading