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

9
from setuptools import find_packages, setup
Minjie Wang's avatar
Minjie Wang committed
10
from setuptools.dist import Distribution
11
from setuptools.extension import Extension
Minjie Wang's avatar
Minjie Wang committed
12

13

Minjie Wang's avatar
Minjie Wang committed
14
15
class BinaryDistribution(Distribution):
    def has_ext_modules(self):
16
        return True
Minjie Wang's avatar
Minjie Wang committed
17

18

Minjie Wang's avatar
Minjie Wang committed
19
20
CURRENT_DIR = os.path.dirname(__file__)

sangwzh's avatar
sangwzh committed
21
22
23
24
25
26
27
def get_dtk_version():
    ROCM_PATH = os.getenv('ROCM_PATH', "/opt/dtk/")
    dtk_path = ROCM_PATH + '/.info/rocm_version'
    with open(dtk_path, 'r') as file:
        content = file.read().strip()
    dtk_version = "dtk"+content.replace('.', '')
    return dtk_version
28

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

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

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

47

peizhou001's avatar
peizhou001 committed
48
def get_lib_pattern(lib_name):
49
    if sys.platform.startswith("linux"):
peizhou001's avatar
peizhou001 committed
50
        lib_pattern = f"lib{lib_name}_*.so"
51
    elif sys.platform.startswith("darwin"):
peizhou001's avatar
peizhou001 committed
52
        lib_pattern = f"lib{lib_name}_*.dylib"
53
    elif sys.platform.startswith("win"):
peizhou001's avatar
peizhou001 committed
54
        lib_pattern = f"{lib_name}_*.dll"
55
    else:
56
        raise NotImplementedError("Unsupported system: %s" % sys.platform)
peizhou001's avatar
peizhou001 committed
57
    return lib_pattern
58
59


Minjie Wang's avatar
Minjie Wang committed
60
LIBS, VERSION = get_lib_path()
61
BACKENDS = ["pytorch"]
peizhou001's avatar
peizhou001 committed
62
63
64
65
66
67
68
69
70
71


def remove_lib(lib_name):
    for lib_path in glob.glob(
        os.path.join(CURRENT_DIR, "dgl", lib_name, get_lib_pattern(lib_name))
    ):
        try:
            os.remove(lib_path)
        except BaseException:
            pass
72

73

74
75
76
77
def cleanup():
    # Wheel cleanup
    try:
        os.remove("MANIFEST.in")
78
    except BaseException:
79
80
81
82
83
84
        pass

    for path in LIBS:
        _, libname = os.path.split(path)
        try:
            os.remove(os.path.join("dgl", libname))
85
        except BaseException:
86
87
            pass
    for backend in BACKENDS:
peizhou001's avatar
peizhou001 committed
88
        remove_lib("tensoradapter")
Minjie Wang's avatar
Minjie Wang committed
89

90
        if backend == "pytorch":
peizhou001's avatar
peizhou001 committed
91
92
            remove_lib("dgl_sparse")
            remove_lib("graphbolt")
93

94
    # Remove build artifacts.
Rhett Ying's avatar
Rhett Ying committed
95
    dir_to_remove = ["build", "dgl.egg-info"]
96
97
98
99
100
    for dir_ in dir_to_remove:
        print(f"Removing {dir_}")
        if os.path.isdir(dir_):
            shutil.rmtree(dir_)

101

102
103
def config_cython():
    """Try to configure cython and return cython configuration"""
104
105
106
107
    if sys.platform.startswith("win"):
        print(
            "WARNING: Cython is not supported on Windows, will compile without cython module"
        )
108
109
110
111
        return []
    sys_cflags = sysconfig.get_config_var("CFLAGS")

    if "i386" in sys_cflags and "x86_64" in sys_cflags:
112
        print(
113
114
            "WARNING: Cython library may not be compiled correctly with both i386 and x64"
        )
115
116
117
        return []
    try:
        from Cython.Build import cythonize
118

119
120
121
122
123
124
125
        # from setuptools.extension import Extension
        if sys.version_info >= (3, 0):
            subdir = "_cy3"
        else:
            subdir = "_cy2"
        ret = []
        path = "dgl/_ffi/_cython"
126
127
        library_dirs = ["dgl", "../build/Release", "../build"]
        libraries = ["dgl"]
128
129
130
        for fn in os.listdir(path):
            if not fn.endswith(".pyx"):
                continue
131
132
133
134
135
136
137
138
139
140
141
142
            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
143
                    extra_compile_args=["-std=c++17"],
144
145
146
                    language="c++",
                )
            )
Xin Yao's avatar
Xin Yao committed
147
148
149
        return cythonize(
            ret, force=True, compiler_directives={"language_level": "3"}
        )
150
    except ImportError:
151
152
153
        print(
            "WARNING: Cython is not installed, will compile without cython module"
        )
154
155
        return []

156

peizhou001's avatar
peizhou001 committed
157
158
159
160
161
def copy_lib(lib_name, backend=""):
    for lib_path in glob.glob(
        os.path.join(dir_, lib_name, backend, get_lib_pattern(lib_name))
    ):
        lib_file_name = os.path.basename(lib_path)
