This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
build.rs
110 lines (91 loc) · 2.87 KB
/
build.rs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::process::{self, Command};
const TESTS_DIR: &str = "tests";
const RUST_EXAMPLE: &str = "rust";
const AS_EXAMPLE: &str = "as";
const RUST_GUEST_RAW: &str = "crates/wasi-experimental-http/src/raw.rs";
const AS_GUEST_RAW: &str = "crates/as/raw.ts";
const MD_GUEST_API: &str = "witx/readme.md";
const WITX_CODEGEN_VERSION: &str = "0.11.0";
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=tests/rust/src/lib.rs");
println!("cargo:rerun-if-changed=crates/wasi-experimental-http/src/lib.rs");
println!("cargo:rerun-if-changed=tests/as/index.ts");
println!("cargo:rerun-if-changed=crates/as/index.ts");
println!("cargo:rerun-if-changed=witx/wasi_experimental_http.witx");
generate_from_witx("rust", RUST_GUEST_RAW);
generate_from_witx("assemblyscript", AS_GUEST_RAW);
generate_from_witx("markdown", MD_GUEST_API);
cargo_build_example(TESTS_DIR, RUST_EXAMPLE);
as_build_example(TESTS_DIR, AS_EXAMPLE);
}
fn cargo_build_example(dir: &str, example: &str) {
let dir = format!("{}/{}", dir, example);
run(
vec!["cargo", "build", "--target", "wasm32-wasi", "--release"],
Some(dir),
);
}
fn as_build_example(dir: &str, example: &str) {
let dir = format!("{}/{}", dir, example);
run(vec!["npm", "install"], Some(dir.clone()));
run(vec!["npm", "run", "asbuild"], Some(dir));
}
fn check_witx_codegen() {
match process::Command::new("witx-codegen").spawn() {
Ok(_) => {
eprintln!("witx-codegen already installed");
}
Err(_) => {
println!("cannot find witx-codegen, attempting to install");
run(
vec![
"cargo",
"install",
"witx-codegen",
"--version",
WITX_CODEGEN_VERSION,
],
None,
);
}
}
}
fn generate_from_witx(codegen_type: &str, output: &str) {
check_witx_codegen();
run(
vec![
"witx-codegen",
"--output-type",
codegen_type,
"--output",
output,
"witx/wasi_experimental_http.witx",
],
None,
);
}
fn run<S: Into<String> + AsRef<std::ffi::OsStr>>(args: Vec<S>, dir: Option<String>) {
let mut cmd = Command::new(get_os_process());
cmd.stdout(process::Stdio::piped());
cmd.stderr(process::Stdio::piped());
if let Some(dir) = dir {
cmd.current_dir(dir);
};
cmd.arg("-c");
cmd.arg(
args.into_iter()
.map(Into::into)
.collect::<Vec<String>>()
.join(" "),
);
println!("running {:#?}", cmd);
cmd.output().unwrap();
}
fn get_os_process() -> String {
if cfg!(target_os = "windows") {
String::from("powershell.exe")
} else {
String::from("/bin/bash")
}
}