Skip to content

Commit

Permalink
fix(starknet_sierra_compile): resolve out_dir usage
Browse files Browse the repository at this point in the history
  • Loading branch information
avi-starkware committed Dec 10, 2024
1 parent 3e5404b commit 9d3144e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
10 changes: 5 additions & 5 deletions crates/bin/starknet-native-compile/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 crates/starknet_sierra_compile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ rstest.workspace = true

[build-dependencies]
infra_utils.workspace = true
tempfile.workspace = true
25 changes: 12 additions & 13 deletions crates/starknet_sierra_compile/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::process::Command;

use tempfile::TempDir;

include!("src/constants.rs");
include!("src/paths.rs");

Expand All @@ -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
Expand All @@ -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);
}
Expand All @@ -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()
Expand All @@ -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) => {
Expand All @@ -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()
Expand All @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions crates/starknet_sierra_compile/src/command_line_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +66,10 @@ impl SierraToNativeCompiler for CommandLineCompiler {
contract_class: ContractClass,
) -> Result<AotContractExecutor, CompilationUtilError> {
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)?;
Expand Down
9 changes: 7 additions & 2 deletions crates/starknet_sierra_compile/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 9d3144e

Please sign in to comment.