"git@developer.sourcefind.cn:renzhc/diffusers_dcu.git" did not exist on "2246d2c7c4afb5156d68500b53d7f5693428b29e"
setup.py 6.58 KB
Newer Older
Minjie Wang's avatar
Minjie Wang committed
1
2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
3
4
5
6
import sys
import os
import platform
import sysconfig
Minjie Wang's avatar
Minjie Wang committed
7
8
9
10
11
import shutil
import glob

from setuptools import find_packages
from setuptools.dist import Distribution
12
13
14
15
16
17
18
19

# 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
Minjie Wang's avatar
Minjie Wang committed
20

21

Minjie Wang's avatar
Minjie Wang committed
22
23
class BinaryDistribution(Distribution):
    def has_ext_modules(self):
24
        return True
Minjie Wang's avatar
Minjie Wang committed
25

26

Minjie Wang's avatar
Minjie Wang committed
27
28
CURRENT_DIR = os.path.dirname(__file__)

29

Minjie Wang's avatar
Minjie Wang committed
30
31
def get_lib_path():
    """Get library path, name and version"""
32
    # We can not import `libinfo.py` in setup.py directly since __init__.py
Minjie Wang's avatar
Minjie Wang committed
33
34
35
    # Will be invoked which introduces dependences
    libinfo_py = os.path.join(CURRENT_DIR, './dgl/_ffi/libinfo.py')
    libinfo = {'__file__': libinfo_py}
36
37
38
39
    exec(
        compile(open(libinfo_py, "rb").read(), libinfo_py, 'exec'),
        libinfo,
        libinfo)
Minjie Wang's avatar
Minjie Wang committed
40
    version = libinfo['__version__']
Gan Quan's avatar
Gan Quan committed
41
42
43
44

    lib_path = libinfo['find_lib_path']()
    libs = [lib_path[0]]

Minjie Wang's avatar
Minjie Wang committed
45
46
    return libs, version

47

48
49
50
51
52
53
54
55
56
57
58
def get_ta_lib_pattern():
    if sys.platform.startswith('linux'):
        ta_lib_pattern = 'libtensoradapter_*.so'
    elif sys.platform.startswith('darwin'):
        ta_lib_pattern = 'libtensoradapter_*.dylib'
    elif sys.platform.startswith('win'):
        ta_lib_pattern = 'tensoradapter_*.dll'
    else:
        raise NotImplementedError('Unsupported system: %s' % sys.platform)
    return ta_lib_pattern

59

Minjie Wang's avatar
Minjie Wang committed
60
LIBS, VERSION = get_lib_path()
61
62
63
BACKENDS = ['pytorch']
TA_LIB_PATTERN = get_ta_lib_pattern()

64

65
66
67
68
def cleanup():
    # Wheel cleanup
    try:
        os.remove("MANIFEST.in")
69
    except BaseException:
70
71
72
73
74
75
        pass

    for path in LIBS:
        _, libname = os.path.split(path)
        try:
            os.remove(os.path.join("dgl", libname))
76
        except BaseException:
77
78
79
            pass
    for backend in BACKENDS:
        for ta_path in glob.glob(
80
81
82
83
84
85
            os.path.join(
                CURRENT_DIR,
                "dgl",
                "tensoradapter",
                backend,
                TA_LIB_PATTERN)):
86
87
            try:
                os.remove(ta_path)
88
            except BaseException:
89
                pass
Minjie Wang's avatar
Minjie Wang committed
90

91

92
93
def config_cython():
    """Try to configure cython and return cython configuration"""
94
    if sys.platform.startswith('win'):
95
96
97
98
99
        print("WARNING: Cython is not supported on Windows, will compile without cython module")
        return []
    sys_cflags = sysconfig.get_config_var("CFLAGS")

    if "i386" in sys_cflags and "x86_64" in sys_cflags:
100
101
        print(
            "WARNING: Cython library may not be compiled correctly with both i386 and x64")
102
103
104
105
106
107
108
109
110
111
        return []
    try:
        from Cython.Build import cythonize
        # from setuptools.extension import Extension
        if sys.version_info >= (3, 0):
            subdir = "_cy3"
        else:
            subdir = "_cy2"
        ret = []
        path = "dgl/_ffi/_cython"
112
113
        library_dirs = ['dgl', '../build/Release', '../build']
        libraries = ['dgl']
114
115
116
117
118
119
120
121
122
        for fn in os.listdir(path):
            if not fn.endswith(".pyx"):
                continue
            ret.append(Extension(
                "dgl._ffi.%s.%s" % (subdir, fn[:-4]),
                ["dgl/_ffi/_cython/%s" % fn],
                include_dirs=["../include/",
                              "../third_party/dmlc-core/include",
                              "../third_party/dlpack/include",
123
                              ],
124
125
                library_dirs=library_dirs,
                libraries=libraries,
Quan (Andy) Gan's avatar
Quan (Andy) Gan committed
126
127
                # Crashes without this flag with GCC 5.3.1
                extra_compile_args=["-std=c++11"],
128
                language="c++"))
