Skip to content

Commit

Permalink
Implement binfmt detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Jun 15, 2023
1 parent eee6765 commit 7fe964d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
68 changes: 38 additions & 30 deletions lib/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ pub fn wasmer_main() {

fn wasmer_main_inner() -> Result<(), anyhow::Error> {
if is_binfmt_interpreter() {
todo!();
Run::from_binfmt_args().execute();
}

let args = Args::parse();

if args.version {
return print_version(args.output.is_verbose());
}

args.output.initialize_logging();
args.execute()
}

Expand All @@ -56,16 +52,22 @@ pub struct Args {

impl Args {
fn execute(self) -> Result<(), anyhow::Error> {
if self.version {
return print_version(self.output.is_verbose());
let Args {
cmd,
version,
output,
} = self;

if version {
return print_version(output.is_verbose());
}

if let Some(cmd) = self.cmd {
if let Some(cmd) = cmd {
cmd.execute()
} else {
let help = Args::command().render_long_help();
eprintln!("{help}");
Ok(())
Args::command().print_long_help()?;
// Note: clap uses an exit code of 2 when CLI parsing fails
std::process::exit(2);
}
}
}
Expand Down Expand Up @@ -245,15 +247,22 @@ impl Cmd {
}

fn is_binfmt_interpreter() -> bool {
if !cfg!(linux) {
return false;
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
use std::{ffi::OsStr, path::PathBuf};

// Note: we'll be invoked as
let binary_path = match std::env::args_os().next() {
Some(path) => PathBuf::from(path),
None => return false,
};
binary_path.file_name() == OsStr::from(Binfmt::FILENAME)
} else {
false
}
}

// TODO: Figure out if we actually are running as the binfmt interpreter
false
}

#[allow(unused_mut, clippy::vec_init_then_push)]
fn print_version(verbose: bool) -> Result<(), anyhow::Error> {
if !verbose {
println!("wasmer {}", env!("CARGO_PKG_VERSION"));
Expand All @@ -271,18 +280,17 @@ fn print_version(verbose: bool) -> Result<(), anyhow::Error> {
println!("commit-date: {}", env!("WASMER_BUILD_DATE"));
println!("host: {}", target_lexicon::HOST);

println!("compiler: {}", {
let mut s = Vec::<&'static str>::new();

#[cfg(feature = "singlepass")]
s.push("singlepass");
#[cfg(feature = "cranelift")]
s.push("cranelift");
#[cfg(feature = "llvm")]
s.push("llvm");

s.join(",")
});
let mut compilers = Vec::<&'static str>::new();
if cfg!(feature = "singlepass") {
compilers.push("singlepass");
}
if cfg!(feature = "cranelift") {
compilers.push("cranelift");
}
if cfg!(feature = "llvm") {
compilers.push("llvm");
}
println!("compiler: {}", compilers.join(","));

Ok(())
}
4 changes: 3 additions & 1 deletion lib/cli/src/commands/binfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ fn seccheck(path: &Path) -> Result<()> {
}

impl Binfmt {
pub const FILENAME: &str = "wasmer-binfmt-interpreter";

/// execute [Binfmt]
pub fn execute(&self) -> Result<()> {
if !self.binfmt_misc.exists() {
Expand All @@ -66,7 +68,7 @@ impl Binfmt {
let bin_path_orig: PathBuf = env::current_exe()
.and_then(|p| p.canonicalize())
.context("Cannot get path to wasmer executable")?;
let bin_path = temp_dir.path().join("wasmer-binfmt-interpreter");
let bin_path = temp_dir.path().join(Binfmt::FILENAME);
fs::copy(bin_path_orig, &bin_path).context("Copy wasmer binary to temp folder")?;
let bin_path = fs::canonicalize(&bin_path).with_context(|| {
format!(
Expand Down

0 comments on commit 7fe964d

Please sign in to comment.