TODO: Replace the returned `&'static str` with a copy of `HostType` from core.
(
project_path: &Path,
lint_dir: Option<&Path>,
build_debug: bool,
features: Option<&std::ffi::OsString>,
)
| 11 | |
| 12 | // TODO: Replace the returned `&'static str` with a copy of `HostType` from core. |
| 13 | pub fn build( |
| 14 | project_path: &Path, |
| 15 | lint_dir: Option<&Path>, |
| 16 | build_debug: bool, |
| 17 | features: Option<&std::ffi::OsString>, |
| 18 | ) -> anyhow::Result<(PathBuf, &'static str)> { |
| 19 | let lang = util::detect_module_language(project_path)?; |
| 20 | if features.is_some() && lang != ModuleLanguage::Rust { |
| 21 | anyhow::bail!("The --features option is only supported for Rust modules."); |
| 22 | } |
| 23 | let output_path = match lang { |
| 24 | ModuleLanguage::Rust => build_rust(project_path, features, lint_dir, build_debug), |
| 25 | ModuleLanguage::Csharp => build_csharp(project_path, build_debug), |
| 26 | ModuleLanguage::Javascript => build_javascript(project_path, build_debug), |
| 27 | ModuleLanguage::Cpp => build_cpp(project_path, build_debug), |
| 28 | }?; |
| 29 | |
| 30 | if lang == ModuleLanguage::Javascript { |
| 31 | Ok((output_path, "Js")) |
| 32 | } else if build_debug { |
| 33 | Ok((output_path, "Wasm")) |
| 34 | } else { |
| 35 | // for release builds, optimize wasm modules with wasm-opt |
| 36 | let mut wasm_path = output_path; |
| 37 | eprintln!("Optimising module with wasm-opt..."); |
| 38 | let wasm_path_opt = wasm_path.with_extension("opt.wasm"); |
| 39 | match cmd!("wasm-opt", "-all", "-g", "-O2", &wasm_path, "-o", &wasm_path_opt).run() { |
| 40 | Ok(_) => wasm_path = wasm_path_opt, |
| 41 | // Non-critical error for backward compatibility with users who don't have wasm-opt. |
| 42 | Err(err) => { |
| 43 | if err.kind() == std::io::ErrorKind::NotFound { |
| 44 | eprintln!("Could not find wasm-opt to optimise the module."); |
| 45 | eprintln!( |
| 46 | "For best performance install wasm-opt from https://github.com/WebAssembly/binaryen/releases." |
| 47 | ); |
| 48 | } else { |
| 49 | // If wasm-opt exists but failed for some reason, print the error but continue with unoptimised module. |
| 50 | // This is to reduce disruption in case we produce a module that wasm-opt can't handle like happened before. |
| 51 | eprintln!("Failed to optimise module with wasm-opt: {err}"); |
| 52 | } |
| 53 | eprintln!("Continuing with unoptimised module."); |
| 54 | } |
| 55 | } |
| 56 | Ok((wasm_path, "Wasm")) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | pub mod cpp; |
| 61 | pub mod csharp; |
searching dependent graphs…