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 Dec 8, 2024
1 parent 746ed46 commit b5ce23f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 19 deletions.
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 @@ -13,7 +13,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 @@ -196,12 +196,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].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 @@ -84,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 @@ -218,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
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 @@ -97,7 +97,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 @@ -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
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()
}

0 comments on commit b5ce23f

Please sign in to comment.