SummationByParts/webfront/make_wasm.py

71 lines
1.9 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
2020-01-28 21:02:37 +00:00
from shutil import copyfile, copytree
2019-09-09 16:27:54 +00:00
import tempfile
import pathlib
2020-03-30 20:54:31 +00:00
import os
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]
2020-03-30 20:54:31 +00:00
env = os.environ.copy()
2019-04-11 21:57:29 +00:00
if args.release:
2020-06-14 20:20:01 +00:00
env[
"RUSTFLAGS"
] = "-C opt-level=3 -C codegen-units=1 -C lto=fat -C embed-bitcode"
2019-09-09 16:27:54 +00:00
command.append("--release")
2020-03-30 20:54:31 +00:00
check_call(command, env=env)
2020-01-30 17:28:22 +00:00
target = (
pathlib.Path("../target")
.joinpath(target_triple)
.joinpath("release" if args.release else "debug")
.joinpath("sbp_web.wasm")
)
2019-09-09 16:27:54 +00:00
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-30 17:28:22 +00:00
wasm_bg = publish.joinpath("sbp_web_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
2020-06-14 20:20:01 +00:00
copytree("frontend", publish, dirs_exist_ok=True)