Skip to content

Commit

Permalink
Move the wasm-tools CLI to the src/bin directory (#1144)
Browse files Browse the repository at this point in the history
* Move the wasm-tools CLI to the `src/bin` directory

This was a standalone `cli.rs` from when it was originally a separate
executable, but nowadays each crate has its own library interface and
all executables are slurped up into the `src/bin` directory

* Fix dependencies for just-wasm-compose
  • Loading branch information
alexcrichton authored Jul 26, 2023
1 parent c18d59f commit 6f4508a
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 127 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ wasm-encoder = { workspace = true, optional = true }
regex = { version = "1.6.0", optional = true }

# Dependencies of `compose`
wasm-compose = { workspace = true, optional = true, features = ['cli'] }
wasm-compose = { workspace = true, optional = true }

# Dependencies of `demangle`
rustc-demangle = { version = "0.1.21", optional = true }
Expand Down Expand Up @@ -163,7 +163,7 @@ mutate = ['wasm-mutate']
dump = ['dep:wasmparser']
objdump = ['dep:wasmparser']
strip = ['wasm-encoder', 'dep:wasmparser', 'regex']
compose = ['wasm-compose']
compose = ['wasm-compose', 'dep:wasmparser']
demangle = ['rustc-demangle', 'cpp_demangle', 'dep:wasmparser', 'wasm-encoder']
component = [
'wit-component',
Expand Down
5 changes: 0 additions & 5 deletions crates/wasm-compose/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ serde = { workspace = true }
petgraph = "0.6.2"
log = { workspace = true }
serde_yaml = "0.9.22"
clap = { workspace = true, optional = true }
smallvec = "1.10.0"
heck = "0.4.0"

[features]
default = []
cli = ["clap"]

[dev-dependencies]
glob = "0.3.0"
pretty_assertions = "1.2.1"
Expand Down
113 changes: 0 additions & 113 deletions crates/wasm-compose/src/cli.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/wasm-compose/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#![deny(missing_docs)]

#[cfg(feature = "cli")]
pub mod cli;
pub mod composer;
pub mod config;
pub(crate) mod encoding;
Expand Down
108 changes: 103 additions & 5 deletions src/bin/wasm-tools/compose.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
use anyhow::Result;
//! Module for CLI parsing.

use anyhow::{Context, Result};
use clap::Parser;
use wasm_compose::cli::WasmComposeCommand;
use std::path::{Path, PathBuf};
use wasm_compose::{composer::ComponentComposer, config::Config};
use wasmparser::{Validator, WasmFeatures};

/// WebAssembly component composer.
///
/// A tool for composing WebAssembly components together.
#[derive(Parser)]
#[clap(name = "component-encoder", version = env!("CARGO_PKG_VERSION"))]
pub struct Opts {
#[clap(flatten)]
cmd: WasmComposeCommand,
#[clap(flatten)]
general: wasm_tools::GeneralOpts,

/// The path of the output composed WebAssembly component.
#[clap(long, short = 'o', value_name = "OUTPUT")]
output: PathBuf,

/// The path to the configuration file to use.
#[clap(long, short = 'c', value_name = "CONFIG")]
config: Option<PathBuf>,

/// Definition components whose exports define import dependencies to fulfill from.
#[clap(long = "definitions", short = 'd', value_name = "DEFS")]
defs: Vec<PathBuf>,

/// A path to search for imports.
#[clap(long = "search-path", short = 'p', value_name = "PATH")]
paths: Vec<PathBuf>,

/// Skip validation of the composed output component.
#[clap(long)]
skip_validation: bool,

/// Do not allow instance imports in the composed output component.
#[clap(long = "no-imports")]
disallow_imports: bool,

/// The path to the root component to compose.
#[clap(value_name = "COMPONENT")]
component: PathBuf,
}

impl Opts {
Expand All @@ -16,6 +50,70 @@ impl Opts {
}

pub fn run(self) -> Result<()> {
self.cmd.execute()
let config = self.create_config()?;
log::debug!("configuration:\n{:#?}", config);

let bytes = ComponentComposer::new(&self.component, &config).compose()?;

std::fs::write(&self.output, &bytes).with_context(|| {
format!(
"failed to write composed component `{output}`",
output = self.output.display()
)
})?;

if config.skip_validation {
log::debug!("output validation was skipped");
} else {
Validator::new_with_features(WasmFeatures {
component_model: true,
..Default::default()
})
.validate_all(&bytes)
.with_context(|| {
format!(
"failed to validate output component `{output}`",
output = self.output.display()
)
})?;

log::debug!("output component validated successfully");
}

println!(
"composed component `{output}`",
output = self.output.display()
);

Ok(())
}

fn create_config(&self) -> Result<Config> {
let mut config = if let Some(config) = &self.config {
Config::from_file(config)?
} else {
// Pretend a default configuration file is sitting next to the component
Config {
dir: self
.component
.parent()
.map(Path::to_path_buf)
.unwrap_or_default(),
..Default::default()
}
};

// Use paths relative to the current directory; otherwise, the paths are interpreted as
// relative to the configuration file.
let cur_dir = std::env::current_dir().context("failed to get current directory")?;
config
.definitions
.extend(self.defs.iter().map(|p| cur_dir.join(p)));
config
.search_paths
.extend(self.paths.iter().map(|p| cur_dir.join(p)));
config.skip_validation |= self.skip_validation;
config.disallow_imports |= self.disallow_imports;
Ok(config)
}
}

0 comments on commit 6f4508a

Please sign in to comment.