setup.py 4.46 KB
Newer Older
1 2
# pylint: disable=invalid-name, exec-used
"""Setup TVM package."""
3 4
from __future__ import absolute_import
import os
5
import shutil
6
import sys
7 8 9 10 11
import sysconfig
import platform

from setuptools import find_packages
from setuptools.dist import Distribution
12

13 14 15 16 17 18 19 20
# need to use distutils.core for correct placement of cython dll
if "--inplace" in sys.argv:
    from distutils.core import setup
    from distutils.extension import Extension
else:
    from setuptools import setup
    from setuptools.extension import Extension

Clouds committed
21 22
CURRENT_DIR = os.path.dirname(__file__)

23 24 25 26 27 28 29 30
def get_lib_path():
    """Get library path, name and version"""
    # We can not import `libinfo.py` in setup.py directly since __init__.py
    # Will be invoked which introduces dependences
    libinfo_py = os.path.join(CURRENT_DIR, './tvm/_ffi/libinfo.py')
    libinfo = {'__file__': libinfo_py}
    exec(compile(open(libinfo_py, "rb").read(), libinfo_py, 'exec'), libinfo, libinfo)
    version = libinfo['__version__']
31 32 33 34 35 36 37 38 39 40
    if not os.getenv('CONDA_BUILD'):
        lib_path = libinfo['find_lib_path']()
        libs = [lib_path[0]]
        if libs[0].find("runtime") == -1:
            for name in lib_path[1:]:
                if name.find("runtime") != -1:
                    libs.append(name)
                    break
    else:
        libs = None
41
    return libs, version
42

43
LIB_LIST, __version__ = get_lib_path()
44

45 46 47 48 49
def config_cython():
    """Try to configure cython and return cython configuration"""
    if os.name == 'nt':
        print("WARNING: Cython is not supported on Windows, will compile without cython module")
        return []
50 51 52 53 54
    sys_cflags = sysconfig.get_config_var("CFLAGS")

    if "i386" in sys_cflags and "x86_64" in sys_cflags:
        print("WARNING: Cython library may not be compiled correctly with both i386 and x64")
        return []
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    try:
        from Cython.Build import cythonize
        # from setuptools.extension import Extension
        if sys.version_info >= (3, 0):
            subdir = "_cy3"
        else:
            subdir = "_cy2"
        ret = []
        path = "tvm/_ffi/_cython"
        if os.name == 'nt':
            library_dirs = ['tvm', '../build/Release', '../build']
            libraries = ['libtvm']
        else:
            library_dirs = None
            libraries = None
        for fn in os.listdir(path):
            if not fn.endswith(".pyx"):
                continue
            ret.append(Extension(
                "tvm._ffi.%s.%s" % (subdir, fn[:-4]),
                ["tvm/_ffi/_cython/%s" % fn],
                include_dirs=["../include/",
77 78
                              "../3rdparty/dmlc-core/include",
                              "../3rdparty/dlpack/include",
79 80 81 82 83 84 85 86 87
                ],
                library_dirs=library_dirs,
                libraries=libraries,
                language="c++"))
        return cythonize(ret)
    except ImportError:
        print("WARNING: Cython is not installed, will compile without cython module")
        return []

88 89 90 91 92 93 94
class BinaryDistribution(Distribution):
    def has_ext_modules(self):
        return True

    def is_pure(self):
        return False

95 96 97 98 99 100 101 102 103 104
include_libs = False
wheel_include_libs = False
if not os.getenv('CONDA_BUILD'):
    if "bdist_wheel" in sys.argv:
        wheel_include_libs = True
    else:
        include_libs = True

setup_kwargs = {}

105
# For bdist_wheel only
106
if wheel_include_libs:
107 108 109 110 111
    with open("MANIFEST.in", "w") as fo:
        for path in LIB_LIST:
            shutil.copy(path, os.path.join(CURRENT_DIR, 'tvm'))
            _, libname = os.path.split(path)
            fo.write("include tvm/%s\n" % libname)
112
    setup_kwargs = {
113
        "include_package_data": True
114
    }
115 116

if include_libs:
117
    curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
118 119
    for i, path in enumerate(LIB_LIST):
        LIB_LIST[i] = os.path.relpath(path, curr_path)
120 121
    setup_kwargs = {
        "include_package_data": True,
122
        "data_files": [('tvm', LIB_LIST)]
123
    }
124 125 126

setup(name='tvm',
      version=__version__,
127
      description="TVM: An End to End Tensor IR/DSL Stack for Deep Learning Systems",
128 129
      zip_safe=False,
      install_requires=[
130
        'numpy',
131
        'decorator',
132
        'attrs',
133
        ],
134 135 136
      packages=find_packages(),
      distclass=BinaryDistribution,
      url='https://github.com/dmlc/tvm',
137 138
      ext_modules=config_cython(),
      **setup_kwargs)
139

140 141 142

if wheel_include_libs:
    # Wheel cleanup
143
    os.remove("MANIFEST.in")
144 145
    for path in LIB_LIST:
        _, libname = os.path.split(path)
Clouds committed
146
        os.remove("tvm/%s" % libname)