forked from cavalab/feat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
290 lines (246 loc) · 10.2 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
import pdb
#from distutils.core import setup
import sys
import os
import shutil
from setuptools import setup, find_packages
from setuptools.extension import Extension, Library
from setuptools.command.build_ext import build_ext
from distutils.dir_util import remove_tree
# from Cython.Build import cythonize
import subprocess
from sys import platform
################################################################################
# PACKAGE VERSION #####
# Source: https://github.com/Changaco/version.py
from os.path import dirname, isdir, join
import re
PREFIX = ''
tag_re = re.compile(r'\btag: %s([0-9][^,]*)\b' % PREFIX)
version_re = re.compile('^Version: (.+)$', re.M)
def get_version(write=False):
# Return the version if it has been injected into the file by git-archive
try:
version = tag_re.search('$Format:%D$')
if version:
return version.group(1)
d = dirname(__file__) #+ '/../'
if isdir(join(d, '.git')):
# Get the version using "git describe".
cmd = 'git describe --tags --match %s[0-9]* --dirty' % PREFIX
try:
version = subprocess.check_output(
cmd.split()).decode().strip()[len(PREFIX):]
except subprocess.CalledProcessError:
raise RuntimeError('Unable to get version number from git tags')
# PEP 440 compatibility
if '-' in version:
if version.endswith('-dirty'):
raise RuntimeError('The working tree is dirty')
version = '.post'.join(version.split('-')[:2])
else:
# Extract the version from the PKG-INFO file.
with open(join(d, 'PKG-INFO')) as f:
version = version_re.search(f.read()).group(1)
# write version
if write:
with open('feat/versionstr.py','w') as wf:
wf.write('__version__="{}"'.format(version))
return version
except Exception as e:
print('Version error:',e)
print(
'Unable to get version number from git tags. Not updating version.')
with open('feat/versionstr.py','r') as f:
v = f.readline().split('=')[-1][1:-1]
print('returning version=',v)
return v
# from versionstr import __version__
# return __version__
package_version = get_version(write=True)
print('package version:',package_version)
################################################################################
# set paths
env_params = os.environ.keys()
if 'CONDA_PREFIX' in env_params:
ENV_PREFIX = os.environ['CONDA_PREFIX']
LIB_PATH = os.path.join(ENV_PREFIX,'lib')
INCLUDE_PATH = os.path.join(ENV_PREFIX,'include')
EIGEN_DIR = os.environ['CONDA_PREFIX']+'/include/eigen3/'
SHOGUN_INCLUDE_DIR = INCLUDE_PATH
SHOGUN_LIB = os.environ['CONDA_PREFIX']+'/lib/'
else:
# ENV_PREFIX = os.environ
LIB_PATH = os.environ['LD_LIBRARY_PATH'].split(':')[0]
INCLUDE_PATH = '/usr/include/'
if 'EIGEN3_INCLUDE_DIR' in env_params:
EIGEN_DIR = os.environ['EIGEN3_INCLUDE_DIR']
else:
EIGEN_DIR = INCLUDE_PATH+'/eigen3/'
if 'SHOGUN_DIR' in env_params:
SHOGUN_INCLUDE_DIR = os.environ['SHOGUN_DIR']
else:
SHOGUN_INCLUDE_DIR = INCLUDE_PATH
if 'SHOGUN_LIB' in env_params:
SHOGUN_LIB = os.environ['SHOGUN_LIB']
else:
SHOGUN_LIB = '/usr/lib/'
print('INCLUDE_PATH:',INCLUDE_PATH)
print('LIB_PATH:',LIB_PATH)
print('EIGEN_DIR:',EIGEN_DIR)
print('SHOGUN_INCLUDE_DIR:',SHOGUN_INCLUDE_DIR)
print('SHOGUN_LIB:',SHOGUN_LIB)
if 'CMAKE_BUILD_PARALLEL_LEVEL' not in env_params:
print('setting build parallel level to 4')
os.environ['CMAKE_BUILD_PARALLEL_LEVEL'] = '4'
################################################################################
################################################################################
# Cmake build extension
PLAT_TO_CMAKE = {
"win32": "Win32",
"win-amd64": "x64",
"win-arm32": "ARM",
"win-arm64": "ARM64"
}
with open("README.md", 'r', encoding="utf-8") as fp:
long_description = fp.read()
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=""):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
import sysconfig
platlib = sysconfig.get_path('platlib')
class CMakeBuild(build_ext):
def build_extension(self, ext):
if not isinstance(ext, CMakeExtension):
libfeatname = \
'.'.join(self.ext_map['feat.libfeat']._file_name[8:].split('.')[:-1])
extdir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(self.ext_map['feat.libfeat'].name))
)
ext.libraries += [libfeatname]
# ext.library_dirs += [extdir]
ext.library_dirs += [extdir.split('feat/')[-1]]
print('ext.libraries:',ext.libraries)
print('ext.library_dirs:',ext.library_dirs)
# pdb.set_trace()
# add a runtime directory to where this extension is ending up
return super().build_extension(ext)
print("building extension...")
cfg = "Debug" if self.debug else "Release"
# cfg = "Debug"
# cfg = "Release"
# extdir = LIB_PATH
extdir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name))
)
extsuffix = '.'+'.'.join(ext._file_name.split('.')[-2:])
cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
cmake_args = [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}",
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
f"-DSHOGUN_DIR={SHOGUN_INCLUDE_DIR}",
f"-DSHOGUN_LIB={SHOGUN_LIB}",
f"-DEIGEN3_INCLUDE_DIR={EIGEN_DIR}",
f"-DOMP={'OFF' if cfg=='Debug' else 'ON'}",
f"-DGTEST={'OFF' if cfg=='Debug' else 'OFF'}",
f"-DFEAT_LIB_SUFFIX={extsuffix}"
]
# build_args = ['--verbose']
build_args = ['--verbose']
if self.compiler.compiler_type != "msvc":
if not cmake_generator:
cmake_args += ["-GNinja"]
else:
# Single config generators are handled "normally"
single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
# CMake allows an arch-in-generator style for backward compatibility
contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
# Specify the arch if using MSVC generator, but only if it doesn't
# contain a backward-compatibility arch spec already in the
# generator name.
if not single_config and not contains_arch:
cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
# Multi-config generators have a different way to specify configs
if not single_config:
cmake_args += [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}"
]
build_args += ["--config", cfg]
# Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
# across all generators.
if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
# self.parallel is a Python 3 only way to set parallel jobs by hand
# using -j in the build_ext call, not supported by pip or PyPA-build.
if hasattr(self, "parallel") and self.parallel:
# CMake 3.12+ only.
build_args += ["-j{}".format(self.parallel)]
else:
print('cmake parallel level:',os.environ['CMAKE_BUILD_PARALLEL_LEVEL'])
os.makedirs(self.build_temp, exist_ok=True)
print("cmake", ext.sourcedir,cmake_args)
print(["cmake", "--build", "."] + build_args)
subprocess.check_call(
["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp
)
subprocess.check_call(
["cmake", "--build", "."] + build_args, cwd=self.build_temp
)
# # copy libfeat to the library path
# lib_fullname= f'{extdir}/libfeat{extsuffix}'
# lib_copyname= f'{LIB_PATH}/libfeat{extsuffix}'
# # symbolic link the feat library to one without the added platform info
# linksuffix = '.'+extsuffix.split('.')[-1]
# lib_linkname= f'{LIB_PATH}/libfeat{linksuffix}'
# print(f'creating copy of {lib_fullname} named {lib_copyname} ')
# shutil.copy(
# lib_fullname,
# lib_copyname
# )
# print(f'creating a link to {lib_copyname} named {lib_linkname} ')
# os.symlink(
# lib_copyname,
# lib_linkname
# )
# # # # Clean old build/ directory if it exists
# try:
# remove_tree("./build")
# print("Removed old build directory.")
# except FileNotFoundError:
# print("No existing build directory found - skipping removal.")
# add extra compile args based on platform
extra_compile_args = ['-std=c++1y',
'-fopenmp',
'-Wno-sign-compare',
'-Wno-reorder',
'-Wno-unused-variable',
'-Wno-deprecated',
'-Wno-deprecated-declarations'
]
if platform == 'darwin':
extra_compile_args.append('-Winconsistent-missing-override')
################################################################################
# where FEAT ends up getting installed; used to specify runtime library
# directory
target_path = os.path.join(sysconfig.get_path('platlib'), 'feat')
setup(
name="feat-ml",
version=package_version,
author='William La Cava',
author_email='williamlacava@gmail.com',
url = 'https://cavalab.org/feat',
download_url=('https://github.com/cavalab/feat/releases/tag/'
+package_version),
license='GNU/GPLv3',
description='A Feature Engineering Automation Tool',
python_requires='>=3.7',
install_requires=[
'Numpy',
'scikit-learn',
'pandas'
],
packages = ['feat'],
ext_modules = ([CMakeExtension("_feat")]),
cmdclass={"build_ext": CMakeBuild},
zip_safe=False
)