emscripten.py 1.46 KB
Newer Older
1 2 3 4 5 6 7 8 9
"""Util to invoke emscripten compilers in the system."""
# pylint: disable=invalid-name
from __future__ import absolute_import as _abs
import subprocess
from .._ffi.libinfo import find_lib_path

def create_js(output,
              objects,
              options=None,
10
              side_module=False,
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
              cc="emcc"):
    """Create emscripten javascript library.

    Parameters
    ----------
    output : str
        The target shared library.

    objects : list
        List of object files.

    options : str
        The additional options.

    cc : str, optional
        The compile string.
    """
    cmd = [cc]
    cmd += ["-s", "RESERVED_FUNCTION_POINTERS=2"]
    cmd += ["-s", "NO_EXIT_RUNTIME=1"]
    cmd += ["-Oz"]
    cmd += ["-o", output]
33 34
    if side_module:
        cmd += ["-s", "SIDE_MODULE=1"]
35 36 37 38 39 40 41

    objects = [objects] if isinstance(objects, str) else objects
    with_runtime = False
    for obj in objects:
        if obj.find("libtvm_web_runtime.bc") != -1:
            with_runtime = True

42
    if not with_runtime and not side_module:
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
        objects += [find_lib_path("libtvm_web_runtime.bc")[0]]

    cmd += objects

    if options:
        cmd += options

    args = ' '.join(cmd)
    proc = subprocess.Popen(
        args, shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)
    (out, _) = proc.communicate()

    if proc.returncode != 0:
        msg = "Compilation error:\n"
        msg += out
        raise RuntimeError(msg)