Skip to content

Commit

Permalink
feat(infra_utils): add compile-time manifest dir macro
Browse files Browse the repository at this point in the history
Signed-off-by: Dori Medini <[email protected]>
  • Loading branch information
dorimedini-starkware committed Nov 27, 2024
1 parent 1326d49 commit dad665e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const Configuration = {
'gateway',
'helm',
'infra',
'infra_utils',
'JSON-RPC',
'l1_provider',
'load_test',
Expand Down
8 changes: 4 additions & 4 deletions crates/blockifier/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ fn compile_cairo_native_aot_runtime() {
use std::path::PathBuf;
use std::process::Command;

use infra_utils::path::{cargo_manifest_dir, current_dir};
use infra_utils::compile_time_cargo_manifest_dir;
use infra_utils::path::current_dir;

let cairo_native_dir = cargo_manifest_dir()
.expect("Failed to get current directory")
.join(PathBuf::from("cairo_native"));
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
9 changes: 2 additions & 7 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::path::PathBuf;

use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use infra_utils::path::cargo_manifest_dir;
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 @@ -199,12 +199,7 @@ pub fn pad_address_to_64(address: &str) -> String {
}

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

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

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

Expand Down Expand Up @@ -70,7 +70,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 = cargo_manifest_dir().unwrap();
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 @@ -200,7 +200,7 @@ fn verify_cairo0_compiler_deps() {
} else {
format!("installed version: {cairo_lang_version}")
},
cargo_manifest_dir().unwrap().to_str().unwrap(),
compile_time_cargo_manifest_dir!(),
CAIRO0_PIP_REQUIREMENTS_FILE
);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +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::path::cargo_manifest_dir;
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 @@ -95,7 +95,7 @@ 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 = [
cargo_manifest_dir().expect("CARGO_MANIFEST_DIR should be defined."),
compile_time_cargo_manifest_dir!(),
"src".into(),
VersionedConstants::path_to_json(&latest_variant)
.expect("Latest variant should have a path to json.").into()
Expand Down
3 changes: 1 addition & 2 deletions crates/blockifier/src/versioned_constants_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +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", cargo_manifest_dir().unwrap().to_str().unwrap()).as_str())
.unwrap()
glob(format!("{}/resources/*.json", compile_time_cargo_manifest_dir!()).as_str()).unwrap()
}

#[test]
Expand Down
8 changes: 8 additions & 0 deletions crates/infra_utils/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ pub fn cargo_manifest_dir() -> Option<PathBuf> {
PATH_TO_CARGO_MANIFEST_DIR.clone()
}

// 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 {
() => {
option_env!("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set at compile time.")
};
}

// 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

0 comments on commit dad665e

Please sign in to comment.