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

from setuptools import find_packages
from setuptools.dist import Distribution
12
13

# need to use distutils.core for correct placement of cython dll
14
if "--inplace" in sys.argv:
15
16
17
18
19
    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
    # Will be invoked which introduces dependences
34
35
    libinfo_py = os.path.join(CURRENT_DIR, "./dgl/_ffi/libinfo.py")
    libinfo = {"__file__": libinfo_py}
36
    exec(
37
38
        compile(open(libinfo_py, "rb").read(), libinfo_py, "exec"),
        libinfo,
39
        libinfo,
40
41
    )
    version = libinfo["__version__"]
Gan Quan's avatar
Gan Quan committed
42

43
    lib_path = libinfo["find_lib_path"]()
Gan Quan's avatar
Gan Quan committed
44
45
    libs = [lib_path[0]]

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

48

49
def get_ta_lib_pattern():
50
51
52
53
54
55
    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"
56
    else:
57
        raise NotImplementedError("Unsupported system: %s" % sys.platform)
58
59
    return ta_lib_pattern

60

61
62
63
64
65
66
67
68
69
70
71
72
def get_dgl_sparse_pattern():
    if sys.platform.startswith("linux"):
        dgl_sparse_lib_pattern = "libdgl_sparse_*.so"
    elif sys.platform.startswith("darwin"):
        dgl_sparse_lib_pattern = "libdgl_sparse_*.dylib"
    elif sys.platform.startswith("win"):
        dgl_sparse_lib_pattern = "dgl_sparse_*.dll"
    else:
        raise NotImplementedError("Unsupported system: %s" % sys.platform)
    return dgl_sparse_lib_pattern


Minjie Wang's avatar
Minjie Wang committed
73
LIBS, VERSION = get_lib_path()
74
BACKENDS = ["pytorch"]
75
TA_LIB_PATTERN = get_ta_lib_pattern()
76
SPARSE_LIB_PATTERN = get_dgl_sparse_pattern()
77

78

79
80
81
82
def cleanup():
    # Wheel cleanup
    try:
        os.remove("MANIFEST.in")
83
    except BaseException:
84
85
86
87
88
89
        pass

    for path in LIBS:
        _, libname = os.path.split(path)
        try:
            os.remove(os.path.join("dgl", libname))
90
        except BaseException:
91
92
93
            pass
    for backend in BACKENDS:
        for ta_path in glob.glob(
94
            os.path.join(
95
96
97
                CURRENT_DIR, "dgl", "tensoradapter", backend, TA_LIB_PATTERN
            )
        ):
98
99
            try:
                os.remove(ta_path)
100
            except BaseException:
101
                pass
Minjie Wang's avatar
Minjie Wang committed
102

103
104
105
106
107
108
109
110
111
112
113
        if backend == "pytorch":
            for sparse_path in glob.glob(
                os.path.join(
                    CURRENT_DIR, "dgl", "dgl_sparse", SPARSE_LIB_PATTERN
                )
            ):
                try:
                    os.remove(sparse_path)
                except BaseException:
                    pass

114

115
116
def config_cython():
    """Try to configure cython and return cython configuration"""
117
118
119
120
    if sys.platform.startswith("win"):
        print(
            "WARNING: Cython is not supported on Windows, will compile without cython module"
        )
121
122
123
124
        return []
    sys_cflags = sysconfig.get_config_var("CFLAGS")

    if "i386" in sys_cflags and "x86_64" in sys_cflags:
125
        print(
126
127
            "WARNING: Cython library may not be compiled correctly with both i386 and x64"
        )
128
129
130
        return []
    try:
        from Cython.Build import cythonize
131

132
133
134
135
136
137
138
        # from setuptools.extension import Extension
        if sys.version_info >= (3, 0):
            subdir = "_cy3"
        else:
            subdir = "_cy2"
        ret = []
        path = "dgl/_ffi/_cython"
139
140
        library_dirs = ["dgl", "../build/Release", "../build"]
        libraries = ["dgl"]
141
142
143
        for fn in os.listdir(path):
            if not fn.endswith(".pyx"):
                continue
144
145
146
147
148
149
150
151
152
153
154
155
            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",
                    ],
                    library_dirs=library_dirs,
                    libraries=libraries,
                    # Crashes without this flag with GCC 5.3.1
156
                    extra_compile_args=["-std=c++14"],
157
158
159
                    language="c++",
                )
            )
160
        return cythonize(ret, force=True)
161
    except ImportError:
162
163
164
        print(
            "WARNING: Cython is not installed, will compile without cython module"
        )
165
166
        return []

167

