-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use the constant starknet-sierra-compile binary to compile
- Loading branch information
1 parent
f53c3c5
commit 5afc761
Showing
8 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,51 @@ | ||
use std::env; | ||
use std::path::Path; | ||
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() { | ||
let out_dir = env::var("OUT_DIR").expect("OUT_DIR not set"); | ||
let bin_path = Path::new(&out_dir).join("bin"); | ||
|
||
let binary_name = "starknet-sierra-compile"; | ||
|
||
// Create the bin directory if it doesn't exist | ||
std::fs::create_dir_all(bin_path.clone()).expect("Failed to create bin directory"); | ||
|
||
// Path to the local binary | ||
let binary_path = bin_path.join(binary_name); | ||
// TODO(Arni): Add the configurable parameters to the function. | ||
let _starknet_sierra_compile_version: Option<String> = None; | ||
|
||
// Check if the binary is already installed locally | ||
if !binary_path.exists() { | ||
// Install the binary locally | ||
|
||
let status = Command::new("cargo") | ||
.args({ | ||
let args = vec![ | ||
"install", | ||
"starknet-sierra-compile", | ||
"--root", | ||
bin_path.to_str().expect("Failed to convert bin_path to str"), | ||
]; | ||
|
||
args | ||
}) | ||
.status() | ||
.expect("Failed to install starknet-sierra-compile"); | ||
|
||
if !status.success() { | ||
panic!("Failed to install starknet-sierra-compile"); | ||
} | ||
} | ||
|
||
// Print the path to the installed binary so that it can be used in the main application | ||
println!("cargo::rustc-env=STARKNET_SIERRA_COMPILE_BIN={}", binary_path.display()); | ||
} |
59 changes: 59 additions & 0 deletions
59
crates/starknet_sierra_compile/src/command_line_compiler.rs
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,59 @@ | ||
use std::env; | ||
use std::io::Write; | ||
use std::process::Command; | ||
use std::sync::OnceLock; | ||
|
||
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; | ||
use cairo_lang_starknet_classes::contract_class::ContractClass; | ||
use tempfile::NamedTempFile; | ||
|
||
use crate::config::SierraToCasmCompilationConfig; | ||
use crate::errors::CompilationUtilError; | ||
use crate::SierraToCasmCompiler; | ||
|
||
#[derive(Clone)] | ||
pub struct CommandLineCompiler { | ||
pub config: SierraToCasmCompilationConfig, | ||
} | ||
|
||
impl SierraToCasmCompiler for CommandLineCompiler { | ||
fn compile( | ||
&self, | ||
contract_class: ContractClass, | ||
) -> Result<CasmContractClass, CompilationUtilError> { | ||
// Create a temporary file to store the Sierra contract class. | ||
let serialized_contract_class = serde_json::to_string(&contract_class)?; | ||
|
||
let mut temp_file = NamedTempFile::new()?; | ||
temp_file.write_all(serialized_contract_class.as_bytes())?; | ||
let temp_file_path = temp_file.path().to_str().ok_or( | ||
CompilationUtilError::CompilationError("Failed to get temporary file path".to_owned()), | ||
)?; | ||
|
||
// Set the parameters for the compile process. | ||
let mut command = Command::new(excutable_file_location()); | ||
command.arg(temp_file_path); | ||
|
||
command.arg("--add-pythonic-hints"); | ||
command.args(["--max-bytecode-size", &self.config.max_bytecode_size.to_string()]); | ||
|
||
// Run the compile process. | ||
let compile_output = command.output()?; | ||
|
||
if !compile_output.status.success() { | ||
let stderr_output = String::from_utf8(compile_output.stderr) | ||
.unwrap_or("Failed to get stderr output".into()); | ||
return Err(CompilationUtilError::CompilationError(stderr_output)); | ||
}; | ||
|
||
Ok(serde_json::from_slice::<CasmContractClass>(&compile_output.stdout)?) | ||
} | ||
} | ||
|
||
/// Returns the location of the "starknet-sierra-compile" executable. | ||
/// This executable compiles Sierra contracts into Casm. | ||
fn excutable_file_location() -> &'static str { | ||
static COMPILER_PATH: OnceLock<&str> = OnceLock::new(); | ||
COMPILER_PATH | ||
.get_or_init(|| Box::leak(env!("STARKNET_SIERRA_COMPILE_BIN").to_string().into_boxed_str())) | ||
} |
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