-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
119 lines (102 loc) · 4 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
use std::io::Write;
use std::path::PathBuf;
use std::{env, fs};
use wasmer_compiler::ArtifactCreate;
#[cfg(feature = "build-cranelift")]
use wasmer_compiler_cranelift::Cranelift as Compiler;
#[cfg(feature = "build-llvm")]
use wasmer_compiler_llvm::LLVM as Compiler;
enum CompilationSource {
Precompiled(PathBuf),
#[cfg(any(feature = "build-cranelift", feature = "build-llvm"))]
Compiler {
engine: wasmer::Engine,
runners_dir: PathBuf,
tunables: wasmer::BaseTunables,
},
}
fn main() {
let source = match env::var_os("COMPILED_RUNNERS") {
Some(dir) => CompilationSource::Precompiled(fs::canonicalize(&dir).unwrap()),
#[cfg(not(any(feature = "build-cranelift", feature = "build-llvm")))]
None => {
panic!("need build-cranelift or build-llvm or the COMPILED_RUNNERS env var")
}
#[cfg(any(feature = "build-cranelift", feature = "build-llvm"))]
None => {
let mut features = wasmer::CpuFeature::set();
for feat in env::var("CARGO_CFG_TARGET_FEATURE").unwrap().split(',') {
if let Ok(feat) = feat.parse() {
features.insert(feat);
}
}
let target =
wasmer::Target::new(env::var("TARGET").unwrap().parse().unwrap(), features);
let tunables = wasmer::BaseTunables::for_target(&target);
let engine = wasmer::EngineBuilder::new(Compiler::default()).engine();
let runners_dir = fs::canonicalize("../logic/wasm-dist/lang-runners")
.expect("need to run logic/build-wasm.sh");
CompilationSource::Compiler {
engine,
runners_dir,
tunables,
}
}
};
let lang_runners = [("Python", "pyrunner"), ("Javascript", "jsrunner")];
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let mut match_lang = fs::File::create(out_dir.join("lang_runners.rs")).unwrap();
writeln!(match_lang, "match lang {{").unwrap();
for (lang, runner) in &lang_runners {
let (path, include_bin) = match &source {
CompilationSource::Precompiled(dir) => {
let mut wasmu = dir.join(runner);
wasmu.set_extension("wasmu");
(wasmu, true)
}
#[cfg(any(feature = "build-cranelift", feature = "build-llvm"))]
CompilationSource::Compiler {
engine,
runners_dir,
tunables,
} => {
let mut src = runners_dir.join(runner);
src.set_extension("wasm");
let mut dst = out_dir.join(runner);
dst.set_extension("wasmu");
println!("compiling {}", runner);
println!("cargo:rerun-if-changed={}", src.display());
let needs_updating = src
.metadata()
.and_then(|m| Ok((m, dst.metadata()?)))
.and_then(|(src, dst)| Ok(src.modified()? > dst.modified()?))
.unwrap_or(true);
if needs_updating {
let wasm_source = fs::read(&src).unwrap();
let artifact =
wasmer::Artifact::new(engine, &wasm_source, tunables).unwrap();
fs::write(&dst, artifact.serialize().unwrap()).unwrap();
}
(dst, !cfg!(debug_assertions))
}
};
writeln!(
match_lang,
" Lang::{} => lang_runner!({}({:?}){}),",
lang,
if include_bin {
"include_bytes!"
} else {
"std::fs::read"
},
path,
if include_bin {
""
} else {
r#".expect("Error loading runtime file, if this is a production build then include_bin should have been set to true")"#
}
)
.unwrap();
}
writeln!(match_lang, "}}").unwrap();
}