Pass solc version into compiler rather than returning from compilation. Allows for better reporting

This commit is contained in:
James Wilson
2025-08-27 13:13:55 +01:00
parent 8229675ba8
commit 5c75228496
10 changed files with 174 additions and 171 deletions
-4
View File
@@ -1,4 +0,0 @@
use semver::Version;
/// This is the first version of solc that supports the `--via-ir` flag / "viaIR" input JSON.
pub const SOLC_VERSION_SUPPORTING_VIA_YUL_IR: Version = Version::new(0, 8, 13);
+8 -18
View File
@@ -3,7 +3,6 @@
//! - Polkadot revive resolc compiler
//! - Polkadot revive Wasm compiler
mod constants;
mod utils;
use std::{
@@ -15,7 +14,6 @@ use std::{
use alloy::json_abi::JsonAbi;
use alloy_primitives::Address;
use anyhow::Context;
use semver::{Version, VersionReq};
use serde::{Deserialize, Serialize};
use revive_common::EVMVersion;
@@ -25,6 +23,9 @@ use revive_dt_config::Arguments;
// Re-export this as it's a part of the compiler interface.
pub use revive_dt_common::types::{Mode, ModeOptimizerSetting, ModePipeline};
// Expose functionality for instantiating a SolcCompiler.
pub use utils::{SolcCompiler, solc_compiler};
pub mod revive_js;
pub mod revive_resolc;
pub mod solc;
@@ -53,7 +54,7 @@ pub trait SolidityCompiler {
pub struct CompilerInput {
pub pipeline: Option<ModePipeline>,
pub optimization: Option<ModeOptimizerSetting>,
pub solc_version: Option<VersionReq>,
pub solc: Option<SolcCompiler>,
pub evm_version: Option<EVMVersion>,
pub allow_paths: Vec<PathBuf>,
pub base_path: Option<PathBuf>,
@@ -63,24 +64,13 @@ pub struct CompilerInput {
}
/// The generic compilation output configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CompilerOutput {
/// Information about the `solc` compiler used to compile the contracts.
pub solc: SolcCompilerInformation,
/// The compiled contracts. The bytecode of the contract is kept as a string incase linking is
/// required and the compiled source has placeholders.
pub contracts: HashMap<PathBuf, HashMap<String, (String, JsonAbi)>>,
}
/// Information about the `solc` compiler.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SolcCompilerInformation {
/// Version of the compiler.
pub version: Version,
/// Path to the compiler executable.
pub path: PathBuf,
}
/// A generic builder style interface for configuring the supported compiler options.
pub struct Compiler<T: SolidityCompiler> {
input: CompilerInput,
@@ -102,7 +92,7 @@ where
input: CompilerInput {
pipeline: Default::default(),
optimization: Default::default(),
solc_version: Default::default(),
solc: Default::default(),
evm_version: Default::default(),
allow_paths: Default::default(),
base_path: Default::default(),
@@ -114,8 +104,8 @@ where
}
}
pub fn with_solc_version_req(mut self, value: impl Into<Option<VersionReq>>) -> Self {
self.input.solc_version = value.into();
pub fn with_solc(mut self, value: impl Into<Option<SolcCompiler>>) -> Self {
self.input.solc = value.into();
self
}
+8 -42
View File
@@ -1,9 +1,8 @@
//! Implements the [SolidityCompiler] trait with `resolc` for
//! compiling contracts to PolkaVM (PVM) bytecode.
use std::{collections::HashMap, path::PathBuf, process::Stdio};
use std::{path::PathBuf, process::Stdio};
use revive_dt_common::types::VersionOrRequirement;
use revive_dt_config::Arguments;
use revive_solc_json_interface::{
SolcStandardJsonInput, SolcStandardJsonInputLanguage, SolcStandardJsonInputSettings,
@@ -11,12 +10,7 @@ use revive_solc_json_interface::{
SolcStandardJsonOutput,
};
use super::constants::SOLC_VERSION_SUPPORTING_VIA_YUL_IR;
use super::utils;
use crate::{
CompilerInput, CompilerOutput, ModeOptimizerSetting, ModePipeline, SolcCompilerInformation,
SolidityCompiler,
};
use crate::{CompilerInput, CompilerOutput, ModeOptimizerSetting, ModePipeline, SolidityCompiler};
use alloy::json_abi::JsonAbi;
use anyhow::Context;
@@ -26,11 +20,6 @@ use tokio::{io::AsyncWriteExt, process::Command as AsyncCommand};
/// A wrapper around the `resolc` binary, emitting PVM-compatible bytecode.
#[derive(Debug)]
pub struct Resolc {
// Where to cache compiler executables.
compiler_executables_cache_directory: PathBuf,
// We'll use this version when no explicit version
// requirement is given in the test mode.
solc_version: Version,
/// Path to the `resolc` executable
resolc_path: PathBuf,
}
@@ -44,7 +33,7 @@ impl SolidityCompiler for Resolc {
CompilerInput {
pipeline,
optimization,
solc_version,
solc,
evm_version,
allow_paths,
base_path,
@@ -62,22 +51,7 @@ impl SolidityCompiler for Resolc {
);
}
let solc_version_req = solc_version
.unwrap_or_else(|| VersionOrRequirement::version_to_requirement(&self.solc_version));
let solc_path = revive_dt_solc_binaries::download_solc(
&self.compiler_executables_cache_directory,
solc_version_req,
false,
)
.await?;
let solc_version = utils::solc_version(&solc_path).await?;
if solc_version < SOLC_VERSION_SUPPORTING_VIA_YUL_IR {
anyhow::bail!(
"We are trying to run the test with solc version {solc_version}, but require {SOLC_VERSION_SUPPORTING_VIA_YUL_IR} or greater"
);
}
let solc = solc.ok_or_else(|| anyhow::anyhow!("solc compiler not provided to resolc."))?;
let input = SolcStandardJsonInput {
language: SolcStandardJsonInputLanguage::Solidity,
sources: sources
@@ -124,7 +98,7 @@ impl SolidityCompiler for Resolc {
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.arg("--solc")
.arg(&solc_path)
.arg(&solc.path)
.arg("--standard-json");
if let Some(ref base_path) = base_path {
@@ -203,14 +177,14 @@ impl SolidityCompiler for Resolc {
anyhow::bail!("Unexpected error - resolc output doesn't have a contracts section");
};
let mut compiled_contracts = HashMap::<PathBuf, HashMap<_, _>>::new();
let mut compiler_output = CompilerOutput::default();
for (source_path, contracts) in contracts.into_iter() {
let src_for_msg = source_path.clone();
let source_path = PathBuf::from(source_path)
.canonicalize()
.with_context(|| format!("Failed to canonicalize path {src_for_msg}"))?;
let map = compiled_contracts.entry(source_path).or_default();
let map = compiler_output.contracts.entry(source_path).or_default();
for (contract_name, contract_information) in contracts.into_iter() {
let bytecode = contract_information
.evm
@@ -255,19 +229,11 @@ impl SolidityCompiler for Resolc {
}
}
Ok(CompilerOutput {
solc: SolcCompilerInformation {
version: solc_version,
path: solc_path,
},
contracts: compiled_contracts,
})
Ok(compiler_output)
}
fn new(config: &Arguments) -> Self {
Resolc {
compiler_executables_cache_directory: config.directory().to_path_buf(),
solc_version: config.solc.clone(),
resolc_path: config.resolc.clone(),
}
}
+15 -44
View File
@@ -1,18 +1,8 @@
//! Implements the [SolidityCompiler] trait with solc for
//! compiling contracts to EVM bytecode.
use std::{collections::HashMap, path::PathBuf, process::Stdio};
use revive_dt_common::types::VersionOrRequirement;
use revive_dt_config::Arguments;
use super::constants::SOLC_VERSION_SUPPORTING_VIA_YUL_IR;
use super::utils;
use crate::{
CompilerInput, CompilerOutput, ModeOptimizerSetting, ModePipeline, SolcCompilerInformation,
SolidityCompiler,
};
use crate::{CompilerInput, CompilerOutput, ModeOptimizerSetting, ModePipeline, SolidityCompiler};
use anyhow::Context;
use foundry_compilers_artifacts::{
output_selection::{
@@ -21,17 +11,12 @@ use foundry_compilers_artifacts::{
solc::CompilerOutput as SolcOutput,
solc::*,
};
use semver::Version;
use revive_dt_config::Arguments;
use std::process::Stdio;
use tokio::{io::AsyncWriteExt, process::Command as AsyncCommand};
#[derive(Debug)]
pub struct Solc {
// Where to cache artifacts.
cache_directory: PathBuf,
// We'll use this version when no explicit version requirement
// is given in the test mode.
solc_version: Version,
}
pub struct Solc {}
impl SolidityCompiler for Solc {
type Options = ();
@@ -42,7 +27,7 @@ impl SolidityCompiler for Solc {
CompilerInput {
pipeline,
optimization,
solc_version,
solc,
evm_version,
allow_paths,
base_path,
@@ -52,15 +37,9 @@ impl SolidityCompiler for Solc {
}: CompilerInput,
_: Self::Options,
) -> anyhow::Result<CompilerOutput> {
let solc_version_req = solc_version
.unwrap_or_else(|| VersionOrRequirement::version_to_requirement(&self.solc_version));
let solc_path =
revive_dt_solc_binaries::download_solc(&self.cache_directory, solc_version_req, false)
.await?;
let solc_version = utils::solc_version(&solc_path).await?;
let solc = solc.ok_or_else(|| anyhow::anyhow!("solc compiler not provided to resolc."))?;
let compiler_supports_via_ir =
utils::solc_version(&solc_path).await? >= SOLC_VERSION_SUPPORTING_VIA_YUL_IR;
utils::solc_versions_supporting_yul_ir().matches(&solc.version);
// Be careful to entirely omit the viaIR field if the compiler does not support it,
// as it will error if you provide fields it does not know about. Because
@@ -126,7 +105,7 @@ impl SolidityCompiler for Solc {
},
};
let mut command = AsyncCommand::new(&solc_path);
let mut command = AsyncCommand::new(&solc.path);
command
.stdin(Stdio::piped())
.stdout(Stdio::piped())
@@ -147,7 +126,7 @@ impl SolidityCompiler for Solc {
}
let mut child = command
.spawn()
.with_context(|| format!("Failed to spawn solc at {}", solc_path.display()))?;
.with_context(|| format!("Failed to spawn solc at {}", solc.path.display()))?;
let stdin = child.stdin.as_mut().expect("should be piped");
let serialized_input = serde_json::to_vec(&input)
@@ -197,9 +176,10 @@ impl SolidityCompiler for Solc {
"Compiled successfully"
);
let mut compiled_contracts = HashMap::<PathBuf, HashMap<_, _>>::new();
let mut compiler_output = CompilerOutput::default();
for (contract_path, contracts) in parsed.contracts {
let map = compiled_contracts
let map = compiler_output
.contracts
.entry(contract_path.canonicalize().with_context(|| {
format!(
"Failed to canonicalize contract path {}",
@@ -223,20 +203,11 @@ impl SolidityCompiler for Solc {
}
}
Ok(CompilerOutput {
solc: SolcCompilerInformation {
version: solc_version,
path: solc_path,
},
contracts: compiled_contracts,
})
Ok(compiler_output)
}
fn new(config: &Arguments) -> Self {
Self {
cache_directory: config.directory().to_path_buf(),
solc_version: config.solc.clone(),
}
fn new(_config: &Arguments) -> Self {
Self {}
}
fn supports_mode(_optimize_setting: ModeOptimizerSetting, pipeline: ModePipeline) -> bool {
+68 -2
View File
@@ -1,3 +1,4 @@
use serde::{Deserialize, Serialize};
use std::{
path::{Path, PathBuf},
process::{Command, Stdio},
@@ -6,10 +7,61 @@ use std::{
use anyhow::Context;
use dashmap::DashMap;
use semver::Version;
use revive_dt_common::types::{ModePipeline, VersionOrRequirement};
use semver::{Version, VersionReq};
/// Return the path and version of a suitable `solc` compiler given the requirements provided.
///
/// This caches any compiler binaries/paths that are downloaded as a result of calling this.
pub async fn solc_compiler(
cache_directory: &Path,
fallback_version: &Version,
required_version: Option<&VersionReq>,
pipeline: ModePipeline,
) -> anyhow::Result<SolcCompiler> {
// Require Yul compatible solc, or any if we don't care about compiling via Yul.
let mut version_req = if pipeline == ModePipeline::ViaYulIR {
solc_versions_supporting_yul_ir()
} else {
VersionReq::STAR
};
// Take into account the version requirements passed in, too.
if let Some(other_version_req) = required_version {
version_req
.comparators
.extend(other_version_req.comparators.iter().cloned());
}
// If no requirements yet then fall back to the fallback version.
let version_req = if version_req == VersionReq::STAR {
VersionOrRequirement::version_to_requirement(fallback_version)
} else {
version_req
};
// Download (or pull from cache) a suitable solc compiler given this.
let solc_path =
revive_dt_solc_binaries::download_solc(cache_directory, version_req, false).await?;
let solc_version = solc_version(&solc_path).await?;
Ok(SolcCompiler {
version: solc_version,
path: solc_path,
})
}
/// A `solc` compiler, returned from [`solc_compiler`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SolcCompiler {
/// Version of the compiler.
pub version: Version,
/// Path to the compiler executable.
pub path: PathBuf,
}
/// Fetch the solc version given a path to the executable
pub async fn solc_version(solc_path: &Path) -> anyhow::Result<semver::Version> {
async fn solc_version(solc_path: &Path) -> anyhow::Result<semver::Version> {
/// This is a cache of the path of the compiler to the version number of the compiler. We
/// choose to cache the version in this way rather than through a field on the struct since
/// compiler objects are being created all the time from the path and the compiler object is
@@ -49,6 +101,20 @@ pub async fn solc_version(solc_path: &Path) -> anyhow::Result<semver::Version> {
}
}
/// This returns the solc versions which support Yul IR.
pub fn solc_versions_supporting_yul_ir() -> VersionReq {
use semver::{Comparator, Op, Prerelease, VersionReq};
VersionReq {
comparators: vec![Comparator {
op: Op::GreaterEq,
major: 0,
minor: Some(8),
patch: Some(13),
pre: Prerelease::EMPTY,
}],
}
}
#[cfg(test)]
mod test {
use super::*;