diff --git a/.gitignore b/.gitignore index 05ba60f..6fc8f03 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ target .cargo .DS_Store .idea +.vscode diff --git a/.travis.yml b/.travis.yml index d856856..691eaf8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,5 @@ rust: - stable script: - - cargo build --all --release --verbose - - cargo test --all --verbose - - cargo build --no-default-features + - cargo test --all-features --verbose + - cargo build --no-default-features --verbose diff --git a/Cargo.toml b/Cargo.toml index b77aa0e..80755bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "pwasm-utils" version = "0.15.0" +edition = "2018" authors = ["Nikolay Volf ", "Sergey Pepyakin "] license = "MIT/Apache-2.0" readme = "README.md" @@ -8,6 +9,41 @@ description = "Collection of command-line utilities and corresponding Rust api f keywords = ["wasm", "webassembly", "pwasm"] repository = "https://github.com/paritytech/wasm-utils" +[[bin]] +name = "wasm-prune" +path = "cli/prune/main.rs" +required-features = ["cli"] + +[[bin]] +name = "wasm-ext" +path = "cli/ext/main.rs" +required-features = ["cli"] + +[[bin]] +name = "wasm-gas" +path = "cli/gas/main.rs" +required-features = ["cli"] + +[[bin]] +name = "wasm-build" +path = "cli/build/main.rs" +required-features = ["cli"] + +[[bin]] +name = "wasm-stack-height" +path = "cli/stack_height/main.rs" +required-features = ["cli"] + +[[bin]] +name = "wasm-pack" +path = "cli/pack/main.rs" +required-features = ["cli"] + +[[bin]] +name = "wasm-check" +path = "cli/check/main.rs" +required-features = ["cli"] + [dependencies] # If you add the feature "bulk", make sure you fixed all expects that say # "parity-wasm is compiled without bulk-memory operations" @@ -15,6 +51,12 @@ parity-wasm = { version = "0.41.0", default-features = false } log = { version = "0.4", default-features = false } byteorder = { version = "1", default-features = false } +# Dependencies only used by the binaries +glob = { version = "0.2", optional = true } +clap = { version = "2.24", optional = true } +env_logger = { version = "0.5", optional = true } +lazy_static = { version = "1.0", optional = true } + [dev-dependencies] tempdir = "0.3" wabt = "0.10" @@ -26,8 +68,10 @@ binaryen = "0.8" [features] default = ["std"] std = ["parity-wasm/std", "log/std", "byteorder/std"] - -[workspace] -members = [ - "./cli", +cli = [ + "std", + "glob", + "clap", + "env_logger", + "lazy_static", ] diff --git a/README.md b/README.md index f780e45..37032d3 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,29 @@ -# wasm-utils +# pwasm-utils [![Build Status](https://travis-ci.org/paritytech/wasm-utils.svg?branch=master)](https://travis-ci.org/paritytech/wasm-utils) -Collection of WASM utilities used in pwasm-ethereum and substrate contract development +A collection of WASM utilities used in pwasm-ethereum and substrate contract development. -## Build tools for cargo - -Easiest way to use is to install via `cargo install`: +This repository contains the package `pwasm-utils` which consists of a library crate +and a collection of cli binaries that make use of this library. +## Installation of cli tools ``` -cargo install pwasm-utils-cli --bin wasm-build +cargo install pwasm-utils --features cli ``` +This will install the following binaries: +* wasm-build +* wasm-check +* wasm-ext +* wasm-gas +* wasm-pack +* wasm-prune +* wasm-stack-height + ## Symbols pruning (wasm-prune) ``` -cargo install pwasm-utils-cli --bin wasm-prune wasm-prune ``` @@ -26,14 +34,9 @@ This will optimize WASM symbols tree to leave only those elements that are used For development puposes, raw WASM contract can be injected with gas counters (the same way as it done by pwasm-ethereum/substrate runtime when running contracts) ``` -cargo install pwasm-utils-cli --bin wasm-gas wasm-gas ``` -## API - -All executables use corresponding api methods of the root crate and can be combined in other build tools. - # License `wasm-utils` is primarily distributed under the terms of both the MIT diff --git a/cli/Cargo.toml b/cli/Cargo.toml deleted file mode 100644 index 0aa1508..0000000 --- a/cli/Cargo.toml +++ /dev/null @@ -1,51 +0,0 @@ -[package] -name = "pwasm-utils-cli" -version = "0.15.0" -authors = ["Nikolay Volf ", "Sergey Pepyakin "] -license = "MIT/Apache-2.0" -readme = "README.md" -description = "Collection of command-line utilities and corresponding Rust api for producing pwasm-compatible executables" -keywords = ["wasm", "webassembly", "pwasm"] -repository = "https://github.com/paritytech/wasm-utils" - -[lib] - -[[bin]] -name = "wasm-prune" -path = "prune/main.rs" - -[[bin]] -name = "wasm-ext" -path = "ext/main.rs" - -[[bin]] -name = "wasm-gas" -path = "gas/main.rs" - -[[bin]] -name = "wasm-build" -path = "build/main.rs" - -[[bin]] -name = "wasm-stack-height" -path = "stack_height/main.rs" - -[[bin]] -name = "wasm-pack" -path = "pack/main.rs" - -[[bin]] -name = "wasm-check" -path = "check/main.rs" - -[dependencies] -parity-wasm = "0.41.0" -pwasm-utils = { path = "..", version = "0.15" } -glob = "0.2" -clap = "2.24" -log = "0.4" -env_logger = "0.5" -lazy_static = "1.0" - -[dev-dependencies] -tempdir = "0.3" diff --git a/cli/README.md b/cli/README.md deleted file mode 100644 index 7604c69..0000000 --- a/cli/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# pwasm-utils-cli - -Collection of WASM utilities used in Parity and WASM contract devepment - -## Install - -Easiest way to use is to install via `cargo install`: - -``` -cargo install pwasm-utils-cli -``` diff --git a/cli/build/main.rs b/cli/build/main.rs index a03229a..f374d15 100644 --- a/cli/build/main.rs +++ b/cli/build/main.rs @@ -5,7 +5,7 @@ extern crate clap; extern crate glob; extern crate pwasm_utils as utils; extern crate parity_wasm; -extern crate pwasm_utils_cli as logger; +use pwasm_utils::logger; mod source; @@ -67,7 +67,7 @@ pub fn process_output(input: &source::SourceInput) -> Result<(), Error> { } fn do_main() -> Result<(), Error> { - logger::init_log(); + logger::init(); let matches = App::new("wasm-build") .version(crate_version!()) diff --git a/cli/check/main.rs b/cli/check/main.rs index d3d7a3b..7c95c7b 100644 --- a/cli/check/main.rs +++ b/cli/check/main.rs @@ -1,6 +1,6 @@ extern crate parity_wasm; extern crate pwasm_utils as utils; -extern crate pwasm_utils_cli as logger; +use pwasm_utils::logger; extern crate clap; use clap::{App, Arg}; @@ -40,7 +40,7 @@ const ALLOWED_IMPORTS: &[&str] = &[ ]; fn main() { - logger::init_log(); + logger::init(); let matches = App::new("wasm-check") .arg(Arg::with_name("input") diff --git a/cli/ext/main.rs b/cli/ext/main.rs index 02d8ba3..aabb13c 100644 --- a/cli/ext/main.rs +++ b/cli/ext/main.rs @@ -1,12 +1,12 @@ extern crate parity_wasm; extern crate pwasm_utils as utils; -extern crate pwasm_utils_cli as logger; +use pwasm_utils::logger; use std::env; fn main() { - logger::init_log(); + logger::init(); let args = env::args().collect::>(); if args.len() != 3 { diff --git a/cli/gas/main.rs b/cli/gas/main.rs index 6eec5b6..6ec45ee 100644 --- a/cli/gas/main.rs +++ b/cli/gas/main.rs @@ -1,11 +1,11 @@ extern crate parity_wasm; extern crate pwasm_utils as utils; -extern crate pwasm_utils_cli as logger; +use pwasm_utils::logger; use std::env; fn main() { - logger::init_log(); + logger::init(); let args = env::args().collect::>(); if args.len() != 3 { diff --git a/cli/pack/main.rs b/cli/pack/main.rs index 5f163e2..f96219b 100644 --- a/cli/pack/main.rs +++ b/cli/pack/main.rs @@ -1,12 +1,12 @@ extern crate parity_wasm; extern crate pwasm_utils as utils; -extern crate pwasm_utils_cli as logger; +use pwasm_utils::logger; extern crate clap; use clap::{App, Arg}; fn main() { - logger::init_log(); + logger::init(); let target_runtime = utils::TargetRuntime::pwasm(); diff --git a/cli/prune/main.rs b/cli/prune/main.rs index c23ba58..d4fcd41 100644 --- a/cli/prune/main.rs +++ b/cli/prune/main.rs @@ -1,12 +1,12 @@ extern crate parity_wasm; extern crate pwasm_utils as utils; -extern crate pwasm_utils_cli as logger; +use pwasm_utils::logger; extern crate clap; use clap::{App, Arg}; fn main() { - logger::init_log(); + logger::init(); let target_runtime = utils::TargetRuntime::pwasm(); diff --git a/cli/stack_height/main.rs b/cli/stack_height/main.rs index 774baee..1b6842a 100644 --- a/cli/stack_height/main.rs +++ b/cli/stack_height/main.rs @@ -1,12 +1,12 @@ extern crate pwasm_utils as utils; extern crate parity_wasm; -extern crate pwasm_utils_cli as logger; +use pwasm_utils::logger; use std::env; use utils::stack_height; fn main() { - logger::init_log(); + logger::init(); let args = env::args().collect::>(); if args.len() != 3 { diff --git a/src/build.rs b/src/build.rs index e79b72d..8f742fc 100644 --- a/src/build.rs +++ b/src/build.rs @@ -1,4 +1,3 @@ -use std; use super::{ optimize, pack_instance, @@ -9,8 +8,8 @@ use super::{ PackingError, OptimizerError, TargetRuntime, + std::fmt, }; -use parity_wasm; use parity_wasm::elements; #[derive(Debug)] @@ -38,8 +37,8 @@ pub enum SourceTarget { Unknown, } -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { use self::Error::*; match self { Encoding(err) => write!(f, "Encoding error ({})", err), diff --git a/src/ext.rs b/src/ext.rs index 5dc1489..3b129e7 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -1,11 +1,12 @@ -use std::string::String; -use std::vec::Vec; -use std::borrow::ToOwned; +use crate::std::string::String; +use crate::std::vec::Vec; +use crate::std::borrow::ToOwned; use parity_wasm::{elements, builder}; -use optimizer::{import_section, export_section}; use byteorder::{LittleEndian, ByteOrder}; +use crate::optimizer::{import_section, export_section}; + type Insertion = (usize, u32, u32, String); pub fn update_call_index(instructions: &mut elements::Instructions, original_imports: usize, inserts: &[Insertion]) { diff --git a/src/gas/mod.rs b/src/gas/mod.rs index 779029c..90d63ca 100644 --- a/src/gas/mod.rs +++ b/src/gas/mod.rs @@ -7,12 +7,12 @@ #[cfg(test)] mod validation; -use std::cmp::min; -use std::mem; -use std::vec::Vec; +use crate::std::cmp::min; +use crate::std::mem; +use crate::std::vec::Vec; use parity_wasm::{elements, builder}; -use rules::Rules; +use crate::rules::Rules; pub fn update_call_index(instructions: &mut elements::Instructions, inserted_index: u32) { use parity_wasm::elements::Instruction::*; @@ -518,7 +518,7 @@ mod tests { use parity_wasm::{serialize, builder, elements}; use parity_wasm::elements::Instruction::*; use super::*; - use rules; + use crate::rules; pub fn get_function_body(module: &elements::Module, index: usize) -> Option<&[elements::Instruction]> diff --git a/src/gas/validation.rs b/src/gas/validation.rs index d0b1c22..5f68dcc 100644 --- a/src/gas/validation.rs +++ b/src/gas/validation.rs @@ -9,11 +9,15 @@ //! the worst case. use super::MeteredBlock; -use rules::Set as RuleSet; -use rules::Rules; +use crate::rules::Set as RuleSet; +use crate::rules::Rules; +use crate::std::vec::Vec; use parity_wasm::elements::{FuncBody, Instruction}; -use std::collections::HashMap; +#[cfg(features = "std")] +use crate::std::collections::HashMap as Map; +#[cfg(not(features = "std"))] +use crate::std::collections::BTreeMap as Map; /// An ID for a node in a ControlFlowGraph. type NodeId = usize; @@ -288,7 +292,7 @@ fn validate_graph_gas_costs(graph: &ControlFlowGraph) -> bool { node_id: NodeId, mut total_actual: u32, mut total_charged: u32, - loop_costs: &mut HashMap, + loop_costs: &mut Map, ) -> bool { let node = graph.get_node(node_id); @@ -325,7 +329,7 @@ fn validate_graph_gas_costs(graph: &ControlFlowGraph) -> bool { } // Recursively explore all paths through the execution graph starting from the entry node. - visit(graph, 0, 0, 0, &mut HashMap::new()) + visit(graph, 0, 0, 0, &mut Map::new()) } /// Validate that the metered blocks are correct with respect to the function body by exhaustively diff --git a/src/graph.rs b/src/graph.rs index 956cc7f..41c4665 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -4,7 +4,7 @@ use parity_wasm::elements; use super::ref_list::{RefList, EntryRef}; -use std::{ +use crate::std::{ vec::Vec, borrow::ToOwned, string::String, diff --git a/src/lib.rs b/src/lib.rs index feae73d..8b42ccd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,8 @@ mod ref_list; mod symbols; #[cfg(feature = "std")] mod export_globals; +#[cfg(feature = "cli")] +pub mod logger; pub mod stack_height; @@ -81,7 +83,7 @@ impl TargetRuntime { #[cfg(not(feature = "std"))] mod std { - pub use alloc::{borrow, boxed, string, vec}; + pub use ::alloc::{borrow, boxed, string, vec}; pub use core::*; pub mod rc { @@ -92,3 +94,9 @@ mod std { pub use alloc::collections::{BTreeMap, BTreeSet}; } } + + +#[cfg(feature = "std")] +mod std { + pub use std::*; +} diff --git a/cli/src/lib.rs b/src/logger.rs similarity index 74% rename from cli/src/lib.rs rename to src/logger.rs index eeaabb0..8993eba 100644 --- a/cli/src/lib.rs +++ b/src/logger.rs @@ -1,10 +1,7 @@ -#[macro_use] extern crate log; -#[macro_use] extern crate lazy_static; -extern crate env_logger; - -use std::env; +use crate::std::env; use log::LevelFilter; use env_logger::Builder; +use lazy_static::lazy_static; lazy_static! { static ref LOG_DUMMY: bool = { @@ -22,6 +19,6 @@ lazy_static! { } /// Intialize log with default settings -pub fn init_log() { +pub fn init() { let _ = *LOG_DUMMY; } diff --git a/src/optimizer.rs b/src/optimizer.rs index 1497544..86fc8b0 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -1,13 +1,13 @@ #[cfg(features = "std")] -use std::collections::{HashSet as Set}; +use crate::std::collections::{HashSet as Set}; #[cfg(not(features = "std"))] -use std::collections::{BTreeSet as Set}; -use std::vec::Vec; -use std::mem; +use crate::std::collections::{BTreeSet as Set}; +use crate::std::vec::Vec; +use crate::std::mem; use parity_wasm::elements; -use symbols::{Symbol, expand_symbols, push_code_symbols, resolve_function}; +use crate::symbols::{Symbol, expand_symbols, push_code_symbols, resolve_function}; #[derive(Debug)] pub enum Error { diff --git a/src/pack.rs b/src/pack.rs index 0816b5f..becda22 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -1,6 +1,6 @@ -use std::fmt; -use std::vec::Vec; -use std::borrow::ToOwned; +use crate::std::fmt; +use crate::std::vec::Vec; +use crate::std::borrow::ToOwned; use parity_wasm::elements::{ self, Section, DataSection, Instruction, DataSegment, InitExpr, Internal, External, diff --git a/src/ref_list.rs b/src/ref_list.rs index 376486d..1925d8a 100644 --- a/src/ref_list.rs +++ b/src/ref_list.rs @@ -1,9 +1,9 @@ #![warn(missing_docs)] -use std::rc::Rc; -use std::cell::RefCell; -use std::vec::Vec; -use std::slice; +use crate::std::rc::Rc; +use crate::std::cell::RefCell; +use crate::std::vec::Vec; +use crate::std::slice; #[derive(Debug)] enum EntryOrigin { @@ -50,7 +50,7 @@ impl Entry { } } -impl ::std::ops::Deref for Entry { +impl crate::std::ops::Deref for Entry { type Target = T; fn deref(&self) -> &T { @@ -58,7 +58,7 @@ impl ::std::ops::Deref for Entry { } } -impl ::std::ops::DerefMut for Entry { +impl crate::std::ops::DerefMut for Entry { fn deref_mut(&mut self) -> &mut T { &mut self.val } @@ -82,14 +82,14 @@ impl From> for EntryRef { impl EntryRef { /// Read the reference data. - pub fn read(&self) -> ::std::cell::Ref> { + pub fn read(&self) -> crate::std::cell::Ref> { self.0.borrow() } /// Try to modify internal content of the referenced object. /// /// May panic if it is already borrowed. - pub fn write(&self) -> ::std::cell::RefMut> { + pub fn write(&self) -> crate::std::cell::RefMut> { self.0.borrow_mut() } diff --git a/src/rules.rs b/src/rules.rs index 0cefabf..2256a6c 100644 --- a/src/rules.rs +++ b/src/rules.rs @@ -1,9 +1,10 @@ #[cfg(features = "std")] -use std::collections::{HashMap as Map}; +use crate::std::collections::HashMap as Map; #[cfg(not(features = "std"))] -use std::collections::{BTreeMap as Map}; +use crate::std::collections::BTreeMap as Map; -use std::num::NonZeroU32; +use crate::std::num::NonZeroU32; +use crate::std::str::FromStr; use parity_wasm::elements; @@ -68,7 +69,7 @@ pub enum InstructionType { GrowMemory, } -impl ::std::str::FromStr for InstructionType { +impl FromStr for InstructionType { type Err = UnknownInstruction; fn from_str(s: &str) -> Result { diff --git a/src/stack_height/max_height.rs b/src/stack_height/max_height.rs index 23c7ea1..4849092 100644 --- a/src/stack_height/max_height.rs +++ b/src/stack_height/max_height.rs @@ -1,4 +1,4 @@ -use std::vec::Vec; +use crate::std::vec::Vec; use parity_wasm::elements::{self, BlockType, Type}; use super::{resolve_func_type, Error}; diff --git a/src/stack_height/mod.rs b/src/stack_height/mod.rs index 737d84a..d619f9e 100644 --- a/src/stack_height/mod.rs +++ b/src/stack_height/mod.rs @@ -49,8 +49,8 @@ //! between the frames. //! - upon entry into the function entire stack frame is allocated. -use std::string::String; -use std::vec::Vec; +use crate::std::string::String; +use crate::std::vec::Vec; use parity_wasm::elements::{self, Type}; use parity_wasm::builder; diff --git a/src/stack_height/thunk.rs b/src/stack_height/thunk.rs index aaa02ee..11a377d 100644 --- a/src/stack_height/thunk.rs +++ b/src/stack_height/thunk.rs @@ -1,8 +1,8 @@ #[cfg(features = "std")] -use std::collections::{HashMap as Map}; +use crate::std::collections::{HashMap as Map}; #[cfg(not(features = "std"))] -use std::collections::{BTreeMap as Map}; -use std::vec::Vec; +use crate::std::collections::{BTreeMap as Map}; +use crate::std::vec::Vec; use parity_wasm::elements::{self, FunctionType, Internal}; use parity_wasm::builder; diff --git a/src/symbols.rs b/src/symbols.rs index 60b6ef1..8586e65 100644 --- a/src/symbols.rs +++ b/src/symbols.rs @@ -1,8 +1,8 @@ #[cfg(features = "std")] -use std::collections::{HashSet as Set}; +use crate::std::collections::{HashSet as Set}; #[cfg(not(features = "std"))] -use std::collections::{BTreeSet as Set}; -use std::vec::Vec; +use crate::std::collections::{BTreeSet as Set}; +use crate::std::vec::Vec; use parity_wasm::elements;