Make native version to compaile again

This commit is contained in:
Sebastian Miasojed
2024-11-07 15:04:35 +01:00
parent 9b23e19479
commit 4f6debcbe3
17 changed files with 119 additions and 373 deletions
+1 -1
View File
@@ -11,6 +11,6 @@ use self::input::Input;
use self::output::Output;
pub trait Process {
fn run() -> anyhow::Result<()>;
fn run(input_file: Option<&mut std::fs::File>) -> anyhow::Result<()>;
fn call(input: Input) -> anyhow::Result<Output>;
}
+40 -19
View File
@@ -18,26 +18,36 @@ pub struct NativeProcess;
impl Process for NativeProcess {
/// Read input from `stdin`, compile a contract, and write the output to `stdout`.
fn run() -> anyhow::Result<()> {
fn run(input_file: Option<&mut std::fs::File>) -> anyhow::Result<()> {
let mut stdin = std::io::stdin();
let mut stdout = std::io::stdout();
let mut stderr = std::io::stderr();
let mut buffer = Vec::with_capacity(16384);
stdin.read_to_end(&mut buffer).expect("Stdin reading error");
let input: Input = revive_common::deserialize_from_slice(buffer.as_slice())?;
if input.enable_test_encoding {
todo!()
match input_file {
Some(ins) => {
if let Err(error) = ins.read_to_end(&mut buffer) {
anyhow::bail!("Failed to read recursive process input file: {:?}", error);
}
}
None => {
if let Err(error) = stdin.read_to_end(&mut buffer) {
anyhow::bail!(
"Failed to read recursive process input from stdin: {:?}",
error
)
}
}
}
let input: Input = revive_common::deserialize_from_slice(buffer.as_slice())?;
let result = input.contract.compile(
input.project,
input.optimizer_settings,
input.is_system_mode,
input.include_metadata_hash,
input.debug_config,
);
match result {
Ok(build) => {
let output = Output::new(build);
@@ -56,16 +66,16 @@ impl Process for NativeProcess {
}
}
}
/// Runs this process recursively to compile a single contract.
fn call(input: Input) -> anyhow::Result<Output> {
let input_json = serde_json::to_vec(&input).expect("Always valid");
let executable = match EXECUTABLE.get() {
Some(executable) => executable.to_owned(),
None => std::env::current_exe()?,
};
let mut command = Command::new(executable.as_path());
command.stdin(std::process::Stdio::piped());
command.stdout(std::process::Stdio::piped());
@@ -74,15 +84,26 @@ impl Process for NativeProcess {
let process = command.spawn().map_err(|error| {
anyhow::anyhow!("{:?} subprocess spawning error: {:?}", executable, error)
})?;
#[cfg(debug_assertions)]
if let Some(dbg_config) = &input.debug_config {
dbg_config
.dump_stage_output(&input.contract.path, Some("stage"), &input_json)
.map_err(|error| {
anyhow::anyhow!(
"{:?} failed to log the recursive process output: {:?}",
executable,
error,
)
})?;
}
process
.stdin
.as_ref()
.ok_or_else(|| anyhow::anyhow!("{:?} stdin getting error", executable))?
.write_all(input_json.as_slice())
.map_err(|error| {
anyhow::anyhow!("{:?} stdin writing error: {:?}", executable, error)
})?;
.map_err(|error| anyhow::anyhow!("{:?} stdin writing error: {:?}", executable, error))?;
let output = process.wait_with_output().map_err(|error| {
anyhow::anyhow!("{:?} subprocess output error: {:?}", executable, error)
})?;
@@ -92,9 +113,9 @@ impl Process for NativeProcess {
String::from_utf8_lossy(output.stderr.as_slice()).to_string(),
);
}
let output: Output = revive_common::deserialize_from_slice(output.stdout.as_slice())
.map_err(|error| {
let output: Output =
revive_common::deserialize_from_slice(output.stdout.as_slice()).map_err(|error| {
anyhow::anyhow!(
"{:?} subprocess output parsing error: {}",
executable,