Skip to content

Commit b70593d

Browse files
committed
Initial commit -- it works!
0 parents  commit b70593d

13 files changed

+545
-0
lines changed

Diff for: .gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
README.md -diff -merge

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/tests/snipped.wasm
2+
/target/
3+
**/*.rs.bk
4+
Cargo.lock

Diff for: CONTRIBUTING.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Contributing to `wasm-snip`
2+
3+
Hi! We'd love to have your contributions! If you want help or mentorship, reach
4+
out to us in a GitHub issue, or ping `fitzgen`
5+
in [#rust on irc.mozilla.org](irc://irc.mozilla.org#rust) and introduce
6+
yourself.
7+
8+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
9+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
10+
11+
12+
- [Code of Conduct](#code-of-conduct)
13+
- [Building](#building)
14+
- [Testing](#testing)
15+
- [Automatic code formatting](#automatic-code-formatting)
16+
17+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
18+
19+
## Code of Conduct
20+
21+
We abide by the [Rust Code of Conduct][coc] and ask that you do as well.
22+
23+
[coc]: https://www.rust-lang.org/en-US/conduct.html
24+
25+
## Building
26+
27+
```
28+
$ cargo build
29+
```
30+
31+
## Testing
32+
33+
The tests require `cargo-readme` to be installed:
34+
35+
```
36+
$ cargo install cargo-readme
37+
```
38+
39+
To run all the tests:
40+
41+
```
42+
$ cargo test
43+
```
44+
45+
## Automatic code formatting
46+
47+
We use [`rustfmt`](https://github.com/rust-lang-nursery/rustfmt) to enforce a
48+
consistent code style across the whole code base.
49+
50+
You can install the latest version of `rustfmt` with this command:
51+
52+
```
53+
$ rustup update nightly
54+
$ cargo install -f rustfmt-nightly
55+
```
56+
57+
Ensure that `~/.cargo/bin` is on your path.
58+
59+
Once that is taken care of, you can (re)format all code by running this command:
60+
61+
```
62+
$ cargo fmt
63+
```

Diff for: Cargo.toml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
authors = ["Nick Fitzgerald <[email protected]>"]
3+
description = "Replace a wasm function with an `unreachable`."
4+
keywords = ["symbol", "wasm", "snip", "size", "profiling"]
5+
license = "Apache-2.0/MIT"
6+
name = "wasm-snip"
7+
readme = "./README.md"
8+
repository = "https://github.com/fitzgen/wasm-snip"
9+
version = "0.1.0"
10+
11+
[[bin]]
12+
doc = false
13+
name = "wasm-snip"
14+
path = "src/bin/wasm-snip.rs"
15+
required-features = ["exe"]
16+
17+
[dependencies]
18+
failure = "0.1.1"
19+
parity-wasm = "0.19.0"
20+
21+
[dependencies.clap]
22+
optional = true
23+
version = "2.29.0"
24+
25+
[features]
26+
default = ["exe"]
27+
exe = ["clap"]

Diff for: README.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# `wasm-snip`
2+
3+
[![](https://docs.rs/wasm-snip/badge.svg)](https://docs.rs/wasm-snip/) [![](https://img.shields.io/crates/v/wasm-snip.svg)](https://crates.io/crates/wasm-snip) [![](https://img.shields.io/crates/d/wasm-snip.png)](https://crates.io/crates/wasm-snip) [![Build Status](https://travis-ci.org/fitzgen/wasm-snip.png?branch=master)](https://travis-ci.org/fitzgen/wasm-snip)
4+
5+
`wasm-snip` replaces a WebAssembly function's body with a `unreachable`.
6+
7+
Maybe you know that some function will never be called at runtime, but the
8+
compiler can't prove that at compile time? Snip it! Then run `wasm-gc`[0] again
9+
and all the functions it transitively called (which could also never be called
10+
at runtime) will get removed too.
11+
12+
Very helpful when shrinking the size of WebAssembly binaries!
13+
14+
This functionality relies on the "name" section being present in the `.wasm`
15+
file, so build with debug symbols.
16+
17+
```toml
18+
[profile.release]
19+
debug = true
20+
```
21+
22+
* [Executable](#executable)
23+
* [Library](#library)
24+
* [License](#license)
25+
* [Contributing](#contributing)
26+
27+
### Executable
28+
29+
To install the `wasm-snip` executable, run
30+
31+
```
32+
$ cargo install wasm-snip
33+
```
34+
35+
For information on using the `wasm-snip` executable, run
36+
37+
```
38+
$ wasm-snip --help
39+
```
40+
41+
### Library
42+
43+
To use `wasm-snip` as a library, add this to your `Cargo.toml`:
44+
45+
```toml
46+
[dependencies.wasm-snip]
47+
# Do not build the executable.
48+
default-features = false
49+
```
50+
51+
See [docs.rs/wasm-snip][docs] for API documentation.
52+
53+
[docs]: https://docs.rs/wasm-snip
54+
55+
### License
56+
57+
Licensed under either of
58+
59+
* [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
60+
61+
* [MIT license](http://opensource.org/licenses/MIT)
62+
63+
at your option.
64+
65+
### Contributing
66+
67+
See
68+
[CONTRIBUTING.md](https://github.com/fitzgen/wasm-snip/blob/master/CONTRIBUTING.md)
69+
for hacking.
70+
71+
Unless you explicitly state otherwise, any contribution intentionally submitted
72+
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
73+
dual licensed as above, without any additional terms or conditions.
74+

Diff for: README.tpl

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `{{crate}}`
2+
3+
{{readme}}

Diff for: src/bin/wasm-snip.rs

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
extern crate clap;
2+
extern crate failure;
3+
extern crate parity_wasm;
4+
extern crate wasm_snip;
5+
6+
use parity_wasm::elements::{self, Serialize};
7+
use std::io;
8+
use std::path;
9+
use std::process;
10+
11+
fn main() {
12+
if let Err(e) = try_main() {
13+
eprintln!("error: {}", e);
14+
for c in e.causes().skip(1) {
15+
eprintln!(" caused by {}", c);
16+
}
17+
eprintln!("{}", e.backtrace());
18+
process::exit(1)
19+
}
20+
}
21+
22+
fn try_main() -> Result<(), failure::Error> {
23+
let matches = parse_args();
24+
25+
let mut opts = wasm_snip::Options::default();
26+
opts.input = path::PathBuf::from(matches.value_of("input").unwrap());
27+
opts.functions = matches
28+
.values_of("function")
29+
.unwrap()
30+
.map(|f| f.to_string())
31+
.collect();
32+
33+
let module = wasm_snip::snip(opts)?;
34+
35+
if let Some(output) = matches.value_of("output") {
36+
elements::serialize_to_file(output, module)?;
37+
} else {
38+
let stdout = io::stdout();
39+
let mut stdout = stdout.lock();
40+
module.serialize(&mut stdout)?;
41+
}
42+
43+
Ok(())
44+
}
45+
46+
fn parse_args() -> clap::ArgMatches<'static> {
47+
clap::App::new(env!("CARGO_PKG_NAME"))
48+
.version(env!("CARGO_PKG_VERSION"))
49+
.author(env!("CARGO_PKG_AUTHORS"))
50+
.about(env!("CARGO_PKG_DESCRIPTION"))
51+
.long_about(
52+
"
53+
`wasm-snip` replaces a WebAssembly function's body with an `unreachable`.
54+
55+
Maybe you know that some function will never be called at runtime, but the
56+
compiler can't prove that at compile time? Snip it! Then run `wasm-gc`[0] again
57+
and all the functions it transitively called (which could also never be called
58+
at runtime) will get removed too.
59+
60+
Very helpful when shrinking the size of WebAssembly binaries!
61+
",
62+
)
63+
.arg(
64+
clap::Arg::with_name("output")
65+
.short("o")
66+
.long("output")
67+
.takes_value(true)
68+
.help("The path to write the output wasm file to. Defaults to stdout."),
69+
)
70+
.arg(
71+
clap::Arg::with_name("input")
72+
.required(true)
73+
.help("The input wasm file containing the function(s) to snip."),
74+
)
75+
.arg(
76+
clap::Arg::with_name("function")
77+
.required(true)
78+
.multiple(true)
79+
.help("The function(s) to snip."),
80+
)
81+
.get_matches()
82+
}

0 commit comments

Comments
 (0)