Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
chore: verify Cairo0 compiler dependencies before compiling
Browse files Browse the repository at this point in the history
Signed-off-by: Dori Medini <dori@starkware.co>
  • Loading branch information
dorimedini-starkware committed May 6, 2024
1 parent 76dcca0 commit 9d2d44c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions crates/blockifier/src/test_utils/cairo_compile.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use std::process::Command;
use std::{env, fs};

use cached::proc_macro::cached;
use serde::{Deserialize, Serialize};

use crate::test_utils::CairoVersion;

const CAIRO0_PIP_REQUIREMENTS_FILE: &str = "tests/requirements.txt";

/// Objects for simple deserialization of Cargo.toml to fetch the Cairo1 compiler version.
/// The compiler itself isn't actually a dependency, so we compile by using the version of the
/// cairo-lang-casm crate.
Expand Down Expand Up @@ -51,6 +56,7 @@ pub fn cairo1_compiler_version() -> String {

/// Compiles a Cairo0 program using the deprecated compiler.
pub fn cairo0_compile(path: String, extra_arg: Option<String>, debug_info: bool) -> Vec<u8> {
verify_compiler_deps(CairoVersion::Cairo0);
let mut command = Command::new("starknet-compile-deprecated");
if let Some(extra_arg) = extra_arg {
command.arg(extra_arg);
Expand All @@ -66,5 +72,40 @@ pub fn cairo0_compile(path: String, extra_arg: Option<String>, debug_info: bool)

/// Compiles a Cairo1 program using the compiler version set in the Cargo.toml.
pub fn cairo1_compile(_path: String) -> Vec<u8> {
verify_compiler_deps(CairoVersion::Cairo1);
todo!();
}

/// Verifies that the required dependencies are available before compiling.
fn verify_compiler_deps(cairo_version: CairoVersion) {
match cairo_version {
CairoVersion::Cairo0 => {
// Python compiler. Verify correct version.
let cairo_lang_version_output = Command::new("sh")
.arg("-c")
.arg("pip freeze | grep cairo-lang")
.output()
.unwrap()
.stdout;
let cairo_lang_version = String::from_utf8(cairo_lang_version_output).unwrap();

let requirements_contents = fs::read_to_string(CAIRO0_PIP_REQUIREMENTS_FILE).unwrap();
let expected_cairo_lang_version = requirements_contents
.lines()
.nth(1) // Skip docstring.
.expect(
"Expecting requirements file to contain a docstring in the first line, and \
then the required cairo-lang version in the second line."
);

assert_eq!(
cairo_lang_version.trim(),
expected_cairo_lang_version.trim(),
"cairo-lang not found. Please run:\npip3.9 install -r {}/{}\nthen rerun the test.",
env::var("CARGO_MANIFEST_DIR").unwrap(),
CAIRO0_PIP_REQUIREMENTS_FILE
);
}
CairoVersion::Cairo1 => todo!(),
}
}

0 comments on commit 9d2d44c

Please sign in to comment.