1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#!/usr/bin/env python3
import argparse
import os
import re
import subprocess
import sys
DESC = """
Build, trim, and optimize the `.wasm` file for inclusion in the
`mozilla/source-map` library.
Requires:
- wasm-nm: https://github.com/fitzgen/wasm-nm
- wasm-gc: https://github.com/alexcrichton/wasm-gc
- wasm-snip: https://github.com/fitzgen/wasm-snip
- wasm-opt: https://github.com/WebAssembly/binaryen
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=DESC)
parser.add_argument(
"-g",
"--debug",
action="store_true",
help="Include debug info (the \"name\" section) in the final `.wasm` file.")
parser.add_argument(
"-p",
"--profiling",
action="store_true",
help="Enable the `profiling` cargo feature.")
parser.add_argument(
"-o",
"--output",
type=str,
default=None,
help="The path to write the output `.wasm` file to. If not supplied, the `.wasm` file is written to `stdout`.")
parser.add_argument(
"--no-wasm-opt",
dest="wasm_opt",
action="store_false",
help="Do not run `wasm-opt`.")
parser.add_argument(
"--no-wasm-gc",
dest="wasm_gc",
action="store_false",
help="Do not run `wasm-gc`.")
parser.add_argument(
"--no-wasm-snip",
dest="wasm_snip",
action="store_false",
help="Do not run `wasm-snip`.")
def decode(f):
return f.decode(encoding="utf-8", errors="ignore")
def run(cmd, **kwargs):
sys.stderr.write(str(cmd) + "\n")
if "stdout" not in kwargs:
kwargs["stdout"] = subprocess.PIPE
child = subprocess.run(cmd, **kwargs)
if child.returncode != 0:
raise Exception("{} did not exit OK".format(str(cmd)))
return decode(child.stdout)
def add_path_ext_prefix(path, prefix):
(root, ext) = os.path.splitext(path)
return root + "." + prefix + ext
def build(args):
cmd = ["cargo", "build", "--release", "--target", "wasm32-unknown-unknown"]
if args.profiling:
cmd.extend(["--features", "profiling"])
run(cmd)
return "./target/wasm32-unknown-unknown/release/source_map_mappings_wasm_api.wasm"
def wasm_gc(args, wasm_path):
if not args.wasm_gc:
return wasm_path
out_path = add_path_ext_prefix(wasm_path, "gc")
run(["wasm-gc", wasm_path, out_path])
return out_path
SHOULD_SNIP = [
re.compile(r".*(std|core)(9|::)panicking.*"),
re.compile(r".*(std|core)(3|::)fmt.*"),
re.compile(r".*core(6|::)option(13|::)expect_failed.*"),
re.compile(r".*core(5|::)slice(\d+|::)slice_index_.*_fail.*"),
re.compile(r".*core(3|::)str(\d+|::)slice_.*_fail.*"),
re.compile(r".*core(6|::)result(13|::)unwrap_failed.*"),
re.compile(r".*std(6|::)thread(5|::)local.*"),
re.compile(r".*std(2|::)io(5|::).*"),
re.compile(r"__.*2"),
re.compile(r".*(std|core)(5|::)error.*"),
re.compile(r".*(std|core)(3|::)any(3|::)Any.*"),
]
def wasm_snip(args, wasm_path):
if not args.wasm_snip:
return wasm_path
out_path = add_path_ext_prefix(wasm_path, "snip")
private_functions = run(["wasm-nm", "-j", wasm_path]).splitlines()
snip_functions = set()
for snip in SHOULD_SNIP:
snip_functions.update(filter(lambda f: re.match(snip, f),
private_functions))
run(["wasm-snip", "-o", out_path, wasm_path, *snip_functions]),
return out_path
def wasm_opt(args, wasm_path):
if not args.wasm_opt:
return wasm_path
out_path = add_path_ext_prefix(wasm_path, "opt")
cmd = [
"wasm-opt",
"-O3",
"-Oz",
"--duplicate-function-elimination",
"-o", out_path,
wasm_path
]
if args.debug:
cmd.append("-g")
run(cmd)
return out_path
def main():
args = parser.parse_args()
os.chdir(os.path.dirname(sys.argv[0]))
wasm_path = build(args)
wasm_path = wasm_gc(args, wasm_path)
wasm_path = wasm_snip(args, wasm_path)
# GC again after snipping.
wasm_path = wasm_gc(args, wasm_path)
wasm_path = wasm_opt(args, wasm_path)
if args.output:
run(["cp", wasm_path, args.output])
else:
run(["cat", wasm_path], stdout=subprocess.STDOUT)
if __name__ == "__main__":
main()
|