From f146187e00f489c88c5e3968634d4f014f6de9a4 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Wed, 27 Dec 2017 12:12:09 +0300 Subject: [PATCH] finalize tool --- Cargo.toml | 2 +- build/Cargo.lock | 8 ++++---- build/Cargo.toml | 2 +- build/src/main.rs | 25 +++++++++++++++++++++---- build/src/source.rs | 21 ++++++++++++++++++--- src/ext.rs | 16 +++++++++++++++- src/lib.rs | 2 +- 7 files changed, 61 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1564f03..1b52325 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["NikVolf "] [dependencies] -parity-wasm = "0.15" +parity-wasm = "0.18" log = "0.3" env_logger = "0.4" lazy_static = "0.2" diff --git a/build/Cargo.lock b/build/Cargo.lock index d5dcbf5..3f9003c 100644 --- a/build/Cargo.lock +++ b/build/Cargo.lock @@ -123,7 +123,7 @@ dependencies = [ [[package]] name = "parity-wasm" -version = "0.15.4" +version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -275,7 +275,7 @@ version = "0.1.0" dependencies = [ "clap 2.27.1 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "parity-wasm 0.15.4 (registry+https://github.com/rust-lang/crates.io-index)", + "parity-wasm 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "wasm-utils 0.1.0", ] @@ -290,7 +290,7 @@ dependencies = [ "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", - "parity-wasm 0.15.4 (registry+https://github.com/rust-lang/crates.io-index)", + "parity-wasm 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -321,7 +321,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b" "checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a" "checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37" -"checksum parity-wasm 0.15.4 (registry+https://github.com/rust-lang/crates.io-index)" = "235801e9531998c4bb307f4ea6833c9f40a4cf132895219ac8c2cd25a9b310f7" +"checksum parity-wasm 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f02e35fda913b8873799b817dcab145d1f935a900722ab7274027949d9947a56" "checksum parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "149d8f5b97f3c1133e3cfcd8886449959e856b557ff281e292b733d7c69e005e" "checksum parking_lot_core 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "4f610cb9664da38e417ea3225f23051f589851999535290e077939838ab7a595" "checksum rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6475140dfd8655aeb72e1fd4b7a1cc1c202be65d71669476e392fe62532b9edd" diff --git a/build/Cargo.toml b/build/Cargo.toml index 00febda..6871035 100644 --- a/build/Cargo.toml +++ b/build/Cargo.toml @@ -7,7 +7,7 @@ authors = ["NikVolf "] glob = "0.2" wasm-utils = { path = "../" } clap = "2.24" -parity-wasm = "0.15" +parity-wasm = "0.18" [dev-dependencies] tempdir = "0.3" diff --git a/build/src/main.rs b/build/src/main.rs index eaf1b5d..a3355a9 100644 --- a/build/src/main.rs +++ b/build/src/main.rs @@ -14,7 +14,7 @@ use std::path::PathBuf; use clap::{App, Arg}; use parity_wasm::elements; -use wasm_utils::{CREATE_SYMBOL, CALL_SYMBOL}; +use wasm_utils::{CREATE_SYMBOL, CALL_SYMBOL, underscore_funcs}; #[derive(Debug)] pub enum Error { @@ -32,19 +32,24 @@ impl From for Error { pub fn wasm_path(input: &source::SourceInput) -> String { let mut path = PathBuf::from(input.target_dir()); - path.push(format!("{}.wasm", input.bin_name())); + path.push(format!("{}.wasm", input.final_name())); path.to_string_lossy().to_string() } pub fn process_output(input: &source::SourceInput) -> Result<(), Error> { let mut cargo_path = PathBuf::from(input.target_dir()); let wasm_name = input.bin_name().to_string().replace("-", "_"); - cargo_path.push(source::EMSCRIPTEN_PATH); + cargo_path.push( + match input.target() { + source::SourceTarget::Emscripten => source::EMSCRIPTEN_PATH, + source::SourceTarget::Unknown => source::UNKNOWN_PATH, + } + ); cargo_path.push("release"); cargo_path.push(format!("{}.wasm", wasm_name)); let mut target_path = PathBuf::from(input.target_dir()); - target_path.push(format!("{}.wasm", input.bin_name())); + target_path.push(format!("{}.wasm", input.final_name())); fs::copy(cargo_path, target_path)?; Ok(()) @@ -85,6 +90,10 @@ fn main() { .help("Skip symbol optimization step producing final wasm") .takes_value(true) .long("target")) + .arg(Arg::with_name("final_name") + .help("Final wasm binary name") + .takes_value(true) + .long("final")) .get_matches(); let target_dir = matches.value_of("target").expect("is required; qed"); @@ -101,12 +110,20 @@ fn main() { ::std::process::exit(1); } + if let Some(final_name) = matches.value_of("final_name") { + source_input = source_input.with_final(final_name); + } + process_output(&source_input).expect("Failed to process cargo target directory"); let path = wasm_path(&source_input); let mut module = parity_wasm::deserialize_file(&path).unwrap(); + if let source::SourceTarget::Unknown = source_input.target() { + module = underscore_funcs(module) + } + if let Some(runtime_type) = matches.value_of("runtime_type") { let runtime_type: &[u8] = runtime_type.as_bytes(); if runtime_type.len() != 4 { diff --git a/build/src/source.rs b/build/src/source.rs index ff01083..bdeb230 100644 --- a/build/src/source.rs +++ b/build/src/source.rs @@ -4,7 +4,7 @@ pub const UNKNOWN_PATH: &str = "wasm32-unknown-unknown"; pub const EMSCRIPTEN_PATH: &str = "wasm32-unknown-emscripten"; /// Target configiration of previous build step -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub enum SourceTarget { Emscripten, Unknown, @@ -15,6 +15,7 @@ pub enum SourceTarget { pub struct SourceInput<'a> { target_dir: &'a str, bin_name: &'a str, + final_name: &'a str, target: SourceTarget, } @@ -23,6 +24,7 @@ impl<'a> SourceInput<'a> { SourceInput { target_dir: target_dir, bin_name: bin_name, + final_name: bin_name, target: SourceTarget::Emscripten, } } @@ -37,11 +39,24 @@ impl<'a> SourceInput<'a> { self } + pub fn with_final(mut self, final_name: &'a str) -> Self { + self.final_name = final_name; + self + } + pub fn target_dir(&self) -> &str { - &self.target_dir + self.target_dir } pub fn bin_name(&self) -> &str { - &self.bin_name + self.bin_name + } + + pub fn final_name(&self) -> &str { + self.final_name + } + + pub fn target(&self) -> SourceTarget { + self.target } } \ No newline at end of file diff --git a/src/ext.rs b/src/ext.rs index 479d770..10572e7 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -1,5 +1,5 @@ use parity_wasm::{elements, builder}; -use optimizer::import_section; +use optimizer::{import_section, export_section}; type Insertion = (usize, u32, u32, String); @@ -49,6 +49,20 @@ pub fn externalize_mem(mut module: elements::Module) -> elements::Module { module } +pub fn underscore_funcs(mut module: elements::Module) -> elements::Module { + for entry in import_section(&mut module).expect("Import section to exist").entries_mut() { + if let elements::External::Function(_) = *entry.external() { + entry.field_mut().insert(0, '_'); + } + } + for entry in export_section(&mut module).expect("Import section to exist").entries_mut() { + if let elements::Internal::Function(_) = *entry.internal() { + entry.field_mut().insert(0, '_'); + } + } + module +} + pub fn externalize( module: elements::Module, replaced_funcs: Vec<&str>, diff --git a/src/lib.rs b/src/lib.rs index 6fe4b75..afdb5e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,7 +21,7 @@ mod runtime_type; pub use optimizer::{optimize, Error as OptimizerError}; pub use gas::inject_gas_counter; pub use logger::init_log; -pub use ext::{externalize, externalize_mem}; +pub use ext::{externalize, externalize_mem, underscore_funcs}; pub use pack::pack_instance; pub use nondeterminism_check::is_deterministic; pub use runtime_type::inject_runtime_type;