Skip to content

Commit

Permalink
wasm-builder: Make it easier to build a WASM binary (#4177)
Browse files Browse the repository at this point in the history
Basically combines all the recommended calls into one
`build_using_defaults()` call or `init_with_defaults()` when there are
some custom changes required.
  • Loading branch information
bkchr authored Apr 22, 2024
1 parent 3380e21 commit bd9287f
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 86 deletions.
6 changes: 1 addition & 5 deletions cumulus/parachains/runtimes/assets/asset-hub-rococo/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@

#[cfg(feature = "std")]
fn main() {
substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build()
substrate_wasm_builder::WasmBuilder::build_using_defaults();
}

#[cfg(not(feature = "std"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@

#[cfg(feature = "std")]
fn main() {
substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build()
substrate_wasm_builder::WasmBuilder::build_using_defaults();
}

#[cfg(not(feature = "std"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@

#[cfg(feature = "std")]
fn main() {
substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build()
substrate_wasm_builder::WasmBuilder::build_using_defaults();
}

#[cfg(not(feature = "std"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@

#[cfg(feature = "std")]
fn main() {
substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build()
substrate_wasm_builder::WasmBuilder::build_using_defaults();
}

#[cfg(not(feature = "std"))]
Expand Down
6 changes: 1 addition & 5 deletions cumulus/parachains/runtimes/glutton/glutton-westend/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,5 @@
use substrate_wasm_builder::WasmBuilder;

fn main() {
WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build()
WasmBuilder::build_using_defaults();
}
6 changes: 1 addition & 5 deletions cumulus/parachains/runtimes/testing/penpal/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

#[cfg(feature = "std")]
fn main() {
substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build()
substrate_wasm_builder::WasmBuilder::build_using_defaults();
}

#[cfg(not(feature = "std"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@

#[cfg(feature = "std")]
fn main() {
substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build()
substrate_wasm_builder::WasmBuilder::build_using_defaults();
}

#[cfg(not(feature = "std"))]
Expand Down
10 changes: 2 additions & 8 deletions cumulus/test/runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@
fn main() {
use substrate_wasm_builder::WasmBuilder;

WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build();
WasmBuilder::build_using_defaults();

WasmBuilder::new()
.with_current_project()
WasmBuilder::init_with_defaults()
.enable_feature("increment-spec-version")
.import_memory()
.set_file_name("wasm_binary_spec_version_incremented.rs")
.build();
}
Expand Down
11 changes: 2 additions & 9 deletions polkadot/runtime/rococo/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@

#[cfg(feature = "std")]
fn main() {
substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
.import_memory()
.export_heap_base()
.build();
substrate_wasm_builder::WasmBuilder::build_using_defaults();

substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
substrate_wasm_builder::WasmBuilder::init_with_defaults()
.set_file_name("fast_runtime_binary.rs")
.enable_feature("fast-runtime")
.import_memory()
.export_heap_base()
.build();
}

Expand Down
6 changes: 1 addition & 5 deletions polkadot/runtime/test-runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,5 @@
use substrate_wasm_builder::WasmBuilder;

fn main() {
WasmBuilder::new()
.with_current_project()
.import_memory()
.export_heap_base()
.build()
WasmBuilder::build_using_defaults();
}
6 changes: 1 addition & 5 deletions polkadot/runtime/westend/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,5 @@
use substrate_wasm_builder::WasmBuilder;

fn main() {
WasmBuilder::new()
.with_current_project()
.import_memory()
.export_heap_base()
.build()
WasmBuilder::build_using_defaults();
}
12 changes: 12 additions & 0 deletions prdoc/pr_4177.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
title: "wasm-builder: Make it easier to build a WASM binary"

doc:
- audience: [Runtime Dev, Node Dev]
description: |
Combines all the recommended calls of the `WasmBuilder` into
`build_using_defaults()` or `init_with_defaults()` if more changes are required.
Otherwise the interface doesn't change and users can still continue to use
the "old" interface.

crates:
- name: substrate-wasm-builder
33 changes: 33 additions & 0 deletions substrate/utils/wasm-builder/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,39 @@ impl WasmBuilder {
WasmBuilderSelectProject { _ignore: () }
}

/// Build the WASM binary using the recommended default values.
///
/// This is the same as calling:
/// ```no_run
/// substrate_wasm_builder::WasmBuilder::new()
/// .with_current_project()
/// .import_memory()
/// .export_heap_base()
/// .build();
/// ```
pub fn build_using_defaults() {
WasmBuilder::new()
.with_current_project()
.import_memory()
.export_heap_base()
.build();
}

/// Init the wasm builder with the recommended default values.
///
/// In contrast to [`Self::build_using_defaults`] it does not build the WASM binary directly.
///
/// This is the same as calling:
/// ```no_run
/// substrate_wasm_builder::WasmBuilder::new()
/// .with_current_project()
/// .import_memory()
/// .export_heap_base();
/// ```
pub fn init_with_defaults() -> Self {
WasmBuilder::new().with_current_project().import_memory().export_heap_base()
}

/// Enable exporting `__heap_base` as global variable in the WASM binary.
///
/// This adds `-Clink-arg=--export=__heap_base` to `RUST_FLAGS`.
Expand Down
12 changes: 3 additions & 9 deletions substrate/utils/wasm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,9 @@
//! use substrate_wasm_builder::WasmBuilder;
//!
//! fn main() {
//! WasmBuilder::new()
//! // Tell the builder to build the project (crate) this `build.rs` is part of.
//! .with_current_project()
//! // Make sure to export the `heap_base` global, this is required by Substrate
//! .export_heap_base()
//! // Build the Wasm file so that it imports the memory (need to be provided by at instantiation)
//! .import_memory()
//! // Build it.
//! .build()
//! // Builds the WASM binary using the recommended defaults.
//! // If you need more control, you can call `new` or `init_with_defaults`.
//! WasmBuilder::build_using_defaults();
//! }
//! ```
//!
Expand Down
6 changes: 1 addition & 5 deletions templates/minimal/runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
fn main() {
#[cfg(feature = "std")]
{
substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build();
substrate_wasm_builder::WasmBuilder::build_using_defaults();
}
}
6 changes: 1 addition & 5 deletions templates/parachain/runtime/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#[cfg(feature = "std")]
fn main() {
substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build()
substrate_wasm_builder::WasmBuilder::build_using_defaults();
}

/// The wasm builder is deactivated when compiling
Expand Down
6 changes: 1 addition & 5 deletions templates/solochain/runtime/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
fn main() {
#[cfg(feature = "std")]
{
substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build();
substrate_wasm_builder::WasmBuilder::build_using_defaults();
}
}

0 comments on commit bd9287f

Please sign in to comment.