cc_compiler.py 1.1 KB
Newer Older
1
"""Util to invoke c++ compilers in the system."""
2
# pylint: disable=invalid-name
3 4 5 6
from __future__ import absolute_import as _abs
import sys
import subprocess

7 8 9 10
def create_shared(output,
                  objects,
                  options=None,
                  cc="g++"):
11 12 13 14
    """Create shared library.

    Parameters
    ----------
15
    output : str
16 17 18 19 20 21 22 23
        The target shared library.

    objects : list
        List of object files.

    options : str
        The additional options.

24
    cc : str, optional
25 26 27 28
        The compile string.
    """
    cmd = [cc]
    cmd += ["-shared"]
29

30 31
    if sys.platform == "darwin":
        cmd += ["-undefined", "dynamic_lookup"]
32 33 34 35 36 37 38
    cmd += ["-o", output]

    if isinstance(objects, str):
        cmd += [objects]
    else:
        cmd += objects

39 40 41
    if options:
        cmd += options

42
    args = ' '.join(cmd)
43 44 45 46 47 48 49
    proc = subprocess.Popen(
        args, shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)
    (out, _) = proc.communicate()

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