diff --git a/Cargo.lock b/Cargo.lock index 7bee816fec..97d9e8aa92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1498,6 +1498,7 @@ dependencies = [ "derive_more 0.99.18", "glob", "indexmap 2.6.0", + "infra_utils", "itertools 0.12.1", "keccak", "log", diff --git a/crates/blockifier/Cargo.toml b/crates/blockifier/Cargo.toml index 7f05e3127d..b752a6531f 100644 --- a/crates/blockifier/Cargo.toml +++ b/crates/blockifier/Cargo.toml @@ -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 @@ -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" diff --git a/crates/blockifier/build.rs b/crates/blockifier/build.rs index f23af037cf..552782168d 100644 --- a/crates/blockifier/build.rs +++ b/crates/blockifier/build.rs @@ -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!( @@ -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) } }; @@ -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(); } diff --git a/crates/blockifier/src/test_utils.rs b/crates/blockifier/src/test_utils.rs index 0949853591..a2d8164767 100644 --- a/crates/blockifier/src/test_utils.rs +++ b/crates/blockifier/src/test_utils.rs @@ -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}; @@ -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() } diff --git a/crates/blockifier/src/test_utils/cairo_compile.rs b/crates/blockifier/src/test_utils/cairo_compile.rs index 14c26b0636..a01ca773b5 100644 --- a/crates/blockifier/src/test_utils/cairo_compile.rs +++ b/crates/blockifier/src/test_utils/cairo_compile.rs @@ -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; @@ -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) @@ -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 ); } diff --git a/crates/blockifier/src/versioned_constants.rs b/crates/blockifier/src/versioned_constants.rs index e30236ea64..3975f45f7a 100644 --- a/crates/blockifier/src/versioned_constants.rs +++ b/crates/blockifier/src/versioned_constants.rs @@ -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}; @@ -96,10 +97,10 @@ macro_rules! define_versioned_constants { pub static VERSIONED_CONSTANTS_LATEST_JSON: LazyLock = 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())) diff --git a/crates/blockifier/src/versioned_constants_test.rs b/crates/blockifier/src/versioned_constants_test.rs index 26953061ce..f18a9f2e59 100644 --- a/crates/blockifier/src/versioned_constants_test.rs +++ b/crates/blockifier/src/versioned_constants_test.rs @@ -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] diff --git a/crates/blockifier_reexecution/src/state_reader/rpc_https_test.rs b/crates/blockifier_reexecution/src/state_reader/rpc_https_test.rs index bce11597c6..55e48c3911 100644 --- a/crates/blockifier_reexecution/src/state_reader/rpc_https_test.rs +++ b/crates/blockifier_reexecution/src/state_reader/rpc_https_test.rs @@ -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") { diff --git a/crates/infra_utils/src/path.rs b/crates/infra_utils/src/path.rs index 2beb36cc75..98f2adcf8e 100644 --- a/crates/infra_utils/src/path.rs +++ b/crates/infra_utils/src/path.rs @@ -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. /// @@ -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 { + std::env::current_dir() +} diff --git a/crates/native_blockifier/src/lib.rs b/crates/native_blockifier/src/lib.rs index 5b94780628..0524d52fa3 100644 --- a/crates/native_blockifier/src/lib.rs +++ b/crates/native_blockifier/src/lib.rs @@ -51,7 +51,6 @@ fn native_blockifier(py: Python<'_>, py_module: &PyModule) -> PyResult<()> { py_module.add("UndeclaredClassHashError", py.get_type::())?; 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 @@ -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 { - Ok(env!("CARGO_PKG_VERSION").to_string()) -} - /// Returns the latest Starknet version for versioned constants. #[pyfunction] pub fn starknet_version() -> PyResult { diff --git a/crates/starknet_sierra_compile/Cargo.toml b/crates/starknet_sierra_compile/Cargo.toml index 8e40f025b3..bd559d226e 100644 --- a/crates/starknet_sierra_compile/Cargo.toml +++ b/crates/starknet_sierra_compile/Cargo.toml @@ -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 diff --git a/crates/starknet_sierra_compile/build.rs b/crates/starknet_sierra_compile/build.rs index 298d996035..b2b531eb23 100644 --- a/crates/starknet_sierra_compile/build.rs +++ b/crates/starknet_sierra_compile/build.rs @@ -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() }