the compiler interface

Signed-off-by: xermicus <bigcyrill@hotmail.com>
This commit is contained in:
xermicus
2025-03-20 23:26:39 +01:00
parent d6c2535853
commit 6a0d705371
11 changed files with 251 additions and 1 deletions
+113
View File
@@ -0,0 +1,113 @@
//! This crate provides compiler helpers for all supported Solidity targets:
//! - Ethereum solc compiler
//! - Polkadot revive compiler
use std::{fs::read_to_string, path::Path};
use revive_common::EVMVersion;
use revive_solc_json_interface::{
SolcStandardJsonInput, SolcStandardJsonInputLanguage, SolcStandardJsonInputSettings,
SolcStandardJsonInputSettingsOptimizer, SolcStandardJsonInputSettingsSelection,
SolcStandardJsonOutput,
};
use semver::Version;
pub mod revive_js;
pub mod revive_resolc;
pub mod solc;
/// A common interface for all supported Solidity compilers.
pub trait SolidityCompiler {
/// Extra options specific to the compiler.
type Options: Sized;
fn build(
&self,
input: &SolcStandardJsonInput,
extra_options: &Option<Self::Options>,
) -> anyhow::Result<SolcStandardJsonOutput>;
fn new(solc_version: &Version) -> Self;
}
/// A generic builder style interface for configuring all compiler options.
pub struct Compiler<T: SolidityCompiler> {
input: SolcStandardJsonInput,
extra_options: Option<T::Options>,
solc_version: Version,
allow_paths: Vec<String>,
base_path: Option<String>,
}
impl<T> Compiler<T>
where
T: SolidityCompiler,
{
pub fn new(solc_version: Version) -> Self {
Self {
input: SolcStandardJsonInput {
language: SolcStandardJsonInputLanguage::Solidity,
sources: Default::default(),
settings: SolcStandardJsonInputSettings::new(
None,
Default::default(),
None,
SolcStandardJsonInputSettingsSelection::new_required(),
SolcStandardJsonInputSettingsOptimizer::new(
false,
None,
&Version::new(0, 0, 0),
false,
),
None,
),
},
extra_options: None,
solc_version,
allow_paths: Default::default(),
base_path: None,
}
}
pub fn solc_optimizer(mut self, enabled: bool) -> Self {
self.input.settings.optimizer.enabled = enabled;
self
}
pub fn with_source(mut self, path: &Path) -> anyhow::Result<Self> {
self.input
.sources
.insert(path.display().to_string(), read_to_string(path)?.into());
Ok(self)
}
pub fn evm_version(mut self, evm_version: EVMVersion) -> Self {
self.input.settings.evm_version = Some(evm_version);
self
}
pub fn solc_version(mut self, solc_version: Version) -> Self {
self.solc_version = solc_version;
self
}
pub fn extra_options(mut self, extra_options: T::Options) -> Self {
self.extra_options = Some(extra_options);
self
}
pub fn allow_path(mut self, path: String) -> Self {
self.allow_paths.push(path);
self
}
pub fn base_path(mut self, base_path: String) -> Self {
self.base_path = Some(base_path);
self
}
pub fn try_build(&self) -> anyhow::Result<SolcStandardJsonOutput> {
let compiler = T::new(&self.solc_version);
compiler.build(&self.input, &self.extra_options)
}
}
+2
View File
@@ -0,0 +1,2 @@
//! Implements the [SolidityCompiler] trait with revive Wasm for
//! compiling contracts to PVM bytecode (via Wasm).
+2
View File
@@ -0,0 +1,2 @@
//! Implements the [SolidityCompiler] trait with resolc for
//! compiling contracts to PVM bytecode.
+2
View File
@@ -0,0 +1,2 @@
//! Implements the [SolidityCompiler] trait with solc for
//! compiling contracts to EVM bytecode.