Skip to content

Commit

Permalink
feat: add debug flag
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Oct 23, 2024
1 parent 1be5d4e commit 09e23d6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
26 changes: 17 additions & 9 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fn main() -> Result<(), Error> {
if opts.core {
opt::Optimizer::new(&core)
.wizen(true)
.debug(opts.debug)
.write_optimized_wasm(opts.output)?;
return Ok(());
}
Expand All @@ -67,13 +68,16 @@ fn main() -> Result<(), Error> {

let self_cmd = env::args().next().expect("Expected a command argument");
{
let mut command = Command::new(self_cmd)
let mut command = Command::new(self_cmd);
command
.arg("-c")
.arg(&opts.input_py)
.arg("-o")
.arg(&core_path)
.stdin(Stdio::piped())
.spawn()?;
.arg(&core_path);
if opts.debug {
command.arg("-g");
}
let mut command = command.stdin(Stdio::piped()).spawn()?;
command
.stdin
.take()
Expand Down Expand Up @@ -104,20 +108,24 @@ fn main() -> Result<(), Error> {
}

// Merge the shim with the core module
let status = Command::new("wasm-merge")
.arg(&core_path)
let mut cmd = Command::new("wasm-merge");
cmd.arg(&core_path)
.arg("core")
.arg(&shim_path)
.arg("shim")
.arg("-o")
.arg(&opts.output)
.arg("--enable-reference-types")
.arg("--enable-bulk-memory")
.status()?;
.arg("--enable-bulk-memory");
if opts.debug {
cmd.arg("-g");
}

let status = cmd.status()?;
if !status.success() {
bail!("wasm-merge failed. Couldn't merge shim");
}

opt::optimize_wasm_file(opts.output)?;
opt::optimize_wasm_file(opts.output, opts.debug)?;
Ok(())
}
25 changes: 18 additions & 7 deletions bin/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(crate) struct Optimizer<'a> {
wizen: bool,
optimize: bool,
wasm: &'a [u8],
debug: bool,
}

fn find_deps() -> PathBuf {
Expand Down Expand Up @@ -48,6 +49,7 @@ impl<'a> Optimizer<'a> {
wasm,
optimize: false,
wizen: false,
debug: false,
}
}

Expand All @@ -56,6 +58,11 @@ impl<'a> Optimizer<'a> {
Self { optimize, ..self }
}

#[allow(unused)]
pub fn debug(self, debug: bool) -> Self {
Self { debug, ..self }
}

pub fn wizen(self, wizen: bool) -> Self {
Self { wizen, ..self }
}
Expand Down Expand Up @@ -95,14 +102,14 @@ impl<'a> Optimizer<'a> {
}

if self.optimize {
optimize_wasm_file(dest)?;
optimize_wasm_file(dest, self.debug)?;
}

Ok(())
}
}

pub(crate) fn optimize_wasm_file(dest: impl AsRef<Path>) -> Result<(), Error> {
pub(crate) fn optimize_wasm_file(dest: impl AsRef<Path>, debug: bool) -> Result<(), Error> {
let output = Command::new("wasm-opt")
.arg("--version")
.stdout(Stdio::null())
Expand All @@ -111,12 +118,16 @@ pub(crate) fn optimize_wasm_file(dest: impl AsRef<Path>) -> Result<(), Error> {
if output.is_err() {
anyhow::bail!("Failed to detect wasm-opt. Please install binaryen and make sure wasm-opt is on your path: https://github.com/WebAssembly/binaryen");
}
Command::new("wasm-opt")
.arg("--enable-reference-types")
let mut cmd = Command::new("wasm-opt");
cmd.arg("--enable-reference-types")
.arg("--enable-bulk-memory")
.arg("--strip")
.arg("-O2")
.arg(dest.as_ref())
.arg("-O2");
if debug {
cmd.arg("-g");
} else {
cmd.arg("--strip");
}
cmd.arg(dest.as_ref())
.arg("-o")
.arg(dest.as_ref())
.status()?;
Expand Down
5 changes: 3 additions & 2 deletions bin/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ pub struct Options {
#[structopt(parse(from_os_str))]
pub input_py: PathBuf,

// #[structopt(parse(from_os_str))]
// pub other_files: Vec<PathBuf>,
#[structopt(short = "o", parse(from_os_str), default_value = "index.wasm")]
pub output: PathBuf,

#[structopt(short = "c")]
pub core: bool,

#[structopt(short = "g")]
pub debug: bool,
}

0 comments on commit 09e23d6

Please sign in to comment.