Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(blockifier): verify cairo compiler repo (with correct tag) #183

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion crates/blockifier/src/test_utils/cairo_compile.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::process::Command;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::{env, fs};

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

const CAIRO0_PIP_REQUIREMENTS_FILE: &str = "tests/requirements.txt";
const CAIRO1_REPO_RELATIVE_PATH_OVERRIDE_ENV_VAR: &str = "CAIRO1_REPO_RELATIVE_PATH";
const DEFAULT_CAIRO1_REPO_RELATIVE_PATH: &str = "../../../cairo";

/// 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
Expand Down Expand Up @@ -55,6 +58,30 @@ pub fn cairo1_compiler_version() -> String {
}
}

/// Returns the path to the local Cairo1 compiler repository.
/// Returns <sequencer_repo_root>/<RELATIVE_PATH_TO_CAIRO_REPO>, where the relative path can be
/// overridden by the environment variable (otherwise, the default is used).
fn local_cairo1_compiler_repo_path() -> PathBuf {
// Location of blockifier's Cargo.toml.
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

Path::new(&manifest_dir).join(match std::env::var(CAIRO1_REPO_RELATIVE_PATH_OVERRIDE_ENV_VAR) {
Ok(cairo1_repo_relative_path) => cairo1_repo_relative_path,
Err(_) => DEFAULT_CAIRO1_REPO_RELATIVE_PATH.into(),
})
}

/// Runs a command. If it has succeeded, it returns the command's output; otherwise, it panics with
/// stderr output.
fn run_and_verify_output(command: &mut Command) -> Output {
let output = command.output().unwrap();
if !output.status.success() {
let stderr_output = String::from_utf8(output.stderr).unwrap();
panic!("{stderr_output}");
}
output
}

/// Compiles a Cairo0 program using the deprecated compiler.
pub fn cairo0_compile(path: String, extra_arg: Option<String>, debug_info: bool) -> Vec<u8> {
verify_cairo0_compiler_deps();
Expand All @@ -74,6 +101,7 @@ 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_cairo1_compiler_deps();
todo!();
}

Expand Down Expand Up @@ -107,3 +135,14 @@ fn verify_cairo0_compiler_deps() {
CAIRO0_PIP_REQUIREMENTS_FILE
);
}

fn verify_cairo1_compiler_deps() {
// Checkout the required version in the compiler repo.
run_and_verify_output(Command::new("git").args([
"-C",
// TODO(Dori, 1/6/2024): Handle CI case (repo path will be different).
local_cairo1_compiler_repo_path().to_str().unwrap(),
"checkout",
&format!("v{}", cairo1_compiler_version()),
]));
}
Loading