use std::path::PathBuf;
use anyhow::{Context, Result};
use clap::Parser;
use wasmer::*;
use crate::{common::HashAlgorithm, store::StoreOptions, warning};
#[derive(Debug, Parser)]
pub struct Compile {
#[clap(name = "FILE")]
path: PathBuf,
#[clap(name = "OUTPUT PATH", short = 'o')]
output: PathBuf,
#[clap(long = "target")]
target_triple: Option<Triple>,
#[clap(flatten)]
store: StoreOptions,
#[clap(short = 'm')]
cpu_features: Vec<CpuFeature>,
#[clap(long, value_enum)]
hash_algorithm: Option<HashAlgorithm>,
}
impl Compile {
pub fn execute(&self) -> Result<()> {
self.inner_execute()
.context(format!("failed to compile `{}`", self.path.display()))
}
fn inner_execute(&self) -> Result<()> {
let target = self
.target_triple
.as_ref()
.map(|target_triple| {
let mut features = self
.cpu_features
.clone()
.into_iter()
.fold(CpuFeature::set(), |a, b| a | b);
if target_triple.architecture == Architecture::X86_64 {
features |= CpuFeature::SSE2;
}
Target::new(target_triple.clone(), features)
})
.unwrap_or_default();
let (mut store, compiler_type) = self.store.get_store_for_target(target.clone())?;
let engine = store.engine_mut();
let hash_algorithm = self.hash_algorithm.unwrap_or_default().into();
engine.set_hash_algorithm(Some(hash_algorithm));
let output_filename = self
.output
.file_stem()
.map(|osstr| osstr.to_string_lossy().to_string())
.unwrap_or_default();
let recommended_extension = "wasmu";
match self.output.extension() {
Some(ext) => {
if ext != recommended_extension {
warning!("the output file has a wrong extension. We recommend using `{}.{}` for the chosen target", &output_filename, &recommended_extension)
}
}
None => {
warning!("the output file has no extension. We recommend using `{}.{}` for the chosen target", &output_filename, &recommended_extension)
}
}
println!("Compiler: {compiler_type}");
println!("Target: {}", target.triple());
let module = Module::from_file(&store, &self.path)?;
module.serialize_to_file(&self.output)?;
eprintln!(
"✔ File compiled successfully to `{}`.",
self.output.display(),
);
Ok(())
}
}