1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
#![doc = include_str!("../README.md")]
#![warn(rustdoc::all)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#[macro_use]
extern crate tracing;
#[macro_use]
pub mod error;
pub mod artifacts;
pub use artifacts::{CompilerOutput, EvmVersion, SolcInput};
pub mod sourcemap;
mod artifact_output;
pub use artifact_output::*;
pub mod buildinfo;
pub mod cache;
pub mod flatten;
pub mod hh;
use compilers::{Compiler, CompilerSettings, CompilerVersionManager};
pub use filter::SparseOutputFileFilter;
pub use hh::{HardhatArtifact, HardhatArtifacts};
pub mod resolver;
use resolver::parse::SolData;
pub use resolver::Graph;
pub mod compilers;
mod compile;
pub use compile::{
output::{AggregatedCompilerOutput, ProjectCompileOutput},
*,
};
mod config;
pub use config::{PathStyle, ProjectPaths, ProjectPathsConfig, SolcConfig};
pub mod remappings;
mod filter;
pub use filter::{
FileFilter, FilteredSources, SolcSparseFileFilter, SourceCompilationKind, SparseOutputFilter,
TestFileFilter,
};
use solang_parser::pt::SourceUnitPart;
pub mod report;
pub mod utils;
use crate::{
artifacts::{Source, SourceFile, Sources, StandardJsonCompilerInput},
cache::CompilerCache,
error::{SolcError, SolcIoError},
sources::{VersionedSourceFile, VersionedSourceFiles},
};
use artifacts::{contract::Contract, output_selection::OutputSelection, Severity};
use compile::output::contracts::VersionedContracts;
use error::Result;
use semver::Version;
use std::{
collections::{BTreeMap, HashMap, HashSet},
fs,
path::{Path, PathBuf},
sync::Arc,
};
/// Utilities for creating, mocking and testing of (temporary) projects
#[cfg(feature = "project-util")]
pub mod project_util;
#[derive(Debug, Clone)]
pub enum CompilerConfig<C: Compiler> {
Specific(C),
AutoDetect(Arc<dyn CompilerVersionManager<Compiler = C>>),
}
#[cfg(feature = "svm-solc")]
impl Default for CompilerConfig<Solc> {
fn default() -> Self {
CompilerConfig::AutoDetect(Arc::new(compilers::solc::SolcVersionManager))
}
}
/// Represents a project workspace and handles `solc` compiling of all contracts in that workspace.
#[derive(Clone, Debug)]
pub struct Project<C: Compiler = Solc, T: ArtifactOutput = ConfigurableArtifacts> {
pub compiler_config: CompilerConfig<C>,
/// The layout of the project
pub paths: ProjectPathsConfig<C>,
/// The compiler settings
pub settings: C::Settings,
/// Whether caching is enabled
pub cached: bool,
/// Whether to output build information with each solc call.
pub build_info: bool,
/// Whether writing artifacts to disk is enabled
pub no_artifacts: bool,
/// Handles all artifacts related tasks, reading and writing from the artifact dir.
pub artifacts: T,
/// Errors/Warnings which match these error codes are not going to be logged
pub ignored_error_codes: Vec<u64>,
/// Errors/Warnings which match these file paths are not going to be logged
pub ignored_file_paths: Vec<PathBuf>,
/// The minimum severity level that is treated as a compiler error
pub compiler_severity_filter: Severity,
/// Maximum number of `solc` processes to run simultaneously.
solc_jobs: usize,
/// Offline mode, if set, network access (download solc) is disallowed
pub offline: bool,
/// Windows only config value to ensure the all paths use `/` instead of `\\`, same as `solc`
///
/// This is a noop on other platforms
pub slash_paths: bool,
}
impl Project {
/// Convenience function to call `ProjectBuilder::default()`.
///
/// # Examples
///
/// Configure with `ConfigurableArtifacts` artifacts output:
///
/// ```
/// use foundry_compilers::Project;
///
/// let config = Project::builder().build()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// To configure any a project with any `ArtifactOutput` use either:
///
/// ```
/// use foundry_compilers::Project;
///
/// let config = Project::builder().build()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// or use the builder directly:
///
/// ```
/// use foundry_compilers::{ConfigurableArtifacts, ProjectBuilder};
///
/// let config = ProjectBuilder::<ConfigurableArtifacts>::default().build()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn builder() -> ProjectBuilder {
ProjectBuilder::default()
}
}
impl<T: ArtifactOutput, C: Compiler> Project<C, T> {
/// Returns the handler that takes care of processing all artifacts
pub fn artifacts_handler(&self) -> &T {
&self.artifacts
}
}
impl<T: ArtifactOutput> Project<Solc, T> {
/// Returns standard-json-input to compile the target contract
pub fn standard_json_input(
&self,
target: impl AsRef<Path>,
) -> Result<StandardJsonCompilerInput> {
let target = target.as_ref();
trace!("Building standard-json-input for {:?}", target);
let graph = Graph::<SolData>::resolve(&self.paths)?;
let target_index = graph.files().get(target).ok_or_else(|| {
SolcError::msg(format!("cannot resolve file at {:?}", target.display()))
})?;
let mut sources = Vec::new();
let mut unique_paths = HashSet::new();
let (path, source) = graph.node(*target_index).unpack();
unique_paths.insert(path.clone());
sources.push((path, source));
sources.extend(
graph
.all_imported_nodes(*target_index)
.map(|index| graph.node(index).unpack())
.filter(|(p, _)| unique_paths.insert(p.to_path_buf())),
);
let root = self.root();
let sources = sources
.into_iter()
.map(|(path, source)| (rebase_path(root, path), source.clone()))
.collect();
let mut settings = self.settings.clone();
// strip the path to the project root from all remappings
settings.remappings = self
.paths
.remappings
.clone()
.into_iter()
.map(|r| r.into_relative(self.root()).to_relative_remapping())
.collect::<Vec<_>>();
let input = StandardJsonCompilerInput::new(sources, settings);
Ok(input)
}
/// Flattens the target solidity file into a single string suitable for verification.
///
/// This method uses a dependency graph to resolve imported files and substitute
/// import directives with the contents of target files. It will strip the pragma
/// version directives and SDPX license identifiers from all imported files.
///
/// NB: the SDPX license identifier will be removed from the imported file
/// only if it is found at the beginning of the file.
pub fn flatten(&self, target: &Path) -> Result<String> {
self.paths.flatten(target)
}
}
impl<T: ArtifactOutput, C: Compiler> Project<C, T> {
/// Returns the path to the artifacts directory
pub fn artifacts_path(&self) -> &PathBuf {
&self.paths.artifacts
}
/// Returns the path to the sources directory
pub fn sources_path(&self) -> &PathBuf {
&self.paths.sources
}
/// Returns the path to the cache file
pub fn cache_path(&self) -> &PathBuf {
&self.paths.cache
}
/// Returns the path to the `build-info` directory nested in the artifacts dir
pub fn build_info_path(&self) -> &PathBuf {
&self.paths.build_infos
}
/// Returns the root directory of the project
pub fn root(&self) -> &PathBuf {
&self.paths.root
}
/// Convenience function to read the cache file.
/// See also [CompilerCache::read_joined()]
pub fn read_cache_file(&self) -> Result<CompilerCache<C::Settings>> {
CompilerCache::read_joined(&self.paths)
}
/// Sets the maximum number of parallel `solc` processes to run simultaneously.
///
/// # Panics
///
/// if `jobs == 0`
pub fn set_solc_jobs(&mut self, jobs: usize) {
assert!(jobs > 0);
self.solc_jobs = jobs;
}
/// Returns all sources found under the project's configured sources path
#[instrument(skip_all, fields(name = "sources"))]
pub fn sources(&self) -> Result<Sources> {
self.paths.read_sources()
}
/// Emit the cargo [`rerun-if-changed`](https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorerun-if-changedpath) instruction.
///
/// This 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.
///
/// # Examples
///
/// ```no_run
/// use foundry_compilers::{Project, ProjectPathsConfig};
///
/// // Configure the project with all its paths, solc, cache etc.
/// // where the root dir is the current Rust project.
/// let paths = ProjectPathsConfig::hardhat(env!("CARGO_MANIFEST_DIR"))?;
/// let project = Project::builder().paths(paths).build()?;
/// let output = project.compile()?;
///
/// // Tell Cargo to rerun this build script that if a source file changes.
/// project.rerun_if_sources_changed();
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
pub fn rerun_if_sources_changed(&self) {
println!("cargo:rerun-if-changed={}", self.paths.sources.display())
}
pub fn compile(&self) -> Result<ProjectCompileOutput<C::CompilationError, T>> {
project::ProjectCompiler::new(self)?.compile()
}
/// Convenience function to compile a single solidity file with the project's settings.
///
/// # Examples
///
/// ```no_run
/// use foundry_compilers::Project;
///
/// let project = Project::builder().build()?;
/// let output = project.compile_file("example/Greeter.sol")?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn compile_file(
&self,
file: impl Into<PathBuf>,
) -> Result<ProjectCompileOutput<C::CompilationError, T>> {
let file = file.into();
let source = Source::read(&file)?;
project::ProjectCompiler::with_sources(self, Sources::from([(file, source)]))?.compile()
}
/// Convenience function to compile a series of solidity files with the project's settings.
/// Same as [`Self::compile()`] but with the given `files` as input.
///
/// # Examples
///
/// ```no_run
/// use foundry_compilers::Project;
///
/// let project = Project::builder().build()?;
/// let output = project.compile_files(["examples/Foo.sol", "examples/Bar.sol"])?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn compile_files<P, I>(
&self,
files: I,
) -> Result<ProjectCompileOutput<C::CompilationError, T>>
where
I: IntoIterator<Item = P>,
P: Into<PathBuf>,
{
let sources = Source::read_all(files)?;
project::ProjectCompiler::with_sources(self, sources)?.compile()
}
/// Convenience function to compile only files that match the provided [FileFilter].
///
/// Same as [`Self::compile()`] but with only with the input files that match
/// [`FileFilter::is_match()`].
///
/// # Examples
///
/// Only compile test files:
///
/// ```no_run
/// use foundry_compilers::{Project, TestFileFilter};
///
/// let project = Project::builder().build()?;
/// let output = project.compile_sparse(Box::new(TestFileFilter::default()))?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// Apply a custom filter:
///
/// ```no_run
/// use foundry_compilers::Project;
/// use std::path::Path;
///
/// let project = Project::builder().build()?;
/// let output = project.compile_sparse(Box::new(|path: &Path| path.ends_with("Greeter.sol")))?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn compile_sparse(
&self,
filter: Box<dyn SparseOutputFileFilter<C::ParsedSource>>,
) -> Result<ProjectCompileOutput<C::CompilationError, T>> {
let sources =
Source::read_all(self.paths.input_files().into_iter().filter(|p| filter.is_match(p)))?;
project::ProjectCompiler::with_sources(self, sources)?.with_sparse_output(filter).compile()
}
/// Compiles the given source files with the exact [Compiler] instance
///
/// First all libraries for the sources are resolved by scanning all their imports.
/// If caching is enabled for the `Project`, then all unchanged files are filtered from the
/// sources and their existing artifacts are read instead. This will also update the cache
/// file and cleans up entries for files which may have been removed. Unchanged files that
/// for which an artifact exist, are not compiled again.
///
/// # Examples
///
/// ```no_run
/// use foundry_compilers::{Project, Solc};
///
/// let project = Project::builder().build()?;
/// let sources = project.paths.read_sources()?;
/// let solc = Solc::find_svm_installed_version("0.8.11")?.unwrap();
/// project.compile_with_version(&solc, sources)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn compile_with_version(
&self,
compiler: &C,
sources: Sources,
) -> Result<ProjectCompileOutput<C::CompilationError, T>> {
project::ProjectCompiler::with_sources_and_compiler(self, sources, compiler.clone())?
.compile()
}
/// Removes the project's artifacts and cache file
///
/// If the cache file was the only file in the folder, this also removes the empty folder.
///
/// # Examples
///
/// ```
/// use foundry_compilers::Project;
///
/// let project = Project::builder().build()?;
/// let _ = project.compile()?;
/// assert!(project.artifacts_path().exists());
/// assert!(project.cache_path().exists());
///
/// project.cleanup();
/// assert!(!project.artifacts_path().exists());
/// assert!(!project.cache_path().exists());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn cleanup(&self) -> std::result::Result<(), SolcIoError> {
trace!("clean up project");
if self.cache_path().exists() {
std::fs::remove_file(self.cache_path())
.map_err(|err| SolcIoError::new(err, self.cache_path()))?;
if let Some(cache_folder) =
self.cache_path().parent().filter(|cache_folder| self.root() != cache_folder)
{
// remove the cache folder if the cache file was the only file
if cache_folder
.read_dir()
.map_err(|err| SolcIoError::new(err, cache_folder))?
.next()
.is_none()
{
std::fs::remove_dir(cache_folder)
.map_err(|err| SolcIoError::new(err, cache_folder))?;
}
}
trace!("removed cache file \"{}\"", self.cache_path().display());
}
// clean the artifacts dir
if self.artifacts_path().exists() && self.root() != self.artifacts_path() {
std::fs::remove_dir_all(self.artifacts_path())
.map_err(|err| SolcIoError::new(err, self.artifacts_path().clone()))?;
trace!("removed artifacts dir \"{}\"", self.artifacts_path().display());
}
// also clean the build-info dir, in case it's not nested in the artifacts dir
if self.build_info_path().exists() && self.root() != self.build_info_path() {
std::fs::remove_dir_all(self.build_info_path())
.map_err(|err| SolcIoError::new(err, self.build_info_path().clone()))?;
tracing::trace!("removed build-info dir \"{}\"", self.build_info_path().display());
}
Ok(())
}
/// Runs solc compiler without requesting any output and collects a mapping from contract names
/// to source files containing artifact with given name.
fn collect_contract_names_solc(&self) -> Result<HashMap<String, Vec<PathBuf>>>
where
T: Clone,
C: Clone,
{
let mut temp_project = (*self).clone();
temp_project.no_artifacts = true;
*temp_project.settings.output_selection_mut() =
OutputSelection::common_output_selection([]);
let output = temp_project.compile()?;
if output.has_compiler_errors() {
return Err(SolcError::msg(output));
}
let contracts = output.into_artifacts().fold(
HashMap::new(),
|mut contracts: HashMap<_, Vec<_>>, (id, _)| {
contracts.entry(id.name).or_default().push(id.source);
contracts
},
);
Ok(contracts)
}
/// Parses project sources via solang parser, collecting mapping from contract name to source
/// files containing artifact with given name. On parser failure, fallbacks to
/// [Self::collect_contract_names_solc].
fn collect_contract_names(&self) -> Result<HashMap<String, Vec<PathBuf>>>
where
T: Clone,
C: Clone,
{
let graph = Graph::resolve(&self.paths)?;
let mut contracts: HashMap<String, Vec<PathBuf>> = HashMap::new();
for file in graph.files().keys() {
let src = fs::read_to_string(file).map_err(|e| SolcError::io(e, file))?;
let Ok((parsed, _)) = solang_parser::parse(&src, 0) else {
return self.collect_contract_names_solc();
};
for part in parsed.0 {
if let SourceUnitPart::ContractDefinition(contract) = part {
if let Some(name) = contract.name {
contracts.entry(name.name).or_default().push(file.clone());
}
}
}
}
Ok(contracts)
}
/// Finds the path of the contract with the given name.
/// Throws error if multiple or no contracts with the same name are found.
pub fn find_contract_path(&self, target_name: &str) -> Result<PathBuf>
where
T: Clone,
C: Clone,
{
let mut contracts = self.collect_contract_names()?;
if contracts.get(target_name).map_or(true, |paths| paths.is_empty()) {
return Err(SolcError::msg(format!(
"No contract found with the name `{}`",
target_name
)));
}
let mut paths = contracts.remove(target_name).unwrap();
if paths.len() > 1 {
return Err(SolcError::msg(format!(
"Multiple contracts found with the name `{}`",
target_name
)));
}
Ok(paths.remove(0))
}
}
pub struct ProjectBuilder<T: ArtifactOutput = ConfigurableArtifacts, C: Compiler = Solc> {
/// The layout of the
paths: Option<ProjectPathsConfig<C>>,
/// How solc invocation should be configured.
settings: Option<C::Settings>,
/// Whether caching is enabled, default is true.
cached: bool,
/// Whether to output build information with each solc call.
build_info: bool,
/// Whether writing artifacts to disk is enabled, default is true.
no_artifacts: bool,
/// Use offline mode
offline: bool,
/// Whether to slash paths of the `ProjectCompilerOutput`
slash_paths: bool,
/// handles all artifacts related tasks
artifacts: T,
/// Which error codes to ignore
pub ignored_error_codes: Vec<u64>,
/// Which file paths to ignore
pub ignored_file_paths: Vec<PathBuf>,
/// The minimum severity level that is treated as a compiler error
compiler_severity_filter: Severity,
solc_jobs: Option<usize>,
}
impl<T: ArtifactOutput, C: Compiler> ProjectBuilder<T, C> {
/// Create a new builder with the given artifacts handler
pub fn new(artifacts: T) -> Self {
Self {
paths: None,
cached: true,
build_info: false,
no_artifacts: false,
offline: false,
slash_paths: true,
artifacts,
ignored_error_codes: Vec::new(),
ignored_file_paths: Vec::new(),
compiler_severity_filter: Severity::Error,
solc_jobs: None,
settings: None,
}
}
#[must_use]
pub fn paths(mut self, paths: ProjectPathsConfig<C>) -> Self {
self.paths = Some(paths);
self
}
#[must_use]
pub fn settings(mut self, settings: C::Settings) -> Self {
self.settings = Some(settings);
self
}
#[must_use]
pub fn ignore_error_code(mut self, code: u64) -> Self {
self.ignored_error_codes.push(code);
self
}
#[must_use]
pub fn ignore_error_codes(mut self, codes: impl IntoIterator<Item = u64>) -> Self {
for code in codes {
self = self.ignore_error_code(code);
}
self
}
pub fn ignore_paths(mut self, paths: Vec<PathBuf>) -> Self {
self.ignored_file_paths = paths;
self
}
#[must_use]
pub fn set_compiler_severity_filter(mut self, compiler_severity_filter: Severity) -> Self {
self.compiler_severity_filter = compiler_severity_filter;
self
}
/// Disables cached builds
#[must_use]
pub fn ephemeral(self) -> Self {
self.set_cached(false)
}
/// Sets the cache status
#[must_use]
pub fn set_cached(mut self, cached: bool) -> Self {
self.cached = cached;
self
}
/// Sets the build info value
#[must_use]
pub fn set_build_info(mut self, build_info: bool) -> Self {
self.build_info = build_info;
self
}
/// Activates offline mode
///
/// Prevents network possible access to download/check solc installs
#[must_use]
pub fn offline(self) -> Self {
self.set_offline(true)
}
/// Sets the offline status
#[must_use]
pub fn set_offline(mut self, offline: bool) -> Self {
self.offline = offline;
self
}
/// Sets whether to slash all paths on windows
///
/// If set to `true` all `\\` separators are replaced with `/`, same as solc
#[must_use]
pub fn set_slashed_paths(mut self, slashed_paths: bool) -> Self {
self.slash_paths = slashed_paths;
self
}
/// Disables writing artifacts to disk
#[must_use]
pub fn no_artifacts(self) -> Self {
self.set_no_artifacts(true)
}
/// Sets the no artifacts status
#[must_use]
pub fn set_no_artifacts(mut self, artifacts: bool) -> Self {
self.no_artifacts = artifacts;
self
}
/// Sets the maximum number of parallel `solc` processes to run simultaneously.
///
/// # Panics
///
/// `jobs` must be at least 1
#[must_use]
pub fn solc_jobs(mut self, jobs: usize) -> Self {
assert!(jobs > 0);
self.solc_jobs = Some(jobs);
self
}
/// Sets the number of parallel `solc` processes to `1`, no parallelization
#[must_use]
pub fn single_solc_jobs(self) -> Self {
self.solc_jobs(1)
}
/// Set arbitrary `ArtifactOutputHandler`
pub fn artifacts<A: ArtifactOutput>(self, artifacts: A) -> ProjectBuilder<A, C> {
let ProjectBuilder {
paths,
cached,
no_artifacts,
ignored_error_codes,
compiler_severity_filter,
solc_jobs,
offline,
build_info,
slash_paths,
ignored_file_paths,
settings,
..
} = self;
ProjectBuilder {
paths,
cached,
no_artifacts,
offline,
slash_paths,
artifacts,
ignored_error_codes,
ignored_file_paths,
compiler_severity_filter,
solc_jobs,
build_info,
settings,
}
}
pub fn build(self, compiler_config: CompilerConfig<C>) -> Result<Project<C, T>> {
let Self {
paths,
cached,
no_artifacts,
artifacts,
ignored_error_codes,
ignored_file_paths,
compiler_severity_filter,
solc_jobs,
offline,
build_info,
slash_paths,
settings,
} = self;
let mut paths = paths.map(Ok).unwrap_or_else(ProjectPathsConfig::current_hardhat)?;
if slash_paths {
// ensures we always use `/` paths
paths.slash_paths();
}
Ok(Project {
compiler_config,
paths,
cached,
build_info,
no_artifacts,
artifacts,
ignored_error_codes,
ignored_file_paths,
compiler_severity_filter,
solc_jobs: solc_jobs
.or_else(|| std::thread::available_parallelism().ok().map(|n| n.get()))
.unwrap_or(1),
offline,
slash_paths,
settings: settings.unwrap_or_default(),
})
}
}
impl<T: ArtifactOutput + Default> Default for ProjectBuilder<T> {
fn default() -> Self {
Self::new(T::default())
}
}
impl<T: ArtifactOutput, C: Compiler> ArtifactOutput for Project<C, T> {
type Artifact = T::Artifact;
fn on_output<CP>(
&self,
contracts: &VersionedContracts,
sources: &VersionedSourceFiles,
layout: &ProjectPathsConfig<CP>,
ctx: OutputContext<'_>,
) -> Result<Artifacts<Self::Artifact>> {
self.artifacts_handler().on_output(contracts, sources, layout, ctx)
}
fn handle_artifacts(
&self,
contracts: &VersionedContracts,
artifacts: &Artifacts<Self::Artifact>,
) -> Result<()> {
self.artifacts_handler().handle_artifacts(contracts, artifacts)
}
fn output_file_name(name: impl AsRef<str>) -> PathBuf {
T::output_file_name(name)
}
fn output_file_name_versioned(name: impl AsRef<str>, version: &Version) -> PathBuf {
T::output_file_name_versioned(name, version)
}
fn output_file(contract_file: impl AsRef<Path>, name: impl AsRef<str>) -> PathBuf {
T::output_file(contract_file, name)
}
fn output_file_versioned(
contract_file: impl AsRef<Path>,
name: impl AsRef<str>,
version: &Version,
) -> PathBuf {
T::output_file_versioned(contract_file, name, version)
}
fn contract_name(file: impl AsRef<Path>) -> Option<String> {
T::contract_name(file)
}
fn output_exists(
contract_file: impl AsRef<Path>,
name: impl AsRef<str>,
root: impl AsRef<Path>,
) -> bool {
T::output_exists(contract_file, name, root)
}
fn read_cached_artifact(path: impl AsRef<Path>) -> Result<Self::Artifact> {
T::read_cached_artifact(path)
}
fn read_cached_artifacts<P, I>(files: I) -> Result<BTreeMap<PathBuf, Self::Artifact>>
where
I: IntoIterator<Item = P>,
P: Into<PathBuf>,
{
T::read_cached_artifacts(files)
}
fn contract_to_artifact(
&self,
file: &Path,
name: &str,
contract: Contract,
source_file: Option<&SourceFile>,
) -> Self::Artifact {
self.artifacts_handler().contract_to_artifact(file, name, contract, source_file)
}
fn output_to_artifacts<CP>(
&self,
contracts: &VersionedContracts,
sources: &VersionedSourceFiles,
ctx: OutputContext<'_>,
layout: &ProjectPathsConfig<CP>,
) -> Artifacts<Self::Artifact> {
self.artifacts_handler().output_to_artifacts(contracts, sources, ctx, layout)
}
fn standalone_source_file_to_artifact(
&self,
path: &Path,
file: &VersionedSourceFile,
) -> Option<Self::Artifact> {
self.artifacts_handler().standalone_source_file_to_artifact(path, file)
}
fn is_dirty(&self, artifact_file: &ArtifactFile<Self::Artifact>) -> Result<bool> {
self.artifacts_handler().is_dirty(artifact_file)
}
fn handle_cached_artifacts(&self, artifacts: &Artifacts<Self::Artifact>) -> Result<()> {
self.artifacts_handler().handle_cached_artifacts(artifacts)
}
}
// Rebases the given path to the base directory lexically.
//
// For instance, given the base `/home/user/project` and the path `/home/user/project/src/A.sol`,
// this function returns `src/A.sol`.
//
// This function transforms a path into a form that is relative to the base directory. The returned
// path starts either with a normal component (e.g., `src`) or a parent directory component (i.e.,
// `..`). It also converts the path into a UTF-8 string and replaces all separators with forward
// slashes (`/`), if they're not.
//
// The rebasing process can be conceptualized as follows:
//
// 1. Remove the leading components from the path that match those in the base.
// 2. Prepend `..` components to the path, matching the number of remaining components in the base.
//
// # Examples
//
// `rebase_path("/home/user/project", "/home/user/project/src/A.sol")` returns `src/A.sol`. The
// common part, `/home/user/project`, is removed from the path.
//
// `rebase_path("/home/user/project", "/home/user/A.sol")` returns `../A.sol`. First, the common
// part, `/home/user`, is removed, leaving `A.sol`. Next, as `project` remains in the base, `..` is
// prepended to the path.
//
// On Windows, paths like `a\b\c` are converted to `a/b/c`.
//
// For more examples, see the test.
fn rebase_path(base: impl AsRef<Path>, path: impl AsRef<Path>) -> PathBuf {
use path_slash::PathExt;
let mut base_components = base.as_ref().components();
let mut path_components = path.as_ref().components();
let mut new_path = PathBuf::new();
while let Some(path_component) = path_components.next() {
let base_component = base_components.next();
if Some(path_component) != base_component {
if base_component.is_some() {
new_path.extend(
std::iter::repeat(std::path::Component::ParentDir)
.take(base_components.count() + 1),
);
}
new_path.push(path_component);
new_path.extend(path_components);
break;
}
}
new_path.to_slash_lossy().into_owned().into()
}
#[cfg(test)]
#[cfg(feature = "svm-solc")]
mod tests {
use super::*;
use crate::remappings::Remapping;
#[test]
#[cfg_attr(windows, ignore = "<0.7 solc is flaky")]
fn test_build_all_versions() {
let paths = ProjectPathsConfig::builder()
.root("./test-data/test-contract-versions")
.sources("./test-data/test-contract-versions")
.build()
.unwrap();
let project = Project::builder()
.paths(paths)
.no_artifacts()
.ephemeral()
.build(Default::default())
.unwrap();
let contracts = project.compile().unwrap().succeeded().into_output().contracts;
// Contracts A to F
assert_eq!(contracts.contracts().count(), 3);
}
#[test]
fn test_build_many_libs() {
let root = utils::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"))
.remappings(
Remapping::find_many(root.join("lib1"))
.into_iter()
.chain(Remapping::find_many(root.join("lib2"))),
)
.build()
.unwrap();
let project = Project::builder()
.paths(paths)
.no_artifacts()
.ephemeral()
.no_artifacts()
.build(Default::default())
.unwrap();
let contracts = project.compile().unwrap().succeeded().into_output().contracts;
assert_eq!(contracts.contracts().count(), 3);
}
#[test]
fn test_build_remappings() {
let root = utils::canonicalize("./test-data/test-contract-remappings").unwrap();
let paths = ProjectPathsConfig::builder()
.root(&root)
.sources(root.join("src"))
.lib(root.join("lib"))
.remappings(Remapping::find_many(root.join("lib")))
.build()
.unwrap();
let project = Project::builder()
.no_artifacts()
.paths(paths)
.ephemeral()
.build(Default::default())
.unwrap();
let contracts = project.compile().unwrap().succeeded().into_output().contracts;
assert_eq!(contracts.contracts().count(), 2);
}
#[test]
fn can_rebase_path() {
assert_eq!(rebase_path("a/b", "a/b/c"), PathBuf::from("c"));
assert_eq!(rebase_path("a/b", "a/c"), PathBuf::from("../c"));
assert_eq!(rebase_path("a/b", "c"), PathBuf::from("../../c"));
assert_eq!(
rebase_path("/home/user/project", "/home/user/project/A.sol"),
PathBuf::from("A.sol")
);
assert_eq!(
rebase_path("/home/user/project", "/home/user/project/src/A.sol"),
PathBuf::from("src/A.sol")
);
assert_eq!(
rebase_path("/home/user/project", "/home/user/project/lib/forge-std/src/Test.sol"),
PathBuf::from("lib/forge-std/src/Test.sol")
);
assert_eq!(
rebase_path("/home/user/project", "/home/user/A.sol"),
PathBuf::from("../A.sol")
);
assert_eq!(rebase_path("/home/user/project", "/home/A.sol"), PathBuf::from("../../A.sol"));
assert_eq!(rebase_path("/home/user/project", "/A.sol"), PathBuf::from("../../../A.sol"));
assert_eq!(
rebase_path("/home/user/project", "/tmp/A.sol"),
PathBuf::from("../../../tmp/A.sol")
);
assert_eq!(
rebase_path("/Users/ah/temp/verif", "/Users/ah/temp/remapped/Child.sol"),
PathBuf::from("../remapped/Child.sol")
);
assert_eq!(
rebase_path("/Users/ah/temp/verif", "/Users/ah/temp/verif/../remapped/Parent.sol"),
PathBuf::from("../remapped/Parent.sol")
);
}
}