|
1 | | -//! Implements the [crate::SolidityCompiler] trait with resolc for |
2 | | -//! compiling contracts to PVM bytecode. |
| 1 | +//! Implements the [SolidityCompiler] trait with `resolc` for |
| 2 | +//! compiling contracts to PolkaVM (PVM) bytecode. |
| 3 | +
|
| 4 | +use std::{ |
| 5 | + path::PathBuf, |
| 6 | + process::{Command, Stdio}, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::{CompilerInput, CompilerOutput, SolidityCompiler}; |
| 10 | +use revive_dt_config::Arguments; |
| 11 | +use revive_solc_json_interface::SolcStandardJsonOutput; |
| 12 | + |
| 13 | +/// A wrapper around the `resolc` binary, emitting PVM-compatible bytecode. |
| 14 | +pub struct Resolc { |
| 15 | + /// Path to the `resolc` executable |
| 16 | + resolc_path: PathBuf, |
| 17 | +} |
| 18 | + |
| 19 | +impl SolidityCompiler for Resolc { |
| 20 | + type Options = Vec<String>; |
| 21 | + |
| 22 | + fn build( |
| 23 | + &self, |
| 24 | + input: CompilerInput<Self::Options>, |
| 25 | + ) -> anyhow::Result<CompilerOutput<Self::Options>> { |
| 26 | + let mut child = Command::new(&self.resolc_path) |
| 27 | + .arg("--standard-json") |
| 28 | + .args(&input.extra_options) |
| 29 | + .stdin(Stdio::piped()) |
| 30 | + .stdout(Stdio::piped()) |
| 31 | + .stderr(Stdio::piped()) |
| 32 | + .spawn()?; |
| 33 | + |
| 34 | + let stdin_pipe = child.stdin.as_mut().expect("stdin must be piped"); |
| 35 | + serde_json::to_writer(stdin_pipe, &input.input)?; |
| 36 | + |
| 37 | + let json_in = serde_json::to_string_pretty(&input.input)?; |
| 38 | + |
| 39 | + let output = child.wait_with_output()?; |
| 40 | + let stdout = output.stdout; |
| 41 | + let stderr = output.stderr; |
| 42 | + |
| 43 | + if !output.status.success() { |
| 44 | + log::error!( |
| 45 | + "resolc failed exit={} stderr={} JSON-in={} ", |
| 46 | + output.status, |
| 47 | + String::from_utf8_lossy(&stderr), |
| 48 | + json_in, |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + let parsed: SolcStandardJsonOutput = serde_json::from_slice(&stdout).map_err(|e| { |
| 53 | + anyhow::anyhow!( |
| 54 | + "failed to parse resolc JSON output: {e}\nstderr: {}", |
| 55 | + String::from_utf8_lossy(&stderr) |
| 56 | + ) |
| 57 | + })?; |
| 58 | + |
| 59 | + Ok(CompilerOutput { |
| 60 | + input, |
| 61 | + output: parsed, |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + fn new(resolc_path: PathBuf) -> Self { |
| 66 | + Resolc { resolc_path } |
| 67 | + } |
| 68 | + |
| 69 | + fn get_compiler_executable( |
| 70 | + config: &Arguments, |
| 71 | + _version: semver::Version, |
| 72 | + ) -> anyhow::Result<PathBuf> { |
| 73 | + if !config.resolc.as_os_str().is_empty() { |
| 74 | + return Ok(config.resolc.clone()); |
| 75 | + } |
| 76 | + |
| 77 | + Ok(PathBuf::from("resolc")) |
| 78 | + } |
| 79 | +} |
0 commit comments