|
15 | 15 | // See the License for the specific language governing permissions and |
16 | 16 | // limitations under the License. |
17 | 17 |
|
18 | | -#![doc = include_str!("../README.md")] |
| 18 | +//! # Wasm builder is a utility for building a project as a Wasm binary |
| 19 | +//! |
| 20 | +//! The Wasm builder is a tool that integrates the process of building the WASM binary of your |
| 21 | +//! project into the main `cargo` build process. |
| 22 | +//! |
| 23 | +//! ## Project setup |
| 24 | +//! |
| 25 | +//! A project that should be compiled as a Wasm binary needs to: |
| 26 | +//! |
| 27 | +//! 1. Add a `build.rs` file. |
| 28 | +//! 2. Add `wasm-builder` as dependency into `build-dependencies`. |
| 29 | +//! |
| 30 | +//! The `build.rs` file needs to contain the following code: |
| 31 | +//! |
| 32 | +//! ```no_run |
| 33 | +//! use substrate_wasm_builder::WasmBuilder; |
| 34 | +//! |
| 35 | +//! fn main() { |
| 36 | +//! WasmBuilder::new() |
| 37 | +//! // Tell the builder to build the project (crate) this `build.rs` is part of. |
| 38 | +//! .with_current_project() |
| 39 | +//! // Make sure to export the `heap_base` global, this is required by Substrate |
| 40 | +//! .export_heap_base() |
| 41 | +//! // Build the Wasm file so that it imports the memory (need to be provided by at instantiation) |
| 42 | +//! .import_memory() |
| 43 | +//! // Build it. |
| 44 | +//! .build() |
| 45 | +//! } |
| 46 | +//! ``` |
| 47 | +//! |
| 48 | +//! As the final step, you need to add the following to your project: |
| 49 | +//! |
| 50 | +//! ```ignore |
| 51 | +//! include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); |
| 52 | +//! ``` |
| 53 | +//! |
| 54 | +//! This will include the generated Wasm binary as two constants `WASM_BINARY` and |
| 55 | +//! `WASM_BINARY_BLOATY`. The former is a compact Wasm binary and the latter is the Wasm binary as |
| 56 | +//! being generated by the compiler. Both variables have `Option<&'static [u8]>` as type. |
| 57 | +//! |
| 58 | +//! ### Feature |
| 59 | +//! |
| 60 | +//! Wasm builder supports to enable cargo features while building the Wasm binary. By default it |
| 61 | +//! will enable all features in the wasm build that are enabled for the native build except the |
| 62 | +//! `default` and `std` features. Besides that, wasm builder supports the special `runtime-wasm` |
| 63 | +//! feature. This `runtime-wasm` feature will be enabled by the wasm builder when it compiles the |
| 64 | +//! Wasm binary. If this feature is not present, it will not be enabled. |
| 65 | +//! |
| 66 | +//! ## Environment variables |
| 67 | +//! |
| 68 | +//! By using environment variables, you can configure which Wasm binaries are built and how: |
| 69 | +//! |
| 70 | +//! - `SKIP_WASM_BUILD` - Skips building any Wasm binary. This is useful when only native should be |
| 71 | +//! recompiled. If this is the first run and there doesn't exist a Wasm binary, this will set both |
| 72 | +//! variables to `None`. |
| 73 | +//! - `WASM_BUILD_TYPE` - Sets the build type for building Wasm binaries. Supported values are |
| 74 | +//! `release` or `debug`. By default the build type is equal to the build type used by the main |
| 75 | +//! build. |
| 76 | +//! - `FORCE_WASM_BUILD` - Can be set to force a Wasm build. On subsequent calls the value of the |
| 77 | +//! variable needs to change. As wasm-builder instructs `cargo` to watch for file changes this |
| 78 | +//! environment variable should only be required in certain circumstances. |
| 79 | +//! - `WASM_BUILD_RUSTFLAGS` - Extend `RUSTFLAGS` given to `cargo build` while building the wasm |
| 80 | +//! binary. |
| 81 | +//! - `WASM_BUILD_NO_COLOR` - Disable color output of the wasm build. |
| 82 | +//! - `WASM_TARGET_DIRECTORY` - Will copy any build Wasm binary to the given directory. The path |
| 83 | +//! needs to be absolute. |
| 84 | +//! - `WASM_BUILD_TOOLCHAIN` - The toolchain that should be used to build the Wasm binaries. The |
| 85 | +//! format needs to be the same as used by cargo, e.g. `nightly-2020-02-20`. |
| 86 | +//! - `WASM_BUILD_WORKSPACE_HINT` - Hint the workspace that is being built. This is normally not |
| 87 | +//! required as we walk up from the target directory until we find a `Cargo.toml`. If the target |
| 88 | +//! directory is changed for the build, this environment variable can be used to point to the |
| 89 | +//! actual workspace. |
| 90 | +//! - `WASM_BUILD_STD` - Sets whether the Rust's standard library crates will also be built. This is |
| 91 | +//! necessary to make sure the standard library crates only use the exact WASM feature set that |
| 92 | +//! our executor supports. Enabled by default. |
| 93 | +//! - `CARGO_NET_OFFLINE` - If `true`, `--offline` will be passed to all processes launched to |
| 94 | +//! prevent network access. Useful in offline environments. |
| 95 | +//! |
| 96 | +//! Each project can be skipped individually by using the environment variable |
| 97 | +//! `SKIP_PROJECT_NAME_WASM_BUILD`. Where `PROJECT_NAME` needs to be replaced by the name of the |
| 98 | +//! cargo project, e.g. `kitchensink-runtime` will be `NODE_RUNTIME`. |
| 99 | +//! |
| 100 | +//! ## Prerequisites: |
| 101 | +//! |
| 102 | +//! Wasm builder requires the following prerequisites for building the Wasm binary: |
| 103 | +//! |
| 104 | +//! - rust nightly + `wasm32-unknown-unknown` toolchain |
| 105 | +//! |
| 106 | +//! or |
| 107 | +//! |
| 108 | +//! - rust stable and version at least 1.68.0 + `wasm32-unknown-unknown` toolchain |
| 109 | +//! |
| 110 | +//! If a specific rust is installed with `rustup`, it is important that the wasm target is |
| 111 | +//! installed as well. For example if installing the rust from 20.02.2020 using `rustup |
| 112 | +//! install nightly-2020-02-20`, the wasm target needs to be installed as well `rustup target add |
| 113 | +//! wasm32-unknown-unknown --toolchain nightly-2020-02-20`. |
19 | 114 |
|
20 | 115 | use std::{ |
21 | 116 | env, fs, |
|
0 commit comments