-
Notifications
You must be signed in to change notification settings - Fork 28
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 starknet-sierra-compile downloaded binary to compile
- Loading branch information
1 parent
3635605
commit e3e4d61
Showing
8 changed files
with
126 additions
and
14 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,13 @@ | ||
/// Get the crate's `OUT_DIR` and navigates up to reach the `target/BUILD_FLAVOR` directory. | ||
/// This directory is shared accross all crates in this project. | ||
pub fn get_traget_build_flavor_dir() -> &'static std::path::Path { | ||
let out_dir = std::env::var("OUT_DIR").expect("Failed to get the OUT_DIR environment variable"); | ||
// Navigate from this crate's build folder to reach the `target/BUILD_FLAVOR` directory. | ||
Box::leak( | ||
std::path::Path::new(&out_dir) | ||
.ancestors() | ||
.nth(3) | ||
.expect("Failed to navigate up three levels from OUT_DIR") | ||
.into(), | ||
) | ||
} |
77 changes: 77 additions & 0 deletions
77
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,77 @@ | ||
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::build_utils::get_traget_build_flavor_dir; | ||
use crate::config::SierraToCasmCompilationConfig; | ||
use crate::errors::CompilationUtilError; | ||
use crate::SierraToCasmCompiler; | ||
|
||
#[derive(Clone)] | ||
pub struct CommandLineCompiler { | ||
pub config: SierraToCasmCompilationConfig, | ||
path_to_starknet_sierra_compile_binary: &'static str, | ||
} | ||
|
||
impl CommandLineCompiler { | ||
pub fn new(config: SierraToCasmCompilationConfig) -> Self { | ||
Self { config, path_to_starknet_sierra_compile_binary: get_compiler_path() } | ||
} | ||
} | ||
|
||
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::UnexpectedError("Failed to get temporary file path".to_owned()), | ||
)?; | ||
|
||
// Set the parameters for the compile process. | ||
let mut command = Command::new(self.path_to_starknet_sierra_compile_binary); | ||
command.args([ | ||
temp_file_path, | ||
"--add-pythonic-hints", | ||
"--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)?) | ||
} | ||
} | ||
|
||
static COMPILER_PATH: OnceLock<&str> = OnceLock::new(); | ||
fn get_compiler_path() -> &'static str { | ||
COMPILER_PATH.get_or_init(|| { | ||
let target_dir = get_traget_build_flavor_dir().join("cairo"); | ||
|
||
let binary_name = "starknet-sierra-compile"; | ||
Box::leak( | ||
target_dir | ||
.join("bin") | ||
.join(binary_name) | ||
.to_str() | ||
.expect("Failed to get compiler path") | ||
.into(), | ||
) | ||
}) | ||
} |
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