use std::path::Path;
use anyhow::{bail, Context, Result};
use crate::proving_key::recursive::ensure_pil2com_exec;
pub struct CompilePilOptions {
pub pil_path: String,
pub output_path: String,
pub include_paths: Vec<String>,
pub fixed_dir: Option<String>,
pub fixed_to_file: bool,
pub no_proto_fixed_data: bool,
}
pub fn run_compile_pil(opts: &CompilePilOptions) -> Result<()> {
tracing::info!("Compiling PIL: {}", opts.pil_path);
let pil2com_exec = ensure_pil2com_exec().context(
"Cannot find pil2com and automatic `npm install` did not produce it. Install Node.js/npm \
and run `npm install` in the pil2-proofman repo root",
)?;
tracing::info!("Using pil2com: {}", pil2com_exec);
if opts.include_paths.is_empty() {
bail!("at least one include path (-I) is required");
}
let include_arg = opts.include_paths.join(",");
let mut cmd = std::process::Command::new(&pil2com_exec);
cmd.arg(&opts.pil_path).arg("-I").arg(&include_arg).arg("-o").arg(&opts.output_path);
if let Some(fixed_dir) = &opts.fixed_dir {
cmd.arg("-u").arg(fixed_dir);
}
if opts.fixed_to_file {
cmd.arg("-O").arg("fixed-to-file");
}
if opts.no_proto_fixed_data {
cmd.arg("-O").arg("no-proto-fixed-data");
}
if std::env::var_os("NODE_OPTIONS").is_none() {
cmd.env("NODE_OPTIONS", "--max-old-space-size=65536");
}
let status = cmd
.stdout(std::process::Stdio::inherit())
.stderr(std::process::Stdio::inherit())
.status()
.with_context(|| format!("Failed to execute pil2com: {}", pil2com_exec))?;
if !status.success() {
bail!("pil2com exited with status {} for {}", status.code().unwrap_or(-1), opts.pil_path);
}
if !Path::new(&opts.output_path).exists() {
bail!("pil2com did not produce output file: {}", opts.output_path);
}
Ok(())
}