-
Notifications
You must be signed in to change notification settings - Fork 24
/
setup.py
221 lines (193 loc) · 6.64 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
#! /usr/bin/env python
##########################################################################
# XXX - Copyright (C) XXX, 2017
# Distributed under the terms of the CeCILL-B license, as published by
# the CEA-CNRS-INRIA. Refer to the LICENSE file or to
# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
# for details.
##########################################################################
# System import
import atexit
import os
import re
import sys
import platform
import subprocess
from pprint import pprint
from distutils.version import LooseVersion
from setuptools.command.build_ext import build_ext
from setuptools import setup, find_packages, Extension
from setuptools.command.test import test as TestCommand
from setuptools.command.install import install
from importlib import import_module
# Package information
release_info = {}
infopath = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'pysap', 'info.py'))
with open(infopath) as open_file:
exec(open_file.read(), release_info)
pkgdata = {
'pysap': [
os.path.join('test', '*.py'),
os.path.join('test', '*.json'),
os.path.join('apps', '*.json')]
}
scripts = [
os.path.join('pysap', 'apps', 'pysapview3')
]
# Workaround
rm_args = []
if '--release' in sys.argv:
rm_args.append('--release')
scripts = [
os.path.join('pysap', 'apps', 'pysapview3'),
os.path.join('pysap', 'apps', 'pysapview')
]
# Optional install of Sparse2D, default option is to build Sparse2D
build_sparse2d = True
if '--nosparse2d' in sys.argv:
rm_args.append('--nosparse2d')
build_sparse2d = False
# Optional install of PySAP plug-ins, default option is to install all plug-ins
no_plugins = False
if '--noplugins' in sys.argv:
rm_args.append('--noplugins')
no_plugins = True
only_plugins = []
for arg in sys.argv:
if '--only' in arg:
rm_args.append(arg)
only_plugins = arg.split('--only=')[1].split(',')
# Clean up system arguments
for arg in rm_args:
sys.argv.remove(arg)
class CMakeExtension(Extension):
"""Use absolute path in setuptools extension."""
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
"""Define a cmake build extension."""
def run(self):
"""Redefine the run method."""
# Check that an appropriate version of cmake is installed.
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)
)
cmake_version = LooseVersion(
re.search(r'version\s*([\d.]+)', out.decode()).group(1)
)
if cmake_version < '3.12.0':
raise RuntimeError('CMake >= 3.12.0 is required.')
# Build extensions
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
"""Build extension with cmake."""
# Define cmake arguments
extdir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name))
)
cmake_args = [
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_EXECUTABLE=' + sys.executable,
]
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
if platform.system() == 'Windows':
cmake_args += [
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{0}={1}'.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 += ['--', '-j8']
# Call cmake in specific environment
env = os.environ.copy()
env['CXXFLAGS'] = '{0} -DVERSION_INFO=\\"{1}\\"'.format(
env.get('CXXFLAGS', ''),
self.distribution.get_version(),
)
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
print('Building "pysparse" in {0}...'.format(self.build_temp))
print('Cmake args:')
pprint(cmake_args)
print('Cmake build args:')
pprint(build_args)
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()
class HybridTestCommand(TestCommand):
"""Define custom mix Python/C++ test runner.
We will execute both Python unittest tests and C++ Catch tests.
"""
def distutils_dir_name(self, dname):
"""Returns the name of a distutils build directory."""
dir_name = '{dirname}.{platform}-{version[0]}.{version[1]}'
return dir_name.format(
dirname=dname,
platform=sysconfig.get_platform(),
version=sys.version_info,
)
def run(self):
"""Run hybrid tests."""
# Run Python tests
super(HybridTestCommand, self).run()
print('\nPython tests complete, now running C++ tests...\n')
# Run catch tests
test_dir = os.path.join(
'build',
self.distutils_dir_name('temp'),
'sparse2d',
'src',
'sparse2d',
'tests',
)
print('\nExpect C++ test script in {0}.\n'.format(test_dir))
subprocess.call(['./*_test'], cwd=test_dir, shell=True)
# Set default values for ext_modules and cmdclass
ext_modules = None
cmdclass = {'install': install}
# Add Sparse2D build commands
if build_sparse2d:
ext_modules = [CMakeExtension(
'pysparse', sourcedir=os.path.join('sparse2d', 'python')
)]
cmdclass['build_ext'] = CMakeBuild
cmdclass['test'] = HybridTestCommand
# Write setup
setup(
name=release_info['NAME'],
description=release_info['DESCRIPTION'],
long_description=release_info['LONG_DESCRIPTION'],
license=release_info['LICENSE'],
classifiers=release_info['CLASSIFIERS'],
author=release_info['AUTHOR'],
author_email=release_info['AUTHOR_EMAIL'],
version=release_info['VERSION'],
url=release_info['URL'],
packages=find_packages(exclude='doc'),
platforms=release_info['PLATFORMS'],
install_requires=release_info['REQUIRES'],
package_data=pkgdata,
scripts=scripts,
ext_modules=ext_modules,
cmdclass=cmdclass,
)