2021-11-26 16:49:19 +00:00
|
|
|
#![doc = include_str ! ("../README.md")]
|
2021-10-26 11:28:10 +00:00
|
|
|
|
|
|
|
pub mod artifacts;
|
|
|
|
|
|
|
|
pub use artifacts::{CompilerInput, CompilerOutput, EvmVersion};
|
2021-10-30 17:59:44 +00:00
|
|
|
use std::collections::btree_map::Entry;
|
2021-10-26 11:28:10 +00:00
|
|
|
|
|
|
|
pub mod cache;
|
|
|
|
|
|
|
|
mod compile;
|
2021-11-26 16:49:19 +00:00
|
|
|
|
2021-10-31 14:41:36 +00:00
|
|
|
pub use compile::*;
|
2021-10-26 11:28:10 +00:00
|
|
|
|
|
|
|
mod config;
|
2021-11-26 16:49:19 +00:00
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
pub use config::{
|
|
|
|
AllowedLibPaths, Artifact, ArtifactOutput, MinimalCombinedArtifacts, ProjectPathsConfig,
|
|
|
|
SolcConfig,
|
|
|
|
};
|
2021-10-30 17:59:44 +00:00
|
|
|
|
2021-11-13 19:31:55 +00:00
|
|
|
pub mod remappings;
|
|
|
|
|
2021-10-30 17:59:44 +00:00
|
|
|
use crate::{artifacts::Source, cache::SolFilesCache};
|
2021-10-26 11:28:10 +00:00
|
|
|
|
|
|
|
pub mod error;
|
|
|
|
pub mod utils;
|
2021-11-26 16:49:19 +00:00
|
|
|
|
2021-10-30 17:59:44 +00:00
|
|
|
use crate::artifacts::Sources;
|
2021-10-26 11:28:10 +00:00
|
|
|
use error::Result;
|
2021-10-30 17:59:44 +00:00
|
|
|
use std::{
|
2021-11-15 23:29:06 +00:00
|
|
|
borrow::Cow,
|
2021-10-30 17:59:44 +00:00
|
|
|
collections::{BTreeMap, HashMap},
|
2021-11-08 20:11:45 +00:00
|
|
|
convert::TryInto,
|
2021-10-30 17:59:44 +00:00
|
|
|
fmt, fs, io,
|
2021-11-15 23:29:06 +00:00
|
|
|
marker::PhantomData,
|
2021-10-30 17:59:44 +00:00
|
|
|
path::PathBuf,
|
|
|
|
};
|
2021-10-26 11:28:10 +00:00
|
|
|
|
|
|
|
/// Handles contract compiling
|
|
|
|
#[derive(Debug)]
|
2021-11-15 23:29:06 +00:00
|
|
|
pub struct Project<Artifacts: ArtifactOutput = MinimalCombinedArtifacts> {
|
2021-10-26 11:28:10 +00:00
|
|
|
/// The layout of the
|
2021-10-30 17:59:44 +00:00
|
|
|
pub paths: ProjectPathsConfig,
|
2021-10-26 11:28:10 +00:00
|
|
|
/// Where to find solc
|
|
|
|
pub solc: Solc,
|
2021-10-30 17:59:44 +00:00
|
|
|
/// How solc invocation should be configured.
|
|
|
|
pub solc_config: SolcConfig,
|
2021-10-26 11:28:10 +00:00
|
|
|
/// Whether caching is enabled
|
|
|
|
pub cached: bool,
|
2021-11-15 23:29:06 +00:00
|
|
|
/// Whether writing artifacts to disk is enabled
|
|
|
|
pub no_artifacts: bool,
|
2021-11-18 13:10:41 +00:00
|
|
|
/// Whether writing artifacts to disk is enabled
|
|
|
|
pub auto_detect: bool,
|
2021-10-26 11:28:10 +00:00
|
|
|
/// How to handle compiler output
|
2021-11-15 23:29:06 +00:00
|
|
|
pub artifacts: PhantomData<Artifacts>,
|
2021-10-30 17:59:44 +00:00
|
|
|
/// Errors/Warnings which match these error codes are not going to be logged
|
|
|
|
pub ignored_error_codes: Vec<u64>,
|
2021-11-08 20:11:45 +00:00
|
|
|
/// The paths which will be allowed for library inclusion
|
|
|
|
pub allowed_lib_paths: AllowedLibPaths,
|
2021-10-26 11:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Project {
|
2021-11-15 23:29:06 +00:00
|
|
|
/// Convenience function to call `ProjectBuilder::default()`
|
2021-10-30 17:59:44 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2021-11-15 23:29:06 +00:00
|
|
|
/// Configure with `MinimalCombinedArtifacts` artifacts output
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use ethers_solc::Project;
|
|
|
|
/// let config = Project::builder().build().unwrap();
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// To configure any a project with any `ArtifactOutput` use either
|
|
|
|
///
|
2021-10-30 17:59:44 +00:00
|
|
|
/// ```rust
|
|
|
|
/// use ethers_solc::Project;
|
|
|
|
/// let config = Project::builder().build().unwrap();
|
|
|
|
/// ```
|
2021-11-15 23:29:06 +00:00
|
|
|
///
|
|
|
|
/// or use the builder directly
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use ethers_solc::{MinimalCombinedArtifacts, ProjectBuilder};
|
|
|
|
/// let config = ProjectBuilder::<MinimalCombinedArtifacts>::default().build().unwrap();
|
|
|
|
/// ```
|
2021-10-30 17:59:44 +00:00
|
|
|
pub fn builder() -> ProjectBuilder {
|
|
|
|
ProjectBuilder::default()
|
2021-10-26 11:28:10 +00:00
|
|
|
}
|
2021-11-15 23:29:06 +00:00
|
|
|
}
|
2021-10-26 11:28:10 +00:00
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
impl<Artifacts: ArtifactOutput> Project<Artifacts> {
|
|
|
|
fn write_cache_file(
|
|
|
|
&self,
|
|
|
|
sources: Sources,
|
|
|
|
artifacts: Vec<(PathBuf, Vec<String>)>,
|
|
|
|
) -> Result<()> {
|
|
|
|
let mut cache = SolFilesCache::builder()
|
2021-10-30 17:59:44 +00:00
|
|
|
.root(&self.paths.root)
|
|
|
|
.solc_config(self.solc_config.clone())
|
|
|
|
.insert_files(sources)?;
|
2021-11-15 23:29:06 +00:00
|
|
|
|
|
|
|
// add the artifacts for each file to the cache entry
|
|
|
|
for (file, artifacts) in artifacts {
|
|
|
|
if let Some(entry) = cache.files.get_mut(&file) {
|
|
|
|
entry.artifacts = artifacts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 17:59:44 +00:00
|
|
|
if let Some(cache_dir) = self.paths.cache.parent() {
|
|
|
|
fs::create_dir_all(cache_dir)?
|
|
|
|
}
|
|
|
|
cache.write(&self.paths.cache)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns all sources found under the project's sources path
|
|
|
|
pub fn sources(&self) -> io::Result<Sources> {
|
|
|
|
Source::read_all_from(self.paths.sources.as_path())
|
|
|
|
}
|
|
|
|
|
2021-11-20 14:47:36 +00:00
|
|
|
/// This emits the cargo [`rerun-if-changed`](https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorerun-if-changedpath) instruction.
|
|
|
|
/// Which tells Cargo to re-run the build script if a file inside the project's sources
|
|
|
|
/// directory has changed.
|
|
|
|
///
|
|
|
|
/// Use this if you compile a project in a `build.rs` file.
|
|
|
|
///
|
|
|
|
/// # Example `build.rs` file
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use ethers_solc::{Project, ProjectPathsConfig};
|
|
|
|
/// // configure the project with all its paths, solc, cache etc.
|
|
|
|
/// let project = Project::builder()
|
|
|
|
/// .paths(ProjectPathsConfig::hardhat(env!("CARGO_MANIFEST_DIR")).unwrap())
|
|
|
|
/// .build()
|
|
|
|
/// .unwrap();
|
|
|
|
/// let output = project.compile().unwrap();
|
|
|
|
/// // Tell Cargo that if a source file changes, to rerun this build script.
|
|
|
|
/// project.rerun_if_sources_changed();
|
|
|
|
/// ```
|
|
|
|
pub fn rerun_if_sources_changed(&self) {
|
|
|
|
println!("cargo:rerun-if-changed={}", self.paths.sources.display())
|
|
|
|
}
|
|
|
|
|
2021-10-30 17:59:44 +00:00
|
|
|
/// Attempts to read all unique libraries that are used as imports like "hardhat/console.sol"
|
|
|
|
fn resolved_libraries(
|
|
|
|
&self,
|
|
|
|
sources: &Sources,
|
|
|
|
) -> io::Result<BTreeMap<PathBuf, (Source, PathBuf)>> {
|
|
|
|
let mut libs = BTreeMap::default();
|
|
|
|
for source in sources.values() {
|
|
|
|
for import in source.parse_imports() {
|
|
|
|
if let Some(lib) = utils::resolve_library(&self.paths.libraries, import) {
|
|
|
|
if let Entry::Vacant(entry) = libs.entry(import.into()) {
|
|
|
|
entry.insert((Source::read(&lib)?, lib));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(libs)
|
2021-10-26 11:28:10 +00:00
|
|
|
}
|
|
|
|
|
2021-10-30 17:59:44 +00:00
|
|
|
/// Attempts to compile the contracts found at the configured location.
|
|
|
|
///
|
|
|
|
/// NOTE: this does not check if the contracts were successfully compiled, see
|
|
|
|
/// `CompilerOutput::has_error` instead.
|
2021-11-03 08:05:09 +00:00
|
|
|
|
|
|
|
/// NB: If the `svm` feature is enabled, this function will automatically detect
|
|
|
|
/// solc versions across files.
|
2021-11-15 23:29:06 +00:00
|
|
|
pub fn compile(&self) -> Result<ProjectCompileOutput<Artifacts>> {
|
2021-11-03 08:05:09 +00:00
|
|
|
let sources = self.sources()?;
|
|
|
|
|
|
|
|
#[cfg(all(feature = "svm", feature = "async"))]
|
2021-11-18 13:10:41 +00:00
|
|
|
if self.auto_detect {
|
|
|
|
return self.svm_compile(sources)
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut solc = self.solc.clone();
|
|
|
|
if !self.allowed_lib_paths.0.is_empty() {
|
|
|
|
solc = solc.arg("--allow-paths").arg(self.allowed_lib_paths.to_string());
|
|
|
|
}
|
|
|
|
self.compile_with_version(&solc, sources)
|
2021-11-03 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(feature = "svm", feature = "async"))]
|
2021-11-15 23:29:06 +00:00
|
|
|
fn svm_compile(&self, sources: Sources) -> Result<ProjectCompileOutput<Artifacts>> {
|
2021-11-03 08:05:09 +00:00
|
|
|
// split them by version
|
|
|
|
let mut sources_by_version = BTreeMap::new();
|
2021-11-26 10:30:14 +00:00
|
|
|
// we store the solc versions by path, in case there exists a corrupt solc binary
|
|
|
|
let mut solc_versions = HashMap::new();
|
|
|
|
|
2021-11-03 08:05:09 +00:00
|
|
|
for (path, source) in sources.into_iter() {
|
|
|
|
// will detect and install the solc version
|
|
|
|
let version = Solc::detect_version(&source)?;
|
|
|
|
// gets the solc binary for that version, it is expected tha this will succeed
|
|
|
|
// AND find the solc since it was installed right above
|
2021-11-08 20:11:45 +00:00
|
|
|
let mut solc = Solc::find_svm_installed_version(version.to_string())?
|
2021-11-03 08:05:09 +00:00
|
|
|
.expect("solc should have been installed");
|
2021-11-08 20:11:45 +00:00
|
|
|
|
|
|
|
if !self.allowed_lib_paths.0.is_empty() {
|
|
|
|
solc = solc.arg("--allow-paths").arg(self.allowed_lib_paths.to_string());
|
|
|
|
}
|
2021-11-26 10:30:14 +00:00
|
|
|
solc_versions.insert(solc.solc.clone(), version);
|
2021-11-03 08:05:09 +00:00
|
|
|
let entry = sources_by_version.entry(solc).or_insert_with(BTreeMap::new);
|
2021-11-26 10:30:14 +00:00
|
|
|
entry.insert(path.clone(), source);
|
2021-11-03 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
let mut compiled =
|
|
|
|
ProjectCompileOutput::with_ignored_errors(self.ignored_error_codes.clone());
|
|
|
|
|
2021-11-03 08:05:09 +00:00
|
|
|
// run the compilation step for each version
|
|
|
|
for (solc, sources) in sources_by_version {
|
2021-11-26 10:30:14 +00:00
|
|
|
// verify that this solc version's checksum matches the checksum found remotely. If
|
|
|
|
// not, re-install the same version.
|
|
|
|
let version = solc_versions.get(&solc.solc).unwrap();
|
|
|
|
if let Err(_e) = solc.verify_checksum() {
|
|
|
|
Solc::blocking_install(version)?;
|
|
|
|
}
|
|
|
|
// once matched, proceed to compile with it
|
2021-11-15 23:29:06 +00:00
|
|
|
compiled.extend(self.compile_with_version(&solc, sources)?);
|
2021-11-03 08:05:09 +00:00
|
|
|
}
|
2021-11-15 23:29:06 +00:00
|
|
|
if !compiled.has_compiled_contracts() &&
|
|
|
|
!compiled.has_compiler_errors() &&
|
|
|
|
self.cached &&
|
|
|
|
self.paths.cache.exists()
|
|
|
|
{
|
|
|
|
let cache = SolFilesCache::read(&self.paths.cache)?;
|
|
|
|
let artifacts = cache.read_artifacts::<Artifacts>(&self.paths.artifacts)?;
|
|
|
|
compiled.artifacts.extend(artifacts);
|
|
|
|
}
|
|
|
|
Ok(compiled)
|
2021-11-03 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compile_with_version(
|
|
|
|
&self,
|
|
|
|
solc: &Solc,
|
|
|
|
mut sources: Sources,
|
2021-11-15 23:29:06 +00:00
|
|
|
) -> Result<ProjectCompileOutput<Artifacts>> {
|
2021-10-30 17:59:44 +00:00
|
|
|
// add all libraries to the source set while keeping track of their actual disk path
|
2021-11-15 23:29:06 +00:00
|
|
|
// (`contracts/contract.sol` -> `/Users/.../contracts.sol`)
|
|
|
|
let mut source_name_to_path = HashMap::new();
|
|
|
|
// inverse of `source_name_to_path` : (`/Users/.../contracts.sol` ->
|
|
|
|
// `contracts/contract.sol`)
|
|
|
|
let mut path_to_source_name = HashMap::new();
|
|
|
|
|
2021-10-30 17:59:44 +00:00
|
|
|
for (import, (source, path)) in self.resolved_libraries(&sources)? {
|
|
|
|
// inserting with absolute path here and keep track of the source name <-> path mappings
|
|
|
|
sources.insert(path.clone(), source);
|
2021-11-15 23:29:06 +00:00
|
|
|
path_to_source_name.insert(path.clone(), import.clone());
|
|
|
|
source_name_to_path.insert(import, path);
|
2021-10-30 17:59:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-30 18:27:17 +00:00
|
|
|
// If there's a cache set, filter to only re-compile the files which were changed
|
2021-11-26 16:49:19 +00:00
|
|
|
let (sources, cached_artifacts) = if self.cached && self.paths.cache.exists() {
|
|
|
|
let mut cache = SolFilesCache::read(&self.paths.cache)?;
|
|
|
|
cache.remove_missing_files();
|
2021-11-15 23:29:06 +00:00
|
|
|
let changed_files = cache.get_changed_or_missing_artifacts_files::<Artifacts>(
|
|
|
|
sources,
|
|
|
|
Some(&self.solc_config),
|
|
|
|
&self.paths.artifacts,
|
|
|
|
);
|
|
|
|
|
2021-11-26 16:49:19 +00:00
|
|
|
let cached_artifacts = if self.paths.artifacts.exists() {
|
|
|
|
cache.read_artifacts::<Artifacts>(&self.paths.artifacts)?
|
|
|
|
} else {
|
|
|
|
BTreeMap::default()
|
|
|
|
};
|
2021-11-15 23:29:06 +00:00
|
|
|
// if nothing changed and all artifacts still exist
|
2021-10-30 18:27:17 +00:00
|
|
|
if changed_files.is_empty() {
|
2021-11-26 16:49:19 +00:00
|
|
|
return Ok(ProjectCompileOutput::from_unchanged(cached_artifacts))
|
2021-10-30 17:59:44 +00:00
|
|
|
}
|
2021-11-26 16:49:19 +00:00
|
|
|
// There are changed files and maybe some cached files
|
|
|
|
(changed_files, cached_artifacts)
|
2021-10-30 18:27:17 +00:00
|
|
|
} else {
|
2021-11-26 16:49:19 +00:00
|
|
|
(sources, BTreeMap::default())
|
2021-10-30 18:27:17 +00:00
|
|
|
};
|
2021-10-30 17:59:44 +00:00
|
|
|
|
|
|
|
// replace absolute path with source name to make solc happy
|
2021-11-15 23:29:06 +00:00
|
|
|
let sources = apply_mappings(sources, path_to_source_name);
|
2021-10-30 17:59:44 +00:00
|
|
|
|
2021-11-13 19:31:55 +00:00
|
|
|
let input = CompilerInput::with_sources(sources)
|
|
|
|
.normalize_evm_version(&solc.version()?)
|
|
|
|
.with_remappings(self.paths.remappings.clone());
|
2021-11-03 08:05:09 +00:00
|
|
|
let output = solc.compile(&input)?;
|
2021-10-30 17:59:44 +00:00
|
|
|
if output.has_error() {
|
2021-11-15 23:29:06 +00:00
|
|
|
return Ok(ProjectCompileOutput::from_compiler_output(
|
|
|
|
output,
|
|
|
|
self.ignored_error_codes.clone(),
|
|
|
|
))
|
2021-10-30 17:59:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 11:28:10 +00:00
|
|
|
if self.cached {
|
2021-11-15 23:29:06 +00:00
|
|
|
// get all contract names of the files and map them to the disk file
|
|
|
|
let artifacts = output
|
|
|
|
.contracts
|
|
|
|
.iter()
|
|
|
|
.map(|(path, contracts)| {
|
|
|
|
let path = PathBuf::from(path);
|
|
|
|
let file = source_name_to_path.get(&path).cloned().unwrap_or(path);
|
|
|
|
(file, contracts.keys().cloned().collect::<Vec<_>>())
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2021-10-30 17:59:44 +00:00
|
|
|
// reapply to disk paths
|
2021-11-15 23:29:06 +00:00
|
|
|
let sources = apply_mappings(input.sources, source_name_to_path);
|
|
|
|
|
2021-10-30 17:59:44 +00:00
|
|
|
// create cache file
|
2021-11-15 23:29:06 +00:00
|
|
|
self.write_cache_file(sources, artifacts)?;
|
2021-10-30 17:59:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
// TODO: There seems to be some type redundancy here, c.f. discussion with @mattsse
|
|
|
|
if !self.no_artifacts {
|
|
|
|
Artifacts::on_output(&output, &self.paths)?;
|
|
|
|
}
|
2021-11-26 16:49:19 +00:00
|
|
|
Ok(ProjectCompileOutput::from_compiler_output_and_cache(
|
|
|
|
output,
|
|
|
|
cached_artifacts,
|
|
|
|
self.ignored_error_codes.clone(),
|
|
|
|
))
|
2021-10-30 17:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_mappings(sources: Sources, mut mappings: HashMap<PathBuf, PathBuf>) -> Sources {
|
|
|
|
sources
|
|
|
|
.into_iter()
|
|
|
|
.map(|(import, source)| {
|
|
|
|
if let Some(path) = mappings.remove(&import) {
|
|
|
|
(path, source)
|
2021-10-26 11:28:10 +00:00
|
|
|
} else {
|
2021-10-30 17:59:44 +00:00
|
|
|
(import, source)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
pub struct ProjectBuilder<Artifacts: ArtifactOutput = MinimalCombinedArtifacts> {
|
2021-10-30 17:59:44 +00:00
|
|
|
/// The layout of the
|
|
|
|
paths: Option<ProjectPathsConfig>,
|
|
|
|
/// Where to find solc
|
|
|
|
solc: Option<Solc>,
|
|
|
|
/// How solc invocation should be configured.
|
|
|
|
solc_config: Option<SolcConfig>,
|
|
|
|
/// Whether caching is enabled, default is true.
|
|
|
|
cached: bool,
|
2021-11-15 23:29:06 +00:00
|
|
|
/// Whether writing artifacts to disk is enabled, default is true.
|
|
|
|
no_artifacts: bool,
|
2021-11-18 13:10:41 +00:00
|
|
|
/// Whether automatic solc version detection is enabled
|
|
|
|
auto_detect: bool,
|
2021-11-15 23:29:06 +00:00
|
|
|
artifacts: PhantomData<Artifacts>,
|
2021-10-30 17:59:44 +00:00
|
|
|
/// Which error codes to ignore
|
|
|
|
pub ignored_error_codes: Vec<u64>,
|
2021-11-08 20:11:45 +00:00
|
|
|
/// All allowed paths
|
|
|
|
pub allowed_paths: Vec<PathBuf>,
|
2021-10-30 17:59:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
impl<Artifacts: ArtifactOutput> ProjectBuilder<Artifacts> {
|
2021-10-30 17:59:44 +00:00
|
|
|
pub fn paths(mut self, paths: ProjectPathsConfig) -> Self {
|
|
|
|
self.paths = Some(paths);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn solc(mut self, solc: impl Into<Solc>) -> Self {
|
|
|
|
self.solc = Some(solc.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn solc_config(mut self, solc_config: SolcConfig) -> Self {
|
|
|
|
self.solc_config = Some(solc_config);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ignore_error_code(mut self, code: u64) -> Self {
|
|
|
|
self.ignored_error_codes.push(code);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Disables cached builds
|
|
|
|
pub fn ephemeral(mut self) -> Self {
|
|
|
|
self.cached = false;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
/// Disables writing artifacts to disk
|
|
|
|
pub fn no_artifacts(mut self) -> Self {
|
|
|
|
self.no_artifacts = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:10:41 +00:00
|
|
|
/// Disables automatic solc version detection
|
|
|
|
pub fn no_auto_detect(mut self) -> Self {
|
|
|
|
self.auto_detect = false;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
/// Set arbitrary `ArtifactOutputHandler`
|
|
|
|
pub fn artifacts<A: ArtifactOutput>(self) -> ProjectBuilder<A> {
|
|
|
|
let ProjectBuilder {
|
|
|
|
paths,
|
|
|
|
solc,
|
|
|
|
solc_config,
|
|
|
|
cached,
|
|
|
|
no_artifacts,
|
2021-11-18 13:10:41 +00:00
|
|
|
auto_detect,
|
2021-11-15 23:29:06 +00:00
|
|
|
ignored_error_codes,
|
|
|
|
allowed_paths,
|
|
|
|
..
|
|
|
|
} = self;
|
|
|
|
ProjectBuilder {
|
|
|
|
paths,
|
|
|
|
solc,
|
|
|
|
solc_config,
|
|
|
|
cached,
|
|
|
|
no_artifacts,
|
2021-11-18 13:10:41 +00:00
|
|
|
auto_detect,
|
2021-11-15 23:29:06 +00:00
|
|
|
artifacts: PhantomData::default(),
|
|
|
|
ignored_error_codes,
|
|
|
|
allowed_paths,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 20:11:45 +00:00
|
|
|
/// Adds an allowed-path to the solc executable
|
|
|
|
pub fn allowed_path<T: Into<PathBuf>>(mut self, path: T) -> Self {
|
|
|
|
self.allowed_paths.push(path.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds multiple allowed-path to the solc executable
|
|
|
|
pub fn allowed_paths<I, S>(mut self, args: I) -> Self
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = S>,
|
|
|
|
S: Into<PathBuf>,
|
|
|
|
{
|
|
|
|
for arg in args {
|
|
|
|
self = self.allowed_path(arg);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
pub fn build(self) -> Result<Project<Artifacts>> {
|
2021-11-08 20:11:45 +00:00
|
|
|
let Self {
|
|
|
|
paths,
|
|
|
|
solc,
|
|
|
|
solc_config,
|
|
|
|
cached,
|
2021-11-15 23:29:06 +00:00
|
|
|
no_artifacts,
|
2021-11-18 13:10:41 +00:00
|
|
|
auto_detect,
|
2021-11-08 20:11:45 +00:00
|
|
|
artifacts,
|
|
|
|
ignored_error_codes,
|
|
|
|
mut allowed_paths,
|
|
|
|
} = self;
|
2021-10-30 17:59:44 +00:00
|
|
|
|
|
|
|
let solc = solc.unwrap_or_default();
|
2021-11-16 23:25:39 +00:00
|
|
|
let solc_config = solc_config.map(Ok).unwrap_or_else(|| SolcConfig::builder().build())?;
|
2021-10-30 17:59:44 +00:00
|
|
|
|
2021-11-08 20:11:45 +00:00
|
|
|
let paths = paths.map(Ok).unwrap_or_else(ProjectPathsConfig::current_hardhat)?;
|
|
|
|
|
|
|
|
if allowed_paths.is_empty() {
|
|
|
|
// allow every contract under root by default
|
|
|
|
allowed_paths.push(paths.root.clone())
|
|
|
|
}
|
|
|
|
|
2021-10-30 17:59:44 +00:00
|
|
|
Ok(Project {
|
2021-11-08 20:11:45 +00:00
|
|
|
paths,
|
2021-10-30 17:59:44 +00:00
|
|
|
solc,
|
|
|
|
solc_config,
|
|
|
|
cached,
|
2021-11-15 23:29:06 +00:00
|
|
|
no_artifacts,
|
2021-11-18 13:10:41 +00:00
|
|
|
auto_detect,
|
2021-11-15 23:29:06 +00:00
|
|
|
artifacts,
|
2021-10-30 17:59:44 +00:00
|
|
|
ignored_error_codes,
|
2021-11-08 20:11:45 +00:00
|
|
|
allowed_lib_paths: allowed_paths.try_into()?,
|
2021-10-30 17:59:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
impl<Artifacts: ArtifactOutput> Default for ProjectBuilder<Artifacts> {
|
2021-10-30 17:59:44 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
paths: None,
|
|
|
|
solc: None,
|
|
|
|
solc_config: None,
|
|
|
|
cached: true,
|
2021-11-15 23:29:06 +00:00
|
|
|
no_artifacts: false,
|
2021-11-18 13:10:41 +00:00
|
|
|
auto_detect: true,
|
2021-11-15 23:29:06 +00:00
|
|
|
artifacts: PhantomData::default(),
|
2021-10-30 17:59:44 +00:00
|
|
|
ignored_error_codes: Vec::new(),
|
2021-11-08 20:11:45 +00:00
|
|
|
allowed_paths: vec![],
|
2021-10-26 11:28:10 +00:00
|
|
|
}
|
2021-10-30 17:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-26 11:28:10 +00:00
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
/// The outcome of `Project::compile`
|
|
|
|
#[derive(Debug, Clone, PartialEq, Default)]
|
|
|
|
pub struct ProjectCompileOutput<T: ArtifactOutput> {
|
|
|
|
/// If solc was invoked multiple times in `Project::compile` then this contains a merged
|
|
|
|
/// version of all `CompilerOutput`s. If solc was called only once then `compiler_output`
|
|
|
|
/// holds the `CompilerOutput` of that call.
|
|
|
|
compiler_output: Option<CompilerOutput>,
|
|
|
|
/// All artifacts that were read from cache
|
|
|
|
artifacts: BTreeMap<PathBuf, T::Artifact>,
|
|
|
|
ignored_error_codes: Vec<u64>,
|
2021-10-30 17:59:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
impl<T: ArtifactOutput> ProjectCompileOutput<T> {
|
|
|
|
pub fn with_ignored_errors(ignored_errors: Vec<u64>) -> Self {
|
|
|
|
Self {
|
|
|
|
compiler_output: None,
|
|
|
|
artifacts: Default::default(),
|
|
|
|
ignored_error_codes: ignored_errors,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_unchanged(artifacts: BTreeMap<PathBuf, T::Artifact>) -> Self {
|
|
|
|
Self { compiler_output: None, artifacts, ignored_error_codes: vec![] }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_compiler_output(
|
|
|
|
compiler_output: CompilerOutput,
|
|
|
|
ignored_error_codes: Vec<u64>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
compiler_output: Some(compiler_output),
|
|
|
|
artifacts: Default::default(),
|
|
|
|
ignored_error_codes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 16:49:19 +00:00
|
|
|
pub fn from_compiler_output_and_cache(
|
|
|
|
compiler_output: CompilerOutput,
|
|
|
|
cache: BTreeMap<PathBuf, T::Artifact>,
|
|
|
|
ignored_error_codes: Vec<u64>,
|
|
|
|
) -> Self {
|
|
|
|
Self { compiler_output: Some(compiler_output), artifacts: cache, ignored_error_codes }
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:29:06 +00:00
|
|
|
/// Get the (merged) solc compiler output
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::collections::BTreeMap;
|
|
|
|
/// use ethers_solc::artifacts::Contract;
|
|
|
|
/// use ethers_solc::Project;
|
|
|
|
///
|
|
|
|
/// let project = Project::builder().build().unwrap();
|
|
|
|
/// let contracts: BTreeMap<String, Contract> =
|
|
|
|
/// project.compile().unwrap().output().contracts_into_iter().collect();
|
|
|
|
/// ```
|
|
|
|
pub fn output(self) -> CompilerOutput {
|
|
|
|
self.compiler_output.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Combine two outputs
|
|
|
|
pub fn extend(&mut self, compiled: ProjectCompileOutput<T>) {
|
|
|
|
let ProjectCompileOutput { compiler_output, artifacts, .. } = compiled;
|
|
|
|
self.artifacts.extend(artifacts);
|
|
|
|
if let Some(compiled) = compiler_output {
|
|
|
|
if let Some(output) = self.compiler_output.as_mut() {
|
|
|
|
output.errors.extend(compiled.errors);
|
|
|
|
output.sources.extend(compiled.sources);
|
|
|
|
output.contracts.extend(compiled.contracts);
|
|
|
|
} else {
|
|
|
|
self.compiler_output = Some(compiled);
|
2021-10-30 17:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-26 11:28:10 +00:00
|
|
|
}
|
2021-11-15 23:29:06 +00:00
|
|
|
|
|
|
|
/// Whether this type does not contain compiled contracts
|
|
|
|
pub fn is_unchanged(&self) -> bool {
|
|
|
|
!self.has_compiled_contracts()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether this type has a compiler output
|
|
|
|
pub fn has_compiled_contracts(&self) -> bool {
|
|
|
|
if let Some(output) = self.compiler_output.as_ref() {
|
|
|
|
!output.contracts.is_empty()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether there were errors
|
|
|
|
pub fn has_compiler_errors(&self) -> bool {
|
|
|
|
if let Some(output) = self.compiler_output.as_ref() {
|
|
|
|
output.has_error()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds the first contract with the given name and removes it from the set
|
|
|
|
pub fn remove(&mut self, contract: impl AsRef<str>) -> Option<T::Artifact> {
|
|
|
|
let contract = contract.as_ref();
|
|
|
|
if let Some(output) = self.compiler_output.as_mut() {
|
|
|
|
if let contract @ Some(_) = output
|
|
|
|
.contracts
|
|
|
|
.values_mut()
|
|
|
|
.find_map(|c| c.remove(contract).map(T::contract_to_artifact))
|
|
|
|
{
|
|
|
|
return contract
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let key = self
|
|
|
|
.artifacts
|
|
|
|
.iter()
|
|
|
|
.find_map(|(path, _)| {
|
|
|
|
T::contract_name(path).filter(|name| name == contract).map(|_| path)
|
|
|
|
})?
|
|
|
|
.clone();
|
|
|
|
self.artifacts.remove(&key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ArtifactOutput> ProjectCompileOutput<T>
|
|
|
|
where
|
|
|
|
T::Artifact: Clone,
|
|
|
|
{
|
|
|
|
/// Finds the first contract with the given name
|
|
|
|
pub fn find(&self, contract: impl AsRef<str>) -> Option<Cow<T::Artifact>> {
|
|
|
|
let contract = contract.as_ref();
|
|
|
|
if let Some(output) = self.compiler_output.as_ref() {
|
|
|
|
if let contract @ Some(_) = output.contracts.values().find_map(|c| {
|
|
|
|
c.get(contract).map(|c| T::contract_to_artifact(c.clone())).map(Cow::Owned)
|
|
|
|
}) {
|
|
|
|
return contract
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.artifacts.iter().find_map(|(path, art)| {
|
|
|
|
T::contract_name(path).filter(|name| name == contract).map(|_| Cow::Borrowed(art))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ArtifactOutput + 'static> ProjectCompileOutput<T> {
|
|
|
|
/// All artifacts together with their contract name
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::collections::BTreeMap;
|
|
|
|
/// use ethers_solc::artifacts::CompactContract;
|
|
|
|
/// use ethers_solc::Project;
|
|
|
|
///
|
|
|
|
/// let project = Project::builder().build().unwrap();
|
|
|
|
/// let contracts: BTreeMap<String, CompactContract> = project.compile().unwrap().into_artifacts().collect();
|
|
|
|
/// ```
|
|
|
|
pub fn into_artifacts(mut self) -> Box<dyn Iterator<Item = (String, T::Artifact)>> {
|
|
|
|
let artifacts = self.artifacts.into_iter().filter_map(|(path, art)| {
|
|
|
|
T::contract_name(&path)
|
|
|
|
.map(|name| (format!("{:?}:{}", path.file_name().unwrap(), name), art))
|
|
|
|
});
|
|
|
|
|
|
|
|
let artifacts: Box<dyn Iterator<Item = (String, T::Artifact)>> =
|
|
|
|
if let Some(output) = self.compiler_output.take() {
|
|
|
|
Box::new(artifacts.chain(T::output_to_artifacts(output).into_values().flatten()))
|
|
|
|
} else {
|
|
|
|
Box::new(artifacts)
|
|
|
|
};
|
|
|
|
artifacts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ArtifactOutput> fmt::Display for ProjectCompileOutput<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
if let Some(output) = self.compiler_output.as_ref() {
|
|
|
|
output.diagnostics(&self.ignored_error_codes).fmt(f)
|
|
|
|
} else {
|
|
|
|
f.write_str("Nothing to compile")
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 11:28:10 +00:00
|
|
|
}
|
2021-11-03 08:05:09 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
#[cfg(all(feature = "svm", feature = "async"))]
|
|
|
|
fn test_build_all_versions() {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
let paths = ProjectPathsConfig::builder()
|
|
|
|
.root("./test-data/test-contract-versions")
|
|
|
|
.sources("./test-data/test-contract-versions")
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2021-11-15 23:29:06 +00:00
|
|
|
let project = Project::builder().paths(paths).no_artifacts().ephemeral().build().unwrap();
|
2021-11-03 08:05:09 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
2021-11-15 23:29:06 +00:00
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
let contracts = compiled.output().contracts;
|
2021-11-03 08:05:09 +00:00
|
|
|
// Contracts A to F
|
|
|
|
assert_eq!(contracts.keys().count(), 5);
|
|
|
|
}
|
2021-11-08 20:11:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(all(feature = "svm", feature = "async"))]
|
|
|
|
fn test_build_many_libs() {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
let root = std::fs::canonicalize("./test-data/test-contract-libs").unwrap();
|
|
|
|
|
|
|
|
let paths = ProjectPathsConfig::builder()
|
|
|
|
.root(&root)
|
|
|
|
.sources(root.join("src"))
|
|
|
|
.lib(root.join("lib1"))
|
|
|
|
.lib(root.join("lib2"))
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let project = Project::builder()
|
|
|
|
.paths(paths)
|
2021-11-15 23:29:06 +00:00
|
|
|
.no_artifacts()
|
2021-11-08 20:11:45 +00:00
|
|
|
.ephemeral()
|
2021-11-15 23:29:06 +00:00
|
|
|
.no_artifacts()
|
2021-11-08 20:11:45 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let compiled = project.compile().unwrap();
|
2021-11-15 23:29:06 +00:00
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
let contracts = compiled.output().contracts;
|
2021-11-08 20:11:45 +00:00
|
|
|
assert_eq!(contracts.keys().count(), 3);
|
|
|
|
}
|
2021-11-13 19:31:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(all(feature = "svm", feature = "async"))]
|
|
|
|
fn test_build_remappings() {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
let root = std::fs::canonicalize("./test-data/test-contract-remappings").unwrap();
|
|
|
|
let paths = ProjectPathsConfig::builder()
|
|
|
|
.root(&root)
|
|
|
|
.sources(root.join("src"))
|
|
|
|
.lib(root.join("lib"))
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2021-11-15 23:29:06 +00:00
|
|
|
let project = Project::builder().no_artifacts().paths(paths).ephemeral().build().unwrap();
|
2021-11-13 19:31:55 +00:00
|
|
|
let compiled = project.compile().unwrap();
|
2021-11-15 23:29:06 +00:00
|
|
|
assert!(!compiled.has_compiler_errors());
|
|
|
|
let contracts = compiled.output().contracts;
|
2021-11-13 19:31:55 +00:00
|
|
|
assert_eq!(contracts.keys().count(), 2);
|
|
|
|
}
|
2021-11-03 08:05:09 +00:00
|
|
|
}
|