SummationByParts/make_wasm.py

66 lines
1.8 KiB
Python
Raw Normal View History

2019-04-11 21:57:29 +00:00
#! /usr/bin/env python3
from argparse import ArgumentParser
2019-07-22 20:33:05 +00:00
from subprocess import check_call
from shutil import copyfile
2019-09-09 16:27:54 +00:00
import tempfile
import pathlib
2019-04-11 21:57:29 +00:00
if __name__ == "__main__":
parser = ArgumentParser(description="Build js and wasm")
2019-09-09 16:27:54 +00:00
parser.add_argument(
"-r", help="Build release type", dest="release", action="store_true"
)
parser.add_argument(
"--destdir",
default=pathlib.Path("publish"),
type=pathlib.Path,
help="Destination suitable for being copied directly to the webserver",
)
2019-04-11 21:57:29 +00:00
args = parser.parse_args()
2019-09-09 16:27:54 +00:00
publish = args.destdir
publish.mkdir(exist_ok=True)
target_triple = "wasm32-unknown-unknown"
command = ["cargo", "build", "--target", target_triple]
target = (
pathlib.Path("target")
.joinpath(target_triple)
.joinpath("release" if args.release else "debug")
2020-01-27 20:20:34 +00:00
.joinpath("sbp.wasm")
2019-09-09 16:27:54 +00:00
)
2019-04-11 21:57:29 +00:00
if args.release:
2019-09-09 16:27:54 +00:00
command.append("--release")
check_call(command)
assert target.exists()
2019-04-11 21:57:29 +00:00
2019-09-09 16:27:54 +00:00
check_call(
[
"wasm-bindgen",
str(target),
"--out-dir",
str(publish),
"--no-typescript",
"--target",
"web",
]
)
2019-07-22 20:33:05 +00:00
if args.release:
try:
2019-09-09 16:27:54 +00:00
with tempfile.TemporaryDirectory() as d_:
d = pathlib.Path(d_)
2020-01-27 20:20:34 +00:00
wasm_bg = publish.joinpath("sbp_bg.wasm")
2019-09-09 16:27:54 +00:00
wasm_to_opt = d.joinpath("before-wasm-opt.wasm")
copyfile(wasm_bg, wasm_to_opt)
check_call(["wasm-opt", "-O4", str(wasm_to_opt), "-o", str(wasm_bg)])
2019-07-22 20:33:05 +00:00
except FileNotFoundError:
print("wasm-opt not found, not optimising further")
pass
2019-09-09 16:27:54 +00:00
for f in ["index.html", "main.js", "style.css"]:
copyfile(f, publish.joinpath(f))