diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index ac74b57a52..06b1b42b10 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -8256,7 +8256,7 @@ version = "2.0.0-rc2" [[package]] name = "substrate-wasm-builder" -version = "1.0.10" +version = "1.0.11" dependencies = [ "atty", "build-helper", diff --git a/substrate/bin/node-template/runtime/build.rs b/substrate/bin/node-template/runtime/build.rs index 39f7f56feb..1f40a41ff8 100644 --- a/substrate/bin/node-template/runtime/build.rs +++ b/substrate/bin/node-template/runtime/build.rs @@ -3,7 +3,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates("1.0.9") + .with_wasm_builder_from_crates("1.0.11") .export_heap_base() .import_memory() .build() diff --git a/substrate/bin/node/runtime/build.rs b/substrate/bin/node/runtime/build.rs index cd5db582f3..a4f3235660 100644 --- a/substrate/bin/node/runtime/build.rs +++ b/substrate/bin/node/runtime/build.rs @@ -20,7 +20,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates_or_path("1.0.9", "../../../utils/wasm-builder") + .with_wasm_builder_from_crates_or_path("1.0.11", "../../../utils/wasm-builder") .export_heap_base() .import_memory() .build() diff --git a/substrate/client/executor/runtime-test/build.rs b/substrate/client/executor/runtime-test/build.rs index 647b476814..c5f1f2402b 100644 --- a/substrate/client/executor/runtime-test/build.rs +++ b/substrate/client/executor/runtime-test/build.rs @@ -19,7 +19,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates_or_path("1.0.9", "../../../utils/wasm-builder") + .with_wasm_builder_from_crates_or_path("1.0.11", "../../../utils/wasm-builder") .export_heap_base() .import_memory() .build() diff --git a/substrate/primitives/runtime-interface/test-wasm-deprecated/build.rs b/substrate/primitives/runtime-interface/test-wasm-deprecated/build.rs index cd5db582f3..a4f3235660 100644 --- a/substrate/primitives/runtime-interface/test-wasm-deprecated/build.rs +++ b/substrate/primitives/runtime-interface/test-wasm-deprecated/build.rs @@ -20,7 +20,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates_or_path("1.0.9", "../../../utils/wasm-builder") + .with_wasm_builder_from_crates_or_path("1.0.11", "../../../utils/wasm-builder") .export_heap_base() .import_memory() .build() diff --git a/substrate/primitives/runtime-interface/test-wasm/build.rs b/substrate/primitives/runtime-interface/test-wasm/build.rs index cd5db582f3..a4f3235660 100644 --- a/substrate/primitives/runtime-interface/test-wasm/build.rs +++ b/substrate/primitives/runtime-interface/test-wasm/build.rs @@ -20,7 +20,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates_or_path("1.0.9", "../../../utils/wasm-builder") + .with_wasm_builder_from_crates_or_path("1.0.11", "../../../utils/wasm-builder") .export_heap_base() .import_memory() .build() diff --git a/substrate/test-utils/runtime/build.rs b/substrate/test-utils/runtime/build.rs index 7d30dacfc8..69ff73a3ff 100644 --- a/substrate/test-utils/runtime/build.rs +++ b/substrate/test-utils/runtime/build.rs @@ -20,7 +20,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates_or_path("1.0.9", "../../utils/wasm-builder") + .with_wasm_builder_from_crates_or_path("1.0.11", "../../utils/wasm-builder") .export_heap_base() // Note that we set the stack-size to 1MB explicitly even though it is set // to this value by default. This is because some of our tests (`restoration_of_globals`) diff --git a/substrate/utils/wasm-builder/Cargo.toml b/substrate/utils/wasm-builder/Cargo.toml index 74e5f20524..e46db43268 100644 --- a/substrate/utils/wasm-builder/Cargo.toml +++ b/substrate/utils/wasm-builder/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "substrate-wasm-builder" -version = "1.0.10" +version = "1.0.11" authors = ["Parity Technologies "] description = "Utility for building WASM binaries" edition = "2018" diff --git a/substrate/utils/wasm-builder/README.md b/substrate/utils/wasm-builder/README.md index 5f4ca615d5..b72e7e16d4 100644 --- a/substrate/utils/wasm-builder/README.md +++ b/substrate/utils/wasm-builder/README.md @@ -1,11 +1,11 @@ -# WASM builder is a utility for building a project as a WASM binary +# Wasm builder is a utility for building a project as a Wasm binary -The WASM builder is a tool that integrates the process of building the WASM binary of your project into the main +The Wasm builder is a tool that integrates the process of building the WASM binary of your project into the main `cargo` build process. ## Project setup -A project that should be compiled as a WASM binary needs to: +A project that should be compiled as a Wasm binary needs to: 1. Add a `build.rs` file. 2. Add `substrate-wasm-builder` as dependency into `build-dependencies`. @@ -31,12 +31,20 @@ As the final step, you need to add the following to your project: include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); ``` -This will include the generated WASM binary as two constants `WASM_BINARY` and `WASM_BINARY_BLOATY`. -The former is a compact WASM binary and the latter is not compacted. +This will include the generated Wasm binary as two constants `WASM_BINARY` and `WASM_BINARY_BLOATY`. +The former is a compact Wasm binary and the latter is not compacted. + +### Feature + +Wasm builder supports to enable cargo features while building the Wasm binary. By default it will +enable all features in the wasm build that are enabled for the native build except the +`default` and `std` features. Besides that, wasm builder supports the special `runtime-wasm` +feature. This `runtime-wasm` feature will be enabled by the wasm builder when it compiles the +Wasm binary. If this feature is not present, it will not be enabled. ## Environment variables -By using environment variables, you can configure which WASM binaries are built and how: +By using environment variables, you can configure which Wasm binaries are built and how: - `SKIP_WASM_BUILD` - Skips building any wasm binary. This is useful when only native should be recompiled. - `BUILD_DUMMY_WASM_BINARY` - Builds dummy wasm binaries. These dummy binaries are empty and useful @@ -44,7 +52,7 @@ By using environment variables, you can configure which WASM binaries are built - `WASM_BUILD_TYPE` - Sets the build type for building wasm binaries. Supported values are `release` or `debug`. By default the build type is equal to the build type used by the main build. - `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 + 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. - `WASM_BUILD_NO_COLOR` - Disable color output of the wasm build. @@ -59,7 +67,7 @@ be `NODE_RUNTIME`. ## Prerequisites: -WASM builder requires the following prerequisites for building the WASM binary: +Wasm builder requires the following prerequisites for building the Wasm binary: - rust nightly + `wasm32-unknown-unknown` toolchain @@ -67,4 +75,4 @@ If a specific rust nightly is installed with `rustup`, it is important that the as well. For example if installing the rust nightly from 20.02.2020 using `rustup install nightly-2020-02-20`, the wasm target needs to be installed as well `rustup target add wasm32-unknown-unknown --toolchain nightly-2020-02-20`. -License: GPL-3.0 +License: Apache-2.0 diff --git a/substrate/utils/wasm-builder/src/lib.rs b/substrate/utils/wasm-builder/src/lib.rs index a4c546c802..95b75c5867 100644 --- a/substrate/utils/wasm-builder/src/lib.rs +++ b/substrate/utils/wasm-builder/src/lib.rs @@ -15,14 +15,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! # WASM builder is a utility for building a project as a WASM binary +//! # Wasm builder is a utility for building a project as a Wasm binary //! -//! The WASM builder is a tool that integrates the process of building the WASM binary of your project into the main +//! The Wasm builder is a tool that integrates the process of building the WASM binary of your project into the main //! `cargo` build process. //! //! ## Project setup //! -//! A project that should be compiled as a WASM binary needs to: +//! A project that should be compiled as a Wasm binary needs to: //! //! 1. Add a `build.rs` file. //! 2. Add `substrate-wasm-builder` as dependency into `build-dependencies`. @@ -48,12 +48,20 @@ //! include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); //! ``` //! -//! This will include the generated WASM binary as two constants `WASM_BINARY` and `WASM_BINARY_BLOATY`. -//! The former is a compact WASM binary and the latter is not compacted. +//! This will include the generated Wasm binary as two constants `WASM_BINARY` and `WASM_BINARY_BLOATY`. +//! The former is a compact Wasm binary and the latter is not compacted. +//! +//! ### Feature +//! +//! Wasm builder supports to enable cargo features while building the Wasm binary. By default it will +//! enable all features in the wasm build that are enabled for the native build except the +//! `default` and `std` features. Besides that, wasm builder supports the special `runtime-wasm` +//! feature. This `runtime-wasm` feature will be enabled by the wasm builder when it compiles the +//! Wasm binary. If this feature is not present, it will not be enabled. //! //! ## Environment variables //! -//! By using environment variables, you can configure which WASM binaries are built and how: +//! By using environment variables, you can configure which Wasm binaries are built and how: //! //! - `SKIP_WASM_BUILD` - Skips building any wasm binary. This is useful when only native should be recompiled. //! - `BUILD_DUMMY_WASM_BINARY` - Builds dummy wasm binaries. These dummy binaries are empty and useful @@ -61,7 +69,7 @@ //! - `WASM_BUILD_TYPE` - Sets the build type for building wasm binaries. Supported values are `release` or `debug`. //! By default the build type is equal to the build type used by the main build. //! - `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 +//! 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. //! - `WASM_BUILD_NO_COLOR` - Disable color output of the wasm build. @@ -76,7 +84,7 @@ //! //! ## Prerequisites: //! -//! WASM builder requires the following prerequisites for building the WASM binary: +//! Wasm builder requires the following prerequisites for building the Wasm binary: //! //! - rust nightly + `wasm32-unknown-unknown` toolchain //! diff --git a/substrate/utils/wasm-builder/src/wasm_project.rs b/substrate/utils/wasm-builder/src/wasm_project.rs index 4e927f4e85..7df3524e8a 100644 --- a/substrate/utils/wasm-builder/src/wasm_project.rs +++ b/substrate/utils/wasm-builder/src/wasm_project.rs @@ -308,15 +308,25 @@ fn create_wasm_workspace_project(wasm_workspace: &Path, workspace_root_path: &Pa ); } +/// Find a package by the given `manifest_path` in the metadata. +/// +/// Panics if the package could not be found. +fn find_package_by_manifest_path<'a>( + manifest_path: &Path, + crate_metadata: &'a cargo_metadata::Metadata, +) -> &'a cargo_metadata::Package { + crate_metadata.packages + .iter() + .find(|p| p.manifest_path == manifest_path) + .expect("Wasm project exists in its own metadata; qed") +} + /// Get a list of enabled features for the project. fn project_enabled_features( cargo_manifest: &Path, crate_metadata: &cargo_metadata::Metadata, ) -> Vec { - let package = crate_metadata.packages - .iter() - .find(|p| p.manifest_path == cargo_manifest) - .expect("Wasm project exists in its own metadata; qed"); + let package = find_package_by_manifest_path(cargo_manifest, crate_metadata); let mut enabled_features = package.features.keys() .filter(|f| { @@ -338,6 +348,16 @@ fn project_enabled_features( enabled_features } +/// Returns if the project has the `runtime-wasm` feature +fn has_runtime_wasm_feature_declared( + cargo_manifest: &Path, + crate_metadata: &cargo_metadata::Metadata, +) -> bool { + let package = find_package_by_manifest_path(cargo_manifest, crate_metadata); + + package.features.keys().any(|k| k == "runtime-wasm") +} + /// Create the project used to build the wasm binary. /// /// # Returns @@ -348,9 +368,14 @@ fn create_project(cargo_manifest: &Path, wasm_workspace: &Path, crate_metadata: let wasm_binary = get_wasm_binary_name(cargo_manifest); let project_folder = wasm_workspace.join(&crate_name); - fs::create_dir_all(project_folder.join("src")).expect("Wasm project dir create can not fail; qed"); + fs::create_dir_all(project_folder.join("src")) + .expect("Wasm project dir create can not fail; qed"); - let enabled_features = project_enabled_features(&cargo_manifest, &crate_metadata); + let mut enabled_features = project_enabled_features(&cargo_manifest, &crate_metadata); + + if has_runtime_wasm_feature_declared(cargo_manifest, crate_metadata) { + enabled_features.push("runtime-wasm".into()); + } write_file_if_changed( project_folder.join("Cargo.toml"),