-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add compile sierra to casm util
- Loading branch information
1 parent
77398c0
commit 7d36848
Showing
6 changed files
with
85 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::process::Command; | ||
|
||
fn main() { | ||
println!("cargo::rerun-if-changed=../../Cargo.lock"); | ||
println!("cargo::rerun-if-changed=build.rs"); | ||
install_starknet_sierra_compile(); | ||
} | ||
|
||
fn install_starknet_sierra_compile() { | ||
println!("Installing starknet-sierra-compile"); | ||
let mut command = Command::new("cargo"); | ||
command.arg("install"); | ||
command.arg("--root"); | ||
command.arg("tmp/cargo"); // TODO: Don't dup the path. | ||
command.arg("starknet-sierra-compile"); | ||
let compile_output = | ||
command.output().unwrap_or_else(|e| panic!("Failed to execute command: {}", e)); | ||
|
||
if !compile_output.status.success() { | ||
let stderr_output = String::from_utf8(compile_output.stderr).unwrap(); // TODO:handle error | ||
panic!("Failed to compile Sierra code: {}", stderr_output); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,71 @@ | ||
use cairo_lang_starknet_classes::allowed_libfuncs::ListSelector; | ||
use std::env; | ||
use std::env::temp_dir; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
|
||
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; | ||
use cairo_lang_starknet_classes::contract_class::ContractClass; | ||
|
||
use crate::errors::CompilationUtilError; | ||
|
||
// Solve Code duplication. | ||
pub fn get_absolute_path(relative_path: &str) -> PathBuf { | ||
Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("../..").join(relative_path) | ||
} | ||
|
||
#[cfg(test)] | ||
#[path = "compile_test.rs"] | ||
pub mod compile_test; | ||
pub struct SierraToCasmCompilationArgs { | ||
list_selector: ListSelector, | ||
|
||
const STARKNET_SIERRA_COMPILE_EXE: &str = | ||
"crates/starknet_sierra_compile/tmp/cargo/bin/starknet-sierra-compile"; | ||
|
||
struct SierraToCasmCompilationArgs { | ||
add_pythonic_hints: bool, | ||
max_bytecode_size: usize, | ||
} | ||
|
||
/// This function may panic. | ||
// TODO(Arni, 1/05/2024): Add the configurable parameters to the function. | ||
pub fn compile_sierra_to_casm( | ||
contract_class: ContractClass, | ||
) -> Result<CasmContractClass, CompilationUtilError> { | ||
let compilation_args = SierraToCasmCompilationArgs { | ||
list_selector: ListSelector::DefaultList, | ||
add_pythonic_hints: true, | ||
max_bytecode_size: 1000000, | ||
}; | ||
env::set_current_dir(get_absolute_path("")).expect("Failed to set current dir."); | ||
|
||
let serialized_contract_class = serde_json::to_string(&contract_class).expect("number 1"); | ||
|
||
// Create a temporary file path | ||
let mut temp_path = temp_dir(); | ||
temp_path.push("temp_file.sierra.json"); | ||
|
||
contract_class.validate_version_compatible(compilation_args.list_selector)?; | ||
// Create and open the file | ||
let mut file = File::create(&temp_path).expect("number 2"); | ||
|
||
// Write the content to the file | ||
file.write_all(serialized_contract_class.as_bytes()).expect("number 3"); | ||
|
||
let compilation_args = | ||
SierraToCasmCompilationArgs { add_pythonic_hints: true, max_bytecode_size: 180000 }; | ||
let compiler_path = STARKNET_SIERRA_COMPILE_EXE; | ||
|
||
let mut command = Command::new(compiler_path); | ||
command.arg(temp_path.to_str().expect("number 4")); | ||
|
||
// Add aditional arguments. | ||
if compilation_args.add_pythonic_hints { | ||
command.arg("--add-pythonic-hints"); | ||
} | ||
// TODO(Arni): use max-bytecode-size. | ||
let _max_bytecode_size = compilation_args.max_bytecode_size; | ||
|
||
let compile_output = | ||
command.output().unwrap_or_else(|e| panic!("Failed to execute command: {}", e)); | ||
|
||
if !compile_output.status.success() { | ||
let stderr_output = String::from_utf8(compile_output.stderr).expect("number 5"); // TODO: handle error | ||
return Err(CompilationUtilError::CompilationError(stderr_output)); | ||
}; | ||
|
||
Ok(CasmContractClass::from_contract_class( | ||
contract_class, | ||
compilation_args.add_pythonic_hints, | ||
compilation_args.max_bytecode_size, | ||
)?) | ||
Ok(serde_json::from_slice::<CasmContractClass>(&compile_output.stdout).expect("number 6")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
use cairo_lang_starknet_classes::allowed_libfuncs::AllowedLibfuncsError; | ||
use cairo_lang_starknet_classes::casm_contract_class::StarknetSierraCompilationError; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum CompilationUtilError { | ||
#[error(transparent)] | ||
AllowedLibfuncsError(#[from] AllowedLibfuncsError), | ||
#[error(transparent)] | ||
StarknetSierraCompilationError(#[from] StarknetSierraCompilationError), | ||
#[error("Compilation panicked")] | ||
CompilationPanic, | ||
#[error("Starknet Sierra compilation error: {0}")] | ||
CompilationError(String), | ||
} |