129
        return cythonize(ret, force=True)
130
131
132
133
    except ImportError:
        print("WARNING: Cython is not installed, will compile without cython module")
        return []

134

Minjie Wang's avatar
Minjie Wang committed
135
136
include_libs = False
wheel_include_libs = False
Gan Quan's avatar
Gan Quan committed
137
138
if "bdist_wheel" in sys.argv or os.getenv('CONDA_BUILD'):
    wheel_include_libs = True
139
140
elif "clean" in sys.argv:
    cleanup()
Gan Quan's avatar
Gan Quan committed
141
142
else:
    include_libs = True
Minjie Wang's avatar
Minjie Wang committed
143
144
145
146
147
148
149
150

setup_kwargs = {}

# For bdist_wheel only
if wheel_include_libs:
    with open("MANIFEST.in", "w") as fo:
        for path in LIBS:
            shutil.copy(path, os.path.join(CURRENT_DIR, 'dgl'))
151
            dir_, libname = os.path.split(path)
Minjie Wang's avatar
Minjie Wang committed
152
            fo.write("include dgl/%s\n" % libname)
153
154

        for backend in BACKENDS:
155
156
157
158
159
160
            for ta_path in glob.glob(
                os.path.join(
                    dir_,
                    "tensoradapter",
                    backend,
                    TA_LIB_PATTERN)):
161
                ta_name = os.path.basename(ta_path)
162
163
164
165
166
167
168
                os.makedirs(
                    os.path.join(
                        CURRENT_DIR,
                        'dgl',
                        'tensoradapter',
                        backend),
                    exist_ok=True)
169
170
171
                shutil.copy(
                    os.path.join(dir_, 'tensoradapter', backend, ta_name),
                    os.path.join(CURRENT_DIR, 'dgl', 'tensoradapter', backend))
172
173
174
                fo.write(
                    "include dgl/tensoradapter/%s/%s\n" %
                    (backend, ta_name))
175

Minjie Wang's avatar
Minjie Wang committed
176
177
178
179
180
    setup_kwargs = {
        "include_package_data": True
    }

# For source tree setup
Gan Quan's avatar
Gan Quan committed
181
# Conda build also includes the binary library
Minjie Wang's avatar
Minjie Wang committed
182
183
if include_libs:
    rpath = [os.path.relpath(path, CURRENT_DIR) for path in LIBS]
184
185
186
187
188
189
190
191
    data_files = [('dgl', rpath)]
    for path in LIBS:
        for backend in BACKENDS:
            data_files.append((
                'dgl/tensoradapter/%s' % backend,
                glob.glob(os.path.join(
                    os.path.dirname(os.path.relpath(path, CURRENT_DIR)),
                    'tensoradapter', backend, TA_LIB_PATTERN))))
Minjie Wang's avatar
Minjie Wang committed
192
193
    setup_kwargs = {
        "include_package_data": True,
194
        "data_files": data_files
Minjie Wang's avatar
Minjie Wang committed
195
196
197
    }

setup(
198
    name='dgl' + os.getenv('DGL_PACKAGE_SUFFIX', ''),
Minjie Wang's avatar
Minjie Wang committed
199
    version=VERSION,
Minjie Wang's avatar
Minjie Wang committed
200
    description='Deep Graph Library',
Minjie Wang's avatar
Minjie Wang committed
201
    zip_safe=False,
Minjie Wang's avatar
Minjie Wang committed
202
203
    maintainer='DGL Team',
    maintainer_email='wmjlyjemaine@gmail.com',
Minjie Wang's avatar
Minjie Wang committed
204
    packages=find_packages(),
Minjie Wang's avatar
Minjie Wang committed
205
206
207
208
    install_requires=[
        'numpy>=1.14.0',
        'scipy>=1.1.0',
        'networkx>=2.1',
209
        'requests>=2.19.0',
210
211
        'tqdm',
        'psutil>=5.8.0',
Minjie Wang's avatar
Minjie Wang committed
212
    ],
Minjie Wang's avatar
Minjie Wang committed
213
    url='https://github.com/dmlc/dgl',
Minjie Wang's avatar
Minjie Wang committed
214
    distclass=BinaryDistribution,
215
    ext_modules=config_cython(),
Minjie Wang's avatar
Minjie Wang committed
216
217
218
219
220
221
222
223
224
225
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Programming Language :: Python :: 3',
        'License :: OSI Approved :: Apache Software License',
    ],
    license='APACHE',
    **setup_kwargs
)

if wheel_include_libs:
226
    cleanup()