1 use std::env;
2 use std::path::Path;
3 use std::process::Command;
4 use std::process::exit;
5 
6 #[allow(dead_code)]
build_c_plugin()7 fn build_c_plugin() {
8     // Run wit-bindgen
9     let mut status = Command::new("wit-bindgen")
10         .args(["c", "../wit/calculator.wit"])
11         .current_dir("c-plugin")
12         .status()
13         .expect("wit-bindgen c ../wit/calculator.wit failed (cwd = c-plugin)");
14     if !status.success() {
15         println!(
16             "wit-bindgen c ../wit/calculator.wit failed (cwd = c-plugin): status {}",
17             status
18         );
19         exit(1);
20     }
21 
22     // Check that wasi-sdk is installed
23     let wasi_sdk_path = env::var("WASI_SDK_PATH");
24     if wasi_sdk_path.is_err() {
25         println!("You must set WASI_SDK_PATH to the location where you installed the WASI SDK");
26         exit(1);
27     }
28     // Compile the plugin
29     let clang_path = Path::new(&wasi_sdk_path.unwrap()).join("bin/wasm32-wasip2-clang");
30     status = Command::new(&clang_path)
31         .args([
32             "-o",
33             "add.wasm",
34             "-mexec-model=reactor",
35             "add.c",
36             "plugin.c",
37             "plugin_component_type.o",
38         ])
39         .current_dir("c-plugin")
40         .status()
41         .unwrap_or_else(|_| {
42             panic!(
43                 "{} failed to compile the plugin (cwd = c-plugin)",
44                 clang_path.display()
45             )
46         });
47     if !status.success() {
48         println!(
49             "{} failed to compile the plugin (cwd = c-plugin): status {}",
50             clang_path.display(),
51             status
52         );
53         exit(1);
54     }
55 }
56 
57 #[allow(dead_code)]
build_js_plugin()58 fn build_js_plugin() {
59     Command::new("jco")
60         .args([
61             "componentize",
62             "--wit",
63             "../wit/calculator.wit",
64             "--world-name",
65             "plugin",
66             "--out",
67             "subtract.wasm",
68             "--disable=all",
69             "subtract.js",
70         ])
71         .current_dir("js-plugin")
72         .status()
73         .expect("jco componentize failed (is jco installed?) (cwd = js-plugin)");
74 }
75 
main()76 fn main() {
77     #[cfg(feature = "build-plugins")]
78     build_c_plugin();
79     #[cfg(feature = "build-plugins")]
80     build_js_plugin();
81 }
82