| 45 | } |
| 46 | |
| 47 | fn compile(&self, runner: &Runner, compile: &crate::Compile) -> anyhow::Result<()> { |
| 48 | let config = compile.component.deserialize_lang_config::<LangConfig>()?; |
| 49 | // Copy the file to the bindings directory |
| 50 | if !config.path.is_empty() { |
| 51 | let src_path = &compile.component.path; |
| 52 | let dest_path = compile.bindings_dir.join(&config.path); |
| 53 | std::fs::copy(src_path, dest_path)?; |
| 54 | |
| 55 | // Write the moon.pkg.json if provided |
| 56 | if let Some(pkg_config) = config.pkg_config { |
| 57 | let dest_path = compile |
| 58 | .bindings_dir |
| 59 | .join(&config.path) |
| 60 | .parent() |
| 61 | .unwrap() |
| 62 | .join("moon.pkg.json"); |
| 63 | std::fs::write(dest_path, pkg_config)?; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Compile the MoonBit bindings to a wasm file |
| 68 | let mut cmd = Command::new("moon"); |
| 69 | cmd.arg("build") |
| 70 | .arg("--target") |
| 71 | .arg("wasm") |
| 72 | .arg("--release") |
| 73 | .arg("--no-strip") // for debugging |
| 74 | .current_dir(&compile.bindings_dir); |
| 75 | runner.run_command(&mut cmd)?; |
| 76 | // Build the component. MoonBit toolchains may use either `_build` or |
| 77 | // `target` output roots depending on version/configuration. |
| 78 | let artifact_candidates = [ |
| 79 | compile |
| 80 | .bindings_dir |
| 81 | .join("_build/wasm/release/build/gen/gen.wasm"), |
| 82 | compile |
| 83 | .bindings_dir |
| 84 | .join("target/wasm/release/build/gen/gen.wasm"), |
| 85 | compile |
| 86 | .bindings_dir |
| 87 | .join("_build/wasm/debug/build/gen/gen.wasm"), |
| 88 | compile |
| 89 | .bindings_dir |
| 90 | .join("target/wasm/debug/build/gen/gen.wasm"), |
| 91 | ]; |
| 92 | let artifact = artifact_candidates |
| 93 | .iter() |
| 94 | .find(|path| path.exists()) |
| 95 | .cloned() |
| 96 | .with_context(|| { |
| 97 | format!("failed to locate MoonBit output wasm, looked in: {artifact_candidates:?}",) |
| 98 | })?; |
| 99 | // Embed WIT files |
| 100 | let manifest_dir = compile.component.path.parent().unwrap(); |
| 101 | let embedded = artifact.with_extension("embedded.wasm"); |
| 102 | let mut cmd = Command::new("wasm-tools"); |
| 103 | cmd.arg("component") |
| 104 | .arg("embed") |