mirror of
https://github.com/pezkuwichain/wasm-instrument.git
synced 2026-06-13 19:51:08 +00:00
finalize tool
This commit is contained in:
+21
-4
@@ -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<io::Error> 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 {
|
||||
|
||||
+18
-3
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user