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:
Bastian Köcher
2019-07-04 11:34:06 +02:00
committed by GitHub
parent eca9c36b75
commit fe08221479
57 changed files with 1187 additions and 11869 deletions
@@ -0,0 +1,8 @@
[package]
name = "fork-tree"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
parity-codec = { version = "4.1.1", features = ["derive"] }
+873
View File
@@ -0,0 +1,873 @@
// 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/>.
//! Utility library for managing tree-like ordered data with logic for pruning
//! the tree while finalizing nodes.
#![warn(missing_docs)]
use std::fmt;
use parity_codec::{Decode, Encode};
/// Error occured when iterating with the tree.
#[derive(Clone, Debug, PartialEq)]
pub enum Error<E> {
/// Adding duplicate node to tree.
Duplicate,
/// Finalizing descendent of tree node without finalizing ancestor(s).
UnfinalizedAncestor,
/// Imported or finalized node that is an ancestor of previously finalized node.
Revert,
/// Error throw by client when checking for node ancestry.
Client(E),
}
impl<E: std::error::Error> fmt::Display for Error<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let message = match *self {
Error::Duplicate => "Hash already exists in Tree".into(),
Error::UnfinalizedAncestor => "Finalized descendent of Tree node without finalizing its ancestor(s) first".into(),
Error::Revert => "Tried to import or finalize node that is an ancestor of a previously finalized node".into(),
Error::Client(ref err) => format!("Client error: {}", err),
};
write!(f, "{}", message)
}
}
impl<E: std::error::Error> std::error::Error for Error<E> {
fn cause(&self) -> Option<&dyn std::error::Error> {
None
}
}
impl<E: std::error::Error> From<E> for Error<E> {
fn from(err: E) -> Error<E> {
Error::Client(err)
}
}
/// Result of finalizing a node (that could be a part of the tree or not).
#[derive(Debug, PartialEq)]
pub enum FinalizationResult<V> {
/// The tree has changed, optionally return the value associated with the finalized node.
Changed(Option<V>),
/// The tree has not changed.
Unchanged,
}
/// A tree data structure that stores several nodes across multiple branches.
/// Top-level branches are called roots. The tree has functionality for
/// finalizing nodes, which means that that node is traversed, and all competing
/// branches are pruned. It also guarantees that nodes in the tree are finalized
/// in order. Each node is uniquely identified by its hash but can be ordered by
/// its number. In order to build the tree an external function must be provided
/// when interacting with the tree to establish a node's ancestry.
#[derive(Clone, Debug, Decode, Encode, PartialEq)]
pub struct ForkTree<H, N, V> {
roots: Vec<Node<H, N, V>>,
best_finalized_number: Option<N>,
}
impl<H, N, V> ForkTree<H, N, V> where
H: PartialEq,
N: Ord,
{
/// Create a new empty tree.
pub fn new() -> ForkTree<H, N, V> {
ForkTree {
roots: Vec::new(),
best_finalized_number: None,
}
}
/// Import a new node into the tree. The given function `is_descendent_of`
/// should return `true` if the second hash (target) is a descendent of the
/// first hash (base). This method assumes that nodes in the same branch are
/// imported in order.
pub fn import<F, E>(
&mut self,
mut hash: H,
mut number: N,
mut data: V,
is_descendent_of: &F,
) -> Result<bool, Error<E>>
where E: std::error::Error,
F: Fn(&H, &H) -> Result<bool, E>,
{
if let Some(ref best_finalized_number) = self.best_finalized_number {
if number <= *best_finalized_number {
return Err(Error::Revert);
}
}
for root in self.roots.iter_mut() {
if root.hash == hash {
return Err(Error::Duplicate);
}
match root.import(hash, number, data, is_descendent_of)? {
Some((h, n, d)) => {
hash = h;
number = n;
data = d;
},
None => return Ok(false),
}
}
self.roots.push(Node {
data,
hash: hash,
number: number,
children: Vec::new(),
});
Ok(true)
}
/// Iterates over the existing roots in the tree.
pub fn roots(&self) -> impl Iterator<Item=(&H, &N, &V)> {
self.roots.iter().map(|node| (&node.hash, &node.number, &node.data))
}
fn node_iter(&self) -> impl Iterator<Item=&Node<H, N, V>> {
ForkTreeIterator { stack: self.roots.iter().collect() }
}
/// Iterates the nodes in the tree in pre-order.
pub fn iter(&self) -> impl Iterator<Item=(&H, &N, &V)> {
self.node_iter().map(|node| (&node.hash, &node.number, &node.data))
}
/// Finalize a root in the tree and return it, return `None` in case no root
/// with the given hash exists. All other roots are pruned, and the children
/// of the finalized node become the new roots.
pub fn finalize_root(&mut self, hash: &H) -> Option<V> {
if let Some(position) = self.roots.iter().position(|node| node.hash == *hash) {
let node = self.roots.swap_remove(position);
self.roots = node.children;
self.best_finalized_number = Some(node.number);
return Some(node.data);
}
None
}
/// Finalize a node in the tree. This method will make sure that the node
/// being finalized is either an existing root (an return its data), or a
/// node from a competing branch (not in the tree), tree pruning is done
/// accordingly. The given function `is_descendent_of` should return `true`
/// if the second hash (target) is a descendent of the first hash (base).
pub fn finalize<F, E>(
&mut self,
hash: &H,
number: N,
is_descendent_of: &F,
) -> Result<FinalizationResult<V>, Error<E>>
where E: std::error::Error,
F: Fn(&H, &H) -> Result<bool, E>
{
if let Some(ref best_finalized_number) = self.best_finalized_number {
if number <= *best_finalized_number {
return Err(Error::Revert);
}
}
// check if one of the current roots is being finalized
if let Some(root) = self.finalize_root(hash) {
return Ok(FinalizationResult::Changed(Some(root)));
}
// make sure we're not finalizing a descendent of any root
for root in self.roots.iter() {
if number > root.number && is_descendent_of(&root.hash, hash)? {
return Err(Error::UnfinalizedAncestor);
}
}
// we finalized a block earlier than any existing root (or possibly
// another fork not part of the tree). make sure to only keep roots that
// are part of the finalized branch
let mut changed = false;
self.roots.retain(|root| {
let retain = root.number > number && is_descendent_of(hash, &root.hash).unwrap_or(false);
if !retain {
changed = true;
}
retain
});
self.best_finalized_number = Some(number);
if changed {
Ok(FinalizationResult::Changed(None))
} else {
Ok(FinalizationResult::Unchanged)
}
}
/// Checks if any node in the tree is finalized by either finalizing the
/// node itself or a child node that's not in the tree, guaranteeing that
/// the node being finalized isn't a descendent of any of the node's
/// children. Returns `Some(true)` if the node being finalized is a root,
/// `Some(false)` if the node being finalized is not a root, and `None` if
/// no node in the tree is finalized. The given `predicate` is checked on
/// the prospective finalized root and must pass for finalization to occur.
/// The given function `is_descendent_of` should return `true` if the second
/// hash (target) is a descendent of the first hash (base).
pub fn finalizes_any_with_descendent_if<F, P, E>(
&self,
hash: &H,
number: N,
is_descendent_of: &F,
predicate: P,
) -> Result<Option<bool>, Error<E>>
where E: std::error::Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
{
if let Some(ref best_finalized_number) = self.best_finalized_number {
if number <= *best_finalized_number {
return Err(Error::Revert);
}
}
// check if the given hash is equal or a descendent of any node in the
// tree, if we find a valid node that passes the predicate then we must
// ensure that we're not finalizing past any of its child nodes.
for node in self.node_iter() {
if predicate(&node.data) {
if node.hash == *hash || is_descendent_of(&node.hash, hash)? {
for node in node.children.iter() {
if node.number <= number && is_descendent_of(&node.hash, &hash)? {
return Err(Error::UnfinalizedAncestor);
}
}
return Ok(Some(self.roots.iter().any(|root| root.hash == node.hash)));
}
}
}
Ok(None)
}
/// Finalize a root in the tree by either finalizing the node itself or a
/// child node that's not in the tree, guaranteeing that the node being
/// finalized isn't a descendent of any of the root's children. The given
/// `predicate` is checked on the prospective finalized root and must pass for
/// finalization to occur. The given function `is_descendent_of` should
/// return `true` if the second hash (target) is a descendent of the first
/// hash (base).
pub fn finalize_with_descendent_if<F, P, E>(
&mut self,
hash: &H,
number: N,
is_descendent_of: &F,
predicate: P,
) -> Result<FinalizationResult<V>, Error<E>>
where E: std::error::Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
{
if let Some(ref best_finalized_number) = self.best_finalized_number {
if number <= *best_finalized_number {
return Err(Error::Revert);
}
}
// check if the given hash is equal or a a descendent of any root, if we
// find a valid root that passes the predicate then we must ensure that
// we're not finalizing past any children node.
let mut position = None;
for (i, root) in self.roots.iter().enumerate() {
if predicate(&root.data) {
if root.hash == *hash || is_descendent_of(&root.hash, hash)? {
for node in root.children.iter() {
if node.number <= number && is_descendent_of(&node.hash, &hash)? {
return Err(Error::UnfinalizedAncestor);
}
}
position = Some(i);
break;
}
}
}
let node_data = position.map(|i| {
let node = self.roots.swap_remove(i);
self.roots = node.children;
self.best_finalized_number = Some(node.number);
node.data
});
// if the block being finalized is earlier than a given root, then it
// must be its ancestor, otherwise we can prune the root. if there's a
// root at the same height then the hashes must match. otherwise the
// node being finalized is higher than the root so it must be its
// descendent (in this case the node wasn't finalized earlier presumably
// because the predicate didn't pass).
let mut changed = false;
self.roots.retain(|root| {
let retain =
root.number > number && is_descendent_of(hash, &root.hash).unwrap_or(false) ||
root.number == number && root.hash == *hash ||
is_descendent_of(&root.hash, hash).unwrap_or(false);
if !retain {
changed = true;
}
retain
});
self.best_finalized_number = Some(number);
match (node_data, changed) {
(Some(data), _) => Ok(FinalizationResult::Changed(Some(data))),
(None, true) => Ok(FinalizationResult::Changed(None)),
(None, false) => Ok(FinalizationResult::Unchanged),
}
}
}
// Workaround for: https://github.com/rust-lang/rust/issues/34537
mod node_implementation {
use super::*;
#[derive(Clone, Debug, Decode, Encode, PartialEq)]
pub struct Node<H, N, V> {
pub hash: H,
pub number: N,
pub data: V,
pub children: Vec<Node<H, N, V>>,
}
impl<H: PartialEq, N: Ord, V> Node<H, N, V> {
pub fn import<F, E: std::error::Error>(
&mut self,
mut hash: H,
mut number: N,
mut data: V,
is_descendent_of: &F,
) -> Result<Option<(H, N, V)>, Error<E>>
where E: fmt::Debug,
F: Fn(&H, &H) -> Result<bool, E>,
{
if self.hash == hash {
return Err(Error::Duplicate);
};
if number <= self.number { return Ok(Some((hash, number, data))); }
for node in self.children.iter_mut() {
match node.import(hash, number, data, is_descendent_of)? {
Some((h, n, d)) => {
hash = h;
number = n;
data = d;
},
None => return Ok(None),
}
}
if is_descendent_of(&self.hash, &hash)? {
self.children.push(Node {
data,
hash: hash,
number: number,
children: Vec::new(),
});
Ok(None)
} else {
Ok(Some((hash, number, data)))
}
}
}
}
// Workaround for: https://github.com/rust-lang/rust/issues/34537
use node_implementation::Node;
struct ForkTreeIterator<'a, H, N, V> {
stack: Vec<&'a Node<H, N, V>>,
}
impl<'a, H, N, V> Iterator for ForkTreeIterator<'a, H, N, V> {
type Item = &'a Node<H, N, V>;
fn next(&mut self) -> Option<Self::Item> {
self.stack.pop().map(|node| {
self.stack.extend(node.children.iter());
node
})
}
}
#[cfg(test)]
mod test {
use super::{FinalizationResult, ForkTree, Error};
#[derive(Debug, PartialEq)]
struct TestError;
impl std::fmt::Display for TestError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "TestError")
}
}
impl std::error::Error for TestError {}
fn test_fork_tree<'a>() -> (ForkTree<&'a str, u64, ()>, impl Fn(&&str, &&str) -> Result<bool, TestError>) {
let mut tree = ForkTree::new();
//
// - B - C - D - E
// /
// / - G
// / /
// A - F - H - I
// \
// — J - K
//
let is_descendent_of = |base: &&str, block: &&str| -> Result<bool, TestError> {
let letters = vec!["B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
match (*base, *block) {
("A", b) => Ok(letters.into_iter().any(|n| n == b)),
("B", b) => Ok(b == "C" || b == "D" || b == "E"),
("C", b) => Ok(b == "D" || b == "E"),
("D", b) => Ok(b == "E"),
("E", _) => Ok(false),
("F", b) => Ok(b == "G" || b == "H" || b == "I"),
("G", _) => Ok(false),
("H", b) => Ok(b == "I"),
("I", _) => Ok(false),
("J", b) => Ok(b == "K"),
("K", _) => Ok(false),
("0", _) => Ok(true),
_ => Ok(false),
}
};
tree.import("A", 1, (), &is_descendent_of).unwrap();
tree.import("B", 2, (), &is_descendent_of).unwrap();
tree.import("C", 3, (), &is_descendent_of).unwrap();
tree.import("D", 4, (), &is_descendent_of).unwrap();
tree.import("E", 5, (), &is_descendent_of).unwrap();
tree.import("F", 2, (), &is_descendent_of).unwrap();
tree.import("G", 3, (), &is_descendent_of).unwrap();
tree.import("H", 3, (), &is_descendent_of).unwrap();
tree.import("I", 4, (), &is_descendent_of).unwrap();
tree.import("J", 2, (), &is_descendent_of).unwrap();
tree.import("K", 3, (), &is_descendent_of).unwrap();
(tree, is_descendent_of)
}
#[test]
fn import_doesnt_revert() {
let (mut tree, is_descendent_of) = test_fork_tree();
tree.finalize_root(&"A");
assert_eq!(
tree.best_finalized_number,
Some(1),
);
assert_eq!(
tree.import("A", 1, (), &is_descendent_of),
Err(Error::Revert),
);
}
#[test]
fn import_doesnt_add_duplicates() {
let (mut tree, is_descendent_of) = test_fork_tree();
assert_eq!(
tree.import("A", 1, (), &is_descendent_of),
Err(Error::Duplicate),
);
assert_eq!(
tree.import("I", 4, (), &is_descendent_of),
Err(Error::Duplicate),
);
assert_eq!(
tree.import("G", 3, (), &is_descendent_of),
Err(Error::Duplicate),
);
assert_eq!(
tree.import("K", 3, (), &is_descendent_of),
Err(Error::Duplicate),
);
}
#[test]
fn finalize_root_works() {
let finalize_a = || {
let (mut tree, ..) = test_fork_tree();
assert_eq!(
tree.roots().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![("A", 1)],
);
// finalizing "A" opens up three possible forks
tree.finalize_root(&"A");
assert_eq!(
tree.roots().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![("B", 2), ("F", 2), ("J", 2)],
);
tree
};
{
let mut tree = finalize_a();
// finalizing "B" will progress on its fork and remove any other competing forks
tree.finalize_root(&"B");
assert_eq!(
tree.roots().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![("C", 3)],
);
// all the other forks have been pruned
assert!(tree.roots.len() == 1);
}
{
let mut tree = finalize_a();
// finalizing "J" will progress on its fork and remove any other competing forks
tree.finalize_root(&"J");
assert_eq!(
tree.roots().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![("K", 3)],
);
// all the other forks have been pruned
assert!(tree.roots.len() == 1);
}
}
#[test]
fn finalize_works() {
let (mut tree, is_descendent_of) = test_fork_tree();
let original_roots = tree.roots.clone();
// finalizing a block prior to any in the node doesn't change the tree
assert_eq!(
tree.finalize(&"0", 0, &is_descendent_of),
Ok(FinalizationResult::Unchanged),
);
assert_eq!(tree.roots, original_roots);
// finalizing "A" opens up three possible forks
assert_eq!(
tree.finalize(&"A", 1, &is_descendent_of),
Ok(FinalizationResult::Changed(Some(()))),
);
assert_eq!(
tree.roots().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![("B", 2), ("F", 2), ("J", 2)],
);
// finalizing anything lower than what we observed will fail
assert_eq!(
tree.best_finalized_number,
Some(1),
);
assert_eq!(
tree.finalize(&"Z", 1, &is_descendent_of),
Err(Error::Revert),
);
// trying to finalize a node without finalizing its ancestors first will fail
assert_eq!(
tree.finalize(&"H", 3, &is_descendent_of),
Err(Error::UnfinalizedAncestor),
);
// after finalizing "F" we can finalize "H"
assert_eq!(
tree.finalize(&"F", 2, &is_descendent_of),
Ok(FinalizationResult::Changed(Some(()))),
);
assert_eq!(
tree.finalize(&"H", 3, &is_descendent_of),
Ok(FinalizationResult::Changed(Some(()))),
);
assert_eq!(
tree.roots().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![("I", 4)],
);
// finalizing a node from another fork that isn't part of the tree clears the tree
assert_eq!(
tree.finalize(&"Z", 5, &is_descendent_of),
Ok(FinalizationResult::Changed(None)),
);
assert!(tree.roots.is_empty());
}
#[test]
fn finalize_with_descendent_works() {
#[derive(Debug, PartialEq)]
struct Change { effective: u64 };
let (mut tree, is_descendent_of) = {
let mut tree = ForkTree::new();
let is_descendent_of = |base: &&str, block: &&str| -> Result<bool, TestError> {
//
// A0 #1 - (B #2) - (C #5) - D #10 - E #15 - (F #100)
// \
// - (G #100)
//
// A1 #1
//
// Nodes B, C, F and G are not part of the tree.
match (*base, *block) {
("A0", b) => Ok(b == "B" || b == "C" || b == "D" || b == "G"),
("A1", _) => Ok(false),
("C", b) => Ok(b == "D"),
("D", b) => Ok(b == "E" || b == "F" || b == "G"),
("E", b) => Ok(b == "F"),
_ => Ok(false),
}
};
tree.import("A0", 1, Change { effective: 5 }, &is_descendent_of).unwrap();
tree.import("A1", 1, Change { effective: 5 }, &is_descendent_of).unwrap();
tree.import("D", 10, Change { effective: 10 }, &is_descendent_of).unwrap();
tree.import("E", 15, Change { effective: 50 }, &is_descendent_of).unwrap();
(tree, is_descendent_of)
};
assert_eq!(
tree.finalizes_any_with_descendent_if(
&"B",
2,
&is_descendent_of,
|c| c.effective <= 2,
),
Ok(None),
);
// finalizing "D" will finalize a block from the tree, but it can't be applied yet
// since it is not a root change
assert_eq!(
tree.finalizes_any_with_descendent_if(
&"D",
10,
&is_descendent_of,
|c| c.effective == 10,
),
Ok(Some(false)),
);
// finalizing "B" doesn't finalize "A0" since the predicate doesn't pass,
// although it will clear out "A1" from the tree
assert_eq!(
tree.finalize_with_descendent_if(
&"B",
2,
&is_descendent_of,
|c| c.effective <= 2,
),
Ok(FinalizationResult::Changed(None)),
);
assert_eq!(
tree.roots().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![("A0", 1)],
);
// finalizing "C" will finalize the node "A0" and prune it out of the tree
assert_eq!(
tree.finalizes_any_with_descendent_if(
&"C",
5,
&is_descendent_of,
|c| c.effective <= 5,
),
Ok(Some(true)),
);
assert_eq!(
tree.finalize_with_descendent_if(
&"C",
5,
&is_descendent_of,
|c| c.effective <= 5,
),
Ok(FinalizationResult::Changed(Some(Change { effective: 5 }))),
);
assert_eq!(
tree.roots().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![("D", 10)],
);
// finalizing "F" will fail since it would finalize past "E" without finalizing "D" first
assert_eq!(
tree.finalizes_any_with_descendent_if(
&"F",
100,
&is_descendent_of,
|c| c.effective <= 100,
),
Err(Error::UnfinalizedAncestor),
);
// it will work with "G" though since it is not in the same branch as "E"
assert_eq!(
tree.finalizes_any_with_descendent_if(
&"G",
100,
&is_descendent_of,
|c| c.effective <= 100,
),
Ok(Some(true)),
);
assert_eq!(
tree.finalize_with_descendent_if(
&"G",
100,
&is_descendent_of,
|c| c.effective <= 100,
),
Ok(FinalizationResult::Changed(Some(Change { effective: 10 }))),
);
// "E" will be pruned out
assert_eq!(tree.roots().count(), 0);
}
#[test]
fn iter_iterates_in_preorder() {
let (tree, ..) = test_fork_tree();
assert_eq!(
tree.iter().map(|(h, n, _)| (h.clone(), n.clone())).collect::<Vec<_>>(),
vec![
("A", 1),
("J", 2), ("K", 3),
("F", 2), ("H", 3), ("I", 4),
("G", 3),
("B", 2), ("C", 3), ("D", 4), ("E", 5),
],
);
}
#[test]
fn minimizes_calls_to_is_descendent_of() {
use std::sync::atomic::{AtomicUsize, Ordering};
let n_is_descendent_of_calls = AtomicUsize::new(0);
let is_descendent_of = |_: &&str, _: &&str| -> Result<bool, TestError> {
n_is_descendent_of_calls.fetch_add(1, Ordering::SeqCst);
Ok(true)
};
{
// Deep tree where we want to call `finalizes_any_with_descendent_if`. The
// search for the node should first check the predicate (which is cheaper) and
// only then call `is_descendent_of`
let mut tree = ForkTree::new();
let letters = vec!["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
for (i, letter) in letters.iter().enumerate() {
tree.import::<_, TestError>(*letter, i, i, &|_, _| Ok(true)).unwrap();
}
// "L" is a descendent of "K", but the predicate will only pass for "K",
// therefore only one call to `is_descendent_of` should be made
assert_eq!(
tree.finalizes_any_with_descendent_if(
&"L",
11,
&is_descendent_of,
|i| *i == 10,
),
Ok(Some(false)),
);
assert_eq!(
n_is_descendent_of_calls.load(Ordering::SeqCst),
1,
);
}
n_is_descendent_of_calls.store(0, Ordering::SeqCst);
{
// Multiple roots in the tree where we want to call `finalize_with_descendent_if`.
// The search for the root node should first check the predicate (which is cheaper)
// and only then call `is_descendent_of`
let mut tree = ForkTree::new();
let letters = vec!["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
for (i, letter) in letters.iter().enumerate() {
tree.import::<_, TestError>(*letter, i, i, &|_, _| Ok(false)).unwrap();
}
// "L" is a descendent of "K", but the predicate will only pass for "K",
// therefore only one call to `is_descendent_of` should be made
assert_eq!(
tree.finalize_with_descendent_if(
&"L",
11,
&is_descendent_of,
|i| *i == 10,
),
Ok(FinalizationResult::Changed(Some(10))),
);
assert_eq!(
n_is_descendent_of_calls.load(Ordering::SeqCst),
1,
);
}
}
}
@@ -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);
}