forked from Xilinx/pyxir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
300 lines (254 loc) · 10.4 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright 2020 Xilinx Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Setup PyXIR package """
import os
import sys
import glob
import sysconfig
import unittest
import setuptools
import platform
import subprocess
import multiprocessing
from shutil import copyfile, copymode
from distutils.version import LooseVersion
from distutils.command.install_headers import install_headers
from distutils.command.build_py import build_py
from setuptools.command.install_lib import install_lib
from distutils.command.install_data import install_data
from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext
from pathlib import Path
__version__ = '0.2.1'
FILE_DIR = os.path.dirname(os.path.abspath(__file__))
#################
# SYS ARGUMENTS #
#################
if '--debug' in sys.argv:
debug = True
sys.argv.remove('--debug')
else:
debug = False
# DPUCADX8G/DPUv1 build
if '--use_vai_rt' in sys.argv:
use_vai_rt_dpucadx8g = True
sys.argv.remove('--use_vai_rt')
elif '--use_vai_rt_dpucadx8g' in sys.argv:
use_vai_rt_dpucadx8g = True
sys.argv.remove('--use_vai_rt_dpucadx8g')
else:
use_vai_rt_dpucadx8g = False
# DPUCZDX8G/DPUv2 build
if '--use_vai_rt_dpuczdx8g' in sys.argv:
use_vai_rt_dpuczdx8g = True
sys.argv.remove('--use_vai_rt_dpuczdx8g')
elif '--use_vai_rt_dpuv2' in sys.argv:
use_vai_rt_dpuczdx8g = True
sys.argv.remove('--use_vai_rt_dpuv2')
else:
use_vai_rt_dpuczdx8g = False
if '--use_dpuczdx8g_vart' in sys.argv:
use_dpuczdx8g_vart = True
sys.argv.remove('--use_dpuczdx8g_vart')
else:
use_dpuczdx8g_vart = False
# DPUCAHX8H/DPUv3e build
if '--use_vai_rt_dpucahx8h' in sys.argv:
use_vai_rt_dpucahx8h = True
sys.argv.remove('--use_vai_rt_dpucahx8h')
else:
use_vai_rt_dpucahx8h = False
###############
# STATIC DATA #
###############
package_data = glob.glob("include/pyxir/**/*.hpp", recursive=True)
headers = package_data
static_data = glob.glob("python/pyxir/**/*.json", recursive=True)
static_data.extend(glob.glob("python/pyxir/**/*.dcf", recursive=True))
# From PyBind11: https://github.com/pybind/pybind11/blob/master/setup.py
class InstallHeaders(install_headers):
""" Use custom header installer because the default one flattens
subdirectories"""
def run(self):
if not self.distribution.headers:
return
for header in self.distribution.headers:
subdir = os.path.dirname(
os.path.relpath(header, 'include/pyxir'))
install_dir = os.path.join(self.install_dir, subdir)
self.mkpath(install_dir)
(out, _) = self.copy_file(header, install_dir)
self.outfiles.append(out)
# From PyBind11: https://github.com/pybind/pybind11/blob/master/setup.py
# Install the headers inside the package as well
class BuildPy(build_py):
def build_package_data(self):
build_py.build_package_data(self)
for header in package_data:
target = os.path.join(self.build_lib, 'pyxir', header)
self.mkpath(os.path.dirname(target))
self.copy_file(header, target, preserve_mode=False)
for static_file in static_data:
# strip python
target = os.path.join(self.build_lib, static_file[7:])
self.copy_file(static_file, target, preserve_mode=False)
###################
# CMAKE EXTENSION #
###################
# Based on official Pybind11 example:
# https://github.com/pybind/cmake_example/blob/master/setup.py
class CMakeExtension(Extension):
def __init__(self, name, sourcedir='.'):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
# Based on official Pybind11 example:
# https://github.com/pybind/cmake_example/blob/master/setup.py
class CMakeBuild(build_ext):
def initialize_options(self):
build_ext.initialize_options(self)
self.debug = debug
def run(self):
try:
out = subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError("CMake must be installed to build the following"
" extensions: " +
", ".join(e.name for e in self.extensions))
if platform.system() == "Windows":
cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)',
out.decode()).group(1))
if cmake_version < '3.3.0':
raise RuntimeError("CMake >= 3.3.0 is required on Windows")
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(
self.get_ext_fullpath(ext.name)))
# required for auto-detection of auxiliary "native" libs
if not extdir.endswith(os.path.sep):
extdir += os.path.sep
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_EXECUTABLE=' + sys.executable]
if use_vai_rt_dpucadx8g:
cmake_args.append('-DUSE_VAI_RT_DPUCADX8G=ON')
if use_vai_rt_dpuczdx8g:
cmake_args.append('-DUSE_VAI_RT_DPUCZDX8G=ON')
if use_vai_rt_dpucahx8h:
cmake_args.append('-DUSE_VAI_RT_DPUCAHX8H=ON')
if use_dpuczdx8g_vart:
cmake_args.append('-DUSE_DPUCZDX8G_VART=ON')
if self.debug:
cmake_args.append('-DDEBUG=ON')
# cmake_args.append('-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON')
cfg = 'Debug' if self.debug else 'Release'
print("Cfg: {}".format(cfg))
build_args = ['--config', cfg]
if platform.system() == "Windows":
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'
.format(cfg.upper(), extdir)]
if sys.maxsize > 2**32:
cmake_args += ['-A', 'x64']
build_args += ['--', '/m']
else:
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
build_args += ['--', '-j{}'.format(multiprocessing.cpu_count())]
env = os.environ.copy()
# env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'\
# .format(env.get('CXXFLAGS', ''), self.distribution.get_version())
env['CXXFLAGS'] = '{}'.format(env.get('CXXFLAGS', ''))
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args,
cwd=self.build_temp, env=env)
subprocess.check_call(['cmake', '--build', '.'] + build_args,
cwd=self.build_temp)
print(self.build_lib)
# Copy libpyxir.so to python/ next to python/pyxir for rapid
# prototyping
lib_bin = os.path.join(self.build_lib, 'libpyxir.so')
lib_dest_dir = os.path.join(os.path.dirname(
os.path.abspath(__file__)), 'python')
self.copy_file(lib_bin, lib_dest_dir)
# lib_bin = os.path.join(self.build_lib, 'libpyxir.so.' + str(__version__))
# lib_dest_dir = os.path.join(os.path.dirname(
# os.path.abspath(__file__)), 'python')
# self.copy_file(lib_bin, lib_dest_dir)
# Create symlink
# lib_symlink = lib_dest_dir + "/libpyxir.so"
# if os.path.exists(lib_symlink):
# os.remove(lib_symlink)
# os.symlink(lib_dest_dir + '/libpyxir.so.' + str(__version__), lib_symlink)
# Copy *_test file to tests directory
# test_bin = os.path.join(self.build_temp, 'tests/pyxir_test')
# test_dest_dir = os.path.join(os.path.dirname(
# os.path.abspath(__file__)), 'tests', 'cpp')
# self.copy_file(test_bin, test_dest_dir)
print() # Add empty line for nicer output
# From https://www.benjack.io/2018/02/02/python-cpp-revisited.html
def copy_file(self, src_file, dest_dir):
'''
Copy ``src_file`` to `dest_dir` directory, ensuring parent directory
exists. Messages like `creating directory /path/to/package` and
`copying directory /src/path/to/package -> path/to/package` are
displayed on standard output. Adapted from scikit-build.
'''
# Create directory if needed
if dest_dir != "" and not os.path.exists(dest_dir):
print("creating directory {}".format(dest_dir))
os.makedirs(dest_dir)
# Copy file
dest_file = os.path.join(dest_dir, os.path.basename(src_file))
print("copying {} -> {}".format(src_file, dest_file))
copyfile(src_file, dest_file)
copymode(src_file, dest_file)
setuptools.setup(
name="pyxir",
version=__version__,
author="Xilinx Inc",
author_email="[email protected]",
description=open(os.path.join(FILE_DIR, 'README.md'), encoding='utf-8').read(),
long_description=open(os.path.join(FILE_DIR, 'README.md'), encoding='utf-8').read(),
long_description_content_type="text/markdown",
url="https://github.com/Xilinx/pyxir",
packages=setuptools.find_packages("python"),
package_dir={"": "python"},
include_package_data=True,
# package_data={
# # If any package contains *.txt files, include them:
# "/proj/rdi/staff/jornt/pyxir/include": ["*.hpp"]
# },
# data_files=[('../include/pyxir', '.pyxir.hpp')],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
install_requires=[
'numpy',
'packaging',
'pydot==1.4.1',
'h5py>=2.8.0'],
extra_require={'full': ['tensorflow>=1.12.0,<2']},
# cmdclass={'build_ext': BuildExt},
cmdclass={
'install_headers': InstallHeaders,
'build_py': BuildPy,
'build_ext': CMakeBuild
},
# ext_modules=ext_modules,
ext_modules=[CMakeExtension('pyxir')],
# headers=['../include/pyxir/pyxir.hpp'],
zip_safe=False
)