James Kuszmaul | 27da814 | 2019-07-21 16:13:55 -0700 | [diff] [blame^] | 1 | # Sourced from https://github.com/ribrdb/rules_emscripten/blob/master/toolchain/defs.bzl |
| 2 | # TODO(james): Specialize this more for our purposes--e.g., |
| 3 | # we probably will only actually use one set of the possible options. |
| 4 | def emcc_binary(name, |
| 5 | memory_init_file=0, |
| 6 | wasm=True, |
| 7 | worker=False, |
| 8 | linkopts=[], |
| 9 | **kwargs): |
| 10 | includejs = False |
| 11 | includehtml = False |
| 12 | linkopts = list(linkopts) |
| 13 | if name.endswith(".html"): |
| 14 | basename = name[:-5] |
| 15 | includehtml = True |
| 16 | includejs = True |
| 17 | elif name.endswith(".js"): |
| 18 | basename = name[:-3] |
| 19 | includejs = True |
| 20 | outputs = [] |
| 21 | if includejs: |
| 22 | outputs.append(basename + ".js") |
| 23 | if wasm: |
| 24 | outputs.append(basename + ".wasm") |
| 25 | if memory_init_file: |
| 26 | outputs.append(basename + ".mem") |
| 27 | if worker: |
| 28 | outputs.append(basename + ".worker.js") |
| 29 | linkopts.append('--proxy-to-worker') |
| 30 | |
| 31 | if includehtml: |
| 32 | outputs.append(basename + ".html") |
| 33 | if not wasm: |
| 34 | linkopts.append('-s WASM=0') |
| 35 | linkopts.append('--memory-init-file %d' % memory_init_file) |
| 36 | if includejs: |
| 37 | tarfile = name + ".tar" |
| 38 | # we'll generate a tarfile and extract multiple outputs |
| 39 | native.cc_binary(name=tarfile, linkopts=linkopts, restricted_to = ["//tools:web"], **kwargs) |
| 40 | native.genrule( |
| 41 | name="emcc_extract_" + tarfile, |
| 42 | srcs=[tarfile], |
| 43 | outs=outputs, |
| 44 | output_to_bindir=1, |
| 45 | testonly=kwargs.get('testonly'), |
| 46 | restricted_to = ["//tools:web"], |
| 47 | cmd=""" |
| 48 | tar xf $< -C "$(@D)"/$$(dirname "%s") |
| 49 | """ % [outputs[0]]) |
| 50 | else: |
| 51 | native.cc_binary(name=name, linkopts=linkopts, restricted_to = ["//tools:web"], **kwargs) |