Xin Yao's avatar
Xin Yao committed
162
        dst_dir_ = os.path.join(CURRENT_DIR, "dgl", lib_name, backend)
peizhou001's avatar
peizhou001 committed
163
164
165
166
        os.makedirs(
            dst_dir_,
            exist_ok=True,
        )
sangwz's avatar
sangwz committed
167
168
169
170
171
172
173
        if(os.path.join(dir_, lib_name, backend)==dst_dir_):
            pass
        else:
            shutil.copy(
                os.path.join(dir_, lib_name, backend, lib_file_name),
                dst_dir_,
            )
Xin Yao's avatar
Xin Yao committed
174
        fo.write(f"include dgl/{lib_name}/{backend}/{lib_file_name}\n")
peizhou001's avatar
peizhou001 committed
175
176


Minjie Wang's avatar
Minjie Wang committed
177
178
include_libs = False
wheel_include_libs = False
179
if "bdist_wheel" in sys.argv or os.getenv("CONDA_BUILD"):
Gan Quan's avatar
Gan Quan committed
180
    wheel_include_libs = True
181
182
elif "clean" in sys.argv:
    cleanup()
Gan Quan's avatar
Gan Quan committed
183
184
else:
    include_libs = True
Minjie Wang's avatar
Minjie Wang committed
185
186
187
188
189
190
191

setup_kwargs = {}

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

        for backend in BACKENDS:
peizhou001's avatar
peizhou001 committed
197
            copy_lib("tensoradapter", backend)
198
            if backend == "pytorch":
peizhou001's avatar
peizhou001 committed
199
200
                copy_lib("dgl_sparse")
                copy_lib("graphbolt")
201
    setup_kwargs = {"include_package_data": True}
Minjie Wang's avatar
Minjie Wang committed
202

peizhou001's avatar
peizhou001 committed
203
204
205

def get_lib_file_path(lib_name, backend=""):
    return (
Xin Yao's avatar
Xin Yao committed
206
        f"dgl/{lib_name}/{backend}",
peizhou001's avatar
peizhou001 committed
207
208
209
210
211
212
213
214
215
216
217
        glob.glob(
            os.path.join(
                os.path.dirname(os.path.relpath(path, CURRENT_DIR)),
                lib_name,
                backend,
                get_lib_pattern(lib_name),
            )
        ),
    )


Minjie Wang's avatar
Minjie Wang committed
218
# For source tree setup
Gan Quan's avatar
Gan Quan committed
219
# Conda build also includes the binary library
Minjie Wang's avatar
Minjie Wang committed
220
221
if include_libs:
    rpath = [os.path.relpath(path, CURRENT_DIR) for path in LIBS]
222
    data_files = [("dgl", rpath)]
223
224
    for path in LIBS:
        for backend in BACKENDS:
peizhou001's avatar
peizhou001 committed
225
            data_files.append(get_lib_file_path("tensoradapter", backend))
226
            if backend == "pytorch":
peizhou001's avatar
peizhou001 committed
227
228
                data_files.append(get_lib_file_path("dgl_sparse"))
                data_files.append(get_lib_file_path("graphbolt"))
229
    setup_kwargs = {"include_package_data": True, "data_files": data_files}
Minjie Wang's avatar
Minjie Wang committed
230

231
232
233
234
235
236
237
238
239
# Configure dependencies.
install_requires = [
    "numpy>=1.14.0",
    "scipy>=1.1.0",
    "networkx>=2.1",
    "requests>=2.19.0",
    "tqdm",
    "psutil>=5.8.0",
    "torchdata>=0.5.0",
240
    "pandas",
241
242
243
]
if "DGLBACKEND" in os.environ and os.environ["DGLBACKEND"] != "pytorch":
    install_requires.pop(install_requires.index("torchdata>=0.5.0"))
Minjie Wang's avatar
Minjie Wang committed
244
setup(
245
    name="dgl" + os.getenv("DGL_PACKAGE_SUFFIX", ""),
sangwzh's avatar
sangwzh committed
246
    version=VERSION+str('+das.opt1.')+get_dtk_version(),
247
    description="Deep Graph Library",
Minjie Wang's avatar
Minjie Wang committed
248
    zip_safe=False,
249
250
    maintainer="DGL Team",
    maintainer_email="wmjlyjemaine@gmail.com",
Minjie Wang's avatar
Minjie Wang committed
251
    packages=find_packages(),
252
    install_requires=install_requires,
253
    url="https://github.com/dmlc/dgl",
Minjie Wang's avatar
Minjie Wang committed
254
    distclass=BinaryDistribution,
255
    ext_modules=config_cython(),
Minjie Wang's avatar
Minjie Wang committed
256
    classifiers=[
257
258
259
        "Development Status :: 3 - Alpha",
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: Apache Software License",
Minjie Wang's avatar
Minjie Wang committed
260
    ],
261
    license="APACHE",
peizhou001's avatar
peizhou001 committed
262
    **setup_kwargs,
Minjie Wang's avatar
Minjie Wang committed
263
264
265
)

if wheel_include_libs:
266
    cleanup()