Skip to content

Commit

Permalink
fix: flags for compiling with globset (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrigada authored Mar 18, 2024
1 parent 98e6f42 commit fd46281
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 20 deletions.
12 changes: 3 additions & 9 deletions crates/common/src/zk_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,19 +1113,13 @@ impl ZkSolc {

/// Checks if the contract has been ignored by the user in the configuration file.
fn is_contract_ignored_in_config(&self, contract_path: &Path) -> bool {
let filename = contract_path
.file_name()
.wrap_err(format!("Could not get filename from {:?}", contract_path))
.unwrap()
.to_str()
.unwrap()
.to_string();
let relative_path = contract_path.strip_prefix(self.project.root()).unwrap();

let mut should_compile = match self.config.contracts_to_compile {
Some(ref contracts_to_compile) => {
// compare if there is some member of the vector contracts_to_compile
// present in the filename
contracts_to_compile.iter().any(|c| filename == *c)
contracts_to_compile.iter().any(|c| c.is_match(relative_path))
}
None => true,
};
Expand All @@ -1134,7 +1128,7 @@ impl ZkSolc {
Some(ref avoid_contracts) => {
// compare if there is some member of the vector avoid_contracts
// present in the filename
!avoid_contracts.iter().any(|c| filename == *c)
!avoid_contracts.iter().any(|c| c.is_match(relative_path))
}
None => should_compile,
};
Expand Down
20 changes: 15 additions & 5 deletions crates/config/src/zksolc_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const SOLIDITY: &str = "Solidity";
///
/// This struct holds the configuration settings used for the zkSolc compiler,
/// including the path to the compiler binary and various compiler settings.
#[derive(Clone, Serialize, Deserialize, Debug, Default)]
#[derive(Clone, Debug, Default)]
pub struct ZkSolcConfig {
/// Path to zksolc binary. Can be a URL.
pub compiler_path: PathBuf,
Expand All @@ -34,10 +34,10 @@ pub struct ZkSolcConfig {
pub settings: Settings,

/// contracts to compile
pub contracts_to_compile: Option<Vec<String>>,
pub contracts_to_compile: Option<Vec<globset::GlobMatcher>>,

/// contracts to avoid compiling
pub avoid_contracts: Option<Vec<String>>,
pub avoid_contracts: Option<Vec<globset::GlobMatcher>>,
}

/// Compiler settings for zkSolc.
Expand Down Expand Up @@ -152,8 +152,18 @@ impl ZkSolcConfigBuilder {
Ok(ZkSolcConfig {
compiler_path: self.compiler_path,
settings,
contracts_to_compile: self.contracts_to_compile,
avoid_contracts: self.avoid_contracts,
contracts_to_compile: self.contracts_to_compile.map(|patterns| {
patterns
.into_iter()
.map(|pat| globset::Glob::new(&pat).expect("invalid pattern").compile_matcher())
.collect::<Vec<_>>()
}),
avoid_contracts: self.avoid_contracts.map(|patterns| {
patterns
.into_iter()
.map(|pat| globset::Glob::new(&pat).expect("invalid pattern").compile_matcher())
.collect::<Vec<_>>()
}),
})
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/zkforge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ hyper.workspace = true
tower-http = { workspace = true, features = ["fs"] }
opener = "0.6"
zksync-web3-rs = {git = "https://github.com/lambdaclass/zksync-web3-rs.git", rev = "70327ae5413c517bd4d27502507cdd96ee40cd22"}
globset = "0.4"


[dev-dependencies]
Expand Down
16 changes: 14 additions & 2 deletions crates/zkforge/bin/cmd/script/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,20 @@ impl ScriptArgs {
) -> Result<(Project, ProjectCompileOutput)> {
let mut project = script_config.config.project()?;
let mut zksolc_cfg = script_config.config.zk_solc_config().map_err(|e| eyre::eyre!(e))?;
zksolc_cfg.contracts_to_compile = self.opts.args.compiler.contracts_to_compile.clone();
zksolc_cfg.avoid_contracts = self.opts.args.compiler.avoid_contracts.clone();
zksolc_cfg.contracts_to_compile =
self.opts.args.compiler.contracts_to_compile.clone().map(|patterns| {
patterns
.into_iter()
.map(|pat| globset::Glob::new(&pat).unwrap().compile_matcher())
.collect_vec()
});
zksolc_cfg.avoid_contracts =
self.opts.args.compiler.avoid_contracts.clone().map(|patterns| {
patterns
.into_iter()
.map(|pat| globset::Glob::new(&pat).unwrap().compile_matcher())
.collect_vec()
});
let mut config = script_config.config.clone();
let contract = ContractInfo::from_str(&self.path)?;
self.target_contract = Some(contract.name.clone());
Expand Down
16 changes: 14 additions & 2 deletions crates/zkforge/bin/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use foundry_config::{
get_available_profiles, Config,
};
use foundry_debugger::Debugger;
use itertools::Itertools;
use regex::Regex;
use std::{collections::BTreeMap, fs, sync::mpsc::channel, time::Duration};
use watchexec::config::{InitConfig, RuntimeConfig};
Expand Down Expand Up @@ -152,8 +153,19 @@ impl TestArgs {
let mut project = config.project()?;
// load the zkSolc config
let mut zksolc_cfg = config.zk_solc_config().map_err(|e| eyre::eyre!(e))?;
zksolc_cfg.contracts_to_compile = self.opts.compiler.contracts_to_compile.clone();
zksolc_cfg.avoid_contracts = self.opts.compiler.avoid_contracts.clone();
zksolc_cfg.contracts_to_compile =
self.opts.compiler.contracts_to_compile.clone().map(|patterns| {
patterns
.into_iter()
.map(|pat| globset::Glob::new(&pat).unwrap().compile_matcher())
.collect_vec()
});
zksolc_cfg.avoid_contracts = self.opts.compiler.avoid_contracts.clone().map(|patterns| {
patterns
.into_iter()
.map(|pat| globset::Glob::new(&pat).unwrap().compile_matcher())
.collect_vec()
});
let zk_out_path = project.paths.root.join("zkout");
project.paths.artifacts = zk_out_path;

Expand Down
16 changes: 14 additions & 2 deletions crates/zkforge/bin/cmd/zk_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use foundry_config::{
},
Config,
};
use itertools::Itertools;
use serde::Serialize;
use std::fmt::Debug;
use watchexec::config::{InitConfig, RuntimeConfig};
Expand Down Expand Up @@ -124,8 +125,19 @@ impl ZkBuildArgs {
let mut config = self.try_load_config_emit_warnings()?;
let mut project = config.project()?;
let mut zksolc_cfg = config.zk_solc_config().map_err(|e| eyre::eyre!(e))?;
zksolc_cfg.contracts_to_compile = self.args.compiler.contracts_to_compile.clone();
zksolc_cfg.avoid_contracts = self.args.compiler.avoid_contracts.clone();
zksolc_cfg.contracts_to_compile =
self.args.compiler.contracts_to_compile.clone().map(|patterns| {
patterns
.into_iter()
.map(|pat| globset::Glob::new(&pat).unwrap().compile_matcher())
.collect_vec()
});
zksolc_cfg.avoid_contracts = self.args.compiler.avoid_contracts.clone().map(|patterns| {
patterns
.into_iter()
.map(|pat| globset::Glob::new(&pat).unwrap().compile_matcher())
.collect_vec()
});
//set zk out path
let zk_out_path = project.paths.root.join("zkout");
project.paths.artifacts = zk_out_path;
Expand Down

0 comments on commit fd46281

Please sign in to comment.