mirror of
https://github.com/fluencelabs/wasm-utils
synced 2025-07-01 15:31:44 +00:00
finalize tool
This commit is contained in:
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||||||
authors = ["NikVolf <nikvolf@gmail.com>"]
|
authors = ["NikVolf <nikvolf@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
parity-wasm = "0.15"
|
parity-wasm = "0.18"
|
||||||
log = "0.3"
|
log = "0.3"
|
||||||
env_logger = "0.4"
|
env_logger = "0.4"
|
||||||
lazy_static = "0.2"
|
lazy_static = "0.2"
|
||||||
|
8
build/Cargo.lock
generated
8
build/Cargo.lock
generated
@ -123,7 +123,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parity-wasm"
|
name = "parity-wasm"
|
||||||
version = "0.15.4"
|
version = "0.18.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
@ -275,7 +275,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"clap 2.27.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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)",
|
"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)",
|
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"wasm-utils 0.1.0",
|
"wasm-utils 0.1.0",
|
||||||
]
|
]
|
||||||
@ -290,7 +290,7 @@ dependencies = [
|
|||||||
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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)",
|
"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)",
|
"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]]
|
[[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 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 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 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 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 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"
|
"checksum rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6475140dfd8655aeb72e1fd4b7a1cc1c202be65d71669476e392fe62532b9edd"
|
||||||
|
@ -7,7 +7,7 @@ authors = ["NikVolf <nikvolf@gmail.com>"]
|
|||||||
glob = "0.2"
|
glob = "0.2"
|
||||||
wasm-utils = { path = "../" }
|
wasm-utils = { path = "../" }
|
||||||
clap = "2.24"
|
clap = "2.24"
|
||||||
parity-wasm = "0.15"
|
parity-wasm = "0.18"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempdir = "0.3"
|
tempdir = "0.3"
|
||||||
|
@ -14,7 +14,7 @@ use std::path::PathBuf;
|
|||||||
use clap::{App, Arg};
|
use clap::{App, Arg};
|
||||||
use parity_wasm::elements;
|
use parity_wasm::elements;
|
||||||
|
|
||||||
use wasm_utils::{CREATE_SYMBOL, CALL_SYMBOL};
|
use wasm_utils::{CREATE_SYMBOL, CALL_SYMBOL, underscore_funcs};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
@ -32,19 +32,24 @@ impl From<io::Error> for Error {
|
|||||||
|
|
||||||
pub fn wasm_path(input: &source::SourceInput) -> String {
|
pub fn wasm_path(input: &source::SourceInput) -> String {
|
||||||
let mut path = PathBuf::from(input.target_dir());
|
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()
|
path.to_string_lossy().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_output(input: &source::SourceInput) -> Result<(), Error> {
|
pub fn process_output(input: &source::SourceInput) -> Result<(), Error> {
|
||||||
let mut cargo_path = PathBuf::from(input.target_dir());
|
let mut cargo_path = PathBuf::from(input.target_dir());
|
||||||
let wasm_name = input.bin_name().to_string().replace("-", "_");
|
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("release");
|
||||||
cargo_path.push(format!("{}.wasm", wasm_name));
|
cargo_path.push(format!("{}.wasm", wasm_name));
|
||||||
|
|
||||||
let mut target_path = PathBuf::from(input.target_dir());
|
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)?;
|
fs::copy(cargo_path, target_path)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -85,6 +90,10 @@ fn main() {
|
|||||||
.help("Skip symbol optimization step producing final wasm")
|
.help("Skip symbol optimization step producing final wasm")
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.long("target"))
|
.long("target"))
|
||||||
|
.arg(Arg::with_name("final_name")
|
||||||
|
.help("Final wasm binary name")
|
||||||
|
.takes_value(true)
|
||||||
|
.long("final"))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
let target_dir = matches.value_of("target").expect("is required; qed");
|
let target_dir = matches.value_of("target").expect("is required; qed");
|
||||||
@ -101,12 +110,20 @@ fn main() {
|
|||||||
::std::process::exit(1);
|
::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");
|
process_output(&source_input).expect("Failed to process cargo target directory");
|
||||||
|
|
||||||
let path = wasm_path(&source_input);
|
let path = wasm_path(&source_input);
|
||||||
|
|
||||||
let mut module = parity_wasm::deserialize_file(&path).unwrap();
|
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") {
|
if let Some(runtime_type) = matches.value_of("runtime_type") {
|
||||||
let runtime_type: &[u8] = runtime_type.as_bytes();
|
let runtime_type: &[u8] = runtime_type.as_bytes();
|
||||||
if runtime_type.len() != 4 {
|
if runtime_type.len() != 4 {
|
||||||
|
@ -4,7 +4,7 @@ pub const UNKNOWN_PATH: &str = "wasm32-unknown-unknown";
|
|||||||
pub const EMSCRIPTEN_PATH: &str = "wasm32-unknown-emscripten";
|
pub const EMSCRIPTEN_PATH: &str = "wasm32-unknown-emscripten";
|
||||||
|
|
||||||
/// Target configiration of previous build step
|
/// Target configiration of previous build step
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum SourceTarget {
|
pub enum SourceTarget {
|
||||||
Emscripten,
|
Emscripten,
|
||||||
Unknown,
|
Unknown,
|
||||||
@ -15,6 +15,7 @@ pub enum SourceTarget {
|
|||||||
pub struct SourceInput<'a> {
|
pub struct SourceInput<'a> {
|
||||||
target_dir: &'a str,
|
target_dir: &'a str,
|
||||||
bin_name: &'a str,
|
bin_name: &'a str,
|
||||||
|
final_name: &'a str,
|
||||||
target: SourceTarget,
|
target: SourceTarget,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,6 +24,7 @@ impl<'a> SourceInput<'a> {
|
|||||||
SourceInput {
|
SourceInput {
|
||||||
target_dir: target_dir,
|
target_dir: target_dir,
|
||||||
bin_name: bin_name,
|
bin_name: bin_name,
|
||||||
|
final_name: bin_name,
|
||||||
target: SourceTarget::Emscripten,
|
target: SourceTarget::Emscripten,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -37,11 +39,24 @@ impl<'a> SourceInput<'a> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_final(mut self, final_name: &'a str) -> Self {
|
||||||
|
self.final_name = final_name;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn target_dir(&self) -> &str {
|
pub fn target_dir(&self) -> &str {
|
||||||
&self.target_dir
|
self.target_dir
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bin_name(&self) -> &str {
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
16
src/ext.rs
16
src/ext.rs
@ -1,5 +1,5 @@
|
|||||||
use parity_wasm::{elements, builder};
|
use parity_wasm::{elements, builder};
|
||||||
use optimizer::import_section;
|
use optimizer::{import_section, export_section};
|
||||||
|
|
||||||
type Insertion = (usize, u32, u32, String);
|
type Insertion = (usize, u32, u32, String);
|
||||||
|
|
||||||
@ -49,6 +49,20 @@ pub fn externalize_mem(mut module: elements::Module) -> elements::Module {
|
|||||||
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(
|
pub fn externalize(
|
||||||
module: elements::Module,
|
module: elements::Module,
|
||||||
replaced_funcs: Vec<&str>,
|
replaced_funcs: Vec<&str>,
|
||||||
|
@ -21,7 +21,7 @@ mod runtime_type;
|
|||||||
pub use optimizer::{optimize, Error as OptimizerError};
|
pub use optimizer::{optimize, Error as OptimizerError};
|
||||||
pub use gas::inject_gas_counter;
|
pub use gas::inject_gas_counter;
|
||||||
pub use logger::init_log;
|
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 pack::pack_instance;
|
||||||
pub use nondeterminism_check::is_deterministic;
|
pub use nondeterminism_check::is_deterministic;
|
||||||
pub use runtime_type::inject_runtime_type;
|
pub use runtime_type::inject_runtime_type;
|
||||||
|
Reference in New Issue
Block a user