From 9d3144e110dde1d7140b123917773f49ee0cee10 Mon Sep 17 00:00:00 2001 From: Avi Cohen Date: Tue, 10 Dec 2024 11:25:02 +0200 Subject: [PATCH] fix(starknet_sierra_compile): resolve out_dir usage --- crates/bin/starknet-native-compile/Cargo.lock | 10 ++++---- crates/starknet_sierra_compile/Cargo.toml | 1 + crates/starknet_sierra_compile/build.rs | 25 +++++++++---------- .../src/command_line_compiler.rs | 7 ++++-- crates/starknet_sierra_compile/src/paths.rs | 9 +++++-- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/crates/bin/starknet-native-compile/Cargo.lock b/crates/bin/starknet-native-compile/Cargo.lock index b8bc1ad781..b0e5f2319c 100644 --- a/crates/bin/starknet-native-compile/Cargo.lock +++ b/crates/bin/starknet-native-compile/Cargo.lock @@ -939,9 +939,9 @@ dependencies = [ [[package]] name = "cairo-native" -version = "0.2.3" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "833170f422b08eec0e5ab116573bbb73c73f1601987d4b151cf71760836bd142" +checksum = "9bdebc70c3d563bc30078985ae3e975aa7dc4fa233631b5e0462a924c26c0dd9" dependencies = [ "anyhow", "aquamarine", @@ -993,9 +993,9 @@ dependencies = [ [[package]] name = "cairo-native-runtime" -version = "0.2.3" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fa4ec78d0a6df2988b71838c330a5113f65a4db6ccff53d8d71465f6619a427" +checksum = "5bf997252c402d6844f41357660cde3c11825ac5b3feafd0fb99b9dcdfb58aa3" dependencies = [ "cairo-lang-sierra-gas", "itertools 0.13.0", @@ -2948,7 +2948,7 @@ dependencies = [ [[package]] name = "starknet-native-compile" -version = "0.2.1-alpha.0" +version = "0.2.4" dependencies = [ "cairo-lang-sierra", "cairo-lang-starknet-classes", diff --git a/crates/starknet_sierra_compile/Cargo.toml b/crates/starknet_sierra_compile/Cargo.toml index bd559d226e..d4a85d45d4 100644 --- a/crates/starknet_sierra_compile/Cargo.toml +++ b/crates/starknet_sierra_compile/Cargo.toml @@ -33,3 +33,4 @@ rstest.workspace = true [build-dependencies] infra_utils.workspace = true +tempfile.workspace = true diff --git a/crates/starknet_sierra_compile/build.rs b/crates/starknet_sierra_compile/build.rs index b2b531eb23..0c82b36cc4 100644 --- a/crates/starknet_sierra_compile/build.rs +++ b/crates/starknet_sierra_compile/build.rs @@ -1,5 +1,7 @@ use std::process::Command; +use tempfile::TempDir; + include!("src/constants.rs"); include!("src/paths.rs"); @@ -15,7 +17,7 @@ fn main() { const REQUIRED_CAIRO_LANG_VERSION: &str = "2.7.1"; #[cfg(feature = "cairo_native")] -const REQUIRED_CAIRO_NATIVE_VERSION: &str = "0.2.1-alpha.0"; +const REQUIRED_CAIRO_NATIVE_VERSION: &str = "0.2.4"; /// Downloads the Cairo crate from StarkWare's release page and extracts its contents into the /// `target` directory. This crate includes the `starknet-sierra-compile` binary, which is used to @@ -25,9 +27,6 @@ fn install_starknet_sierra_compile() { let binary_name = CAIRO_LANG_BINARY_NAME; let required_version = REQUIRED_CAIRO_LANG_VERSION; - let cairo_lang_binary_path = binary_path(out_dir(), binary_name); - println!("cargo:rerun-if-changed={:?}", cairo_lang_binary_path); - let cargo_install_args = &[binary_name, "--version", required_version]; install_compiler_binary(binary_name, required_version, cargo_install_args); } @@ -41,15 +40,16 @@ fn install_starknet_native_compile() { let binary_name = CAIRO_NATIVE_BINARY_NAME; let required_version = REQUIRED_CAIRO_NATIVE_VERSION; - let cairo_native_binary_path = binary_path(out_dir(), binary_name); - println!("cargo:rerun-if-changed={:?}", cairo_native_binary_path); - // Set the runtime library path. This is required for Cairo native compilation. let runtime_library_path = repo_root_dir() .join("crates/blockifier/cairo_native/target/release/libcairo_native_runtime.a"); println!("cargo:rustc-env=CAIRO_NATIVE_RUNTIME_LIBRARY={}", runtime_library_path.display()); println!("cargo:rerun-if-env-changed=CAIRO_NATIVE_RUNTIME_LIBRARY"); + // Create a directory to store the compiled outputs. + let compiled_output_dir = compilation_output_dir(out_dir()); + std::fs::create_dir_all(&compiled_output_dir).expect("Failed to create the output directory."); + let starknet_native_compile_crate_path = repo_root_dir().join("crates/bin").join(binary_name); let starknet_native_compile_crate_path_str = starknet_native_compile_crate_path .to_str() @@ -62,6 +62,7 @@ fn install_starknet_native_compile() { fn install_compiler_binary(binary_name: &str, required_version: &str, cargo_install_args: &[&str]) { let binary_path = binary_path(out_dir(), binary_name); + println!("cargo:rerun-if-changed={:?}", binary_path); match Command::new(&binary_path).args(["--version"]).output() { Ok(binary_version) => { @@ -82,16 +83,14 @@ fn install_compiler_binary(binary_name: &str, required_version: &str, cargo_inst } } - let temp_cargo_path = out_dir().join("cargo"); - let post_install_file_path = temp_cargo_path.join("bin").join(binary_name); + let temp_cargo_path = TempDir::new().expect("Failed to create a temporary directory."); + let post_install_file_path = temp_cargo_path.path().join("bin").join(binary_name); - // Create the temporary cargo directory if it doesn't exist - std::fs::create_dir_all(&temp_cargo_path).expect("Failed to create cargo directory"); let install_command_status = Command::new("cargo") .args([ "install", "--root", - temp_cargo_path.to_str().expect("Failed to convert cargo_path to str"), + temp_cargo_path.path().to_str().expect("Failed to convert cargo_path to str"), ]) .args(cargo_install_args) .status() @@ -101,7 +100,7 @@ fn install_compiler_binary(binary_name: &str, required_version: &str, cargo_inst panic!("Failed to install {}", binary_name); } - // Move the 'starknet-sierra-compile' executable to a shared location + // Move the '{binary_name}' executable to a shared location. std::fs::create_dir_all(shared_folder_dir(out_dir())) .expect("Failed to create shared executables folder"); let move_command_status = Command::new("mv") diff --git a/crates/starknet_sierra_compile/src/command_line_compiler.rs b/crates/starknet_sierra_compile/src/command_line_compiler.rs index aebf9b3510..84f78d56b6 100644 --- a/crates/starknet_sierra_compile/src/command_line_compiler.rs +++ b/crates/starknet_sierra_compile/src/command_line_compiler.rs @@ -15,7 +15,7 @@ use crate::constants::CAIRO_NATIVE_BINARY_NAME; use crate::errors::CompilationUtilError; use crate::paths::binary_path; #[cfg(feature = "cairo_native")] -use crate::paths::output_file_path; +use crate::paths::{compilation_output_dir, compilation_output_file_path}; use crate::SierraToCasmCompiler; #[cfg(feature = "cairo_native")] use crate::SierraToNativeCompiler; @@ -66,7 +66,10 @@ impl SierraToNativeCompiler for CommandLineCompiler { contract_class: ContractClass, ) -> Result { let compiler_binary_path = &self.path_to_starknet_native_compile_binary; - let output_file_path = output_file_path(out_dir()); + let output_directory = compilation_output_dir(out_dir()); + std::fs::create_dir_all(&output_directory) + .expect("Failed to create native compilation output directory"); + let output_file_path = compilation_output_file_path(out_dir()); let additional_args = [output_file_path.as_str()]; let _stdout = compile_with_args(compiler_binary_path, contract_class, &additional_args)?; diff --git a/crates/starknet_sierra_compile/src/paths.rs b/crates/starknet_sierra_compile/src/paths.rs index 7d1963b3d0..182c39a0b2 100644 --- a/crates/starknet_sierra_compile/src/paths.rs +++ b/crates/starknet_sierra_compile/src/paths.rs @@ -19,6 +19,11 @@ pub fn binary_path(out_dir: std::path::PathBuf, binary_name: &str) -> std::path: } #[cfg(feature = "cairo_native")] -pub fn output_file_path(out_dir: std::path::PathBuf) -> String { - out_dir.join("output.so").to_str().unwrap().into() +pub fn compilation_output_dir(out_dir: std::path::PathBuf) -> std::path::PathBuf { + target_dir(out_dir).join("native_compile_outputs") +} + +#[cfg(feature = "cairo_native")] +pub fn compilation_output_file_path(out_dir: std::path::PathBuf) -> String { + compilation_output_dir(out_dir).join("output.so").to_str().unwrap().into() }