1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use crate::store::StoreOptions;
use crate::warning;
use anyhow::{Context, Result};
use clap::Parser;
use std::path::{Path, PathBuf};
use wasmer_compiler::{ArtifactBuild, ArtifactCreate, ModuleEnvironment};
use wasmer_types::entity::PrimaryMap;
use wasmer_types::{
Architecture, CompileError, CpuFeature, MemoryIndex, MemoryStyle, TableIndex, TableStyle,
Target, Triple,
};
#[derive(Debug, Parser)]
pub struct Compile {
#[clap(name = "FILE", parse(from_os_str))]
path: PathBuf,
#[clap(name = "OUTPUT PATH", short = 'o', parse(from_os_str))]
output: PathBuf,
#[clap(long = "target")]
target_triple: Option<Triple>,
#[clap(flatten)]
store: StoreOptions,
#[clap(short = 'm')]
cpu_features: Vec<CpuFeature>,
}
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 (engine_builder, compiler_type) = self.store.get_engine_for_target(target.clone())?;
let engine = engine_builder.engine();
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)
}
}
let tunables = self.store.get_tunables_for_target(&target)?;
println!("Compiler: {}", compiler_type.to_string());
println!("Target: {}", target.triple());
let path: &Path = self.path.as_ref();
let wasm_bytes = std::fs::read(path)?;
let environ = ModuleEnvironment::new();
let translation = environ.translate(&wasm_bytes).map_err(CompileError::Wasm)?;
let module = translation.module;
let memory_styles: PrimaryMap<MemoryIndex, MemoryStyle> = module
.memories
.values()
.map(|memory_type| tunables.memory_style(memory_type))
.collect();
let table_styles: PrimaryMap<TableIndex, TableStyle> = module
.tables
.values()
.map(|table_type| tunables.table_style(table_type))
.collect();
let artifact = ArtifactBuild::new(
&mut engine.inner_mut(),
&wasm_bytes,
&target,
memory_styles,
table_styles,
)?;
artifact.serialize_to_file(self.output.as_ref())?;
eprintln!(
"✔ File compiled successfully to `{}`.",
self.output.display(),
);
Ok(())
}
}