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): use infra_utils for fetching manifest dir #2289

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cairo-native = { workspace = true, optional = true }
cairo-vm.workspace = true
derive_more.workspace = true
indexmap.workspace = true
infra_utils.workspace = true
itertools.workspace = true
keccak.workspace = true
log.workspace = true
Expand Down Expand Up @@ -70,6 +71,9 @@ rstest.workspace = true
starknet_api = { workspace = true, features = ["testing"] }
test-case.workspace = true

[build-dependencies]
infra_utils.workspace = true

[[bench]]
harness = false
name = "blockifier_bench"
Expand Down
27 changes: 14 additions & 13 deletions crates/blockifier/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::path::PathBuf;
use std::process::Command;

#[cfg(feature = "cairo_native")]
fn compile_cairo_native_aot_runtime() {
let cairo_native_dir = std::env::current_dir()
.expect("Failed to get current directory")
.join(PathBuf::from("cairo_native"));
use std::path::PathBuf;
use std::process::Command;

use infra_utils::compile_time_cargo_manifest_dir;
use infra_utils::path::current_dir;

let cairo_native_dir =
PathBuf::from(compile_time_cargo_manifest_dir!()).join(PathBuf::from("cairo_native"));

if !cairo_native_dir.exists() || !cairo_native_dir.join(".git").exists() {
panic!(
Expand Down Expand Up @@ -44,7 +47,7 @@ fn compile_cairo_native_aot_runtime() {
if expected_path.is_absolute() {
expected_path
} else {
std::env::current_dir().expect("Failed to get current directory").join(expected_path)
current_dir().expect("Failed to get current directory").join(expected_path)
}
};

Expand All @@ -59,10 +62,8 @@ fn compile_cairo_native_aot_runtime() {
}

fn main() {
// `CARGO_FEATURE_CAIRO_NATIVE` env var is set by Cargo when compiling with the `cairo_native`
// feature flag. Build instructions are defined behind this condition since they are only
// relevant when using Cairo Native.
if std::env::var("CARGO_FEATURE_CAIRO_NATIVE").is_ok() {
compile_cairo_native_aot_runtime();
}
// Build instructions are defined behind this condition since they are only relevant when using
// Cairo Native.
#[cfg(feature = "cairo_native")]
compile_cairo_native_aot_runtime();
}
3 changes: 2 additions & 1 deletion crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::path::PathBuf;

use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use infra_utils::compile_time_cargo_manifest_dir;
use starknet_api::abi::abi_utils::{get_fee_token_var_address, selector_from_name};
use starknet_api::block::{BlockHash, BlockHashAndNumber, BlockNumber, GasPrice, NonzeroGasPrice};
use starknet_api::core::{ClassHash, ContractAddress};
Expand Down Expand Up @@ -195,7 +196,7 @@ pub fn pad_address_to_64(address: &str) -> String {
}

pub fn get_raw_contract_class(contract_path: &str) -> String {
let path: PathBuf = [env!("CARGO_MANIFEST_DIR"), contract_path].iter().collect();
let path: PathBuf = [compile_time_cargo_manifest_dir!(), contract_path].iter().collect();
fs::read_to_string(path).unwrap()
}

Expand Down
5 changes: 3 additions & 2 deletions crates/blockifier/src/test_utils/cairo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::process::{Command, Output};
use std::{env, fs};

use cached::proc_macro::cached;
use infra_utils::compile_time_cargo_manifest_dir;
use serde::{Deserialize, Serialize};
use tempfile::NamedTempFile;

Expand Down Expand Up @@ -83,7 +84,7 @@ pub fn cairo1_compiler_tag() -> String {
/// 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!("CARGO_MANIFEST_DIR");
let manifest_dir = compile_time_cargo_manifest_dir!();

Path::new(&manifest_dir).join(
env::var(CAIRO1_REPO_RELATIVE_PATH_OVERRIDE_ENV_VAR)
Expand Down Expand Up @@ -217,7 +218,7 @@ fn verify_cairo0_compiler_deps() {
} else {
format!("installed version: {cairo_lang_version}")
},
env!("CARGO_MANIFEST_DIR"),
compile_time_cargo_manifest_dir!(),
CAIRO0_PIP_REQUIREMENTS_FILE
);
}
Expand Down
7 changes: 4 additions & 3 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{fs, io};
use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use indexmap::{IndexMap, IndexSet};
use infra_utils::compile_time_cargo_manifest_dir;
use num_rational::Ratio;
use num_traits::Inv;
use papyrus_config::dumping::{ser_param, SerializeConfig};
Expand Down Expand Up @@ -96,10 +97,10 @@ macro_rules! define_versioned_constants {
pub static VERSIONED_CONSTANTS_LATEST_JSON: LazyLock<String> = LazyLock::new(|| {
let latest_variant = StarknetVersion::LATEST;
let path_to_json: PathBuf = [
env!("CARGO_MANIFEST_DIR"),
"src",
compile_time_cargo_manifest_dir!(),
"src".into(),
VersionedConstants::path_to_json(&latest_variant)
.expect("Latest variant should have a path to json.")
.expect("Latest variant should have a path to json.").into()
].iter().collect();
fs::read_to_string(path_to_json.clone())
.expect(&format!("Failed to read file {}.", path_to_json.display()))
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/versioned_constants_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::*;

/// Returns all JSON files in the resources directory (should be all versioned constants files).
fn all_jsons_in_dir() -> Paths {
glob(format!("{}/resources/*.json", env!("CARGO_MANIFEST_DIR")).as_str()).unwrap()
glob(format!("{}/resources/*.json", compile_time_cargo_manifest_dir!()).as_str()).unwrap()
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn get_test_url() -> String {
url
}

/// Retrieves the test block_number from the `TEST_URL` environment variable,
/// Retrieves the test block_number from the `BLOCK_NUMBER` environment variable,
/// falling back to the latest block if not provided.
pub fn get_test_block_id() -> BlockId {
match env::var("BLOCK_NUMBER") {
Expand Down
13 changes: 13 additions & 0 deletions crates/infra_utils/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ mod path_test;

// TODO(tsabary): wrap path-related env::* invocations in the repo as utility functions

// TODO(Tsabary): find a stable way to get access to the current crate directory at compile time.
#[macro_export]
macro_rules! compile_time_cargo_manifest_dir {
() => {
env!("CARGO_MANIFEST_DIR")
};
}

// TODO(Tsabary/ Arni): consolidate with other get_absolute_path functions.
/// Resolves a relative path from the project root directory and returns its absolute path.
///
Expand Down Expand Up @@ -36,3 +44,8 @@ fn path_of_project_root() -> PathBuf {
// directories above the current file.
PathBuf::from(env!("CARGO_MANIFEST_DIR")).ancestors().nth(2).expect("Cannot navigate up").into()
}

// TODO(Tsabary/ Arni): consider alternatives.
pub fn current_dir() -> std::io::Result<PathBuf> {
std::env::current_dir()
}
9 changes: 0 additions & 9 deletions crates/native_blockifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ fn native_blockifier(py: Python<'_>, py_module: &PyModule) -> PyResult<()> {
py_module.add("UndeclaredClassHashError", py.get_type::<UndeclaredClassHashError>())?;
add_py_exceptions(py, py_module)?;

py_module.add_function(wrap_pyfunction!(blockifier_version, py)?)?;
py_module.add_function(wrap_pyfunction!(starknet_version, py)?)?;

// TODO(Dori, 1/4/2023): If and when supported in the Python build environment, gate this code
Expand All @@ -69,14 +68,6 @@ fn native_blockifier(py: Python<'_>, py_module: &PyModule) -> PyResult<()> {
Ok(())
}

/// Returns the version that the `blockifier` and `native_blockifier` crates were built with.
// Assumption: both `blockifier` and `native_blockifier` use `version.workspace` in the package
// section of their `Cargo.toml`.
#[pyfunction]
pub fn blockifier_version() -> PyResult<String> {
Ok(env!("CARGO_PKG_VERSION").to_string())
}

/// Returns the latest Starknet version for versioned constants.
#[pyfunction]
pub fn starknet_version() -> PyResult<String> {
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_sierra_compile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ assert_matches.workspace = true
infra_utils.workspace = true
mempool_test_utils.workspace = true
rstest.workspace = true

[build-dependencies]
infra_utils.workspace = true
4 changes: 3 additions & 1 deletion crates/starknet_sierra_compile/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,7 @@ fn out_dir() -> std::path::PathBuf {

#[cfg(feature = "cairo_native")]
fn repo_root_dir() -> std::path::PathBuf {
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../..").to_path_buf()
std::path::Path::new(infra_utils::compile_time_cargo_manifest_dir!())
.join("../..")
.to_path_buf()
}
Loading