forked from spinframework/spin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
134 lines (111 loc) · 4.03 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::{
collections::HashMap,
path::Path,
process::{self, Command},
};
use cargo_target_dep::build_target_dep;
const RUST_HTTP_INTEGRATION_TEST: &str = "tests/http/simple-spin-rust";
const RUST_HTTP_INTEGRATION_ENV_TEST: &str = "tests/http/headers-env-routes-test";
fn main() {
println!("cargo:rerun-if-changed=build.rs");
if !has_wasm32_wasi_target() {
// Current toolchain: e.g. "stable-x86_64-pc-windows-msvc", "1.60-x86_64-pc-windows-msvc"
let current_toolchain = std::env::var("RUSTUP_TOOLCHAIN").unwrap();
let current_toolchain = current_toolchain.split_once('-').unwrap().0;
// Default toolchain: e.g. "stable (default)", "nightly", "1.60-x86_64-pc-windows-msvc"
let default_toolchain = run(vec!["rustup", "default"], None, None);
let default_toolchain = std::str::from_utf8(&default_toolchain.stdout).unwrap();
let default_toolchain = default_toolchain.split(['-', ' ']).next().unwrap();
let toolchain_override = if current_toolchain != default_toolchain {
format!(" +{}", current_toolchain)
} else {
String::new()
};
println!(
r#"
error: the `wasm32-wasi` target is not installed
= help: consider downloading the target with `rustup{} target add wasm32-wasi`"#,
toolchain_override
);
process::exit(1);
}
std::fs::create_dir_all("target/test-programs").unwrap();
build_wasm_test_program("rust-http-test.wasm", "crates/http/tests/rust-http-test");
build_wasm_test_program("redis-rust.wasm", "crates/redis/tests/rust");
build_wasm_test_program("wagi-test.wasm", "crates/http/tests/wagi-test");
build_wasm_test_program(
"spin-http-benchmark.wasm",
"crates/http/benches/spin-http-benchmark",
);
build_wasm_test_program("wagi-benchmark.wasm", "crates/http/benches/wagi-benchmark");
build_wasm_test_program("echo.wasm", "examples/spin-timer/example");
cargo_build(RUST_HTTP_INTEGRATION_TEST);
cargo_build(RUST_HTTP_INTEGRATION_ENV_TEST);
let mut config = vergen::Config::default();
*config.git_mut().sha_kind_mut() = vergen::ShaKind::Short;
*config.git_mut().commit_timestamp_kind_mut() = vergen::TimestampKind::DateOnly;
vergen::vergen(config).expect("failed to extract build information");
}
fn build_wasm_test_program(name: &'static str, root: &'static str) {
build_target_dep(root, Path::new("target/test-programs").join(name))
.release()
.target("wasm32-wasi")
.build();
}
fn has_wasm32_wasi_target() -> bool {
let output = run(vec!["rustup", "target", "list", "--installed"], None, None);
let output = std::str::from_utf8(&output.stdout).unwrap();
for line in output.lines() {
if line == "wasm32-wasi" {
return true;
}
}
false
}
fn cargo_build(dir: &str) {
run(
vec!["cargo", "build", "--target", "wasm32-wasi", "--release"],
Some(dir),
None,
);
}
fn run<S: Into<String> + AsRef<std::ffi::OsStr>>(
args: Vec<S>,
dir: Option<S>,
env: Option<HashMap<S, S>>,
) -> process::Output {
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.into());
};
if let Some(env) = env {
for (k, v) in env {
cmd.env(k, v);
}
};
cmd.arg("-c");
cmd.arg(
args.into_iter()
.map(Into::into)
.collect::<Vec<String>>()
.join(" "),
);
let output = cmd.output().unwrap();
let code = output.status.code().unwrap();
if code != 0 {
println!("{:#?}", std::str::from_utf8(&output.stderr).unwrap());
println!("{:#?}", std::str::from_utf8(&output.stdout).unwrap());
// just fail
assert_eq!(0, code);
}
output
}
fn get_os_process() -> String {
if cfg!(target_os = "windows") {
String::from("powershell.exe")
} else {
String::from("/bin/bash")
}
}