mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 11:41:04 +00:00
Build WASM binaries as part of cargo build (#2868)
* Introduce `wasm-builder` and `wasm-builder-runner` to retire `build.sh` Make use of `wasm-builder` in `test-runtime`. * Add build script and remove the wasm project * Port `node-runtime` to new wasm-builder * Make `substrate-executor` tests work with `wasm-builder` * Move `node-template` to `wasm-builder` * Remove `build.sh` :) * Remove the last include_bytes * Adds the missing build.rs files * Remove `build.sh` from CI * Debug CI * Make it work in CI * CI attempt 3 * Make `substrate-runtime-test` compile on stable * Ahhh, some missed `include_bytes!` * AHH * Add suggestions * Improve search for `Cargo.lock` and don't panic if it is not found * Searching from manifest path was no good idea * Make the `wasm-builder` source better configurable * Expose the bloaty wasm binary as well * Make sure to rerun WASM recompilation on changes in dependencies * Introduce new `WASM_BUILD_TYPE` env and make sure to call `build.rs` on changes to env variables * Remove `build.sh` from READMEs * Rename the projects * Fixes CI * Update lock file * Fixes merge-conflict * Apply suggestions from code review Co-Authored-By: TriplEight <denis.pisarev@parity.io> * Try to make windows happy * Replace all back slashes in paths with slashes * Apply suggestions from code review Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com> * Use cargo from `CARGO` env variable * Fix compilation * Use `rustup` for running the nightly build * Make individual projects skipable * Fix compilation * Fixes compilation * Build all WASM projects in one workspace * Replace more back slashes! * Remove `inlcude_bytes!` * Adds some documentation * Apply suggestions from code review Co-Authored-By: Shawn Tabrizi <shawntabrizi@gmail.com> * Apply suggestions from code review Co-Authored-By: Shawn Tabrizi <shawntabrizi@gmail.com> * More review comments * Update `Cargo.lock` * Set license * Apply suggestions from code review Co-Authored-By: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * More review comments + adds `TRIGGER_WASM_BUILD` env * Fix doc tests * Increase version + update README * Switch crates.io version of `wasm-builder` * Update README * Switch to released version of `wasm-builder-runner`
This commit is contained in:
@@ -53,7 +53,12 @@ mod tests {
|
||||
use primitives::Blake2Hasher;
|
||||
use hex::*;
|
||||
|
||||
native_executor_instance!(Executor, test_client::runtime::api::dispatch, test_client::runtime::native_version, include_bytes!("../../test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm"));
|
||||
native_executor_instance!(
|
||||
Executor,
|
||||
test_client::runtime::api::dispatch,
|
||||
test_client::runtime::native_version,
|
||||
test_client::runtime::WASM_BINARY
|
||||
);
|
||||
|
||||
fn executor() -> executor::NativeExecutor<Executor> {
|
||||
NativeExecutionDispatch::new(None)
|
||||
|
||||
@@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
derive_more = "0.14.0"
|
||||
derive_more = "0.15.0"
|
||||
parity-codec = "4.1.1"
|
||||
runtime_io = { package = "sr-io", path = "../sr-io" }
|
||||
primitives = { package = "substrate-primitives", path = "../primitives" }
|
||||
@@ -15,8 +15,8 @@ state_machine = { package = "substrate-state-machine", path = "../state-machine"
|
||||
runtime_version = { package = "sr-version", path = "../sr-version" }
|
||||
panic-handler = { package = "substrate-panic-handler", path = "../panic-handler" }
|
||||
wasmi = { version = "0.4.3" }
|
||||
byteorder = "1.1"
|
||||
lazy_static = "1.0"
|
||||
byteorder = "1.3"
|
||||
lazy_static = "1.3"
|
||||
parking_lot = "0.8.0"
|
||||
log = "0.4"
|
||||
libsecp256k1 = "0.2.1"
|
||||
@@ -26,6 +26,7 @@ tiny-keccak = "1.4.2"
|
||||
assert_matches = "1.1"
|
||||
wabt = "~0.7.4"
|
||||
hex-literal = "0.2.0"
|
||||
runtime-test = { package = "substrate-runtime-test", path = "runtime-test" }
|
||||
substrate-client = { path = "../client" }
|
||||
substrate-offchain = { path = "../offchain/" }
|
||||
|
||||
|
||||
+8
-9
@@ -1,11 +1,9 @@
|
||||
[package]
|
||||
name = "runtime-test"
|
||||
name = "substrate-runtime-test"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
rstd = { package = "sr-std", path = "../../sr-std", default-features = false }
|
||||
@@ -13,9 +11,10 @@ runtime_io = { package = "sr-io", path = "../../sr-io", default-features = false
|
||||
sandbox = { package = "sr-sandbox", path = "../../sr-sandbox", default-features = false }
|
||||
substrate-primitives = { path = "../../primitives", default-features = false }
|
||||
|
||||
[profile.release]
|
||||
panic = "abort"
|
||||
lto = true
|
||||
[build-dependencies]
|
||||
wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.0" }
|
||||
|
||||
[workspace]
|
||||
members = []
|
||||
[features]
|
||||
default = [ "std" ]
|
||||
std = []
|
||||
no_std = []
|
||||
+5
-5
@@ -1,4 +1,4 @@
|
||||
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
@@ -14,8 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! The Substrate test runtime reexported for WebAssembly compile.
|
||||
use wasm_builder_runner::{build_current_project, WasmBuilderSource};
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
pub use substrate_test_runtime::*;
|
||||
fn main() {
|
||||
build_current_project("wasm_binary.rs", WasmBuilderSource::Crates("1.0.1"));
|
||||
}
|
||||
+7
-3
@@ -1,7 +1,11 @@
|
||||
#![no_std]
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![cfg_attr(feature = "strict", deny(warnings))]
|
||||
|
||||
use rstd::{slice, vec::Vec, vec};
|
||||
// Make the WASM binary available.
|
||||
#[cfg(feature = "std")]
|
||||
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
|
||||
|
||||
use rstd::{vec::Vec, slice, vec};
|
||||
|
||||
use runtime_io::{
|
||||
set_storage, storage, clear_prefix, print, blake2_128, blake2_256,
|
||||
@@ -31,7 +35,7 @@ macro_rules! impl_stubs {
|
||||
// Leak the output vector to avoid it being freed.
|
||||
// This is fine in a WASM context since the heap
|
||||
// will be discarded after the call.
|
||||
::core::mem::forget(output);
|
||||
rstd::mem::forget(output);
|
||||
res
|
||||
}
|
||||
};
|
||||
@@ -566,13 +566,14 @@ mod tests {
|
||||
use crate::wasm_executor::WasmExecutor;
|
||||
use state_machine::TestExternalities as CoreTestExternalities;
|
||||
use wabt;
|
||||
use runtime_test::WASM_BINARY;
|
||||
|
||||
type TestExternalities<H> = CoreTestExternalities<H, u64>;
|
||||
|
||||
#[test]
|
||||
fn sandbox_should_work() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
let code = wabt::wat2wasm(r#"
|
||||
(module
|
||||
@@ -604,7 +605,7 @@ mod tests {
|
||||
#[test]
|
||||
fn sandbox_trap() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
let code = wabt::wat2wasm(r#"
|
||||
(module
|
||||
@@ -625,7 +626,7 @@ mod tests {
|
||||
#[test]
|
||||
fn sandbox_should_trap_when_heap_exhausted() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
let code = wabt::wat2wasm(r#"
|
||||
(module
|
||||
@@ -650,7 +651,7 @@ mod tests {
|
||||
#[test]
|
||||
fn start_called() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
let code = wabt::wat2wasm(r#"
|
||||
(module
|
||||
@@ -688,7 +689,7 @@ mod tests {
|
||||
#[test]
|
||||
fn invoke_args() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
let code = wabt::wat2wasm(r#"
|
||||
(module
|
||||
@@ -722,7 +723,7 @@ mod tests {
|
||||
#[test]
|
||||
fn return_val() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
let code = wabt::wat2wasm(r#"
|
||||
(module
|
||||
@@ -744,7 +745,7 @@ mod tests {
|
||||
#[test]
|
||||
fn unlinkable_module() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
let code = wabt::wat2wasm(r#"
|
||||
(module
|
||||
@@ -764,7 +765,7 @@ mod tests {
|
||||
#[test]
|
||||
fn corrupted_module() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
// Corrupted wasm file
|
||||
let code = &[0, 0, 0, 0, 1, 0, 0, 0];
|
||||
@@ -778,7 +779,7 @@ mod tests {
|
||||
#[test]
|
||||
fn start_fn_ok() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
let code = wabt::wat2wasm(r#"
|
||||
(module
|
||||
@@ -801,7 +802,7 @@ mod tests {
|
||||
#[test]
|
||||
fn start_fn_traps() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
let code = wabt::wat2wasm(r#"
|
||||
(module
|
||||
|
||||
@@ -1394,6 +1394,7 @@ mod tests {
|
||||
use state_machine::TestExternalities as CoreTestExternalities;
|
||||
use hex_literal::hex;
|
||||
use primitives::map;
|
||||
use runtime_test::WASM_BINARY;
|
||||
use substrate_offchain::testing;
|
||||
|
||||
type TestExternalities<H> = CoreTestExternalities<H, u64>;
|
||||
@@ -1401,7 +1402,7 @@ mod tests {
|
||||
#[test]
|
||||
fn returning_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
let output = WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_empty_return", &[]).unwrap();
|
||||
assert_eq!(output, vec![0u8; 0]);
|
||||
@@ -1410,7 +1411,7 @@ mod tests {
|
||||
#[test]
|
||||
fn panicking_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
let output = WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_panic", &[]);
|
||||
assert!(output.is_err());
|
||||
@@ -1426,7 +1427,7 @@ mod tests {
|
||||
fn storage_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
ext.set_storage(b"foo".to_vec(), b"bar".to_vec());
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
let output = WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_data_in", b"Hello world").unwrap();
|
||||
|
||||
@@ -1448,7 +1449,7 @@ mod tests {
|
||||
ext.set_storage(b"aba".to_vec(), b"3".to_vec());
|
||||
ext.set_storage(b"abb".to_vec(), b"4".to_vec());
|
||||
ext.set_storage(b"bbb".to_vec(), b"5".to_vec());
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
|
||||
// This will clear all entries which prefix is "ab".
|
||||
let output = WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_clear_prefix", b"ab").unwrap();
|
||||
@@ -1466,7 +1467,7 @@ mod tests {
|
||||
#[test]
|
||||
fn blake2_256_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
assert_eq!(
|
||||
WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_blake2_256", &[]).unwrap(),
|
||||
blake2_256(&b""[..]).encode()
|
||||
@@ -1480,7 +1481,7 @@ mod tests {
|
||||
#[test]
|
||||
fn blake2_128_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
assert_eq!(
|
||||
WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_blake2_128", &[]).unwrap(),
|
||||
blake2_128(&b""[..]).encode()
|
||||
@@ -1494,7 +1495,7 @@ mod tests {
|
||||
#[test]
|
||||
fn twox_256_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
assert_eq!(
|
||||
WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_twox_256", &[]).unwrap(),
|
||||
hex!("99e9d85137db46ef4bbea33613baafd56f963c64b1f3685a4eb4abd67ff6203a")
|
||||
@@ -1508,7 +1509,7 @@ mod tests {
|
||||
#[test]
|
||||
fn twox_128_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
assert_eq!(
|
||||
WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_twox_128", &[]).unwrap(),
|
||||
hex!("99e9d85137db46ef4bbea33613baafd5")
|
||||
@@ -1522,7 +1523,7 @@ mod tests {
|
||||
#[test]
|
||||
fn ed25519_verify_should_work() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
let key = ed25519::Pair::from_seed(&blake2_256(b"test"));
|
||||
let sig = key.sign(b"all ok!");
|
||||
let mut calldata = vec![];
|
||||
@@ -1548,7 +1549,7 @@ mod tests {
|
||||
#[test]
|
||||
fn sr25519_verify_should_work() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
let key = sr25519::Pair::from_seed(&blake2_256(b"test"));
|
||||
let sig = key.sign(b"all ok!");
|
||||
let mut calldata = vec![];
|
||||
@@ -1574,7 +1575,7 @@ mod tests {
|
||||
#[test]
|
||||
fn enumerated_trie_root_should_work() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
assert_eq!(
|
||||
WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_enumerated_trie_root", &[]).unwrap(),
|
||||
ordered_trie_root::<Blake2Hasher, _, _>(vec![b"zero".to_vec(), b"one".to_vec(), b"two".to_vec()].iter()).as_fixed_bytes().encode()
|
||||
@@ -1588,7 +1589,7 @@ mod tests {
|
||||
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
||||
let (offchain, state) = testing::TestOffchainExt::new();
|
||||
ext.set_offchain_externalities(offchain);
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
assert_eq!(
|
||||
WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_offchain_local_storage", &[]).unwrap(),
|
||||
vec![0]
|
||||
@@ -1615,7 +1616,7 @@ mod tests {
|
||||
},
|
||||
);
|
||||
|
||||
let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let test_code = WASM_BINARY;
|
||||
assert_eq!(
|
||||
WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_offchain_http", &[]).unwrap(),
|
||||
vec![0]
|
||||
|
||||
Generated
-269
@@ -1,269 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.4.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "crunchy"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "fixed-hash"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hash-db"
|
||||
version = "0.12.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "hash256-std-hasher"
|
||||
version = "0.12.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"crunchy 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "impl-codec"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"parity-codec 4.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nodrop"
|
||||
version = "0.1.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parity-codec"
|
||||
version = "4.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-codec-derive 3.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parity-codec-derive"
|
||||
version = "3.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro-crate 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 0.15.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "primitive-types"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"fixed-hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"impl-codec 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"uint 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-crate"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "0.4.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "0.6.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "runtime-test"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"sr-io 2.0.0",
|
||||
"sr-sandbox 2.0.0",
|
||||
"sr-std 2.0.0",
|
||||
"substrate-primitives 2.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hex"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver-parser"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.94"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "sr-io"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"hash-db 0.12.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-codec 4.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sr-std 2.0.0",
|
||||
"substrate-primitives 2.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sr-sandbox"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"parity-codec 4.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sr-std 2.0.0",
|
||||
"substrate-primitives 2.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sr-std"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "static_assertions"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "substrate-primitives"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"hash-db 0.12.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"hash256-std-hasher 0.12.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-codec 4.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"primitive-types 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sr-std 2.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "0.15.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "uint"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"crunchy 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "92c7fb76bc8826a8b33b4ee5bb07a247a81e76764ab4d55e8f73e3a4d8808c71"
|
||||
"checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf"
|
||||
"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
|
||||
"checksum crunchy 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
|
||||
"checksum fixed-hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "516877b7b9a1cc2d0293cbce23cd6203f0edbfd4090e6ca4489fecb5aa73050e"
|
||||
"checksum hash-db 0.12.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3c95a428c86ed4633d83e07ef9e0a147a906da01e931f07e74a85bedce5a43"
|
||||
"checksum hash256-std-hasher 0.12.4 (registry+https://github.com/rust-lang/crates.io-index)" = "663ce20dae36902c16d12c6aaae400ca40d922407a8cf2b4caf8cae9b39b4f03"
|
||||
"checksum impl-codec 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "62ed8ff267bc916dd848a800b96d3129aec73d5b23a5e3d018c83655d0c55371"
|
||||
"checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945"
|
||||
"checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32"
|
||||
"checksum parity-codec 4.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7902deb39d3b431897f211c1918789938251e67a740f55effd53201e79c0906c"
|
||||
"checksum parity-codec-derive 3.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "00a486fd383382ddcb2de928364b1f82571c1e48274fc43b7667a4738ee4056c"
|
||||
"checksum primitive-types 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "366ef730e56c11fd21ab3e518866cf7feb0fdf7f7c16ddc68485579e9d802787"
|
||||
"checksum proc-macro-crate 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e10d4b51f154c8a7fb96fd6dad097cb74b863943ec010ac94b9fd1be8861fe1e"
|
||||
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
|
||||
"checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db"
|
||||
"checksum rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "403bb3a286107a04825a5f82e1270acc1e14028d3d554d7a1e08914549575ab8"
|
||||
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
|
||||
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
|
||||
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
|
||||
"checksum serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)" = "076a696fdea89c19d3baed462576b8f6d663064414b5c793642da8dfeb99475b"
|
||||
"checksum static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c19be23126415861cb3a23e501d34a708f7f9b2183c5252d690941c2e69199d5"
|
||||
"checksum syn 0.15.39 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d960b829a55e56db167e861ddb43602c003c7be0bee1d345021703fac2fb7c"
|
||||
"checksum toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b8c96d7873fa7ef8bdeb3a9cda3ac48389b4154f32b9803b4bc26220b677b039"
|
||||
"checksum uint 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f5375d2c574f89adad4108ad525c93e39669853a602560bf5ed4ca9943b10799"
|
||||
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
|
||||
@@ -1,13 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
if cargo --version | grep -q "nightly"; then
|
||||
CARGO_CMD="cargo"
|
||||
else
|
||||
CARGO_CMD="cargo +nightly"
|
||||
fi
|
||||
CARGO_INCREMENTAL=0 RUSTFLAGS="-C link-arg=--export-table" $CARGO_CMD build --target=wasm32-unknown-unknown --release "$@"
|
||||
for i in test
|
||||
do
|
||||
wasm-gc target/wasm32-unknown-unknown/release/runtime_$i.wasm target/wasm32-unknown-unknown/release/runtime_$i.compact.wasm
|
||||
done
|
||||
@@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
fork-tree = { path = "../../core/util/fork-tree" }
|
||||
fork-tree = { path = "../../core/utils/fork-tree" }
|
||||
futures = "0.1"
|
||||
log = "0.4"
|
||||
parking_lot = "0.8.0"
|
||||
|
||||
@@ -20,7 +20,7 @@ lru-cache = "0.1.1"
|
||||
rustc-hex = "2.0"
|
||||
rand = "0.6"
|
||||
libp2p = { version = "0.10.0", default-features = false, features = ["secp256k1", "libp2p-websocket"] }
|
||||
fork-tree = { path = "../../core/util/fork-tree" }
|
||||
fork-tree = { path = "../../core/utils/fork-tree" }
|
||||
primitives = { package = "substrate-primitives", path = "../../core/primitives" }
|
||||
consensus = { package = "substrate-consensus-common", path = "../../core/consensus/common" }
|
||||
client = { package = "substrate-client", path = "../../core/client" }
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
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");
|
||||
|
||||
let t = trybuild::TestCases::new();
|
||||
t.compile_fail("tests/ui/*.rs");
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ name = "substrate-test-runtime"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
log = { version = "0.4", optional = true }
|
||||
@@ -29,11 +30,14 @@ cfg-if = "0.1.6"
|
||||
substrate-executor = { path = "../executor" }
|
||||
substrate-test-runtime-client = { path = "./client" }
|
||||
|
||||
[build-dependencies]
|
||||
wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.0" }
|
||||
|
||||
[features]
|
||||
default = [
|
||||
"std",
|
||||
"include-wasm-blob"
|
||||
]
|
||||
no_std = []
|
||||
std = [
|
||||
"log",
|
||||
"serde",
|
||||
@@ -55,5 +59,3 @@ std = [
|
||||
"offchain-primitives/std",
|
||||
"executive/std",
|
||||
]
|
||||
# If enabled, the WASM blob is added to the `GenesisConfig`.
|
||||
include-wasm-blob = []
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use wasm_builder_runner::{build_current_project, WasmBuilderSource};
|
||||
|
||||
fn main() {
|
||||
build_current_project("wasm_binary.rs", WasmBuilderSource::Crates("1.0.1"));
|
||||
}
|
||||
@@ -13,7 +13,6 @@ runtime_primitives = { package = "sr-primitives", path = "../../sr-primitives" }
|
||||
[features]
|
||||
default = [
|
||||
"std",
|
||||
"runtime/include-wasm-blob",
|
||||
]
|
||||
std = [
|
||||
"runtime/std",
|
||||
|
||||
@@ -51,7 +51,7 @@ mod local_executor {
|
||||
pub LocalExecutor,
|
||||
runtime::api::dispatch,
|
||||
runtime::native_version,
|
||||
include_bytes!("../../wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm")
|
||||
runtime::WASM_BINARY
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
use runtime_io::{blake2_256, twox_128};
|
||||
use super::{AuthorityId, AccountId};
|
||||
use super::{AuthorityId, AccountId, WASM_BINARY};
|
||||
use parity_codec::{Encode, KeyedVec, Joiner};
|
||||
use primitives::{ChangesTrieConfiguration, map, storage::well_known_keys};
|
||||
use runtime_primitives::traits::Block;
|
||||
@@ -48,13 +48,11 @@ impl GenesisConfig {
|
||||
}
|
||||
|
||||
pub fn genesis_map(&self) -> HashMap<Vec<u8>, Vec<u8>> {
|
||||
#[cfg(feature = "include-wasm-blob")]
|
||||
let wasm_runtime = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm").to_vec();
|
||||
let wasm_runtime = WASM_BINARY.to_vec();
|
||||
let mut map: HashMap<Vec<u8>, Vec<u8>> = self.balances.iter()
|
||||
.map(|&(ref account, balance)| (account.to_keyed_vec(b"balance:"), vec![].and(&balance)))
|
||||
.map(|(k, v)| (blake2_256(&k[..])[..].to_vec(), v.to_vec()))
|
||||
.chain(vec![
|
||||
#[cfg(feature = "include-wasm-blob")]
|
||||
(well_known_keys::CODE.into(), wasm_runtime),
|
||||
(well_known_keys::HEAP_PAGES.into(), vec![].and(&(16 as u64))),
|
||||
].into_iter())
|
||||
|
||||
@@ -56,6 +56,10 @@ pub type AuraId = AuthorityId;
|
||||
// Ensure Babe and Aura use the same crypto to simplify things a bit.
|
||||
pub type BabeId = AuthorityId;
|
||||
|
||||
// Inlucde the WASM binary
|
||||
#[cfg(feature = "std")]
|
||||
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
|
||||
|
||||
/// Test runtime version.
|
||||
pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
spec_name: create_runtime_str!("test"),
|
||||
|
||||
@@ -313,13 +313,10 @@ mod tests {
|
||||
|
||||
use runtime_io::{with_externalities, TestExternalities};
|
||||
use substrate_test_runtime_client::{AuthorityKeyring, AccountKeyring};
|
||||
use crate::{Header, Transfer};
|
||||
use crate::{Header, Transfer, WASM_BINARY};
|
||||
use primitives::{Blake2Hasher, map};
|
||||
use substrate_executor::WasmExecutor;
|
||||
|
||||
const WASM_CODE: &'static [u8] =
|
||||
include_bytes!("../wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm");
|
||||
|
||||
fn new_test_ext() -> TestExternalities<Blake2Hasher> {
|
||||
let authorities = vec![
|
||||
AuthorityKeyring::Alice.to_raw_public(),
|
||||
@@ -365,7 +362,7 @@ mod tests {
|
||||
#[test]
|
||||
fn block_import_works_wasm() {
|
||||
block_import_works(|b, ext| {
|
||||
WasmExecutor::new().call(ext, 8, &WASM_CODE, "Core_execute_block", &b.encode()).unwrap();
|
||||
WasmExecutor::new().call(ext, 8, &WASM_BINARY, "Core_execute_block", &b.encode()).unwrap();
|
||||
})
|
||||
}
|
||||
|
||||
@@ -453,7 +450,7 @@ mod tests {
|
||||
#[test]
|
||||
fn block_import_with_transaction_works_wasm() {
|
||||
block_import_with_transaction_works(|b, ext| {
|
||||
WasmExecutor::new().call(ext, 8, &WASM_CODE, "Core_execute_block", &b.encode()).unwrap();
|
||||
WasmExecutor::new().call(ext, 8, &WASM_BINARY, "Core_execute_block", &b.encode()).unwrap();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
-3645
File diff suppressed because it is too large
Load Diff
@@ -1,25 +0,0 @@
|
||||
[package]
|
||||
name = "substrate-test-runtime-wasm"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
name = "substrate_test_runtime"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
substrate-test-runtime = { path = "..", default-features = false }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
std = [
|
||||
"substrate-test-runtime/std",
|
||||
]
|
||||
|
||||
[profile.release]
|
||||
panic = "abort"
|
||||
lto = true
|
||||
|
||||
[workspace]
|
||||
members = []
|
||||
@@ -1,13 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
if cargo --version | grep -q "nightly"; then
|
||||
CARGO_CMD="cargo"
|
||||
else
|
||||
CARGO_CMD="cargo +nightly"
|
||||
fi
|
||||
CARGO_INCREMENTAL=0 RUSTFLAGS="-C link-arg=--export-table" $CARGO_CMD build --target=wasm32-unknown-unknown --release "$@"
|
||||
for i in substrate_test_runtime
|
||||
do
|
||||
wasm-gc "target/wasm32-unknown-unknown/release/$i.wasm" "target/wasm32-unknown-unknown/release/$i.compact.wasm"
|
||||
done
|
||||
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "substrate-wasm-builder-runner"
|
||||
version = "1.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
description = "Runner for substrate-wasm-builder"
|
||||
edition = "2018"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/paritytech/substrate"
|
||||
license = "GPL-3.0"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,12 @@
|
||||
## WASM builder runner
|
||||
|
||||
Since cargo contains many bugs when it comes to correct dependency and feature
|
||||
resolution, we need this little tool. See <https://github.com/rust-lang/cargo/issues/5730> 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 <https://crates.io/substrate-wasm-builder>
|
||||
|
||||
License: GPL-3.0
|
||||
@@ -0,0 +1,205 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! # WASM builder runner
|
||||
//!
|
||||
//! Since cargo contains many bugs when it comes to correct dependency and feature
|
||||
//! resolution, we need this little tool. See <https://github.com/rust-lang/cargo/issues/5730> 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 <https://crates.io/substrate-wasm-builder>
|
||||
|
||||
use std::{env, process::Command, fs, path::{PathBuf, Path}};
|
||||
|
||||
/// 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 TRIGGER_WASM_BUILD_ENV: &str = "TRIGGER_WASM_BUILD";
|
||||
|
||||
/// Replace all backslashes with slashes.
|
||||
fn replace_back_slashes<T: ToString>(path: T) -> String {
|
||||
path.to_string().replace("\\", "/")
|
||||
}
|
||||
|
||||
/// 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),
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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`.
|
||||
pub fn build_current_project(file_name: &str, wasm_builder_source: WasmBuilderSource) {
|
||||
generate_rerun_if_changed_instructions();
|
||||
|
||||
if check_skip_build() {
|
||||
return;
|
||||
}
|
||||
|
||||
let manifest_dir = PathBuf::from(
|
||||
env::var("CARGO_MANIFEST_DIR").expect(
|
||||
"`CARGO_MANIFEST_DIR` is always set for `build.rs` files; qed"
|
||||
)
|
||||
);
|
||||
|
||||
let cargo_toml_path = manifest_dir.join("Cargo.toml");
|
||||
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("`OUT_DIR` is set by cargo!"));
|
||||
let file_path = out_dir.join(file_name);
|
||||
let project_folder = out_dir.join("wasm_build_runner");
|
||||
|
||||
if check_provide_dummy_wasm_binary() {
|
||||
provide_dummy_wasm_binary(&file_path);
|
||||
} else {
|
||||
create_project(&project_folder, &file_path, &manifest_dir, wasm_builder_source, &cargo_toml_path);
|
||||
run_project(&project_folder);
|
||||
}
|
||||
}
|
||||
|
||||
fn create_project(
|
||||
project_folder: &Path,
|
||||
file_path: &Path,
|
||||
manifest_dir: &Path,
|
||||
wasm_builder_source: WasmBuilderSource,
|
||||
cargo_toml_path: &Path,
|
||||
) {
|
||||
fs::create_dir_all(project_folder.join("src"))
|
||||
.expect("WASM build runner dir create can not fail; qed");
|
||||
|
||||
fs::write(
|
||||
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(manifest_dir),
|
||||
)
|
||||
).expect("WASM build runner `Cargo.toml` writing can not fail; qed");
|
||||
|
||||
fs::write(
|
||||
project_folder.join("src/main.rs"),
|
||||
format!(
|
||||
r#"
|
||||
fn main() {{
|
||||
substrate_wasm_builder::build_project("{file_path}", "{cargo_toml_path}")
|
||||
}}
|
||||
"#,
|
||||
file_path = replace_back_slashes(file_path.display()),
|
||||
cargo_toml_path = replace_back_slashes(cargo_toml_path.display()),
|
||||
)
|
||||
).expect("WASM build runner `main.rs` writing can not fail; qed");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
if !cmd.status().map(|s| s.success()).unwrap_or(false) {
|
||||
panic!("Running WASM build runner failed!");
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
fn provide_dummy_wasm_binary(file_path: &Path) {
|
||||
fs::write(
|
||||
file_path,
|
||||
"pub const WASM_BINARY: &[u8] = &[]; pub const WASM_BINARY_BLOATY: &[u8] = &[];"
|
||||
).expect("Writing dummy WASM binary should not fail");
|
||||
}
|
||||
|
||||
/// 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={}", TRIGGER_WASM_BUILD_ENV);
|
||||
println!("cargo:rerun-if-env-changed={}", generate_crate_skip_build_env_name());
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "substrate-wasm-builder"
|
||||
version = "1.0.1"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
description = "Utility for building WASM binaries"
|
||||
edition = "2018"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/paritytech/substrate"
|
||||
license = "GPL-3.0"
|
||||
|
||||
[dependencies]
|
||||
build-helper = "0.1.1"
|
||||
cargo_metadata = "0.8"
|
||||
tempfile = "3.1.0"
|
||||
toml = "0.5.1"
|
||||
walkdir = "2.2.8"
|
||||
fs2 = "0.4.3"
|
||||
@@ -0,0 +1,65 @@
|
||||
## 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
|
||||
`cargo` build process.
|
||||
|
||||
### Project setup
|
||||
|
||||
A project that should be compiled as a WASM binary needs to:
|
||||
|
||||
1. Add a `build.rs` file.
|
||||
2. Add `substrate-wasm-builder-runner` as dependency into `build-dependencies`.
|
||||
3. Add a feature called `no-std`.
|
||||
|
||||
The `build.rs` file needs to contain the following code:
|
||||
|
||||
```rust
|
||||
use wasm_builder_runner::{build_current_project, WasmBuilderSource};
|
||||
|
||||
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"),
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
The `no-std` feature will be enabled by WASM builder while compiling your project to WASM.
|
||||
|
||||
As the final step, you need to add the following to your project:
|
||||
|
||||
```rust
|
||||
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.
|
||||
|
||||
### Environment variables
|
||||
|
||||
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`.
|
||||
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
|
||||
this environment variable should only be required in certain circumstances.
|
||||
|
||||
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`.
|
||||
|
||||
### Prerequisites:
|
||||
|
||||
WASM builder requires the following prerequisities for building the WASM binary:
|
||||
|
||||
- rust nightly + `wasm32-unknown-unknown` toolchain
|
||||
- wasm-gc
|
||||
|
||||
|
||||
License: GPL-3.0
|
||||
@@ -0,0 +1,168 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! # 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
|
||||
//! `cargo` build process.
|
||||
//!
|
||||
//! ## Project setup
|
||||
//!
|
||||
//! A project that should be compiled as a WASM binary needs to:
|
||||
//!
|
||||
//! 1. Add a `build.rs` file.
|
||||
//! 2. Add `substrate-wasm-builder-runner` as dependency into `build-dependencies`.
|
||||
//! 3. Add a feature called `no-std`.
|
||||
//!
|
||||
//! The `build.rs` file needs to contain the following code:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! use wasm_builder_runner::{build_current_project, WasmBuilderSource};
|
||||
//!
|
||||
//! 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"),
|
||||
//! );
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! The `no-std` feature will be enabled by WASM builder while compiling your project to WASM.
|
||||
//!
|
||||
//! As the final step, you need to add the following to your project:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! 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.
|
||||
//!
|
||||
//! ## Environment variables
|
||||
//!
|
||||
//! 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`.
|
||||
//! 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
|
||||
//! this environment variable should only be required in certain circumstances.
|
||||
//!
|
||||
//! 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`.
|
||||
//!
|
||||
//! ## Prerequisites:
|
||||
//!
|
||||
//! WASM builder requires the following prerequisities for building the WASM binary:
|
||||
//!
|
||||
//! - rust nightly + `wasm32-unknown-unknown` toolchain
|
||||
//! - wasm-gc
|
||||
//!
|
||||
|
||||
use std::{env, fs, path::PathBuf, process::Command};
|
||||
|
||||
mod prerequisites;
|
||||
mod wasm_project;
|
||||
|
||||
/// Environment variable that tells us to skip building the WASM binary.
|
||||
const SKIP_BUILD_ENV: &str = "SKIP_WASM_BUILD";
|
||||
|
||||
/// Environment variable to force a certain build type when building the WASM binary.
|
||||
/// Expects "debug" or "release" as value.
|
||||
///
|
||||
/// By default the WASM binary uses the same build type as the main cargo build.
|
||||
const WASM_BUILD_TYPE_ENV: &str = "WASM_BUILD_TYPE";
|
||||
|
||||
/// 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) {
|
||||
if check_skip_build() {
|
||||
return;
|
||||
}
|
||||
|
||||
let cargo_manifest = PathBuf::from(cargo_manifest);
|
||||
|
||||
if !cargo_manifest.exists() {
|
||||
create_out_file(
|
||||
file_name,
|
||||
format!("compile_error!(\"'{}' does not exists!\")", cargo_manifest.display())
|
||||
);
|
||||
return
|
||||
}
|
||||
|
||||
if !cargo_manifest.ends_with("Cargo.toml") {
|
||||
create_out_file(
|
||||
file_name,
|
||||
format!("compile_error!(\"'{}' no valid path to a `Cargo.toml`!\")", cargo_manifest.display())
|
||||
);
|
||||
return
|
||||
}
|
||||
|
||||
if let Some(err_msg) = prerequisites::check() {
|
||||
create_out_file(file_name, format!("compile_error!(\"{}\");", err_msg));
|
||||
return
|
||||
}
|
||||
|
||||
let (wasm_binary, bloaty) = wasm_project::create_and_compile(&cargo_manifest);
|
||||
|
||||
create_out_file(
|
||||
file_name,
|
||||
format!(
|
||||
r#"
|
||||
pub const WASM_BINARY: &[u8] = include_bytes!("{wasm_binary}");
|
||||
pub const WASM_BINARY_BLOATY: &[u8] = include_bytes!("{wasm_binary_bloaty}");
|
||||
"#,
|
||||
wasm_binary = wasm_binary.wasm_binary_path(),
|
||||
wasm_binary_bloaty = bloaty.wasm_binary_bloaty_path(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Checks if the build of the WASM binary should be skipped.
|
||||
fn check_skip_build() -> bool {
|
||||
env::var(SKIP_BUILD_ENV).is_ok()
|
||||
}
|
||||
|
||||
fn create_out_file(file_name: &str, content: String) {
|
||||
fs::write(
|
||||
file_name,
|
||||
content
|
||||
).expect("Creating and writing can not fail; qed");
|
||||
}
|
||||
|
||||
/// Get a cargo command that compiles with nightly
|
||||
fn get_nightly_cargo() -> Command {
|
||||
if Command::new("rustup").args(&["run", "nightly", "cargo"])
|
||||
.status().map(|s| s.success()).unwrap_or(false)
|
||||
{
|
||||
let mut cmd = Command::new("rustup");
|
||||
cmd.args(&["run", "nightly", "cargo"]);
|
||||
cmd
|
||||
} else {
|
||||
Command::new("cargo")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{process::{Command, Stdio}, fs};
|
||||
|
||||
use tempfile::tempdir;
|
||||
|
||||
/// Checks that all prerequisites are installed.
|
||||
///
|
||||
/// # Returns
|
||||
/// Returns `None` if everything was found and `Some(ERR_MSG)` if something could not be found.
|
||||
pub fn check() -> Option<&'static str> {
|
||||
if !check_nightly_installed() {
|
||||
return Some("Rust nightly not installed, please install it!")
|
||||
}
|
||||
|
||||
if Command::new("wasm-gc").stdout(Stdio::null()).status().map(|s| !s.success()).unwrap_or(true) {
|
||||
return Some("wasm-gc not installed, please install it!")
|
||||
}
|
||||
|
||||
if !check_wasm_toolchain_installed() {
|
||||
return Some("Rust WASM toolchain not installed, please install it!")
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn check_nightly_installed() -> bool {
|
||||
let version = Command::new("cargo")
|
||||
.arg("--version")
|
||||
.output()
|
||||
.map_err(|_| ())
|
||||
.and_then(|o| String::from_utf8(o.stdout).map_err(|_| ()))
|
||||
.unwrap_or_default();
|
||||
|
||||
let version2 = Command::new("rustup")
|
||||
.args(&["run", "nightly", "cargo", "--version"])
|
||||
.output()
|
||||
.map_err(|_| ())
|
||||
.and_then(|o| String::from_utf8(o.stdout).map_err(|_| ()))
|
||||
.unwrap_or_default();
|
||||
|
||||
version.contains("-nightly") || version2.contains("-nightly")
|
||||
}
|
||||
|
||||
fn check_wasm_toolchain_installed() -> bool {
|
||||
let temp = tempdir().expect("Creating temp dir does not fail; qed");
|
||||
fs::create_dir_all(temp.path().join("src")).expect("Creating src dir does not fail; qed");
|
||||
|
||||
let test_file = temp.path().join("src/lib.rs");
|
||||
let manifest_path = temp.path().join("Cargo.toml");
|
||||
|
||||
fs::write(&manifest_path,
|
||||
r#"
|
||||
[package]
|
||||
name = "wasm-test"
|
||||
version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
name = "wasm_test"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[workspace]
|
||||
"#,
|
||||
).expect("Writing wasm-test manifest does not fail; qed");
|
||||
fs::write(&test_file, "pub fn test() {}")
|
||||
.expect("Writing to the test file does not fail; qed");
|
||||
|
||||
let manifest_path = manifest_path.display().to_string();
|
||||
crate::get_nightly_cargo()
|
||||
.args(&["build", "--target=wasm32-unknown-unknown", "--manifest-path", &manifest_path])
|
||||
.status()
|
||||
.map(|s| s.success())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
@@ -0,0 +1,336 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{fs, path::{Path, PathBuf}, borrow::ToOwned, process::Command, env};
|
||||
|
||||
use toml::value::Table;
|
||||
|
||||
use build_helper::rerun_if_changed;
|
||||
|
||||
use cargo_metadata::MetadataCommand;
|
||||
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use fs2::FileExt;
|
||||
|
||||
/// Holds the path to the bloaty WASM binary.
|
||||
pub struct WasmBinaryBloaty(PathBuf);
|
||||
|
||||
impl WasmBinaryBloaty {
|
||||
/// Returns the path to the bloaty wasm binary.
|
||||
pub fn wasm_binary_bloaty_path(&self) -> String {
|
||||
self.0.display().to_string().replace('\\', "/")
|
||||
}
|
||||
}
|
||||
|
||||
/// Holds the path to the WASM binary.
|
||||
pub struct WasmBinary(PathBuf);
|
||||
|
||||
impl WasmBinary {
|
||||
/// Returns the path to the wasm binary.
|
||||
pub fn wasm_binary_path(&self) -> String {
|
||||
self.0.display().to_string().replace('\\', "/")
|
||||
}
|
||||
}
|
||||
|
||||
/// 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();
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 fn create_and_compile(cargo_manifest: &Path) -> (WasmBinary, 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 project = create_project(cargo_manifest, &wasm_workspace);
|
||||
create_wasm_workspace_project(&wasm_workspace);
|
||||
|
||||
build_project(&project);
|
||||
let (wasm_binary, bloaty) = compact_wasm_file(&project, cargo_manifest, &wasm_workspace);
|
||||
|
||||
generate_rerun_if_changed_instructions(cargo_manifest, &project, &wasm_workspace);
|
||||
|
||||
(wasm_binary, bloaty)
|
||||
}
|
||||
|
||||
/// Find the `Cargo.lock` relative to the `OUT_DIR` environment variable.
|
||||
///
|
||||
/// If the `Cargo.lock` cannot be found, we emit a warning and return `None`.
|
||||
fn find_cargo_lock(cargo_manifest: &Path) -> Option<PathBuf> {
|
||||
let mut path = build_helper::out_dir();
|
||||
|
||||
while path.pop() {
|
||||
if path.join("Cargo.lock").exists() {
|
||||
return Some(path.join("Cargo.lock"))
|
||||
}
|
||||
}
|
||||
|
||||
build_helper::warning!(
|
||||
"Could not find `Cargo.lock` for `{}`, while searching from `{}`.",
|
||||
cargo_manifest.display(),
|
||||
build_helper::out_dir().display()
|
||||
);
|
||||
None
|
||||
}
|
||||
|
||||
/// Extract the crate name from the given `Cargo.toml`.
|
||||
fn get_crate_name(cargo_manifest: &Path) -> String {
|
||||
let cargo_toml: Table = toml::from_str(
|
||||
&fs::read_to_string(cargo_manifest).expect("File exists as checked before; qed")
|
||||
).expect("Cargo manifest is a valid toml file; qed");
|
||||
|
||||
let package = cargo_toml
|
||||
.get("package")
|
||||
.and_then(|t| t.as_table())
|
||||
.expect("`package` key exists in valid `Cargo.toml`; qed");
|
||||
|
||||
package.get("name").and_then(|p| p.as_str()).map(ToOwned::to_owned).expect("Package name exists; qed")
|
||||
}
|
||||
|
||||
/// Returns the name for the wasm binary.
|
||||
fn get_wasm_binary_name(cargo_manifest: &Path) -> String {
|
||||
get_crate_name(cargo_manifest).replace('-', "_")
|
||||
}
|
||||
|
||||
/// Returns the root path of the wasm workspace.
|
||||
fn get_wasm_workspace_root() -> PathBuf {
|
||||
let mut out_dir = build_helper::out_dir();
|
||||
|
||||
while out_dir.parent().is_some() {
|
||||
if out_dir.parent().map(|p| p.ends_with("target")).unwrap_or(false) {
|
||||
return out_dir;
|
||||
}
|
||||
|
||||
out_dir.pop();
|
||||
}
|
||||
|
||||
panic!("Could not find target dir in: {}", build_helper::out_dir().display())
|
||||
}
|
||||
|
||||
fn create_wasm_workspace_project(wasm_workspace: &Path) {
|
||||
let 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() && !p.ends_with("target"))
|
||||
.filter_map(|p| p.file_name().map(|f| f.to_owned()).and_then(|s| s.into_string().ok()))
|
||||
.map(|s| format!("\"{}\", ", s))
|
||||
.collect::<String>();
|
||||
|
||||
fs::write(
|
||||
wasm_workspace.join("Cargo.toml"),
|
||||
format!(
|
||||
r#"
|
||||
[profile.release]
|
||||
panic = "abort"
|
||||
lto = true
|
||||
|
||||
[profile.dev]
|
||||
panic = "abort"
|
||||
|
||||
[workspace]
|
||||
members = [ {members} ]
|
||||
"#,
|
||||
members = members,
|
||||
)
|
||||
).expect("WASM workspace `Cargo.toml` writing can not fail; qed");
|
||||
}
|
||||
|
||||
/// 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) -> 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")).expect("Wasm project dir create can not fail; qed");
|
||||
|
||||
fs::write(
|
||||
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 = [ "no_std" ] }}
|
||||
"#,
|
||||
crate_name = crate_name,
|
||||
crate_path = crate_path.display(),
|
||||
wasm_binary = wasm_binary,
|
||||
)
|
||||
).expect("Project `Cargo.toml` writing can not fail; qed");
|
||||
|
||||
fs::write(
|
||||
project_folder.join("src/lib.rs"),
|
||||
format!(
|
||||
r#"
|
||||
#![no_std]
|
||||
pub use wasm_project::*;
|
||||
"#
|
||||
)
|
||||
).expect("Project `lib.rs` writing can not fail; qed");
|
||||
|
||||
if let Some(crate_lock_file) = find_cargo_lock(cargo_manifest) {
|
||||
// Use the `Cargo.lock` of the main project.
|
||||
fs::copy(crate_lock_file, wasm_workspace.join("Cargo.lock"))
|
||||
.expect("Copying the `Cargo.lock` can not fail; qed");
|
||||
}
|
||||
|
||||
project_folder
|
||||
}
|
||||
|
||||
/// Returns if the project should be built as a release.
|
||||
fn is_release_build() -> bool {
|
||||
if let Ok(var) = env::var(crate::WASM_BUILD_TYPE_ENV) {
|
||||
match var.as_str() {
|
||||
"release" => true,
|
||||
"debug" => false,
|
||||
var => panic!(
|
||||
"Unexpected value for `{}` env variable: {}\nOne of the following are expected: `debug` or `release`.",
|
||||
crate::WASM_BUILD_TYPE_ENV,
|
||||
var,
|
||||
),
|
||||
}
|
||||
} else {
|
||||
!build_helper::debug()
|
||||
}
|
||||
}
|
||||
|
||||
/// Build the project to create the WASM binary.
|
||||
fn build_project(project: &Path) {
|
||||
let manifest_path = project.join("Cargo.toml");
|
||||
let mut build_cmd = crate::get_nightly_cargo();
|
||||
build_cmd.args(&["build", "--target=wasm32-unknown-unknown"])
|
||||
.arg(format!("--manifest-path={}", manifest_path.display()))
|
||||
.env("RUSTFLAGS", "-C link-arg=--export-table")
|
||||
// We don't want to call ourselves recursively
|
||||
.env(crate::SKIP_BUILD_ENV, "");
|
||||
|
||||
if is_release_build() {
|
||||
build_cmd.arg("--release");
|
||||
};
|
||||
|
||||
println!("Executing build command: {:?}", build_cmd);
|
||||
|
||||
match build_cmd.status().map(|s| s.success()) {
|
||||
Ok(true) => {},
|
||||
_ => panic!("Failed to compile WASM binary"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Compact the WASM binary using `wasm-gc`. Returns the path to the bloaty WASM binary.
|
||||
fn compact_wasm_file(
|
||||
project: &Path,
|
||||
cargo_manifest: &Path,
|
||||
wasm_workspace: &Path,
|
||||
) -> (WasmBinary, WasmBinaryBloaty) {
|
||||
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")
|
||||
.join(target)
|
||||
.join(format!("{}.wasm", wasm_binary));
|
||||
let wasm_compact_file = project.join(format!("{}.compact.wasm", wasm_binary));
|
||||
|
||||
let res = Command::new("wasm-gc")
|
||||
.arg(&wasm_file)
|
||||
.arg(&wasm_compact_file)
|
||||
.status()
|
||||
.map(|s| s.success());
|
||||
|
||||
if !res.unwrap_or(false) {
|
||||
panic!("Failed to compact generated WASM binary.");
|
||||
}
|
||||
|
||||
(WasmBinary(wasm_compact_file), WasmBinaryBloaty(wasm_file))
|
||||
}
|
||||
|
||||
/// 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(
|
||||
cargo_manifest: &Path,
|
||||
project_folder: &Path,
|
||||
wasm_workspace: &Path,
|
||||
) {
|
||||
// Rerun `build.rs` if the `Cargo.lock` changes
|
||||
if let Some(cargo_lock) = find_cargo_lock(cargo_manifest) {
|
||||
rerun_if_changed(cargo_lock);
|
||||
}
|
||||
|
||||
let metadata = MetadataCommand::new()
|
||||
.manifest_path(project_folder.join("Cargo.toml"))
|
||||
.exec()
|
||||
.expect("`cargo metadata` can not fail!");
|
||||
|
||||
// Make sure that if any file/folder of a depedency change, we need to rerun the `build.rs`
|
||||
metadata.packages.into_iter()
|
||||
.filter(|package| !package.manifest_path.starts_with(wasm_workspace))
|
||||
.for_each(|package| {
|
||||
let mut manifest_path = package.manifest_path;
|
||||
if manifest_path.ends_with("Cargo.toml") {
|
||||
manifest_path.pop();
|
||||
}
|
||||
|
||||
rerun_if_changed(&manifest_path);
|
||||
|
||||
WalkDir::new(manifest_path)
|
||||
.into_iter()
|
||||
.filter_map(|p| p.ok())
|
||||
.for_each(|p| rerun_if_changed(p.path()));
|
||||
});
|
||||
|
||||
// Register our env variables
|
||||
println!("cargo:rerun-if-env-changed={}", crate::SKIP_BUILD_ENV);
|
||||
println!("cargo:rerun-if-env-changed={}", crate::WASM_BUILD_TYPE_ENV);
|
||||
}
|
||||
Reference in New Issue
Block a user