mirror of
https://github.com/fluencelabs/marine.git
synced 2024-12-12 14:55:32 +00:00
add delete subcommand to wit_embedder
This commit is contained in:
parent
59f9b7555f
commit
5d7fdec0ee
53
crates/fce_wit_interfaces/src/wit_parser/deleter.rs
Normal file
53
crates/fce_wit_interfaces/src/wit_parser/deleter.rs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020 Fluence Labs Limited
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use super::errors::WITParserError;
|
||||||
|
use super::custom::WIT_SECTION_NAME;
|
||||||
|
|
||||||
|
use walrus::ModuleConfig;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
pub fn delete_wit_section(
|
||||||
|
in_wasm_path: PathBuf,
|
||||||
|
out_wasm_path: PathBuf,
|
||||||
|
) -> Result<(), WITParserError> {
|
||||||
|
let mut module = ModuleConfig::new()
|
||||||
|
.parse_file(&in_wasm_path)
|
||||||
|
.map_err(WITParserError::CorruptedWasmFile)?;
|
||||||
|
|
||||||
|
let wit_section_ids = module
|
||||||
|
.customs
|
||||||
|
.iter()
|
||||||
|
.filter_map(|(id, section)| {
|
||||||
|
if section.name() == WIT_SECTION_NAME {
|
||||||
|
Some(id)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
for id in wit_section_ids {
|
||||||
|
module.customs.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
module
|
||||||
|
.emit_wasm_file(&out_wasm_path)
|
||||||
|
.map_err(WITParserError::WasmEmitError)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
@ -25,18 +25,16 @@ use wasmer_wit::{
|
|||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub struct EmbedderConfig {
|
pub fn embed_text_wit(
|
||||||
pub in_wasm_path: PathBuf,
|
in_wasm_path: PathBuf,
|
||||||
pub out_wasm_path: PathBuf,
|
out_wasm_path: PathBuf,
|
||||||
pub wit: String,
|
wit: &str,
|
||||||
}
|
) -> Result<(), WITParserError> {
|
||||||
|
|
||||||
pub fn embed_text_wit(options: &EmbedderConfig) -> Result<(), WITParserError> {
|
|
||||||
let mut module = ModuleConfig::new()
|
let mut module = ModuleConfig::new()
|
||||||
.parse_file(&options.in_wasm_path)
|
.parse_file(&in_wasm_path)
|
||||||
.map_err(WITParserError::CorruptedWasmFile)?;
|
.map_err(WITParserError::CorruptedWasmFile)?;
|
||||||
|
|
||||||
let buffer = Buffer::new(&options.wit)?;
|
let buffer = Buffer::new(wit)?;
|
||||||
let ast = parse(&buffer)?;
|
let ast = parse(&buffer)?;
|
||||||
|
|
||||||
let mut bytes = vec![];
|
let mut bytes = vec![];
|
||||||
@ -45,7 +43,7 @@ pub fn embed_text_wit(options: &EmbedderConfig) -> Result<(), WITParserError> {
|
|||||||
let custom = WITCustom(bytes);
|
let custom = WITCustom(bytes);
|
||||||
module.customs.add(custom);
|
module.customs.add(custom);
|
||||||
module
|
module
|
||||||
.emit_wasm_file(&options.out_wasm_path)
|
.emit_wasm_file(&out_wasm_path)
|
||||||
.map_err(WITParserError::WasmEmitError)?;
|
.map_err(WITParserError::WasmEmitError)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -18,11 +18,11 @@ mod custom;
|
|||||||
mod errors;
|
mod errors;
|
||||||
mod extractor;
|
mod extractor;
|
||||||
mod embedder;
|
mod embedder;
|
||||||
|
mod deleter;
|
||||||
|
|
||||||
pub use errors::WITParserError;
|
pub use errors::WITParserError;
|
||||||
|
|
||||||
pub use embedder::EmbedderConfig;
|
|
||||||
pub use embedder::embed_text_wit;
|
pub use embedder::embed_text_wit;
|
||||||
|
pub use deleter::delete_wit_section;
|
||||||
pub use extractor::extract_fce_wit;
|
pub use extractor::extract_fce_wit;
|
||||||
pub use extractor::extract_text_wit;
|
pub use extractor::extract_text_wit;
|
||||||
|
@ -18,8 +18,8 @@ use super::wit_prelude::*;
|
|||||||
use super::fce_module::FCEModule;
|
use super::fce_module::FCEModule;
|
||||||
|
|
||||||
use fce_wit_interfaces::FCEWITInterfaces;
|
use fce_wit_interfaces::FCEWITInterfaces;
|
||||||
use wasmer_wit::interpreter::wasm;
|
|
||||||
use fce_wit_interfaces::WITAstType;
|
use fce_wit_interfaces::WITAstType;
|
||||||
|
use wasmer_wit::interpreter::wasm;
|
||||||
use wasmer_wit::interpreter::wasm::structures::{LocalImportIndex, TypedIndex};
|
use wasmer_wit::interpreter::wasm::structures::{LocalImportIndex, TypedIndex};
|
||||||
use wasmer_core::Instance as WasmerInstance;
|
use wasmer_core::Instance as WasmerInstance;
|
||||||
|
|
||||||
|
56
tools/wit_embedder/src/args.rs
Normal file
56
tools/wit_embedder/src/args.rs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
use clap::{App, Arg, SubCommand};
|
||||||
|
|
||||||
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
pub const AUTHORS: &str = env!("CARGO_PKG_AUTHORS");
|
||||||
|
pub const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
|
||||||
|
|
||||||
|
pub const IN_WASM_PATH: &str = "in-wasm-path";
|
||||||
|
pub const WIT_PATH: &str = "wit-path";
|
||||||
|
pub const OUT_WASM_PATH: &str = "out-wasm-path";
|
||||||
|
|
||||||
|
pub fn embed_wit<'a, 'b>() -> App<'a, 'b> {
|
||||||
|
SubCommand::with_name("embed")
|
||||||
|
.about("embed WIT to provided Wasm file")
|
||||||
|
.args(&[
|
||||||
|
Arg::with_name(IN_WASM_PATH)
|
||||||
|
.required(true)
|
||||||
|
.takes_value(true)
|
||||||
|
.short("i")
|
||||||
|
.help("path to the wasm file"),
|
||||||
|
Arg::with_name(WIT_PATH)
|
||||||
|
.required(true)
|
||||||
|
.takes_value(true)
|
||||||
|
.short("w")
|
||||||
|
.help("path to file with WIT"),
|
||||||
|
Arg::with_name(OUT_WASM_PATH)
|
||||||
|
.takes_value(true)
|
||||||
|
.short("o")
|
||||||
|
.help("path to result file with embedded WIT"),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show_wit<'a, 'b>() -> App<'a, 'b> {
|
||||||
|
SubCommand::with_name("show")
|
||||||
|
.about("show WIT in provided Wasm file")
|
||||||
|
.args(&[Arg::with_name(IN_WASM_PATH)
|
||||||
|
.required(true)
|
||||||
|
.takes_value(true)
|
||||||
|
.short("i")
|
||||||
|
.help("path to the wasm file")])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete_wit<'a, 'b>() -> App<'a, 'b> {
|
||||||
|
SubCommand::with_name("delete")
|
||||||
|
.about("delete all WIT sections in provided Wasm file")
|
||||||
|
.args(&[
|
||||||
|
Arg::with_name(IN_WASM_PATH)
|
||||||
|
.required(true)
|
||||||
|
.takes_value(true)
|
||||||
|
.short("i")
|
||||||
|
.help("path to the wasm file"),
|
||||||
|
Arg::with_name(OUT_WASM_PATH)
|
||||||
|
.takes_value(true)
|
||||||
|
.short("o")
|
||||||
|
.help("path to result file with deleted WIT sections"),
|
||||||
|
])
|
||||||
|
}
|
@ -13,54 +13,28 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
#![warn(rust_2018_idioms)]
|
||||||
|
#![deny(
|
||||||
|
dead_code,
|
||||||
|
nonstandard_style,
|
||||||
|
unused_imports,
|
||||||
|
unused_mut,
|
||||||
|
unused_variables,
|
||||||
|
unused_unsafe,
|
||||||
|
unreachable_patterns
|
||||||
|
)]
|
||||||
|
|
||||||
|
mod args;
|
||||||
|
|
||||||
|
use args::*;
|
||||||
|
|
||||||
use fce_wit_interfaces::EmbedderConfig;
|
|
||||||
use fce_wit_interfaces::embed_text_wit;
|
use fce_wit_interfaces::embed_text_wit;
|
||||||
use fce_wit_interfaces::extract_text_wit;
|
use fce_wit_interfaces::extract_text_wit;
|
||||||
|
use fce_wit_interfaces::delete_wit_section;
|
||||||
|
|
||||||
use clap::{App, AppSettings, Arg, SubCommand};
|
use clap::{App, AppSettings};
|
||||||
use failure::err_msg;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
|
||||||
const AUTHORS: &str = env!("CARGO_PKG_AUTHORS");
|
|
||||||
const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
|
|
||||||
|
|
||||||
const IN_WASM_PATH: &str = "in-wasm-path";
|
|
||||||
const WIT_PATH: &str = "wit-path";
|
|
||||||
const OUT_WASM_PATH: &str = "out-wasm-path";
|
|
||||||
|
|
||||||
fn embed_wit<'a, 'b>() -> App<'a, 'b> {
|
|
||||||
SubCommand::with_name("embed")
|
|
||||||
.about("embed WIT to provided Wasm file")
|
|
||||||
.args(&[
|
|
||||||
Arg::with_name(IN_WASM_PATH)
|
|
||||||
.required(true)
|
|
||||||
.takes_value(true)
|
|
||||||
.short("i")
|
|
||||||
.help("path to the wasm file"),
|
|
||||||
Arg::with_name(WIT_PATH)
|
|
||||||
.required(true)
|
|
||||||
.takes_value(true)
|
|
||||||
.short("w")
|
|
||||||
.help("path to file with WIT"),
|
|
||||||
Arg::with_name(OUT_WASM_PATH)
|
|
||||||
.takes_value(true)
|
|
||||||
.short("o")
|
|
||||||
.help("path to result file with embedded WIT"),
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
fn show_wit<'a, 'b>() -> App<'a, 'b> {
|
|
||||||
SubCommand::with_name("show")
|
|
||||||
.about("show WIT in provided Wasm file")
|
|
||||||
.args(&[Arg::with_name(IN_WASM_PATH)
|
|
||||||
.required(true)
|
|
||||||
.takes_value(true)
|
|
||||||
.short("i")
|
|
||||||
.help("path to the wasm file")])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn main() -> Result<(), exitfailure::ExitFailure> {
|
pub fn main() -> Result<(), exitfailure::ExitFailure> {
|
||||||
let app = App::new("CLI tool for embedding WIT to provided Wasm file")
|
let app = App::new("CLI tool for embedding WIT to provided Wasm file")
|
||||||
.version(VERSION)
|
.version(VERSION)
|
||||||
@ -68,7 +42,8 @@ pub fn main() -> Result<(), exitfailure::ExitFailure> {
|
|||||||
.about(DESCRIPTION)
|
.about(DESCRIPTION)
|
||||||
.setting(AppSettings::ArgRequiredElseHelp)
|
.setting(AppSettings::ArgRequiredElseHelp)
|
||||||
.subcommand(embed_wit())
|
.subcommand(embed_wit())
|
||||||
.subcommand(show_wit());
|
.subcommand(show_wit())
|
||||||
|
.subcommand(delete_wit());
|
||||||
|
|
||||||
match app.get_matches().subcommand() {
|
match app.get_matches().subcommand() {
|
||||||
("embed", Some(arg)) => {
|
("embed", Some(arg)) => {
|
||||||
@ -81,20 +56,11 @@ pub fn main() -> Result<(), exitfailure::ExitFailure> {
|
|||||||
|
|
||||||
let wit = String::from_utf8(std::fs::read(wit_path)?).unwrap();
|
let wit = String::from_utf8(std::fs::read(wit_path)?).unwrap();
|
||||||
|
|
||||||
let options = EmbedderConfig {
|
embed_text_wit(
|
||||||
in_wasm_path: PathBuf::from(in_wasm_path),
|
PathBuf::from(in_wasm_path),
|
||||||
wit,
|
PathBuf::from(out_wasm_path),
|
||||||
out_wasm_path: PathBuf::from(out_wasm_path),
|
&wit,
|
||||||
};
|
)?;
|
||||||
|
|
||||||
match embed_text_wit(&options) {
|
|
||||||
Ok(_) => {
|
|
||||||
println!("WIT successfully embedded");
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
println!("{}", e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -107,6 +73,17 @@ pub fn main() -> Result<(), exitfailure::ExitFailure> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
c => Err(err_msg(format!("Unexpected command: {}", c.0)).into()),
|
("delete", Some(arg)) => {
|
||||||
|
let in_wasm_path = arg.value_of(IN_WASM_PATH).unwrap();
|
||||||
|
let out_wasm_path = match arg.value_of(OUT_WASM_PATH) {
|
||||||
|
Some(path) => path,
|
||||||
|
None => in_wasm_path,
|
||||||
|
};
|
||||||
|
|
||||||
|
delete_wit_section(PathBuf::from(in_wasm_path), PathBuf::from(out_wasm_path))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
c => Err(failure::err_msg(format!("Unexpected command: {}", c.0)).into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user