Wasm-builder runtime-wasm feature (#6177)

* Enable the `runtime-wasm` for wasm builds

This enables the `runtime-wasm` feature for wasm builds. The feature is
not mandatory and will only be activated if it exists in the
`Cargo.toml`.

* Fix compilation

* Update docs

* Uprgade version

* Apply suggestions from code review
This commit is contained in:
Bastian Köcher
2020-05-28 22:06:25 +02:00
committed by GitHub
parent 02c6d50f40
commit c9cc46de3b
11 changed files with 72 additions and 31 deletions
+16 -8
View File
@@ -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
//!
@@ -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<String> {
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"),