spacetimedb_cli/tasks/
mod.rs

1use std::path::{Path, PathBuf};
2
3use crate::util::{self, ModuleLanguage};
4
5use self::csharp::build_csharp;
6use crate::tasks::rust::build_rust;
7
8use duct::cmd;
9
10pub fn build(project_path: &Path, lint_dir: Option<&Path>, build_debug: bool) -> anyhow::Result<PathBuf> {
11    let lang = util::detect_module_language(project_path)?;
12    let mut wasm_path = match lang {
13        ModuleLanguage::Rust => build_rust(project_path, lint_dir, build_debug),
14        ModuleLanguage::Csharp => build_csharp(project_path, build_debug),
15    }?;
16    if !build_debug {
17        eprintln!("Optimising module with wasm-opt...");
18        let wasm_path_opt = wasm_path.with_extension("opt.wasm");
19        match cmd!("wasm-opt", "-all", "-g", "-O2", &wasm_path, "-o", &wasm_path_opt).run() {
20            Ok(_) => wasm_path = wasm_path_opt,
21            // Non-critical error for backward compatibility with users who don't have wasm-opt.
22            Err(err) => {
23                if err.kind() == std::io::ErrorKind::NotFound {
24                    eprintln!("Could not find wasm-opt to optimise the module.");
25                    eprintln!(
26                        "For best performance install wasm-opt from https://github.com/WebAssembly/binaryen/releases."
27                    );
28                } else {
29                    // If wasm-opt exists but failed for some reason, print the error but continue with unoptimised module.
30                    // This is to reduce disruption in case we produce a module that wasm-opt can't handle like happened before.
31                    eprintln!("Failed to optimise module with wasm-opt: {err}");
32                }
33                eprintln!("Continuing with unoptimised module.");
34            }
35        }
36    }
37    Ok(wasm_path)
38}
39
40pub mod csharp;
41pub mod rust;