Minjie Wang's avatar
Minjie Wang committed
168
169
include_libs = False
wheel_include_libs = False
170
if "bdist_wheel" in sys.argv or os.getenv("CONDA_BUILD"):
Gan Quan's avatar
Gan Quan committed
171
    wheel_include_libs = True
172
173
elif "clean" in sys.argv:
    cleanup()
Gan Quan's avatar
Gan Quan committed
174
175
else:
    include_libs = True
Minjie Wang's avatar
Minjie Wang committed
176
177
178
179
180
181
182

setup_kwargs = {}

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

        for backend in BACKENDS:
188
            for ta_path in glob.glob(
189
190
                os.path.join(dir_, "tensoradapter", backend, TA_LIB_PATTERN)
            ):
191
                ta_name = os.path.basename(ta_path)
192
                os.makedirs(
193
194
195
                    os.path.join(CURRENT_DIR, "dgl", "tensoradapter", backend),
                    exist_ok=True,
                )
196
                shutil.copy(
197
198
199
                    os.path.join(dir_, "tensoradapter", backend, ta_name),
                    os.path.join(CURRENT_DIR, "dgl", "tensoradapter", backend),
                )
200
                fo.write(
201
202
                    "include dgl/tensoradapter/%s/%s\n" % (backend, ta_name)
                )
203
            if backend == "pytorch":
204
205
206
207
208
209
210
211
212
213
214
215
                for sparse_path in glob.glob(
                    os.path.join(dir_, "dgl_sparse", SPARSE_LIB_PATTERN)
                ):
                    sparse_name = os.path.basename(sparse_path)
                    os.makedirs(
                        os.path.join(CURRENT_DIR, "dgl", "dgl_sparse"),
                        exist_ok=True,
                    )
                    shutil.copy(
                        os.path.join(dir_, "dgl_sparse", sparse_name),
                        os.path.join(CURRENT_DIR, "dgl", "dgl_sparse"),
                    )
216
                    fo.write("include dgl/dgl_sparse/%s\n" % sparse_name)
217

218
    setup_kwargs = {"include_package_data": True}
Minjie Wang's avatar
Minjie Wang committed
219
220

# For source tree setup
Gan Quan's avatar
Gan Quan committed
221
# Conda build also includes the binary library
Minjie Wang's avatar
Minjie Wang committed
222
223
if include_libs:
    rpath = [os.path.relpath(path, CURRENT_DIR) for path in LIBS]
224
    data_files = [("dgl", rpath)]
225
226
    for path in LIBS:
        for backend in BACKENDS:
227
228
229
230
231
232
233
234
235
236
237
238
239
            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,
                        )
                    ),
                )
            )
240
            if backend == "pytorch":
241
242
243
244
245
                data_files.append(
                    (
                        "dgl/dgl_sparse",
                        glob.glob(
                            os.path.join(
246
247
248
                                os.path.dirname(
                                    os.path.relpath(path, CURRENT_DIR)
                                ),
249
250
251
252
253
254
                                "dgl_sparse",
                                SPARSE_LIB_PATTERN,
                            )
                        ),
                    )
                )
255
    setup_kwargs = {"include_package_data": True, "data_files": data_files}
Minjie Wang's avatar
Minjie Wang committed
256
257

setup(
258
    name="dgl" + os.getenv("DGL_PACKAGE_SUFFIX", ""),
Minjie Wang's avatar
Minjie Wang committed
259
    version=VERSION,
260
    description="Deep Graph Library",
Minjie Wang's avatar
Minjie Wang committed
261
    zip_safe=False,
262
263
    maintainer="DGL Team",
    maintainer_email="wmjlyjemaine@gmail.com",
Minjie Wang's avatar
Minjie Wang committed
264
    packages=find_packages(),
Minjie Wang's avatar
Minjie Wang committed
265
    install_requires=[
266
267
268
269
270
271
        "numpy>=1.14.0",
        "scipy>=1.1.0",
        "networkx>=2.1",
        "requests>=2.19.0",
        "tqdm",
        "psutil>=5.8.0",
Minjie Wang's avatar
Minjie Wang committed
272
    ],
273
    url="https://github.com/dmlc/dgl",
Minjie Wang's avatar
Minjie Wang committed
274
    distclass=BinaryDistribution,
275
    ext_modules=config_cython(),
Minjie Wang's avatar
Minjie Wang committed
276
    classifiers=[
277
278
279
        "Development Status :: 3 - Alpha",
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: Apache Software License",
Minjie Wang's avatar
Minjie Wang committed
280
    ],
281
    license="APACHE",
Minjie Wang's avatar
Minjie Wang committed
282
283
284
285
    **setup_kwargs
)

if wheel_include_libs:
286
    cleanup()