diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index c724f4c016..16d7939a70 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -2326,7 +2326,7 @@ dependencies = [ "substrate-keyring 2.0.0", "substrate-offchain-primitives 2.0.0", "substrate-primitives 2.0.0", - "substrate-wasm-builder-runner 1.0.1", + "substrate-wasm-builder-runner 1.0.2", ] [[package]] @@ -2380,7 +2380,7 @@ dependencies = [ "substrate-consensus-aura-primitives 2.0.0", "substrate-offchain-primitives 2.0.0", "substrate-primitives 2.0.0", - "substrate-wasm-builder-runner 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "substrate-wasm-builder-runner 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -4609,7 +4609,7 @@ dependencies = [ "sr-sandbox 2.0.0", "sr-std 2.0.0", "substrate-primitives 2.0.0", - "substrate-wasm-builder-runner 1.0.1", + "substrate-wasm-builder-runner 1.0.2", ] [[package]] @@ -4765,7 +4765,7 @@ dependencies = [ "substrate-primitives 2.0.0", "substrate-test-runtime-client 2.0.0", "substrate-trie 2.0.0", - "substrate-wasm-builder-runner 1.0.1", + "substrate-wasm-builder-runner 1.0.2", "trie-db 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -4845,11 +4845,11 @@ dependencies = [ [[package]] name = "substrate-wasm-builder-runner" -version = "1.0.1" +version = "1.0.2" [[package]] name = "substrate-wasm-builder-runner" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -6171,7 +6171,7 @@ dependencies = [ "checksum strum 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1810e25f576e7ffce1ff5243b37066da5ded0310b3274c20baaeccb1145b2806" "checksum strum_macros 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "572a2f4e53dd4c3483fd79e5cc10ddd773a3acb1169bbfe8762365e107110579" "checksum substrate-bip39 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d69ace596e9ca97837cc41f8edcfc4e0a997f227d5fc153d1010b60a0fe9acda" -"checksum substrate-wasm-builder-runner 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "131fa5f29b2a2e459f226411e4718ecda62e360123b7cfd76ae6a7da5bcaeb0d" +"checksum substrate-wasm-builder-runner 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f52ecbff6cc3d6e5c6401828e15937b680f459d6803ce238f01fe615bc40d071" "checksum subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" "checksum subtle 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "01dca13cf6c3b179864ab3292bd794e757618d35a7766b7c46050c614ba00829" "checksum syn 0.15.39 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d960b829a55e56db167e861ddb43602c003c7be0bee1d345021703fac2fb7c" diff --git a/substrate/core/executor/runtime-test/Cargo.toml b/substrate/core/executor/runtime-test/Cargo.toml index 4ad3446a91..26bf2f71bd 100644 --- a/substrate/core/executor/runtime-test/Cargo.toml +++ b/substrate/core/executor/runtime-test/Cargo.toml @@ -12,7 +12,7 @@ sandbox = { package = "sr-sandbox", path = "../../sr-sandbox", default-features substrate-primitives = { path = "../../primitives", default-features = false } [build-dependencies] -wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.1", path = "../../utils/wasm-builder-runner" } +wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.2", path = "../../utils/wasm-builder-runner" } [features] default = [ "std" ] diff --git a/substrate/core/executor/runtime-test/build.rs b/substrate/core/executor/runtime-test/build.rs index 7eaa5e53c3..a8a5e1cba5 100644 --- a/substrate/core/executor/runtime-test/build.rs +++ b/substrate/core/executor/runtime-test/build.rs @@ -21,7 +21,7 @@ fn main() { "wasm_binary.rs", WasmBuilderSource::CratesOrPath { path: "../../utils/wasm-builder", - version: "1.0.3", + version: "1.0.4", }, ); } diff --git a/substrate/core/test-runtime/Cargo.toml b/substrate/core/test-runtime/Cargo.toml index 8e9fc85a8a..d14d324006 100644 --- a/substrate/core/test-runtime/Cargo.toml +++ b/substrate/core/test-runtime/Cargo.toml @@ -31,7 +31,7 @@ substrate-executor = { path = "../executor" } substrate-test-runtime-client = { path = "./client" } [build-dependencies] -wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.1", path = "../utils/wasm-builder-runner" } +wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.2", path = "../utils/wasm-builder-runner" } [features] default = [ diff --git a/substrate/core/test-runtime/build.rs b/substrate/core/test-runtime/build.rs index 1870f11f80..f543f68ccd 100644 --- a/substrate/core/test-runtime/build.rs +++ b/substrate/core/test-runtime/build.rs @@ -21,7 +21,7 @@ fn main() { "wasm_binary.rs", WasmBuilderSource::CratesOrPath { path: "../utils/wasm-builder", - version: "1.0.3", + version: "1.0.4", }, ); } diff --git a/substrate/core/utils/wasm-builder-runner/Cargo.toml b/substrate/core/utils/wasm-builder-runner/Cargo.toml index da21baf1d9..4046b7e4e2 100644 --- a/substrate/core/utils/wasm-builder-runner/Cargo.toml +++ b/substrate/core/utils/wasm-builder-runner/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "substrate-wasm-builder-runner" -version = "1.0.1" +version = "1.0.2" authors = ["Parity Technologies "] description = "Runner for substrate-wasm-builder" edition = "2018" diff --git a/substrate/core/utils/wasm-builder-runner/src/lib.rs b/substrate/core/utils/wasm-builder-runner/src/lib.rs index 4d77bde4d6..1558dbf220 100644 --- a/substrate/core/utils/wasm-builder-runner/src/lib.rs +++ b/substrate/core/utils/wasm-builder-runner/src/lib.rs @@ -30,6 +30,9 @@ 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. @@ -93,6 +96,20 @@ impl WasmBuilderSource { } } +/// Build the currently built project as WASM binary and extend `RUSTFLAGS` with the given rustflags. +/// +/// For more information, see [`build_current_project`]. +pub fn build_current_project_with_rustflags( + file_name: &str, + wasm_builder_source: WasmBuilderSource, + 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. diff --git a/substrate/core/utils/wasm-builder/README.md b/substrate/core/utils/wasm-builder/README.md index 90d7494eb1..8a8c67d6a8 100644 --- a/substrate/core/utils/wasm-builder/README.md +++ b/substrate/core/utils/wasm-builder/README.md @@ -49,6 +49,7 @@ By using environment variables, you can configure which WASM binaries are built - `TRIGGER_WASM_BUILD` - Can be set to trigger a WASM build. On subsequent calls the value of the variable needs to change. As WASM builder instructs `cargo` to watch for file changes this environment variable should only be required in certain circumstances. +- `WASM_BUILD_RUSTFLAGS` - Extend `RUSTFLAGS` given to `cargo build` while building the WASM binary. Each project can be skipped individually by using the environment variable `SKIP_PROJECT_NAME_WASM_BUILD`. Where `PROJECT_NAME` needs to be replaced by the name of the cargo project, e.g. `node-runtime` will diff --git a/substrate/core/utils/wasm-builder/src/lib.rs b/substrate/core/utils/wasm-builder/src/lib.rs index ef999f1008..779fa8db09 100644 --- a/substrate/core/utils/wasm-builder/src/lib.rs +++ b/substrate/core/utils/wasm-builder/src/lib.rs @@ -65,6 +65,7 @@ //! - `TRIGGER_WASM_BUILD` - Can be set to trigger a WASM build. On subsequent calls the value of the variable //! needs to change. As WASM builder instructs `cargo` to watch for file changes //! this environment variable should only be required in certain circumstances. +//! - `WASM_BUILD_RUSTFLAGS` - Extend `RUSTFLAGS` given to `cargo build` while building the WASM binary. //! //! Each project can be skipped individually by using the environment variable `SKIP_PROJECT_NAME_WASM_BUILD`. //! Where `PROJECT_NAME` needs to be replaced by the name of the cargo project, e.g. `node-runtime` will @@ -92,6 +93,9 @@ const SKIP_BUILD_ENV: &str = "SKIP_WASM_BUILD"; /// By default the WASM binary uses the same build type as the main cargo build. const WASM_BUILD_TYPE_ENV: &str = "WASM_BUILD_TYPE"; +/// Environment variable to extend the `RUSTFLAGS` variable given to the WASM build. +const WASM_BUILD_RUSTFLAGS_ENV: &str = "WASM_BUILD_RUSTFLAGS"; + /// Build the currently built project as WASM binary. /// /// The current project is determined by using the `CARGO_MANIFEST_DIR` environment variable. diff --git a/substrate/core/utils/wasm-builder/src/wasm_project.rs b/substrate/core/utils/wasm-builder/src/wasm_project.rs index 2b05774054..0d348a5cf4 100644 --- a/substrate/core/utils/wasm-builder/src/wasm_project.rs +++ b/substrate/core/utils/wasm-builder/src/wasm_project.rs @@ -227,12 +227,7 @@ fn create_project(cargo_manifest: &Path, wasm_workspace: &Path) -> PathBuf { fs::write( project_folder.join("src/lib.rs"), - format!( - r#" - #![no_std] - pub use wasm_project::*; - "# - ) + "#![no_std] pub use wasm_project::*;", ).expect("Project `lib.rs` writing can not fail; qed"); if let Some(crate_lock_file) = find_cargo_lock(cargo_manifest) { @@ -265,9 +260,15 @@ fn is_release_build() -> bool { fn build_project(project: &Path) { let manifest_path = project.join("Cargo.toml"); let mut build_cmd = crate::get_nightly_cargo(); + + let rustflags = format!( + "-C link-arg=--export-table {}", + env::var(crate::WASM_BUILD_RUSTFLAGS_ENV).unwrap_or_default(), + ); + build_cmd.args(&["build", "--target=wasm32-unknown-unknown"]) .arg(format!("--manifest-path={}", manifest_path.display())) - .env("RUSTFLAGS", "-C link-arg=--export-table") + .env("RUSTFLAGS", rustflags) // We don't want to call ourselves recursively .env(crate::SKIP_BUILD_ENV, ""); @@ -347,4 +348,5 @@ fn generate_rerun_if_changed_instructions( // Register our env variables println!("cargo:rerun-if-env-changed={}", crate::SKIP_BUILD_ENV); println!("cargo:rerun-if-env-changed={}", crate::WASM_BUILD_TYPE_ENV); + println!("cargo:rerun-if-env-changed={}", crate::WASM_BUILD_RUSTFLAGS_ENV); } diff --git a/substrate/node-template/runtime/Cargo.toml b/substrate/node-template/runtime/Cargo.toml index 9102ec5eb2..375a0ad64e 100644 --- a/substrate/node-template/runtime/Cargo.toml +++ b/substrate/node-template/runtime/Cargo.toml @@ -26,7 +26,7 @@ consensus-aura = { package = "substrate-consensus-aura-primitives", path = "../. offchain-primitives = { package = "substrate-offchain-primitives", path = "../../core/offchain/primitives", default-features = false } [build-dependencies] -wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.1" } +wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.2" } [features] default = ["std"] diff --git a/substrate/node-template/runtime/build.rs b/substrate/node-template/runtime/build.rs index 73490a6fd3..ccf58b138f 100644 --- a/substrate/node-template/runtime/build.rs +++ b/substrate/node-template/runtime/build.rs @@ -17,5 +17,5 @@ use wasm_builder_runner::{build_current_project, WasmBuilderSource}; fn main() { - build_current_project("wasm_binary.rs", WasmBuilderSource::Crates("1.0.3")); + build_current_project("wasm_binary.rs", WasmBuilderSource::Crates("1.0.4")); } diff --git a/substrate/node/runtime/Cargo.toml b/substrate/node/runtime/Cargo.toml index 9796455957..83708be6da 100644 --- a/substrate/node/runtime/Cargo.toml +++ b/substrate/node/runtime/Cargo.toml @@ -39,7 +39,7 @@ serde = { version = "1.0", optional = true } substrate-keyring = { path = "../../core/keyring", optional = true } [build-dependencies] -wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.1", path = "../../core/utils/wasm-builder-runner" } +wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.2", path = "../../core/utils/wasm-builder-runner" } [features] default = ["std"] diff --git a/substrate/node/runtime/build.rs b/substrate/node/runtime/build.rs index 5252068e56..39aecacb20 100644 --- a/substrate/node/runtime/build.rs +++ b/substrate/node/runtime/build.rs @@ -21,7 +21,7 @@ fn main() { "wasm_binary.rs", WasmBuilderSource::CratesOrPath { path: "../../core/utils/wasm-builder", - version: "1.0.3", + version: "1.0.4", }, ); }