-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmod.rs
More file actions
60 lines (54 loc) · 2.42 KB
/
mod.rs
File metadata and controls
60 lines (54 loc) · 2.42 KB
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
use std::path::{Path, PathBuf};
use crate::util::{self, ModuleLanguage};
use self::csharp::build_csharp;
use self::javascript::build_javascript;
use self::rust::build_rust;
use duct::cmd;
// TODO: Replace the returned `&'static str` with a copy of `HostType` from core.
pub fn build(
project_path: &Path,
lint_dir: Option<&Path>,
build_debug: bool,
features: Option<&std::ffi::OsString>,
) -> anyhow::Result<(PathBuf, &'static str)> {
let lang = util::detect_module_language(project_path)?;
if features.is_some() && lang != ModuleLanguage::Rust {
anyhow::bail!("The --features option is only supported for Rust modules.");
}
let output_path = match lang {
ModuleLanguage::Rust => build_rust(project_path, features, lint_dir, build_debug),
ModuleLanguage::Csharp => build_csharp(project_path, build_debug),
ModuleLanguage::Javascript => build_javascript(project_path, build_debug),
}?;
if lang == ModuleLanguage::Javascript {
Ok((output_path, "Js"))
} else if !build_debug {
Ok((output_path, "Wasm"))
} else {
// for release builds, optimize wasm modules with wasm-opt
let mut wasm_path = output_path;
eprintln!("Optimising module with wasm-opt...");
let wasm_path_opt = wasm_path.with_extension("opt.wasm");
match cmd!("wasm-opt", "-all", "-g", "-O2", &wasm_path, "-o", &wasm_path_opt).run() {
Ok(_) => wasm_path = wasm_path_opt,
// Non-critical error for backward compatibility with users who don't have wasm-opt.
Err(err) => {
if err.kind() == std::io::ErrorKind::NotFound {
eprintln!("Could not find wasm-opt to optimise the module.");
eprintln!(
"For best performance install wasm-opt from https://github.com/WebAssembly/binaryen/releases."
);
} else {
// If wasm-opt exists but failed for some reason, print the error but continue with unoptimised module.
// This is to reduce disruption in case we produce a module that wasm-opt can't handle like happened before.
eprintln!("Failed to optimise module with wasm-opt: {err}");
}
eprintln!("Continuing with unoptimised module.");
}
}
Ok((wasm_path, "Wasm"))
}
}
pub mod csharp;
pub mod javascript;
pub mod rust;