Skip to content

Commit

Permalink
Merge branch 'feature/headless-wasmer' into feature/headless-wasmer-r…
Browse files Browse the repository at this point in the history
…evamp
  • Loading branch information
syrusakbary authored Jan 12, 2021
2 parents 1aa6da0 + 3178121 commit 59de326
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 105 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ jobs:
target: ${{ matrix.target }}
override: true
components: "rust-src"
#if: needs.setup.outputs.DOING_RELEASE == '1'
if: needs.setup.outputs.DOING_RELEASE == '1'
- name: Build Minimal Wasmer Headless
run: |
echo "\n[profile.release]
Expand All @@ -212,7 +212,7 @@ incremental = false
codegen-units = 1
rpath = false" >> Cargo.toml
make build-wasmer-headless-minimal
#if: needs.setup.outputs.DOING_RELEASE == '1'
if: needs.setup.outputs.DOING_RELEASE == '1'
- name: Copy target binaries
run: |
mkdir -p target/release
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ build-wasmer-debug:
# codegen-units = 1
# rpath = false
build-wasmer-headless-minimal:
RUSTFLAGS="-C panic=abort" xargo build -v --target $(HOST_TARGET) --release --manifest-path=lib/cli/Cargo.toml --no-default-features --features headless --bin wasmer-headless
RUSTFLAGS="-C panic=abort" xargo build -v --target $(HOST_TARGET) --release --manifest-path=lib/cli/Cargo.toml --no-default-features --features headless-minimal --bin wasmer-headless
ifeq ($(UNAME_S), Darwin)
strip -u target/$(HOST_TARGET)/release/wasmer-headless
else
Expand Down
6 changes: 6 additions & 0 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ name = "wasmer"
path = "src/bin/wasmer.rs"
doc = false

[[bin]]
name = "wasmer-headless"
path = "src/bin/wasmer_headless.rs"
doc = false
required-features = ["headless"]

[dependencies]
wasmer = { version = "1.0.1", path = "../api", default-features = false }
wasmer-compiler = { version = "1.0.1", path = "../compiler" }
Expand Down
104 changes: 2 additions & 102 deletions lib/cli/src/bin/wasmer.rs
Original file line number Diff line number Diff line change
@@ -1,105 +1,5 @@
use anyhow::Result;
#[cfg(feature = "compiler")]
use wasmer_cli::commands::Compile;
#[cfg(all(feature = "object-file", feature = "compiler"))]
use wasmer_cli::commands::CreateExe;
#[cfg(feature = "wast")]
use wasmer_cli::commands::Wast;
use wasmer_cli::commands::{Cache, Config, Inspect, Run, SelfUpdate, Validate};
use wasmer_cli::error::PrettyError;

use structopt::{clap::ErrorKind, StructOpt};

#[derive(Debug, StructOpt)]
#[structopt(name = "wasmer", about = "WebAssembly standalone runtime.", author)]
/// The options for the wasmer Command Line Interface
enum WasmerCLIOptions {
/// Run a WebAssembly file. Formats accepted: wasm, wat
#[structopt(name = "run")]
Run(Run),

/// Wasmer cache
#[structopt(name = "cache")]
Cache(Cache),

/// Validate a WebAssembly binary
#[structopt(name = "validate")]
Validate(Validate),

/// Compile a WebAssembly binary
#[cfg(feature = "compiler")]
#[structopt(name = "compile")]
Compile(Compile),

/// Compile a WebAssembly binary into a native executable
#[cfg(all(feature = "object-file", feature = "compiler"))]
#[structopt(name = "create-exe")]
CreateExe(CreateExe),

/// Get various configuration information needed
/// to compile programs which use Wasmer
#[structopt(name = "config")]
Config(Config),

/// Update wasmer to the latest version
#[structopt(name = "self-update")]
SelfUpdate(SelfUpdate),

/// Inspect a WebAssembly file
#[structopt(name = "inspect")]
Inspect(Inspect),

/// Run spec testsuite
#[cfg(feature = "wast")]
#[structopt(name = "wast")]
Wast(Wast),
}

impl WasmerCLIOptions {
fn execute(&self) -> Result<()> {
match self {
Self::Run(options) => options.execute(),
Self::SelfUpdate(options) => options.execute(),
Self::Cache(cache) => cache.execute(),
Self::Validate(validate) => validate.execute(),
#[cfg(feature = "compiler")]
Self::Compile(compile) => compile.execute(),
#[cfg(all(feature = "object-file", feature = "compiler"))]
Self::CreateExe(create_exe) => create_exe.execute(),
Self::Config(config) => config.execute(),
Self::Inspect(inspect) => inspect.execute(),
#[cfg(feature = "wast")]
Self::Wast(wast) => wast.execute(),
}
}
}
use wasmer_cli::cli::wasmer_main;

