Make it easier to just rebuild the WASM files (#3510)

* Adds `WASM_TARGET_DIRECTORY` env variable to `wasm-builder`

* Create the `wasm-builder-runner`'s in a common workspace

* Make `wasm-builder` trigger less rebuilds

* Version up

* Adds script for building only the WASM files

* Apply suggestions from code review

Co-Authored-By: André Silva <andre.beat@gmail.com>
Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2019-08-29 09:06:51 +02:00
committed by GitHub
parent c26b7163a3
commit 4ff97bd856
12 changed files with 182 additions and 58 deletions
@@ -30,9 +30,6 @@ use std::{env, process::{Command, self}, fs, path::{PathBuf, Path}};
/// Environment variable that tells us to skip building the WASM binary.
const SKIP_BUILD_ENV: &str = "SKIP_WASM_BUILD";
/// Environment variable to extend the `RUSTFLAGS` variable given to the WASM build.
const WASM_BUILD_RUSTFLAGS_ENV: &str = "WASM_BUILD_RUSTFLAGS";
/// Environment variable that tells us to create a dummy WASM binary.
///
/// This is useful for `cargo check` to speed-up the compilation.
@@ -102,22 +99,8 @@ impl WasmBuilderSource {
pub fn build_current_project_with_rustflags(
file_name: &str,
wasm_builder_source: WasmBuilderSource,
rustflags: &str,
default_rustflags: &str,
) {
let given_rustflags = env::var(WASM_BUILD_RUSTFLAGS_ENV).unwrap_or_default();
env::set_var(WASM_BUILD_RUSTFLAGS_ENV, format!("{} {}", given_rustflags, rustflags));
build_current_project(file_name, wasm_builder_source)
}
/// Build the currently built project as WASM binary.
///
/// The current project is determined using the `CARGO_MANIFEST_DIR` environment variable.
///
/// `file_name` - The name of the file being generated in the `OUT_DIR`. The file contains the
/// constant `WASM_BINARY` which contains the build wasm binary.
/// `wasm_builder_path` - Path to the wasm-builder project, relative to `CARGO_MANIFEST_DIR`.
pub fn build_current_project(file_name: &str, wasm_builder_source: WasmBuilderSource) {
if check_skip_build() {
// If we skip the build, we still want to make sure to be called when an env variable changes
generate_rerun_if_changed_instructions();
@@ -133,12 +116,20 @@ pub fn build_current_project(file_name: &str, wasm_builder_source: WasmBuilderSo
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");
let project_name = env::var("CARGO_PKG_NAME").expect("`CARGO_PKG_NAME` is set by cargo!");
let project_folder = get_workspace_root().join(project_name);
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);
create_project(
&project_folder,
&file_path,
&manifest_dir,
wasm_builder_source,
&cargo_toml_path,
default_rustflags,
);
run_project(&project_folder);
}
@@ -147,12 +138,43 @@ pub fn build_current_project(file_name: &str, wasm_builder_source: WasmBuilderSo
generate_rerun_if_changed_instructions();
}
/// Build the currently built project as WASM binary.
///
/// The current project is determined using the `CARGO_MANIFEST_DIR` environment variable.
///
/// `file_name` - The name of the file being generated in the `OUT_DIR`. The file contains the
/// constant `WASM_BINARY` which contains the build wasm binary.
/// `wasm_builder_path` - Path to the wasm-builder project, relative to `CARGO_MANIFEST_DIR`.
pub fn build_current_project(file_name: &str, wasm_builder_source: WasmBuilderSource) {
build_current_project_with_rustflags(file_name, wasm_builder_source, "");
}
/// Returns the root path of the wasm-builder-runner workspace.
///
/// The wasm-builder-runner workspace contains all wasm-builder-runner's projects.
fn get_workspace_root() -> PathBuf {
let out_dir_env = env::var("OUT_DIR").expect("`OUT_DIR` is set by cargo!");
let mut out_dir = PathBuf::from(&out_dir_env);
loop {
match out_dir.parent() {
Some(parent) if out_dir.ends_with("build") => return parent.join("wbuild-runner"),
_ => if !out_dir.pop() {
break;
}
}
}
panic!("Could not find target dir in: {}", out_dir_env)
}
fn create_project(
project_folder: &Path,
file_path: &Path,
manifest_dir: &Path,
wasm_builder_source: WasmBuilderSource,
cargo_toml_path: &Path,
default_rustflags: &str,
) {
fs::create_dir_all(project_folder.join("src"))
.expect("WASM build runner dir create can not fail; qed");
@@ -179,12 +201,19 @@ fn create_project(
project_folder.join("src/main.rs"),
format!(
r#"
use substrate_wasm_builder::build_project_with_default_rustflags;
fn main() {{
substrate_wasm_builder::build_project("{file_path}", "{cargo_toml_path}")
build_project_with_default_rustflags(
"{file_path}",
"{cargo_toml_path}",
"{default_rustflags}",
)
}}
"#,
file_path = replace_back_slashes(file_path.display()),
cargo_toml_path = replace_back_slashes(cargo_toml_path.display()),
default_rustflags = default_rustflags,
)
).expect("WASM build runner `main.rs` writing can not fail; qed");
}