Return solc_version in CompilerOutput

This commit is contained in:
James Wilson
2025-08-27 10:45:22 +01:00
parent 4af9f6695d
commit dd94b12b34
8 changed files with 131 additions and 178 deletions
+13 -2
View File
@@ -14,8 +14,8 @@ use std::{
use alloy::json_abi::JsonAbi;
use alloy_primitives::Address;
use semver::VersionReq;
use anyhow::Context;
use semver::{Version, VersionReq};
use serde::{Deserialize, Serialize};
use revive_common::EVMVersion;
@@ -63,13 +63,24 @@ pub struct CompilerInput {
}
/// The generic compilation output configuration.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, 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,
+19 -13
View File
@@ -1,7 +1,7 @@
//! Implements the [SolidityCompiler] trait with `resolc` for
//! compiling contracts to PolkaVM (PVM) bytecode.
use std::{path::PathBuf, process::Stdio};
use std::{collections::HashMap, path::PathBuf, process::Stdio};
use revive_dt_common::types::VersionOrRequirement;
use revive_dt_config::Arguments;
@@ -13,7 +13,10 @@ use revive_solc_json_interface::{
use super::constants::SOLC_VERSION_SUPPORTING_VIA_YUL_IR;
use super::utils;
use crate::{CompilerInput, CompilerOutput, ModeOptimizerSetting, ModePipeline, SolidityCompiler};
use crate::{
CompilerInput, CompilerOutput, ModeOptimizerSetting, ModePipeline, SolcCompilerInformation,
SolidityCompiler,
};
use alloy::json_abi::JsonAbi;
use anyhow::Context;
@@ -23,10 +26,8 @@ use tokio::{io::AsyncWriteExt, process::Command as AsyncCommand};
/// A wrapper around the `resolc` binary, emitting PVM-compatible bytecode.
#[derive(Debug)]
pub struct Resolc {
// Enable wasm compilation.
wasm: bool,
// Where to cache artifacts.
cache_directory: PathBuf,
// 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,
@@ -64,9 +65,9 @@ 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.cache_directory,
&self.compiler_executables_cache_directory,
solc_version_req,
self.wasm,
false,
)
.await?;
let solc_version = utils::solc_version(&solc_path).await?;
@@ -202,14 +203,14 @@ impl SolidityCompiler for Resolc {
anyhow::bail!("Unexpected error - resolc output doesn't have a contracts section");
};
let mut compiler_output = CompilerOutput::default();
let mut compiled_contracts = HashMap::<PathBuf, HashMap<_, _>>::new();
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 = compiler_output.contracts.entry(source_path).or_default();
let map = compiled_contracts.entry(source_path).or_default();
for (contract_name, contract_information) in contracts.into_iter() {
let bytecode = contract_information
.evm
@@ -254,13 +255,18 @@ impl SolidityCompiler for Resolc {
}
}
Ok(compiler_output)
Ok(CompilerOutput {
solc: SolcCompilerInformation {
version: solc_version,
path: solc_path,
},
contracts: compiled_contracts,
})
}
fn new(config: &Arguments) -> Self {
Resolc {
wasm: config.wasm,
cache_directory: config.directory().to_path_buf(),
compiler_executables_cache_directory: config.directory().to_path_buf(),
solc_version: config.solc.clone(),
resolc_path: config.resolc.clone(),
}
+18 -11
View File
@@ -1,14 +1,17 @@
//! Implements the [SolidityCompiler] trait with solc for
//! compiling contracts to EVM bytecode.
use std::{path::PathBuf, process::Stdio};
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, SolidityCompiler};
use crate::{
CompilerInput, CompilerOutput, ModeOptimizerSetting, ModePipeline, SolcCompilerInformation,
SolidityCompiler,
};
use anyhow::Context;
use foundry_compilers_artifacts::{
@@ -23,8 +26,6 @@ use tokio::{io::AsyncWriteExt, process::Command as AsyncCommand};
#[derive(Debug)]
pub struct Solc {
// Enable wasm compilation.
wasm: bool,
// Where to cache artifacts.
cache_directory: PathBuf,
// We'll use this version when no explicit version requirement
@@ -51,11 +52,13 @@ impl SolidityCompiler for Solc {
}: CompilerInput,
_: Self::Options,
) -> anyhow::Result<CompilerOutput> {
let solc_version = solc_version
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, self.wasm)
revive_dt_solc_binaries::download_solc(&self.cache_directory, solc_version_req, false)
.await?;
let solc_version = utils::solc_version(&solc_path).await?;
let compiler_supports_via_ir =
utils::solc_version(&solc_path).await? >= SOLC_VERSION_SUPPORTING_VIA_YUL_IR;
@@ -194,10 +197,9 @@ impl SolidityCompiler for Solc {
"Compiled successfully"
);
let mut compiler_output = CompilerOutput::default();
let mut compiled_contracts = HashMap::<PathBuf, HashMap<_, _>>::new();
for (contract_path, contracts) in parsed.contracts {
let map = compiler_output
.contracts
let map = compiled_contracts
.entry(contract_path.canonicalize().with_context(|| {
format!(
"Failed to canonicalize contract path {}",
@@ -221,12 +223,17 @@ impl SolidityCompiler for Solc {
}
}
Ok(compiler_output)
Ok(CompilerOutput {
solc: SolcCompilerInformation {
version: solc_version,
path: solc_path,
},
contracts: compiled_contracts,
})
}
fn new(config: &Arguments) -> Self {
Self {
wasm: config.wasm,
cache_directory: config.directory().to_path_buf(),
solc_version: config.solc.clone(),
}