diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index cbb56fcf72679..b37d60a941d44 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -197,7 +197,7 @@ cargo-check-benches:
<<: *docker-env
<<: *test-refs
script:
- - BUILD_DUMMY_WASM_BINARY=1 time cargo +nightly check --benches --all
+ - SKIP_WASM_BUILD=1 time cargo +nightly check --benches --all
- cargo run --release -p node-bench -- ::node::import::native::sr25519::transfer_keep_alive::paritydb::small
- cargo run --release -p node-bench -- ::trie::read::small
- sccache -s
@@ -208,7 +208,7 @@ cargo-check-subkey:
<<: *test-refs
script:
- cd ./bin/utils/subkey
- - BUILD_DUMMY_WASM_BINARY=1 time cargo check --release
+ - SKIP_WASM_BUILD=1 time cargo check --release
- sccache -s
test-deterministic-wasm:
@@ -222,7 +222,7 @@ test-deterministic-wasm:
# build runtime
- cargo build --verbose --release -p node-runtime
# make checksum
- - sha256sum target/release/wbuild/target/wasm32-unknown-unknown/release/node_runtime.wasm > checksum.sha256
+ - sha256sum target/release/wbuild/node-runtime/target/wasm32-unknown-unknown/release/node_runtime.wasm > checksum.sha256
# clean up – FIXME: can we reuse some of the artifacts?
- cargo clean
# build again
@@ -343,7 +343,7 @@ cargo-check-macos:
<<: *docker-env
<<: *test-refs
script:
- - BUILD_DUMMY_WASM_BINARY=1 time cargo check --release
+ - SKIP_WASM_BUILD=1 time cargo check --release
- sccache -s
tags:
- osx
@@ -451,7 +451,7 @@ build-linux-subkey: &build-subkey
- mkdir -p ./artifacts/subkey
script:
- cd ./bin/utils/subkey
- - BUILD_DUMMY_WASM_BINARY=1 time cargo build --release --verbose
+ - SKIP_WASM_BUILD=1 time cargo build --release --verbose
- cd -
- mv ./target/release/subkey ./artifacts/subkey/.
- echo -n "Subkey version = "
@@ -483,7 +483,7 @@ build-rust-doc:
- ./crate-docs/
script:
- rm -f ./crate-docs/index.html # use it as an indicator if the job succeeds
- - BUILD_DUMMY_WASM_BINARY=1 RUSTDOCFLAGS="--html-in-header $(pwd)/.maintain/rustdoc-header.html"
+ - SKIP_WASM_BUILD=1 RUSTDOCFLAGS="--html-in-header $(pwd)/.maintain/rustdoc-header.html"
time cargo +nightly doc --no-deps --workspace --all-features --verbose
- mv ./target/doc ./crate-docs
- echo "" > ./crate-docs/index.html
diff --git a/Cargo.lock b/Cargo.lock
index 42a26e5041213..57ab273804c05 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -636,13 +636,12 @@ checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba"
[[package]]
name = "cargo_metadata"
-version = "0.10.0"
+version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b8de60b887edf6d74370fc8eb177040da4847d971d6234c7b13a6da324ef0caf"
+checksum = "d5a5f7b42f606b7f23674f6f4d877628350682bc40687d3fae65679a58d55345"
dependencies = [
- "semver 0.9.0",
+ "semver 0.11.0",
"serde",
- "serde_derive",
"serde_json",
]
@@ -1783,16 +1782,6 @@ dependencies = [
"winapi 0.3.9",
]
-[[package]]
-name = "fs2"
-version = "0.4.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
-dependencies = [
- "libc",
- "winapi 0.3.9",
-]
-
[[package]]
name = "fs_extra"
version = "1.2.0"
@@ -4043,7 +4032,7 @@ dependencies = [
"sp-transaction-pool",
"sp-version",
"static_assertions",
- "substrate-wasm-builder-runner",
+ "substrate-wasm-builder",
]
[[package]]
@@ -4114,7 +4103,7 @@ dependencies = [
"sp-std",
"sp-transaction-pool",
"sp-version",
- "substrate-wasm-builder-runner",
+ "substrate-wasm-builder",
]
[[package]]
@@ -7321,7 +7310,7 @@ dependencies = [
"sp-sandbox",
"sp-std",
"sp-tasks",
- "substrate-wasm-builder-runner",
+ "substrate-wasm-builder",
]
[[package]]
@@ -7663,7 +7652,7 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a3186ec9e65071a2095434b1f5bb24838d4e8e130f584c790f6033c79943537"
dependencies = [
- "semver-parser",
+ "semver-parser 0.7.0",
]
[[package]]
@@ -7672,7 +7661,16 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
dependencies = [
- "semver-parser",
+ "semver-parser 0.7.0",
+]
+
+[[package]]
+name = "semver"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6"
+dependencies = [
+ "semver-parser 0.10.1",
"serde",
]
@@ -7682,6 +7680,15 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
+[[package]]
+name = "semver-parser"
+version = "0.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "42ef146c2ad5e5f4b037cd6ce2ebb775401729b19a82040c1beac9d36c7d1428"
+dependencies = [
+ "pest",
+]
+
[[package]]
name = "send_wrapper"
version = "0.3.0"
@@ -8496,7 +8503,7 @@ dependencies = [
"sp-io",
"sp-runtime-interface",
"sp-std",
- "substrate-wasm-builder-runner",
+ "substrate-wasm-builder",
]
[[package]]
@@ -8507,7 +8514,7 @@ dependencies = [
"sp-io",
"sp-runtime-interface",
"sp-std",
- "substrate-wasm-builder-runner",
+ "substrate-wasm-builder",
]
[[package]]
@@ -8993,7 +9000,7 @@ dependencies = [
"sp-trie",
"sp-version",
"substrate-test-runtime-client",
- "substrate-wasm-builder-runner",
+ "substrate-wasm-builder",
"trie-db",
]
@@ -9063,24 +9070,18 @@ dependencies = [
[[package]]
name = "substrate-wasm-builder"
-version = "2.0.1"
+version = "3.0.0"
dependencies = [
"ansi_term 0.12.1",
"atty",
"build-helper",
"cargo_metadata",
- "fs2",
- "itertools 0.8.2",
"tempfile",
"toml",
"walkdir",
"wasm-gc-api",
]
-[[package]]
-name = "substrate-wasm-builder-runner"
-version = "2.0.0"
-
[[package]]
name = "subtle"
version = "1.0.0"
diff --git a/Cargo.toml b/Cargo.toml
index df66cab5414bd..2e3cff821e02e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -59,7 +59,6 @@ members = [
"client/transaction-pool",
"client/transaction-pool/graph",
"utils/prometheus",
- "utils/wasm-builder-runner",
"frame/assets",
"frame/aura",
"frame/atomic-swap",
diff --git a/bin/node-template/runtime/Cargo.toml b/bin/node-template/runtime/Cargo.toml
index ed5a114b813f8..f1b15070ddde9 100644
--- a/bin/node-template/runtime/Cargo.toml
+++ b/bin/node-template/runtime/Cargo.toml
@@ -48,7 +48,7 @@ hex-literal = { version = "0.3.1", optional = true }
template = { version = "2.0.0", default-features = false, path = "../pallets/template", package = "pallet-template" }
[build-dependencies]
-wasm-builder-runner = { version = "2.0.0", package = "substrate-wasm-builder-runner", path = "../../../utils/wasm-builder-runner" }
+substrate-wasm-builder = { version = "3.0.0", path = "../../../utils/wasm-builder" }
[features]
default = ["std"]
diff --git a/bin/node-template/runtime/build.rs b/bin/node-template/runtime/build.rs
index 9654139121f6f..9b53d2457dffd 100644
--- a/bin/node-template/runtime/build.rs
+++ b/bin/node-template/runtime/build.rs
@@ -1,9 +1,8 @@
-use wasm_builder_runner::WasmBuilder;
+use substrate_wasm_builder::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
- .with_wasm_builder_from_crates("2.0.1")
.export_heap_base()
.import_memory()
.build()
diff --git a/bin/node/runtime/Cargo.toml b/bin/node/runtime/Cargo.toml
index 2bad2db510be4..eabc9f61c62e5 100644
--- a/bin/node/runtime/Cargo.toml
+++ b/bin/node/runtime/Cargo.toml
@@ -79,7 +79,7 @@ pallet-transaction-payment-rpc-runtime-api = { version = "2.0.0", default-featur
pallet-vesting = { version = "2.0.0", default-features = false, path = "../../../frame/vesting" }
[build-dependencies]
-wasm-builder-runner = { version = "2.0.0", package = "substrate-wasm-builder-runner", path = "../../../utils/wasm-builder-runner" }
+substrate-wasm-builder = { version = "3.0.0", path = "../../../utils/wasm-builder" }
[dev-dependencies]
sp-io = { version = "2.0.0", path = "../../../primitives/io" }
diff --git a/bin/node/runtime/build.rs b/bin/node/runtime/build.rs
index 4f111bc993007..8a0b4d7a0c157 100644
--- a/bin/node/runtime/build.rs
+++ b/bin/node/runtime/build.rs
@@ -15,12 +15,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-use wasm_builder_runner::WasmBuilder;
+use substrate_wasm_builder::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
- .with_wasm_builder_from_crates_or_path("2.0.1", "../../../utils/wasm-builder")
.export_heap_base()
.import_memory()
.build()
diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs
index 3e08b2cf8a6f8..958d2fe19e03b 100644
--- a/bin/node/runtime/src/lib.rs
+++ b/bin/node/runtime/src/lib.rs
@@ -91,11 +91,11 @@ use sp_runtime::generic::Era;
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
+/// Wasm binary unwrapped. If built with `SKIP_WASM_BUILD`, the function panics.
#[cfg(feature = "std")]
-/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics.
pub fn wasm_binary_unwrap() -> &'static [u8] {
WASM_BINARY.expect("Development wasm binary is not available. This means the client is \
- built with `BUILD_DUMMY_WASM_BINARY` flag and it is only usable for \
+ built with `SKIP_WASM_BUILD` flag and it is only usable for \
production chains. Please rebuild with the flag disabled.")
}
diff --git a/client/executor/runtime-test/Cargo.toml b/client/executor/runtime-test/Cargo.toml
index ba23e31febee5..1a898b92ca9ab 100644
--- a/client/executor/runtime-test/Cargo.toml
+++ b/client/executor/runtime-test/Cargo.toml
@@ -22,7 +22,7 @@ sp-std = { version = "2.0.0", default-features = false, path = "../../../primiti
sp-tasks = { version = "2.0.0", default-features = false, path = "../../../primitives/tasks" }
[build-dependencies]
-wasm-builder-runner = { version = "2.0.0", package = "substrate-wasm-builder-runner", path = "../../../utils/wasm-builder-runner" }
+substrate-wasm-builder = { version = "3.0.0", path = "../../../utils/wasm-builder" }
[features]
default = [ "std" ]
diff --git a/client/executor/runtime-test/build.rs b/client/executor/runtime-test/build.rs
index bc07db900c31e..a83de21db7f0f 100644
--- a/client/executor/runtime-test/build.rs
+++ b/client/executor/runtime-test/build.rs
@@ -14,13 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see .
-use wasm_builder_runner::WasmBuilder;
+use substrate_wasm_builder::WasmBuilder;
fn main() {
// regular build
WasmBuilder::new()
.with_current_project()
- .with_wasm_builder_from_crates_or_path("2.0.1", "../../../utils/wasm-builder")
.export_heap_base()
.import_memory()
.build();
@@ -28,10 +27,9 @@ fn main() {
// and building with tracing activated
WasmBuilder::new()
.with_current_project()
- .with_wasm_builder_from_crates_or_path("2.0.1", "../../../utils/wasm-builder")
.export_heap_base()
.import_memory()
.set_file_name("wasm_binary_with_tracing.rs")
- .append_to_rust_flags("--cfg feature=\\\"with-tracing\\\"")
+ .append_to_rust_flags(r#"--cfg feature="with-tracing""#)
.build();
}
diff --git a/client/executor/runtime-test/src/lib.rs b/client/executor/runtime-test/src/lib.rs
index 404530c1c3ebf..f4cef65b629aa 100644
--- a/client/executor/runtime-test/src/lib.rs
+++ b/client/executor/runtime-test/src/lib.rs
@@ -4,8 +4,8 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
+/// Wasm binary unwrapped. If built with `SKIP_WASM_BUILD`, the function panics.
#[cfg(feature = "std")]
-/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics.
pub fn wasm_binary_unwrap() -> &'static [u8] {
WASM_BINARY.expect("Development wasm binary is not available. Testing is only \
supported with the flag disabled.")
diff --git a/docs/README.adoc b/docs/README.adoc
index 7f3d50faac7d6..71052420b1aa9 100644
--- a/docs/README.adoc
+++ b/docs/README.adoc
@@ -308,28 +308,6 @@ cargo run --release \-- \
Additional Substrate CLI usage options are available and may be shown by running `cargo run \-- --help`.
-=== WASM binaries
-
-The WASM binaries are built during the normal `cargo build` process. To control the WASM binary building,
-we support multiple environment variables:
-
-* `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
- for `cargo check` runs.
-* `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.
-* `FORCE_WASM_BUILD` - Can be set to force 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_TARGET_DIRECTORY` - Will copy release build WASM binary to the given directory. The path needs
- to be absolute.
-* `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.
-
-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
-be `NODE_RUNTIME`.
-
[[flaming-fir]]
=== Joining the Flaming Fir Testnet
diff --git a/frame/support/test/tests/construct_runtime_ui.rs b/frame/support/test/tests/construct_runtime_ui.rs
index e1624c76830ae..83a90c96dd624 100644
--- a/frame/support/test/tests/construct_runtime_ui.rs
+++ b/frame/support/test/tests/construct_runtime_ui.rs
@@ -21,7 +21,7 @@ use std::env;
#[test]
fn ui() {
// As trybuild is using `cargo check`, we don't need the real WASM binaries.
- env::set_var("BUILD_DUMMY_WASM_BINARY", "1");
+ env::set_var("SKIP_WASM_BUILD", "1");
let t = trybuild::TestCases::new();
t.compile_fail("tests/construct_runtime_ui/*.rs");
diff --git a/frame/support/test/tests/decl_module_ui.rs b/frame/support/test/tests/decl_module_ui.rs
index 7df64bc52f412..22237d904aeac 100644
--- a/frame/support/test/tests/decl_module_ui.rs
+++ b/frame/support/test/tests/decl_module_ui.rs
@@ -19,7 +19,7 @@
#[test]
fn decl_module_ui() {
// As trybuild is using `cargo check`, we don't need the real WASM binaries.
- std::env::set_var("BUILD_DUMMY_WASM_BINARY", "1");
+ std::env::set_var("SKIP_WASM_BUILD", "1");
let t = trybuild::TestCases::new();
t.compile_fail("tests/decl_module_ui/*.rs");
diff --git a/frame/support/test/tests/decl_storage_ui.rs b/frame/support/test/tests/decl_storage_ui.rs
index 56529d62c28ff..4b082cb8172a7 100644
--- a/frame/support/test/tests/decl_storage_ui.rs
+++ b/frame/support/test/tests/decl_storage_ui.rs
@@ -19,7 +19,7 @@
#[test]
fn decl_storage_ui() {
// As trybuild is using `cargo check`, we don't need the real WASM binaries.
- std::env::set_var("BUILD_DUMMY_WASM_BINARY", "1");
+ std::env::set_var("SKIP_WASM_BUILD", "1");
let t = trybuild::TestCases::new();
t.compile_fail("tests/decl_storage_ui/*.rs");
diff --git a/frame/support/test/tests/derive_no_bound_ui.rs b/frame/support/test/tests/derive_no_bound_ui.rs
index da276018f7f8e..ba8fff1f3a5c5 100644
--- a/frame/support/test/tests/derive_no_bound_ui.rs
+++ b/frame/support/test/tests/derive_no_bound_ui.rs
@@ -19,7 +19,7 @@
#[test]
fn derive_no_bound_ui() {
// As trybuild is using `cargo check`, we don't need the real WASM binaries.
- std::env::set_var("BUILD_DUMMY_WASM_BINARY", "1");
+ std::env::set_var("SKIP_WASM_BUILD", "1");
let t = trybuild::TestCases::new();
t.compile_fail("tests/derive_no_bound_ui/*.rs");
diff --git a/frame/support/test/tests/reserved_keyword.rs b/frame/support/test/tests/reserved_keyword.rs
index 382b2e498741f..8136d11824ace 100644
--- a/frame/support/test/tests/reserved_keyword.rs
+++ b/frame/support/test/tests/reserved_keyword.rs
@@ -19,7 +19,7 @@
#[test]
fn reserved_keyword() {
// As trybuild is using `cargo check`, we don't need the real WASM binaries.
- std::env::set_var("BUILD_DUMMY_WASM_BINARY", "1");
+ std::env::set_var("SKIP_WASM_BUILD", "1");
let t = trybuild::TestCases::new();
t.compile_fail("tests/reserved_keyword/*.rs");
diff --git a/primitives/api/test/tests/trybuild.rs b/primitives/api/test/tests/trybuild.rs
index 2f7fd6d06bcd3..f23c7291e8ef7 100644
--- a/primitives/api/test/tests/trybuild.rs
+++ b/primitives/api/test/tests/trybuild.rs
@@ -21,7 +21,7 @@ use std::env;
#[test]
fn ui() {
// As trybuild is using `cargo check`, we don't need the real WASM binaries.
- env::set_var("BUILD_DUMMY_WASM_BINARY", "1");
+ env::set_var("SKIP_WASM_BUILD", "1");
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/*.rs");
diff --git a/primitives/runtime-interface/test-wasm-deprecated/Cargo.toml b/primitives/runtime-interface/test-wasm-deprecated/Cargo.toml
index 59790eb172eb3..eba557de5dbab 100644
--- a/primitives/runtime-interface/test-wasm-deprecated/Cargo.toml
+++ b/primitives/runtime-interface/test-wasm-deprecated/Cargo.toml
@@ -19,7 +19,7 @@ sp-io = { version = "2.0.0", default-features = false, path = "../../io" }
sp-core = { version = "2.0.0", default-features = false, path = "../../core" }
[build-dependencies]
-wasm-builder-runner = { version = "2.0.0", package = "substrate-wasm-builder-runner", path = "../../../utils/wasm-builder-runner" }
+substrate-wasm-builder = { version = "3.0.0", path = "../../../utils/wasm-builder" }
[features]
default = [ "std" ]
diff --git a/primitives/runtime-interface/test-wasm-deprecated/build.rs b/primitives/runtime-interface/test-wasm-deprecated/build.rs
index 4f111bc993007..8a0b4d7a0c157 100644
--- a/primitives/runtime-interface/test-wasm-deprecated/build.rs
+++ b/primitives/runtime-interface/test-wasm-deprecated/build.rs
@@ -15,12 +15,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-use wasm_builder_runner::WasmBuilder;
+use substrate_wasm_builder::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
- .with_wasm_builder_from_crates_or_path("2.0.1", "../../../utils/wasm-builder")
.export_heap_base()
.import_memory()
.build()
diff --git a/primitives/runtime-interface/test-wasm-deprecated/src/lib.rs b/primitives/runtime-interface/test-wasm-deprecated/src/lib.rs
index 174cdb8cdf85a..ae0697b2938f4 100644
--- a/primitives/runtime-interface/test-wasm-deprecated/src/lib.rs
+++ b/primitives/runtime-interface/test-wasm-deprecated/src/lib.rs
@@ -26,8 +26,8 @@ use sp_runtime_interface::runtime_interface;
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
+/// Wasm binary unwrapped. If built with `SKIP_WASM_BUILD`, the function panics.
#[cfg(feature = "std")]
-/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics.
pub fn wasm_binary_unwrap() -> &'static [u8] {
WASM_BINARY.expect("Development wasm binary is not available. Testing is only \
supported with the flag disabled.")
diff --git a/primitives/runtime-interface/test-wasm/Cargo.toml b/primitives/runtime-interface/test-wasm/Cargo.toml
index 39c8df976a5ba..3cf36f95145e6 100644
--- a/primitives/runtime-interface/test-wasm/Cargo.toml
+++ b/primitives/runtime-interface/test-wasm/Cargo.toml
@@ -19,7 +19,7 @@ sp-io = { version = "2.0.0", default-features = false, path = "../../io" }
sp-core = { version = "2.0.0", default-features = false, path = "../../core" }
[build-dependencies]
-wasm-builder-runner = { version = "2.0.0", package = "substrate-wasm-builder-runner", path = "../../../utils/wasm-builder-runner" }
+substrate-wasm-builder = { version = "3.0.0", path = "../../../utils/wasm-builder" }
[features]
default = [ "std" ]
diff --git a/primitives/runtime-interface/test-wasm/build.rs b/primitives/runtime-interface/test-wasm/build.rs
index 4f111bc993007..8a0b4d7a0c157 100644
--- a/primitives/runtime-interface/test-wasm/build.rs
+++ b/primitives/runtime-interface/test-wasm/build.rs
@@ -15,12 +15,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-use wasm_builder_runner::WasmBuilder;
+use substrate_wasm_builder::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
- .with_wasm_builder_from_crates_or_path("2.0.1", "../../../utils/wasm-builder")
.export_heap_base()
.import_memory()
.build()
diff --git a/primitives/runtime-interface/test-wasm/src/lib.rs b/primitives/runtime-interface/test-wasm/src/lib.rs
index 28895df2214d1..6cd37a6c1d142 100644
--- a/primitives/runtime-interface/test-wasm/src/lib.rs
+++ b/primitives/runtime-interface/test-wasm/src/lib.rs
@@ -30,8 +30,8 @@ use sp_core::{sr25519::Public, wasm_export_functions};
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
+/// Wasm binary unwrapped. If built with `SKIP_WASM_BUILD`, the function panics.
#[cfg(feature = "std")]
-/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics.
pub fn wasm_binary_unwrap() -> &'static [u8] {
WASM_BINARY.expect("Development wasm binary is not available. Testing is only \
supported with the flag disabled.")
diff --git a/primitives/runtime-interface/tests/ui.rs b/primitives/runtime-interface/tests/ui.rs
index 2f7fd6d06bcd3..f23c7291e8ef7 100644
--- a/primitives/runtime-interface/tests/ui.rs
+++ b/primitives/runtime-interface/tests/ui.rs
@@ -21,7 +21,7 @@ use std::env;
#[test]
fn ui() {
// As trybuild is using `cargo check`, we don't need the real WASM binaries.
- env::set_var("BUILD_DUMMY_WASM_BINARY", "1");
+ env::set_var("SKIP_WASM_BUILD", "1");
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/*.rs");
diff --git a/test-utils/runtime/Cargo.toml b/test-utils/runtime/Cargo.toml
index cb6147adf25c6..cf1a4bcddd5b3 100644
--- a/test-utils/runtime/Cargo.toml
+++ b/test-utils/runtime/Cargo.toml
@@ -56,7 +56,7 @@ sc-executor = { version = "0.8.0", path = "../../client/executor" }
substrate-test-runtime-client = { version = "2.0.0", path = "./client" }
[build-dependencies]
-wasm-builder-runner = { version = "2.0.0", package = "substrate-wasm-builder-runner", path = "../../utils/wasm-builder-runner" }
+substrate-wasm-builder = { version = "3.0.0", path = "../../utils/wasm-builder" }
[features]
default = [
diff --git a/test-utils/runtime/build.rs b/test-utils/runtime/build.rs
index 834551a7ba12d..5c9af20528a0b 100644
--- a/test-utils/runtime/build.rs
+++ b/test-utils/runtime/build.rs
@@ -15,12 +15,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-use wasm_builder_runner::WasmBuilder;
+use substrate_wasm_builder::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
- .with_wasm_builder_from_crates_or_path("2.0.1", "../../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/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs
index e772a28ee33a2..2024467771057 100644
--- a/test-utils/runtime/src/lib.rs
+++ b/test-utils/runtime/src/lib.rs
@@ -66,8 +66,8 @@ pub type AuraId = sp_consensus_aura::sr25519::AuthorityId;
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
+/// Wasm binary unwrapped. If built with `SKIP_WASM_BUILD`, the function panics.
#[cfg(feature = "std")]
-/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics.
pub fn wasm_binary_unwrap() -> &'static [u8] {
WASM_BINARY.expect("Development wasm binary is not available. Testing is only \
supported with the flag disabled.")
diff --git a/utils/wasm-builder-runner/Cargo.toml b/utils/wasm-builder-runner/Cargo.toml
deleted file mode 100644
index 2c54a5ec3a4d6..0000000000000
--- a/utils/wasm-builder-runner/Cargo.toml
+++ /dev/null
@@ -1,15 +0,0 @@
-[package]
-name = "substrate-wasm-builder-runner"
-version = "2.0.0"
-authors = ["Parity Technologies "]
-description = "Runner for substrate-wasm-builder"
-edition = "2018"
-readme = "README.md"
-repository = "https://github.com/paritytech/substrate/"
-license = "Apache-2.0"
-homepage = "https://substrate.dev"
-
-[package.metadata.docs.rs]
-targets = ["x86_64-unknown-linux-gnu"]
-
-[dependencies]
diff --git a/utils/wasm-builder-runner/README.md b/utils/wasm-builder-runner/README.md
deleted file mode 100644
index 1b9e2b08ca444..0000000000000
--- a/utils/wasm-builder-runner/README.md
+++ /dev/null
@@ -1,12 +0,0 @@
-## WASM builder runner
-
-Since cargo contains many bugs when it comes to correct dependency and feature
-resolution, we need this little tool. See for
-more information.
-
-It will create a project that will call `substrate-wasm-builder` to prevent any dependencies
-from `substrate-wasm-builder` influencing the main project's dependencies.
-
-For more information see
-
-License: GPL-3.0
diff --git a/utils/wasm-builder-runner/src/lib.rs b/utils/wasm-builder-runner/src/lib.rs
deleted file mode 100644
index 04e06495c69b4..0000000000000
--- a/utils/wasm-builder-runner/src/lib.rs
+++ /dev/null
@@ -1,498 +0,0 @@
-// This file is part of Substrate.
-
-// Copyright (C) 2019-2020 Parity Technologies (UK) Ltd.
-// SPDX-License-Identifier: Apache-2.0
-
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-//! # WASM builder runner
-//!
-//! Since cargo contains many bugs when it comes to correct dependency and feature
-//! resolution, we need this little tool. See for
-//! more information.
-//!
-//! It will create a project that will call `substrate-wasm-builder` to prevent any dependencies
-//! from `substrate-wasm-builder` influencing the main project's dependencies.
-//!
-//! For more information see
-
-use std::{
- env, process::{Command, self}, fs, path::{PathBuf, Path}, hash::{Hash, Hasher},
- collections::hash_map::DefaultHasher,
-};
-
-/// Environment variable that tells us to skip building the WASM binary.
-const SKIP_BUILD_ENV: &str = "SKIP_WASM_BUILD";
-
-/// Environment variable that tells us to create a dummy WASM binary.
-///
-/// This is useful for `cargo check` to speed-up the compilation.
-///
-/// # Caution
-///
-/// Enabling this option will just provide `&[]` as WASM binary.
-const DUMMY_WASM_BINARY_ENV: &str = "BUILD_DUMMY_WASM_BINARY";
-
-/// Environment variable that makes sure the WASM build is triggered.
-const FORCE_WASM_BUILD_ENV: &str = "FORCE_WASM_BUILD";
-
-/// Replace all backslashes with slashes.
-fn replace_back_slashes(path: T) -> String {
- path.to_string().replace("\\", "/")
-}
-
-/// Returns the manifest dir from the `CARGO_MANIFEST_DIR` env.
-fn get_manifest_dir() -> PathBuf {
- env::var("CARGO_MANIFEST_DIR")
- .expect("`CARGO_MANIFEST_DIR` is always set for `build.rs` files; qed")
- .into()
-}
-
-/// First step of the [`WasmBuilder`] to select the project to build.
-pub struct WasmBuilderSelectProject {
- /// This parameter just exists to make it impossible to construct
- /// this type outside of this crate.
- _ignore: (),
-}
-
-impl WasmBuilderSelectProject {
- /// Use the current project as project for building the WASM binary.
- ///
- /// # Panics
- ///
- /// Panics if the `CARGO_MANIFEST_DIR` variable is not set. This variable
- /// is always set by `Cargo` in `build.rs` files.
- pub fn with_current_project(self) -> WasmBuilderSelectSource {
- WasmBuilderSelectSource(get_manifest_dir().join("Cargo.toml"))
- }
-
- /// Use the given `path` as project for building the WASM binary.
- ///
- /// Returns an error if the given `path` does not points to a `Cargo.toml`.
- pub fn with_project(
- self,
- path: impl Into,
- ) -> Result {
- let path = path.into();
-
- if path.ends_with("Cargo.toml") {
- Ok(WasmBuilderSelectSource(path))
- } else {
- Err("Project path must point to the `Cargo.toml` of the project")
- }
- }
-}
-
-/// Second step of the [`WasmBuilder`] to set the source of the `wasm-builder`.
-pub struct WasmBuilderSelectSource(PathBuf);
-
-impl WasmBuilderSelectSource {
- /// Use the given `path` as source for `wasm-builder`.
- ///
- /// The `path` must be relative and point to the directory that contains the `Cargo.toml` for
- /// `wasm-builder`.
- pub fn with_wasm_builder_from_path(self, path: &'static str) -> WasmBuilder {
- WasmBuilder {
- source: WasmBuilderSource::Path(path),
- rust_flags: Vec::new(),
- file_name: None,
- project_cargo_toml: self.0,
- }
- }
-
- /// Use the given `repo` and `rev` as source for `wasm-builder`.
- pub fn with_wasm_builder_from_git(self, repo: &'static str, rev: &'static str) -> WasmBuilder {
- WasmBuilder {
- source: WasmBuilderSource::Git { repo, rev },
- rust_flags: Vec::new(),
- file_name: None,
- project_cargo_toml: self.0,
- }
- }
-
- /// Use the given `version` to fetch `wasm-builder` source from crates.io.
- pub fn with_wasm_builder_from_crates(self, version: &'static str) -> WasmBuilder {
- WasmBuilder {
- source: WasmBuilderSource::Crates(version),
- rust_flags: Vec::new(),
- file_name: None,
- project_cargo_toml: self.0,
- }
- }
-
- /// Use the given `version` to fetch `wasm-builder` source from crates.io or use
- /// the given `path` as source.
- ///
- /// The `path` must be relative and point to the directory that contains the `Cargo.toml` for
- /// `wasm-builder`.
- pub fn with_wasm_builder_from_crates_or_path(
- self,
- version: &'static str,
- path: &'static str,
- ) -> WasmBuilder {
- WasmBuilder {
- source: WasmBuilderSource::CratesOrPath { version, path },
- rust_flags: Vec::new(),
- file_name: None,
- project_cargo_toml: self.0,
- }
- }
-
- /// Use the given `source` as source for `wasm-builder`.
- pub fn with_wasm_builder_source(self, source: WasmBuilderSource) -> WasmBuilder {
- WasmBuilder {
- source,
- rust_flags: Vec::new(),
- file_name: None,
- project_cargo_toml: self.0,
- }
- }
-}
-
-/// The builder for building a wasm binary.
-///
-/// The builder itself is seperated into multiple structs to make the setup type safe.
-///
-/// Building a wasm binary:
-///
-/// 1. Call [`WasmBuilder::new`] to create a new builder.
-/// 2. Select the project to build using the methods of [`WasmBuilderSelectProject`].
-/// 3. Select the source of the `wasm-builder` crate using the methods of
-/// [`WasmBuilderSelectSource`].
-/// 4. Set additional `RUST_FLAGS` or a different name for the file containing the WASM code
-/// using methods of [`WasmBuilder`].
-/// 5. Build the WASM binary using [`Self::build`].
-pub struct WasmBuilder {
- /// Where should we pull the `wasm-builder` crate from.
- source: WasmBuilderSource,
- /// Flags that should be appended to `RUST_FLAGS` env variable.
- rust_flags: Vec,
- /// The name of the file that is being generated in `OUT_DIR`.
- ///
- /// Defaults to `wasm_binary.rs`.
- file_name: Option,
- /// The path to the `Cargo.toml` of the project that should be build
- /// for wasm.
- project_cargo_toml: PathBuf,
-}
-
-impl WasmBuilder {
- /// Create a new instance of the builder.
- pub fn new() -> WasmBuilderSelectProject {
- WasmBuilderSelectProject {
- _ignore: (),
- }
- }
-
- /// Enable exporting `__heap_base` as global variable in the WASM binary.
- ///
- /// This adds `-Clink-arg=--export=__heap_base` to `RUST_FLAGS`.
- pub fn export_heap_base(mut self) -> Self {
- self.rust_flags.push("-Clink-arg=--export=__heap_base".into());
- self
- }
-
- /// Set the name of the file that will be generated in `OUT_DIR`.
- ///
- /// This file needs to be included to get access to the build WASM binary.
- ///
- /// If this function is not called, `file_name` defaults to `wasm_binary.rs`
- pub fn set_file_name(mut self, file_name: impl Into) -> Self {
- self.file_name = Some(file_name.into());
- self
- }
-
- /// Instruct the linker to import the memory into the WASM binary.
- ///
- /// This adds `-C link-arg=--import-memory` to `RUST_FLAGS`.
- pub fn import_memory(mut self) -> Self {
- self.rust_flags.push("-C link-arg=--import-memory".into());
- self
- }
-
- /// Append the given `flag` to `RUST_FLAGS`.
- ///
- /// `flag` is appended as is, so it needs to be a valid flag.
- pub fn append_to_rust_flags(mut self, flag: impl Into) -> Self {
- self.rust_flags.push(flag.into());
- self
- }
-
- /// Build the WASM binary.
- pub fn build(self) {
- let out_dir = PathBuf::from(env::var("OUT_DIR").expect("`OUT_DIR` is set by cargo!"));
- let file_path = out_dir.join(self.file_name.unwrap_or_else(|| "wasm_binary.rs".into()));
-
- if check_skip_build() {
- // If we skip the build, we still want to make sure to be called when an env variable
- // changes
- generate_rerun_if_changed_instructions();
-
- provide_dummy_wasm_binary(&file_path, true);
-
- return;
- }
-
- // Hash the path to the project cargo toml.
- let mut hasher = DefaultHasher::new();
- self.project_cargo_toml.hash(&mut hasher);
-
- let project_name = env::var("CARGO_PKG_NAME").expect("`CARGO_PKG_NAME` is set by cargo!");
- // Make sure the `wasm-builder-runner` path is unique by concatenating the name of the
- // project that is compiling the WASM binary with the hash of the path to the project that
- // should be compiled as WASM binary.
- let project_folder = get_workspace_root()
- .join(format!("{}{}", project_name, hasher.finish()));
-
- if check_provide_dummy_wasm_binary() {
- provide_dummy_wasm_binary(&file_path, false);
- } else {
- create_project(
- &project_folder,
- &file_path,
- self.source,
- &self.project_cargo_toml,
- &self.rust_flags.into_iter().map(|f| format!("{} ", f)).collect::(),
- );
- run_project(&project_folder);
- }
-
- // As last step we need to generate our `rerun-if-changed` stuff. If a build fails, we don't
- // want to spam the output!
- generate_rerun_if_changed_instructions();
- }
-}
-
-/// The `wasm-builder` dependency source.
-pub enum WasmBuilderSource {
- /// The relative path to the source code from the current manifest dir.
- Path(&'static str),
- /// The git repository that contains the source code.
- Git {
- repo: &'static str,
- rev: &'static str,
- },
- /// Use the given version released on crates.io.
- Crates(&'static str),
- /// Use the given version released on crates.io or from the given path.
- CratesOrPath {
- version: &'static str,
- path: &'static str,
- }
-}
-
-impl WasmBuilderSource {
- /// Convert to a valid cargo source declaration.
- ///
- /// `absolute_path` - The manifest dir.
- fn to_cargo_source(&self, manifest_dir: &Path) -> String {
- match self {
- WasmBuilderSource::Path(path) => {
- replace_back_slashes(format!("path = \"{}\"", manifest_dir.join(path).display()))
- }
- WasmBuilderSource::Git { repo, rev } => {
- format!("git = \"{}\", rev=\"{}\"", repo, rev)
- }
- WasmBuilderSource::Crates(version) => {
- format!("version = \"{}\"", version)
- }
- WasmBuilderSource::CratesOrPath { version, path } => {
- replace_back_slashes(
- format!(
- "path = \"{}\", version = \"{}\"",
- manifest_dir.join(path).display(),
- version
- )
- )
- }
- }
- }
-}
-
-/// Build the currently built project as WASM binary and extend `RUSTFLAGS` with the given rustflags.
-///
-/// For more information, see [`build_current_project`].
-#[deprecated(
- since = "1.0.5",
- note = "Please switch to [`WasmBuilder`]",
-)]
-pub fn build_current_project_with_rustflags(
- file_name: &str,
- wasm_builder_source: WasmBuilderSource,
- default_rust_flags: &str,
-) {
- WasmBuilder::new()
- .with_current_project()
- .with_wasm_builder_source(wasm_builder_source)
- .append_to_rust_flags(default_rust_flags)
- .set_file_name(file_name)
- .build()
-}
-
-/// Build the currently built project as WASM binary.
-///
-/// The current project is determined using the `CARGO_MANIFEST_DIR` environment variable.
-///
-/// `file_name` - The name of the file being generated in the `OUT_DIR`. The file contains the
-/// constant `WASM_BINARY` which contains the build wasm binary.
-/// `wasm_builder_path` - Path to the wasm-builder project, relative to `CARGO_MANIFEST_DIR`.
-#[deprecated(
- since = "1.0.5",
- note = "Please switch to [`WasmBuilder`]",
-)]
-pub fn build_current_project(file_name: &str, wasm_builder_source: WasmBuilderSource) {
- #[allow(deprecated)]
- build_current_project_with_rustflags(file_name, wasm_builder_source, "");
-}
-
-/// Returns the root path of the wasm-builder workspace.
-///
-/// The wasm-builder workspace contains all wasm-builder's projects.
-fn get_workspace_root() -> PathBuf {
- let out_dir_env = env::var("OUT_DIR").expect("`OUT_DIR` is set by cargo!");
- let mut out_dir = PathBuf::from(&out_dir_env);
-
- loop {
- match out_dir.parent() {
- Some(parent) if out_dir.ends_with("build") => return parent.join("wbuild-runner"),
- _ => if !out_dir.pop() {
- break;
- }
- }
- }
-
- panic!("Could not find target dir in: {}", out_dir_env)
-}
-
-fn create_project(
- project_folder: &Path,
- file_path: &Path,
- wasm_builder_source: WasmBuilderSource,
- cargo_toml_path: &Path,
- default_rustflags: &str,
-) {
- fs::create_dir_all(project_folder.join("src"))
- .expect("WASM build runner dir create can not fail; qed");
-
- write_file_if_changed(
- project_folder.join("Cargo.toml"),
- format!(
- r#"
- [package]
- name = "wasm-build-runner-impl"
- version = "1.0.0"
- edition = "2018"
-
- [dependencies]
- substrate-wasm-builder = {{ {wasm_builder_source} }}
-
- [workspace]
- "#,
- wasm_builder_source = wasm_builder_source.to_cargo_source(&get_manifest_dir()),
- ),
- );
-
- write_file_if_changed(
- project_folder.join("src/main.rs"),
- format!(
- r#"
- //! This is automatically generated code by `substrate-wasm-builder`.
-
- use substrate_wasm_builder::build_project_with_default_rustflags;
-
- fn main() {{
- build_project_with_default_rustflags(
- "{file_path}",
- "{cargo_toml_path}",
- "{default_rustflags}",
- )
- }}
- "#,
- file_path = replace_back_slashes(file_path.display()),
- cargo_toml_path = replace_back_slashes(cargo_toml_path.display()),
- default_rustflags = default_rustflags,
- ),
- );
-}
-
-fn run_project(project_folder: &Path) {
- let cargo = env::var("CARGO").expect("`CARGO` env variable is always set when executing `build.rs`.");
- let mut cmd = Command::new(cargo);
- cmd.arg("run").arg(format!("--manifest-path={}", project_folder.join("Cargo.toml").display()));
-
- if env::var("DEBUG") != Ok(String::from("true")) {
- cmd.arg("--release");
- }
-
- // Make sure we always run the `wasm-builder` project for the `HOST` architecture.
- let host_triple = env::var("HOST").expect("`HOST` is always set when executing `build.rs`.");
- cmd.arg(&format!("--target={}", host_triple));
-
- // Unset the `CARGO_TARGET_DIR` to prevent a cargo deadlock (cargo locks a target dir exclusive).
- // The runner project is created in `CARGO_TARGET_DIR` and executing it will create a sub target
- // directory inside of `CARGO_TARGET_DIR`.
- cmd.env_remove("CARGO_TARGET_DIR");
-
- if !cmd.status().map(|s| s.success()).unwrap_or(false) {
- // Don't spam the output with backtraces when a build failed!
- process::exit(1);
- }
-}
-
-/// Generate the name of the skip build environment variable for the current crate.
-fn generate_crate_skip_build_env_name() -> String {
- format!(
- "SKIP_{}_WASM_BUILD",
- env::var("CARGO_PKG_NAME").expect("Package name is set").to_uppercase().replace('-', "_"),
- )
-}
-
-/// Checks if the build of the WASM binary should be skipped.
-fn check_skip_build() -> bool {
- env::var(SKIP_BUILD_ENV).is_ok() || env::var(generate_crate_skip_build_env_name()).is_ok()
-}
-
-/// Check if we should provide a dummy WASM binary.
-fn check_provide_dummy_wasm_binary() -> bool {
- env::var(DUMMY_WASM_BINARY_ENV).is_ok()
-}
-
-/// Provide the dummy WASM binary
-///
-/// If `skip_build` is `true`, it will only generate the wasm binary if it doesn't exist.
-fn provide_dummy_wasm_binary(file_path: &Path, skip_build: bool) {
- if !skip_build || !file_path.exists() {
- write_file_if_changed(
- file_path.into(),
- "pub const WASM_BINARY: Option<&[u8]> = None;\
- pub const WASM_BINARY_BLOATY: Option<&[u8]> = None;".into(),
- );
- }
-}
-
-/// Generate the `rerun-if-changed` instructions for cargo to make sure that the WASM binary is
-/// rebuilt when needed.
-fn generate_rerun_if_changed_instructions() {
- // Make sure that the `build.rs` is called again if one of the following env variables changes.
- println!("cargo:rerun-if-env-changed={}", SKIP_BUILD_ENV);
- println!("cargo:rerun-if-env-changed={}", DUMMY_WASM_BINARY_ENV);
- println!("cargo:rerun-if-env-changed={}", FORCE_WASM_BUILD_ENV);
- println!("cargo:rerun-if-env-changed={}", generate_crate_skip_build_env_name());
-}
-
-/// Write to the given `file` if the `content` is different.
-fn write_file_if_changed(file: PathBuf, content: String) {
- if fs::read_to_string(&file).ok().as_ref() != Some(&content) {
- fs::write(&file, content).unwrap_or_else(|_| panic!("Writing `{}` can not fail!", file.display()));
- }
-}
diff --git a/utils/wasm-builder/Cargo.toml b/utils/wasm-builder/Cargo.toml
index e9dd1a97b89e4..199e26b509e2e 100644
--- a/utils/wasm-builder/Cargo.toml
+++ b/utils/wasm-builder/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "substrate-wasm-builder"
-version = "2.0.1"
+version = "3.0.0"
authors = ["Parity Technologies "]
description = "Utility for building WASM binaries"
edition = "2018"
@@ -14,12 +14,10 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
build-helper = "0.1.1"
-cargo_metadata = "0.10.0"
+cargo_metadata = "0.12.0"
tempfile = "3.1.0"
toml = "0.5.4"
walkdir = "2.3.1"
-fs2 = "0.4.3"
wasm-gc-api = "0.1.11"
atty = "0.2.13"
-itertools = "0.8.2"
ansi_term = "0.12.1"
diff --git a/utils/wasm-builder/README.md b/utils/wasm-builder/README.md
index 1e24d2cebab32..3868faf1acab5 100644
--- a/utils/wasm-builder/README.md
+++ b/utils/wasm-builder/README.md
@@ -8,20 +8,23 @@ The Wasm builder is a tool that integrates the process of building the WASM bina
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`.
+2. Add `wasm-builder` as dependency into `build-dependencies`.
The `build.rs` file needs to contain the following code:
```rust
-use wasm_builder_runner::{build_current_project, WasmBuilderSource};
+use substrate_wasm_builder::WasmBuilder;
fn main() {
- build_current_project(
- // The name of the file being generated in out-dir.
- "wasm_binary.rs",
- // How to include wasm-builder, in this case from crates.io.
- WasmBuilderSource::Crates("1.0.0"),
- );
+ 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()
}
```
@@ -32,9 +35,10 @@ 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.
+The former is a compact Wasm binary and the latter is the Wasm binary as being generated by the compiler.
+Both variables have `Option<&'static [u8]>` as type.
-### Feature
+### Features
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
@@ -46,19 +50,19 @@ Wasm binary. If this feature is not present, it will not be enabled.
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
- for `cargo check` runs.
-- `WASM_BUILD_TYPE` - Sets the build type for building wasm binaries. Supported values are `release` or `debug`.
+- `SKIP_WASM_BUILD` - Skips building any Wasm binary. This is useful when only native should be recompiled.
+ If this is the first run and there doesn't exist a Wasm binary, this will set both
+ variables to `None`.
+- `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.
-- `FORCE_WASM_BUILD` - Can be set to force a wasm build. On subsequent calls the value of the variable
- needs to change. As wasm builder instructs `cargo` to watch for file changes
+- `FORCE_WASM_BUILD` - Can be set to force 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.
- `WASM_BUILD_NO_COLOR` - Disable color output of the wasm build.
-- `WASM_TARGET_DIRECTORY` - Will copy any build wasm binary to the given directory. The path needs
+- `WASM_TARGET_DIRECTORY` - Will copy any build Wasm binary to the given directory. The path needs
to be absolute.
-- `WASM_BUILD_TOOLCHAIN` - The toolchain that should be used to build the wasm binaries. The
+- `WASM_BUILD_TOOLCHAIN` - The toolchain that should be used to build the Wasm binaries. The
format needs to be the same as used by cargo, e.g. `nightly-2020-02-20`.
Each project can be skipped individually by using the environment variable `SKIP_PROJECT_NAME_WASM_BUILD`.
diff --git a/utils/wasm-builder/src/builder.rs b/utils/wasm-builder/src/builder.rs
new file mode 100644
index 0000000000000..75e1d80572012
--- /dev/null
+++ b/utils/wasm-builder/src/builder.rs
@@ -0,0 +1,245 @@
+// This file is part of Substrate.
+
+// Copyright (C) 2019-2020 Parity Technologies (UK) Ltd.
+// SPDX-License-Identifier: Apache-2.0
+
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use std::{env, path::{PathBuf, Path}, process};
+
+/// Returns the manifest dir from the `CARGO_MANIFEST_DIR` env.
+fn get_manifest_dir() -> PathBuf {
+ env::var("CARGO_MANIFEST_DIR")
+ .expect("`CARGO_MANIFEST_DIR` is always set for `build.rs` files; qed")
+ .into()
+}
+
+/// First step of the [`WasmBuilder`] to select the project to build.
+pub struct WasmBuilderSelectProject {
+ /// This parameter just exists to make it impossible to construct
+ /// this type outside of this crate.
+ _ignore: (),
+}
+
+impl WasmBuilderSelectProject {
+ /// Use the current project as project for building the WASM binary.
+ ///
+ /// # Panics
+ ///
+ /// Panics if the `CARGO_MANIFEST_DIR` variable is not set. This variable
+ /// is always set by `Cargo` in `build.rs` files.
+ pub fn with_current_project(self) -> WasmBuilder {
+ WasmBuilder {
+ rust_flags: Vec::new(),
+ file_name: None,
+ project_cargo_toml: get_manifest_dir().join("Cargo.toml"),
+ }
+ }
+
+ /// Use the given `path` as project for building the WASM binary.
+ ///
+ /// Returns an error if the given `path` does not points to a `Cargo.toml`.
+ pub fn with_project(
+ self,
+ path: impl Into,
+ ) -> Result {
+ let path = path.into();
+
+ if path.ends_with("Cargo.toml") && path.exists() {
+ Ok(WasmBuilder {
+ rust_flags: Vec::new(),
+ file_name: None,
+ project_cargo_toml: path,
+ })
+ } else {
+ Err("Project path must point to the `Cargo.toml` of the project")
+ }
+ }
+}
+
+/// The builder for building a wasm binary.
+///
+/// The builder itself is separated into multiple structs to make the setup type safe.
+///
+/// Building a wasm binary:
+///
+/// 1. Call [`WasmBuilder::new`] to create a new builder.
+/// 2. Select the project to build using the methods of [`WasmBuilderSelectProject`].
+/// 3. Set additional `RUST_FLAGS` or a different name for the file containing the WASM code
+/// using methods of [`WasmBuilder`].
+/// 4. Build the WASM binary using [`Self::build`].
+pub struct WasmBuilder {
+ /// Flags that should be appended to `RUST_FLAGS` env variable.
+ rust_flags: Vec,
+ /// The name of the file that is being generated in `OUT_DIR`.
+ ///
+ /// Defaults to `wasm_binary.rs`.
+ file_name: Option,
+ /// The path to the `Cargo.toml` of the project that should be built
+ /// for wasm.
+ project_cargo_toml: PathBuf,
+}
+
+impl WasmBuilder {
+ /// Create a new instance of the builder.
+ pub fn new() -> WasmBuilderSelectProject {
+ WasmBuilderSelectProject {
+ _ignore: (),
+ }
+ }
+
+ /// Enable exporting `__heap_base` as global variable in the WASM binary.
+ ///
+ /// This adds `-Clink-arg=--export=__heap_base` to `RUST_FLAGS`.
+ pub fn export_heap_base(mut self) -> Self {
+ self.rust_flags.push("-Clink-arg=--export=__heap_base".into());
+ self
+ }
+
+ /// Set the name of the file that will be generated in `OUT_DIR`.
+ ///
+ /// This file needs to be included to get access to the build WASM binary.
+ ///
+ /// If this function is not called, `file_name` defaults to `wasm_binary.rs`
+ pub fn set_file_name(mut self, file_name: impl Into) -> Self {
+ self.file_name = Some(file_name.into());
+ self
+ }
+
+ /// Instruct the linker to import the memory into the WASM binary.
+ ///
+ /// This adds `-C link-arg=--import-memory` to `RUST_FLAGS`.
+ pub fn import_memory(mut self) -> Self {
+ self.rust_flags.push("-C link-arg=--import-memory".into());
+ self
+ }
+
+ /// Append the given `flag` to `RUST_FLAGS`.
+ ///
+ /// `flag` is appended as is, so it needs to be a valid flag.
+ pub fn append_to_rust_flags(mut self, flag: impl Into) -> Self {
+ self.rust_flags.push(flag.into());
+ self
+ }
+
+ /// Build the WASM binary.
+ pub fn build(self) {
+ let out_dir = PathBuf::from(env::var("OUT_DIR").expect("`OUT_DIR` is set by cargo!"));
+ let file_path = out_dir.join(self.file_name.unwrap_or_else(|| "wasm_binary.rs".into()));
+
+ if check_skip_build() {
+ // If we skip the build, we still want to make sure to be called when an env variable
+ // changes
+ generate_rerun_if_changed_instructions();
+
+ provide_dummy_wasm_binary_if_not_exist(&file_path);
+
+ return;
+ }
+
+ build_project(
+ file_path,
+ self.project_cargo_toml,
+ self.rust_flags.into_iter().map(|f| format!("{} ", f)).collect(),
+ );
+
+ // As last step we need to generate our `rerun-if-changed` stuff. If a build fails, we don't
+ // want to spam the output!
+ generate_rerun_if_changed_instructions();
+ }
+}
+
+/// Generate the name of the skip build environment variable for the current crate.
+fn generate_crate_skip_build_env_name() -> String {
+ format!(
+ "SKIP_{}_WASM_BUILD",
+ env::var("CARGO_PKG_NAME").expect("Package name is set").to_uppercase().replace('-', "_"),
+ )
+}
+
+/// Checks if the build of the WASM binary should be skipped.
+fn check_skip_build() -> bool {
+ env::var(crate::SKIP_BUILD_ENV).is_ok() || env::var(generate_crate_skip_build_env_name()).is_ok()
+}
+
+/// Provide a dummy WASM binary if there doesn't exist one.
+fn provide_dummy_wasm_binary_if_not_exist(file_path: &Path) {
+ if !file_path.exists() {
+ crate::write_file_if_changed(
+ file_path,
+ "pub const WASM_BINARY: Option<&[u8]> = None;\
+ pub const WASM_BINARY_BLOATY: Option<&[u8]> = None;",
+ );
+ }
+}
+
+/// Generate the `rerun-if-changed` instructions for cargo to make sure that the WASM binary is
+/// rebuilt when needed.
+fn generate_rerun_if_changed_instructions() {
+ // Make sure that the `build.rs` is called again if one of the following env variables changes.
+ println!("cargo:rerun-if-env-changed={}", crate::SKIP_BUILD_ENV);
+ println!("cargo:rerun-if-env-changed={}", crate::FORCE_WASM_BUILD_ENV);
+ println!("cargo:rerun-if-env-changed={}", generate_crate_skip_build_env_name());
+}
+
+/// Build the currently built project as wasm binary.
+///
+/// The current project is determined by using the `CARGO_MANIFEST_DIR` environment variable.
+///
+/// `file_name` - The name + path of the file being generated. The file contains the
+/// constant `WASM_BINARY`, which contains the built WASM binary.
+/// `project_cargo_toml` - The path to the `Cargo.toml` of the project that should be built.
+/// `default_rustflags` - Default `RUSTFLAGS` that will always be set for the build.
+fn build_project(
+ file_name: PathBuf,
+ project_cargo_toml: PathBuf,
+ default_rustflags: String,
+) {
+ let cargo_cmd = match crate::prerequisites::check() {
+ Ok(cmd) => cmd,
+ Err(err_msg) => {
+ eprintln!("{}", err_msg);
+ process::exit(1);
+ },
+ };
+
+ let (wasm_binary, bloaty) = crate::wasm_project::create_and_compile(
+ &project_cargo_toml,
+ &default_rustflags,
+ cargo_cmd,
+ );
+
+ let (wasm_binary, wasm_binary_bloaty) = if let Some(wasm_binary) = wasm_binary {
+ (
+ wasm_binary.wasm_binary_path_escaped(),
+ bloaty.wasm_binary_bloaty_path_escaped(),
+ )
+ } else {
+ (
+ bloaty.wasm_binary_bloaty_path_escaped(),
+ bloaty.wasm_binary_bloaty_path_escaped(),
+ )
+ };
+
+ crate::write_file_if_changed(
+ file_name,
+ format!(
+ r#"
+ pub const WASM_BINARY: Option<&[u8]> = Some(include_bytes!("{wasm_binary}"));
+ pub const WASM_BINARY_BLOATY: Option<&[u8]> = Some(include_bytes!("{wasm_binary_bloaty}"));
+ "#,
+ wasm_binary = wasm_binary,
+ wasm_binary_bloaty = wasm_binary_bloaty,
+ ),
+ );
+}
diff --git a/utils/wasm-builder/src/lib.rs b/utils/wasm-builder/src/lib.rs
index aa63e9596e190..573afbfcb6dc5 100644
--- a/utils/wasm-builder/src/lib.rs
+++ b/utils/wasm-builder/src/lib.rs
@@ -25,20 +25,23 @@
//! 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`.
+//! 2. Add `wasm-builder` as dependency into `build-dependencies`.
//!
//! The `build.rs` file needs to contain the following code:
//!
-//! ```ignore
-//! use wasm_builder_runner::{build_current_project, WasmBuilderSource};
+//! ```no_run
+//! use substrate_wasm_builder::WasmBuilder;
//!
//! fn main() {
-//! build_current_project(
-//! // The name of the file being generated in out-dir.
-//! "wasm_binary.rs",
-//! // How to include wasm-builder, in this case from crates.io.
-//! WasmBuilderSource::Crates("1.0.0"),
-//! );
+//! 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()
//! }
//! ```
//!
@@ -49,7 +52,8 @@
//! ```
//!
//! 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.
+//! The former is a compact Wasm binary and the latter is the Wasm binary as being generated by the compiler.
+//! Both variables have `Option<&'static [u8]>` as type.
//!
//! ### Feature
//!
@@ -63,19 +67,19 @@
//!
//! 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
-//! for `cargo check` runs.
-//! - `WASM_BUILD_TYPE` - Sets the build type for building wasm binaries. Supported values are `release` or `debug`.
+//! - `SKIP_WASM_BUILD` - Skips building any Wasm binary. This is useful when only native should be recompiled.
+//! If this is the first run and there doesn't exist a Wasm binary, this will set both
+//! variables to `None`.
+//! - `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.
-//! - `FORCE_WASM_BUILD` - Can be set to force a wasm build. On subsequent calls the value of the variable
-//! needs to change. As wasm builder instructs `cargo` to watch for file changes
+//! - `FORCE_WASM_BUILD` - Can be set to force 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.
//! - `WASM_BUILD_NO_COLOR` - Disable color output of the wasm build.
-//! - `WASM_TARGET_DIRECTORY` - Will copy any build wasm binary to the given directory. The path needs
+//! - `WASM_TARGET_DIRECTORY` - Will copy any build Wasm binary to the given directory. The path needs
//! to be absolute.
-//! - `WASM_BUILD_TOOLCHAIN` - The toolchain that should be used to build the wasm binaries. The
+//! - `WASM_BUILD_TOOLCHAIN` - The toolchain that should be used to build the Wasm binaries. The
//! format needs to be the same as used by cargo, e.g. `nightly-2020-02-20`.
//!
//! Each project can be skipped individually by using the environment variable `SKIP_PROJECT_NAME_WASM_BUILD`.
@@ -92,11 +96,14 @@
//! 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`.
-use std::{env, fs, path::{PathBuf, Path}, process::{Command, self}, io::BufRead};
+use std::{env, fs, path::{PathBuf, Path}, process::Command, io::BufRead};
+mod builder;
mod prerequisites;
mod wasm_project;
+pub use builder::{WasmBuilder, WasmBuilderSelectProject};
+
/// Environment variable that tells us to skip building the wasm binary.
const SKIP_BUILD_ENV: &str = "SKIP_WASM_BUILD";
@@ -120,87 +127,8 @@ const WASM_BUILD_NO_COLOR: &str = "WASM_BUILD_NO_COLOR";
/// Environment variable to set the toolchain used to compile the wasm binary.
const WASM_BUILD_TOOLCHAIN: &str = "WASM_BUILD_TOOLCHAIN";
-/// Build the currently built project as wasm binary.
-///
-/// The current project is determined by using the `CARGO_MANIFEST_DIR` environment variable.
-///
-/// `file_name` - The name + path of the file being generated. The file contains the
-/// constant `WASM_BINARY`, which contains the built WASM binary.
-/// `cargo_manifest` - The path to the `Cargo.toml` of the project that should be built.
-pub fn build_project(file_name: &str, cargo_manifest: &str) {
- build_project_with_default_rustflags(file_name, cargo_manifest, "");
-}
-
-/// Build the currently built project as wasm binary.
-///
-/// The current project is determined by using the `CARGO_MANIFEST_DIR` environment variable.
-///
-/// `file_name` - The name + path of the file being generated. The file contains the
-/// constant `WASM_BINARY`, which contains the built WASM binary.
-/// `cargo_manifest` - The path to the `Cargo.toml` of the project that should be built.
-/// `default_rustflags` - Default `RUSTFLAGS` that will always be set for the build.
-pub fn build_project_with_default_rustflags(
- file_name: &str,
- cargo_manifest: &str,
- default_rustflags: &str,
-) {
- if check_skip_build() {
- return;
- }
-
- let cargo_manifest = PathBuf::from(cargo_manifest);
-
- if !cargo_manifest.exists() {
- panic!("'{}' does not exist!", cargo_manifest.display());
- }
-
- if !cargo_manifest.ends_with("Cargo.toml") {
- panic!("'{}' no valid path to a `Cargo.toml`!", cargo_manifest.display());
- }
-
- let cargo_cmd = match prerequisites::check() {
- Ok(cmd) => cmd,
- Err(err_msg) => {
- eprintln!("{}", err_msg);
- process::exit(1);
- },
- };
-
- let (wasm_binary, bloaty) = wasm_project::create_and_compile(
- &cargo_manifest,
- default_rustflags,
- cargo_cmd,
- );
-
- let (wasm_binary, wasm_binary_bloaty) = if let Some(wasm_binary) = wasm_binary {
- (
- wasm_binary.wasm_binary_path_escaped(),
- bloaty.wasm_binary_bloaty_path_escaped(),
- )
- } else {
- (
- bloaty.wasm_binary_bloaty_path_escaped(),
- bloaty.wasm_binary_bloaty_path_escaped(),
- )
- };
-
- write_file_if_changed(
- file_name,
- format!(
- r#"
- pub const WASM_BINARY: Option<&[u8]> = Some(include_bytes!("{wasm_binary}"));
- pub const WASM_BINARY_BLOATY: Option<&[u8]> = Some(include_bytes!("{wasm_binary_bloaty}"));
- "#,
- wasm_binary = wasm_binary,
- wasm_binary_bloaty = wasm_binary_bloaty,
- ),
- );
-}
-
-/// Checks if the build of the WASM binary should be skipped.
-fn check_skip_build() -> bool {
- env::var(SKIP_BUILD_ENV).is_ok()
-}
+/// Environment variable that makes sure the WASM build is triggered.
+const FORCE_WASM_BUILD_ENV: &str = "FORCE_WASM_BUILD";
/// Write to the given `file` if the `content` is different.
fn write_file_if_changed(file: impl AsRef, content: impl AsRef) {
@@ -217,7 +145,9 @@ fn copy_file_if_changed(src: PathBuf, dst: PathBuf) {
if src_file != dst_file {
fs::copy(&src, &dst)
- .unwrap_or_else(|_| panic!("Copying `{}` to `{}` can not fail; qed", src.display(), dst.display()));
+ .unwrap_or_else(
+ |_| panic!("Copying `{}` to `{}` can not fail; qed", src.display(), dst.display())
+ );
}
}
diff --git a/utils/wasm-builder/src/wasm_project.rs b/utils/wasm-builder/src/wasm_project.rs
index c27af71988b07..4c4c80e5a8664 100644
--- a/utils/wasm-builder/src/wasm_project.rs
+++ b/utils/wasm-builder/src/wasm_project.rs
@@ -30,10 +30,6 @@ use cargo_metadata::{MetadataCommand, Metadata};
use walkdir::WalkDir;
-use fs2::FileExt;
-
-use itertools::Itertools;
-
/// Colorize an info message.
///
/// Returns the colorized message.
@@ -70,31 +66,6 @@ impl WasmBinary {
}
}
-/// A lock for the WASM workspace.
-struct WorkspaceLock(fs::File);
-
-impl WorkspaceLock {
- /// Create a new lock
- fn new(wasm_workspace_root: &Path) -> Self {
- let lock = fs::OpenOptions::new()
- .read(true)
- .write(true)
- .create(true)
- .open(wasm_workspace_root.join("wasm_workspace.lock"))
- .expect("Opening the lock file does not fail");
-
- lock.lock_exclusive().expect("Locking `wasm_workspace.lock` failed");
-
- WorkspaceLock(lock)
- }
-}
-
-impl Drop for WorkspaceLock {
- fn drop(&mut self) {
- let _ = self.0.unlock();
- }
-}
-
fn crate_metadata(cargo_manifest: &Path) -> Metadata {
let mut cargo_lock = cargo_manifest.to_path_buf();
cargo_lock.set_file_name("Cargo.lock");
@@ -120,35 +91,36 @@ fn crate_metadata(cargo_manifest: &Path) -> Metadata {
/// Creates the WASM project, compiles the WASM binary and compacts the WASM binary.
///
/// # Returns
+///
/// The path to the compact WASM binary and the bloaty WASM binary.
pub(crate) fn create_and_compile(
- cargo_manifest: &Path,
+ project_cargo_toml: &Path,
default_rustflags: &str,
cargo_cmd: CargoCommandVersioned,
) -> (Option, WasmBinaryBloaty) {
let wasm_workspace_root = get_wasm_workspace_root();
let wasm_workspace = wasm_workspace_root.join("wbuild");
- // Lock the workspace exclusively for us
- let _lock = WorkspaceLock::new(&wasm_workspace_root);
+ let crate_metadata = crate_metadata(project_cargo_toml);
- let crate_metadata = crate_metadata(cargo_manifest);
-
- let project = create_project(cargo_manifest, &wasm_workspace, &crate_metadata);
- create_wasm_workspace_project(&wasm_workspace, &crate_metadata.workspace_root);
+ let project = create_project(
+ project_cargo_toml,
+ &wasm_workspace,
+ &crate_metadata,
+ &crate_metadata.workspace_root,
+ );
build_project(&project, default_rustflags, cargo_cmd);
let (wasm_binary, bloaty) = compact_wasm_file(
&project,
- cargo_manifest,
- &wasm_workspace,
+ project_cargo_toml,
);
wasm_binary.as_ref().map(|wasm_binary|
- copy_wasm_to_target_directory(cargo_manifest, wasm_binary)
+ copy_wasm_to_target_directory(project_cargo_toml, wasm_binary)
);
- generate_rerun_if_changed_instructions(cargo_manifest, &project, &wasm_workspace);
+ generate_rerun_if_changed_instructions(project_cargo_toml, &project, &wasm_workspace);
(wasm_binary, bloaty)
}
@@ -221,69 +193,14 @@ fn get_wasm_workspace_root() -> PathBuf {
panic!("Could not find target dir in: {}", build_helper::out_dir().display())
}
-/// Find all workspace members.
-///
-/// Each folder in `wasm_workspace` is seen as a member of the workspace. Exceptions are
-/// folders starting with "." and the "target" folder.
-///
-/// Every workspace member that is not valid anymore is deleted (the folder of it). A
-/// member is not valid anymore when the `wasm-project` dependency points to an non-existing
-/// folder or the package name is not valid.
-fn find_and_clear_workspace_members(wasm_workspace: &Path) -> Vec {
- let mut members = WalkDir::new(wasm_workspace)
- .min_depth(1)
- .max_depth(1)
- .into_iter()
- .filter_map(|p| p.ok())
- .map(|d| d.into_path())
- .filter(|p| p.is_dir())
- .filter_map(|p| p.file_name().map(|f| f.to_owned()).and_then(|s| s.into_string().ok()))
- .filter(|f| !f.starts_with('.') && f != "target")
- .collect::>();
-
- let mut i = 0;
- while i != members.len() {
- let path = wasm_workspace.join(&members[i]).join("Cargo.toml");
-
- // Extract the `wasm-project` dependency.
- // If the path can be extracted and is valid and the package name matches,
- // the member is valid.
- if let Some(mut wasm_project) = fs::read_to_string(path)
- .ok()
- .and_then(|s| toml::from_str::(&s).ok())
- .and_then(|mut t| t.remove("dependencies"))
- .and_then(|p| p.try_into::().ok())
- .and_then(|mut t| t.remove("wasm_project"))
- .and_then(|p| p.try_into::().ok())
- {
- if let Some(path) = wasm_project.remove("path")
- .and_then(|p| p.try_into::().ok())
- {
- if let Some(name) = wasm_project.remove("package")
- .and_then(|p| p.try_into::().ok())
- {
- let path = PathBuf::from(path);
- if path.exists() {
- if name == get_crate_name(&path.join("Cargo.toml")) {
- i += 1;
- continue
- }
- }
- }
- }
- }
-
- fs::remove_dir_all(wasm_workspace.join(&members[i]))
- .expect("Removing invalid workspace member can not fail; qed");
- members.remove(i);
- }
-
- members
-}
-
-fn create_wasm_workspace_project(wasm_workspace: &Path, workspace_root_path: &Path) {
- let members = find_and_clear_workspace_members(wasm_workspace);
-
+fn create_project_cargo_toml(
+ wasm_workspace: &Path,
+ workspace_root_path: &Path,
+ crate_name: &str,
+ crate_path: &Path,
+ wasm_binary: &str,
+ enabled_features: &[String],
+) {
let mut workspace_toml: Table = toml::from_str(
&fs::read_to_string(
workspace_root_path.join("Cargo.toml"),
@@ -306,12 +223,6 @@ fn create_wasm_workspace_project(wasm_workspace: &Path, workspace_root_path: &Pa
wasm_workspace_toml.insert("profile".into(), profile.into());
- // Add `workspace` with members
- let mut workspace = Table::new();
- workspace.insert("members".into(), members.into());
-
- wasm_workspace_toml.insert("workspace".into(), workspace.into());
-
// Add patch section from the project root `Cargo.toml`
if let Some(mut patch) = workspace_toml.remove("patch").and_then(|p| p.try_into::().ok()) {
// Iterate over all patches and make the patch path absolute from the workspace root path.
@@ -335,6 +246,33 @@ fn create_wasm_workspace_project(wasm_workspace: &Path, workspace_root_path: &Pa
wasm_workspace_toml.insert("patch".into(), patch.into());
}
+ let mut package = Table::new();
+ package.insert("name".into(), format!("{}-wasm", crate_name).into());
+ package.insert("version".into(), "1.0.0".into());
+ package.insert("edition".into(), "2018".into());
+
+ wasm_workspace_toml.insert("package".into(), package.into());
+
+ let mut lib = Table::new();
+ lib.insert("name".into(), wasm_binary.into());
+ lib.insert("crate-type".into(), vec!["cdylib".to_string()].into());
+
+ wasm_workspace_toml.insert("lib".into(), lib.into());
+
+ let mut dependencies = Table::new();
+
+ let mut wasm_project = Table::new();
+ wasm_project.insert("package".into(), crate_name.into());
+ wasm_project.insert("path".into(), crate_path.display().to_string().into());
+ wasm_project.insert("default-features".into(), false.into());
+ wasm_project.insert("features".into(), enabled_features.to_vec().into());
+
+ dependencies.insert("wasm-project".into(), wasm_project.into());
+
+ wasm_workspace_toml.insert("dependencies".into(), dependencies.into());
+
+ wasm_workspace_toml.insert("workspace".into(), Table::new().into());
+
write_file_if_changed(
wasm_workspace.join("Cargo.toml"),
toml::to_string_pretty(&wasm_workspace_toml).expect("Wasm workspace toml is valid; qed"),
@@ -394,56 +332,48 @@ fn has_runtime_wasm_feature_declared(
/// Create the project used to build the wasm binary.
///
/// # Returns
-/// The path to the created project.
-fn create_project(cargo_manifest: &Path, wasm_workspace: &Path, crate_metadata: &Metadata) -> PathBuf {
- let crate_name = get_crate_name(cargo_manifest);
- let crate_path = cargo_manifest.parent().expect("Parent path exists; qed");
- 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"))
+///
+/// The path to the created wasm project.
+fn create_project(
+ project_cargo_toml: &Path,
+ wasm_workspace: &Path,
+ crate_metadata: &Metadata,
+ workspace_root_path: &Path,
+) -> PathBuf {
+ let crate_name = get_crate_name(project_cargo_toml);
+ let crate_path = project_cargo_toml.parent().expect("Parent path exists; qed");
+ let wasm_binary = get_wasm_binary_name(project_cargo_toml);
+ let wasm_project_folder = wasm_workspace.join(&crate_name);
+
+ fs::create_dir_all(wasm_project_folder.join("src"))
.expect("Wasm project dir create can not fail; qed");
- let mut enabled_features = project_enabled_features(&cargo_manifest, &crate_metadata);
+ let mut enabled_features = project_enabled_features(&project_cargo_toml, &crate_metadata);
- if has_runtime_wasm_feature_declared(cargo_manifest, crate_metadata) {
+ if has_runtime_wasm_feature_declared(project_cargo_toml, crate_metadata) {
enabled_features.push("runtime-wasm".into());
}
- write_file_if_changed(
- project_folder.join("Cargo.toml"),
- format!(
- r#"
- [package]
- name = "{crate_name}-wasm"
- version = "1.0.0"
- edition = "2018"
-
- [lib]
- name = "{wasm_binary}"
- crate-type = ["cdylib"]
-
- [dependencies]
- wasm_project = {{ package = "{crate_name}", path = "{crate_path}", default-features = false, features = [ {features} ] }}
- "#,
- crate_name = crate_name,
- crate_path = crate_path.display(),
- wasm_binary = wasm_binary,
- features = enabled_features.into_iter().map(|f| format!("\"{}\"", f)).join(","),
- )
+ create_project_cargo_toml(
+ &wasm_project_folder,
+ workspace_root_path,
+ &crate_name,
+ &crate_path,
+ &wasm_binary,
+ &enabled_features,
);
write_file_if_changed(
- project_folder.join("src/lib.rs"),
+ wasm_project_folder.join("src/lib.rs"),
"#![no_std] pub use wasm_project::*;",
);
- if let Some(crate_lock_file) = find_cargo_lock(cargo_manifest) {
+ if let Some(crate_lock_file) = find_cargo_lock(project_cargo_toml) {
// Use the `Cargo.lock` of the main project.
- crate::copy_file_if_changed(crate_lock_file, wasm_workspace.join("Cargo.lock"));
+ crate::copy_file_if_changed(crate_lock_file, wasm_project_folder.join("Cargo.lock"));
}
- project_folder
+ wasm_project_folder
}
/// Returns if the project should be built as a release.
@@ -474,9 +404,13 @@ fn build_project(project: &Path, default_rustflags: &str, cargo_cmd: CargoComman
env::var(crate::WASM_BUILD_RUSTFLAGS_ENV).unwrap_or_default(),
);
- build_cmd.args(&["rustc", "--target=wasm32-unknown-unknown"])
+ build_cmd.args(&["-Zfeatures=build_dep", "rustc", "--target=wasm32-unknown-unknown"])
.arg(format!("--manifest-path={}", manifest_path.display()))
.env("RUSTFLAGS", rustflags)
+ // Unset the `CARGO_TARGET_DIR` to prevent a cargo deadlock (cargo locks a target dir exclusive).
+ // The runner project is created in `CARGO_TARGET_DIR` and executing it will create a sub target
+ // directory inside of `CARGO_TARGET_DIR`.
+ .env_remove("CARGO_TARGET_DIR")
// We don't want to call ourselves recursively
.env(crate::SKIP_BUILD_ENV, "");
@@ -503,14 +437,14 @@ fn build_project(project: &Path, default_rustflags: &str, cargo_cmd: CargoComman
fn compact_wasm_file(
project: &Path,
cargo_manifest: &Path,
- wasm_workspace: &Path,
) -> (Option, WasmBinaryBloaty) {
let is_release_build = is_release_build();
let target = if is_release_build { "release" } else { "debug" };
let wasm_binary = get_wasm_binary_name(cargo_manifest);
- let wasm_file = wasm_workspace.join("target/wasm32-unknown-unknown")
+ let wasm_file = project.join("target/wasm32-unknown-unknown")
.join(target)
.join(format!("{}.wasm", wasm_binary));
+
let wasm_compact_file = if is_release_build {
let wasm_compact_file = project.join(format!("{}.compact.wasm", wasm_binary));
wasm_gc::garbage_collect_file(&wasm_file, &wasm_compact_file)