fn main() {
// We allow windows to print properly colors
#[cfg(windows)]
colored::control::set_virtual_terminal(true).unwrap();

// We try to run wasmer with the normal arguments.
// Eg. `wasmer <SUBCOMMAND>`
// In case that fails, we fallback trying the Run subcommand directly.
// Eg. `wasmer myfile.wasm --dir=.`
let args = std::env::args().collect::<Vec<_>>();
let command = args.get(1);
let options = match command.unwrap_or(&"".to_string()).as_ref() {
"cache" | "compile" | "config" | "create-exe" | "help" | "inspect" | "run"
| "self-update" | "validate" | "wast" => WasmerCLIOptions::from_args(),
_ => {
WasmerCLIOptions::from_iter_safe(args.iter()).unwrap_or_else(|e| {
match e.kind {
// This fixes a issue that:
// 1. Shows the version twice when doing `wasmer -V`
// 2. Shows the run help (instead of normal help) when doing `wasmer --help`
ErrorKind::VersionDisplayed | ErrorKind::HelpDisplayed => e.exit(),
_ => WasmerCLIOptions::Run(Run::from_args()),
}
})
}
};

PrettyError::report(options.execute());
wasmer_main();
}
5 changes: 5 additions & 0 deletions lib/cli/src/bin/wasmer_headless.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use wasmer_cli::cli::wasmer_main;

fn main() {
wasmer_main();
}
119 changes: 119 additions & 0 deletions lib/cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//! The logic for the Wasmer CLI tool.
#[cfg(feature = "compiler")]
use crate::commands::Compile;
#[cfg(all(feature = "object-file", feature = "compiler"))]
use crate::commands::CreateExe;
#[cfg(feature = "wast")]
use crate::commands::Wast;
use crate::commands::{Cache, Config, Inspect, Run, SelfUpdate, Validate};
use crate::error::PrettyError;
use anyhow::Result;

use structopt::{clap::ErrorKind, StructOpt};

#[derive(Debug, StructOpt)]
#[cfg_attr(
not(feature = "headless"),
structopt(name = "wasmer", about = "WebAssembly standalone runtime.", author)
)]
#[cfg_attr(
feature = "headless",
structopt(
name = "wasmer-headless",
about = "Headless WebAssembly standalone runtime.",
author
)
)]
/// The options for the wasmer Command Line Interface
enum WasmerCLIOptions {
/// Run a WebAssembly file. Formats accepted: wasm, wat
#[structopt(name = "run")]
Run(Run),

/// Wasmer cache
#[structopt(name = "cache")]
Cache(Cache),

/// Validate a WebAssembly binary
#[structopt(name = "validate")]
Validate(Validate),

/// Compile a WebAssembly binary
#[cfg(feature = "compiler")]
#[structopt(name = "compile")]
Compile(Compile),

/// Compile a WebAssembly binary into a native executable
#[cfg(all(feature = "object-file", feature = "compiler"))]
#[structopt(name = "create-exe")]
CreateExe(CreateExe),

/// Get various configuration information needed
/// to compile programs which use Wasmer
#[structopt(name = "config")]
Config(Config),

/// Update wasmer to the latest version
#[structopt(name = "self-update")]
SelfUpdate(SelfUpdate),

/// Inspect a WebAssembly file
#[structopt(name = "inspect")]
Inspect(Inspect),

/// Run spec testsuite
#[cfg(feature = "wast")]
#[structopt(name = "wast")]
Wast(Wast),
}

impl WasmerCLIOptions {
fn execute(&self) -> Result<()> {
match self {
Self::Run(options) => options.execute(),
Self::SelfUpdate(options) => options.execute(),
Self::Cache(cache) => cache.execute(),
Self::Validate(validate) => validate.execute(),
#[cfg(feature = "compiler")]
Self::Compile(compile) => compile.execute(),
#[cfg(all(feature = "object-file", feature = "compiler"))]
Self::CreateExe(create_exe) => create_exe.execute(),
Self::Config(config) => config.execute(),
Self::Inspect(inspect) => inspect.execute(),
#[cfg(feature = "wast")]
Self::Wast(wast) => wast.execute(),
}
}
}

/// The main function for the Wasmer CLI tool.
pub fn wasmer_main() {
// We allow windows to print properly colors
#[cfg(windows)]
colored::control::set_virtual_terminal(true).unwrap();

// We try to run wasmer with the normal arguments.
// Eg. `wasmer <SUBCOMMAND>`
// In case that fails, we fallback trying the Run subcommand directly.
// Eg. `wasmer myfile.wasm --dir=.`
let args = std::env::args().collect::<Vec<_>>();
let command = args.get(1);
let options = match command.unwrap_or(&"".to_string()).as_ref() {
"cache" | "compile" | "config" | "create-exe" | "help" | "inspect" | "run"
| "self-update" | "validate" | "wast" => WasmerCLIOptions::from_args(),
_ => {
WasmerCLIOptions::from_iter_safe(args.iter()).unwrap_or_else(|e| {
match e.kind {
// This fixes a issue that:
// 1. Shows the version twice when doing `wasmer -V`
// 2. Shows the run help (instead of normal help) when doing `wasmer --help`
ErrorKind::VersionDisplayed | ErrorKind::HelpDisplayed => e.exit(),
_ => WasmerCLIOptions::Run(Run::from_args()),
}
})
}
};

PrettyError::report(options.execute());
}
1 change: 1 addition & 0 deletions lib/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod common;
#[macro_use]
pub mod error;
pub mod c_gen;
pub mod cli;
#[cfg(feature = "debug")]
pub mod logging;
pub mod store;
Expand Down

0 comments on commit 59de326

Please sign in to comment.