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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use std::{env, process::Command, fs, path::{PathBuf, Path}};
const SKIP_BUILD_ENV: &str = "SKIP_WASM_BUILD";
const DUMMY_WASM_BINARY_ENV: &str = "BUILD_DUMMY_WASM_BINARY";
const TRIGGER_WASM_BUILD_ENV: &str = "TRIGGER_WASM_BUILD";
fn replace_back_slashes<T: ToString>(path: T) -> String {
path.to_string().replace("\\", "/")
}
pub enum WasmBuilderSource {
Path(&'static str),
Git {
repo: &'static str,
rev: &'static str,
},
Crates(&'static str),
}
impl WasmBuilderSource {
fn to_cargo_source(&self, manifest_dir: &Path) -> String {
match self {
WasmBuilderSource::Path(path) => {
replace_back_slashes(format!("path = \"{}\"", manifest_dir.join(path).display()))
}
WasmBuilderSource::Git { repo, rev } => {
format!("git = \"{}\", rev=\"{}\"", repo, rev)
}
WasmBuilderSource::Crates(version) => {
format!("version = \"{}\"", version)
}
}
}
}
pub fn build_current_project(file_name: &str, wasm_builder_source: WasmBuilderSource) {
generate_rerun_if_changed_instructions();
if check_skip_build() {
return;
}
let manifest_dir = PathBuf::from(
env::var("CARGO_MANIFEST_DIR").expect(
"`CARGO_MANIFEST_DIR` is always set for `build.rs` files; qed"
)
);
let cargo_toml_path = manifest_dir.join("Cargo.toml");
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("`OUT_DIR` is set by cargo!"));
let file_path = out_dir.join(file_name);
let project_folder = out_dir.join("wasm_build_runner");
if check_provide_dummy_wasm_binary() {
provide_dummy_wasm_binary(&file_path);
} else {
create_project(&project_folder, &file_path, &manifest_dir, wasm_builder_source, &cargo_toml_path);
run_project(&project_folder);
}
}
fn create_project(
project_folder: &Path,
file_path: &Path,
manifest_dir: &Path,
wasm_builder_source: WasmBuilderSource,
cargo_toml_path: &Path,
) {
fs::create_dir_all(project_folder.join("src"))
.expect("WASM build runner dir create can not fail; qed");
fs::write(
project_folder.join("Cargo.toml"),
format!(
r#"
[package]
name = "wasm-build-runner-impl"
version = "1.0.0"
edition = "2018"
[dependencies]
substrate-wasm-builder = {{ {wasm_builder_source} }}
[workspace]
"#,
wasm_builder_source = wasm_builder_source.to_cargo_source(manifest_dir),
)
).expect("WASM build runner `Cargo.toml` writing can not fail; qed");
fs::write(
project_folder.join("src/main.rs"),
format!(
r#"
fn main() {{
substrate_wasm_builder::build_project("{file_path}", "{cargo_toml_path}")
}}
"#,
file_path = replace_back_slashes(file_path.display()),
cargo_toml_path = replace_back_slashes(cargo_toml_path.display()),
)
).expect("WASM build runner `main.rs` writing can not fail; qed");
}
fn run_project(project_folder: &Path) {
let cargo = env::var("CARGO").expect("`CARGO` env variable is always set when executing `build.rs`.");
let mut cmd = Command::new(cargo);
cmd.arg("run").arg(format!("--manifest-path={}", project_folder.join("Cargo.toml").display()));
if env::var("DEBUG") != Ok(String::from("true")) {
cmd.arg("--release");
}
if !cmd.status().map(|s| s.success()).unwrap_or(false) {
panic!("Running WASM build runner failed!");
}
}
fn generate_crate_skip_build_env_name() -> String {
format!(
"SKIP_{}_WASM_BUILD",
env::var("CARGO_PKG_NAME").expect("Package name is set").to_uppercase().replace('-', "_"),
)
}
fn check_skip_build() -> bool {
env::var(SKIP_BUILD_ENV).is_ok() || env::var(generate_crate_skip_build_env_name()).is_ok()
}
fn check_provide_dummy_wasm_binary() -> bool {
env::var(DUMMY_WASM_BINARY_ENV).is_ok()
}
fn provide_dummy_wasm_binary(file_path: &Path) {
fs::write(
file_path,
"pub const WASM_BINARY: &[u8] = &[]; pub const WASM_BINARY_BLOATY: &[u8] = &[];"
).expect("Writing dummy WASM binary should not fail");
}
fn generate_rerun_if_changed_instructions() {
println!("cargo:rerun-if-env-changed={}", SKIP_BUILD_ENV);
println!("cargo:rerun-if-env-changed={}", DUMMY_WASM_BINARY_ENV);
println!("cargo:rerun-if-env-changed={}", TRIGGER_WASM_BUILD_ENV);
println!("cargo:rerun-if-env-changed={}", generate_crate_skip_build_env_name());
}