-
Notifications
You must be signed in to change notification settings - Fork 7
/
setup.py
334 lines (298 loc) · 11.6 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
########################################################################
#
# pyfastnoisesimd
# License: BSD
# Created: August 13, 2017
# C++ Library Author: Jordan Peck - https://github.com/Auburns
# Python Extension Author: Robert A. McLeod - robbmcleod@gmail.com
#
########################################################################
# flake8: noqa
from __future__ import print_function
import os
import platform
import re
import tempfile
import subprocess
from distutils.ccompiler import new_compiler
from distutils.command.build import build as _build
from distutils.errors import CCompilerError, DistutilsOptionError
from distutils.sysconfig import customize_compiler
from setuptools import Extension
from setuptools import setup
# `pyfastnoisesimd` version
major_ver = 0
minor_ver = 4
nano_ver = 3
branch = ''
VERSION = f'{major_ver}.{minor_ver}.{nano_ver}.{branch}' if branch else f'{major_ver}.{minor_ver}.{nano_ver}'
# Create the version.py file
open('pyfastnoisesimd/version.py', 'w').write('__version__ = "%s"\n' % VERSION)
# Sources and headers
sources = [
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD.cpp',
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD_internal.cpp',
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD_neon.cpp',
'pyfastnoisesimd/wrapper.cpp',
]
# For (some versions of) `pip`, the first command run is `python setup.py egg_info`
# which crashes if `numpy` is not present, so we protect it here.
try:
from numpy import get_include
inc_dirs = [get_include(), 'pyfastnoisesimd', 'pyfastnoisesimd/fastnoisesimd/']
except ImportError:
print('WARNING: NumPy not installed, it is required for compilation.')
inc_dirs = ['pyfastnoisesimd', 'pyfastnoisesimd/fastnoisesimd/']
lib_dirs = []
libs = []
def_macros = []
with open('README.md') as fh:
long_desc = fh.read()
with open('requirements.txt') as fh:
install_requires = [line.strip('\n') for line in fh.readlines()]
if os.name == 'nt':
extra_cflags = []
avx512 = {
'sources': [
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD_avx512.cpp'
],
'cflags': [
'/arch:AVX512', '/arch:AVX512F',
],
}
avx2 = {
'sources': [
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD_avx2.cpp'
],
'cflags': [
'/arch:AVX2',
]
}
if platform.machine() == 'AMD64': # 64-bit windows
#`/arch:SSE2` doesn't exist on Windows x64 builds, and generates a needless warnings
sse41 = {
'sources': [
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD_sse41.cpp'
],
'cflags': [
],
}
sse2 = {
'sources': [
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD_sse2.cpp'
],
'cflags': [
],
}
else: # 32-bit Windows
sse41 = {
'sources': [
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD_sse41.cpp'
],
'cflags': [
'/arch:SSE2',
],
}
sse2 = {
'sources': [
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD_sse2.cpp'
],
'cflags': [
'/arch:SSE2',
],
}
fma_flags = None
else: # Linux
extra_cflags = ['-std=c++11']
avx512 = {
'sources': [
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD_avx512.cpp'
],
'cflags': [
'-std=c++11',
'-mavx512f',
],
}
avx2 = {
'sources': [
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD_avx2.cpp'
],
'cflags': [
'-std=c++11',
'-mavx2',
]
}
sse41 = {
'sources': [
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD_sse41.cpp'
],
'cflags': [
'-std=c++11',
'-msse4.1',
],
}
sse2 = {
'sources': [
'pyfastnoisesimd/fastnoisesimd/FastNoiseSIMD_sse2.cpp'
],
'cflags': [
'-std=c++11',
'-msse2',
],
}
fma_flags = ['-mfma']
clibs = [
('avx512', avx512),
('avx2', avx2),
('sse41', sse41),
('sse2', sse2),
]
class build(_build):
user_options = _build.user_options + [
('with-avx512=', None, 'Use AVX512 instructions: auto|yes|no'),
('with-avx2=', None, 'Use AVX2 instructions: auto|yes|no'),
('with-sse41=', None, 'Use SSE4.1 instructions: auto|yes|no'),
('with-sse2=', None, 'Use SSE2 instructions: auto|yes|no'),
('with-fma=', None, 'Use FMA instructions: auto|yes|no'),
]
def initialize_options(self):
_build.initialize_options(self)
self.with_avx512 = 'auto'
self.with_avx2 = 'auto'
self.with_sse41 = 'auto'
self.with_sse2 = 'auto'
self.with_fma = 'auto'
def finalize_options(self):
_build.finalize_options(self)
compiler = new_compiler(compiler=self.compiler, verbose=self.verbose)
customize_compiler(compiler)
disabled_libraries = []
# Section for custom limits imposed on the SIMD instruction levels based
# on the installed compiler
plat_compiler = platform.python_compiler()
if plat_compiler.lower().startswith('gcc'):
# Check the installed gcc version, as versions older than 7.0 claim to
# support avx512 but are missing some intrinsics that FastNoiseSIMD calls
output = subprocess.check_output('gcc --version', shell=True)
gcc_version = tuple([int(x) for x in re.findall( b'\d+(?:\.\d+)+', output)[0].split(b'.')])
if gcc_version < (7,2): # Disable AVX512
disabled_libraries.append('avx512')
if gcc_version < (4,7): # Disable AVX2
disabled_libraries.append('avx2')
elif plat_compiler.lower().startswith('msc'):
# No versions of Windows Python support AVX512 yet
# MSVC++ 14.1 _MSC_VER == 1911 (Visual Studio 2017)
# MSVC++ 14.1 _MSC_VER == 1910 (Visual Studio 2017)
# Python 3.5/3.6: MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015)
# Python 3.4: MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010)
# Python 2.7: MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008)
# Here we just assume the user has the platform compiler
msc_version = int(re.findall('v\.\d+', plat_compiler)[0].lstrip('v.'))
# print('FOUND MSVC VERSION: ', msc_version)
# Still not working with MSVC2017 yet with 1915 and Python 3.7, it
# cannot find the function `_mm512_floor_ps`
if msc_version < 1916:
disabled_libraries.append('avx512')
if msc_version < 1900:
disabled_libraries.append('avx2')
# End of SIMD limits
for name, lib in self.distribution.libraries:
val = getattr(self, 'with_' + name)
if val not in ('auto', 'yes', 'no'):
raise DistutilsOptionError('with_%s flag must be auto, yes, '
'or no, not "%s".' % (name, val))
if val == 'no':
disabled_libraries.append(name)
continue
if not self.compiler_has_flags(compiler, name, lib['cflags']):
if val == 'yes':
# Explicitly required but not available.
raise CCompilerError('%s is not supported by your '
'compiler.' % (name, ))
disabled_libraries.append(name)
use_fma = False
if (self.with_fma != 'no' and
('avx512' not in disabled_libraries or
'avx2' not in disabled_libraries)):
if fma_flags is None:
# No flags required.
use_fma = True
elif self.compiler_has_flags(compiler, 'fma', fma_flags):
use_fma = True
avx512['cflags'] += fma_flags
avx2['cflags'] += fma_flags
elif self.with_fma == 'yes':
# Explicitly required but not available.
raise CCompilerError('FMA is not supported by your compiler.')
self.distribution.libraries = [lib
for lib in self.distribution.libraries
if lib[0] not in disabled_libraries]
with open('pyfastnoisesimd/fastnoisesimd/x86_flags.h', 'wb') as fh:
fh.write(b'// This file is generated by setup.py, '
b'do not edit it by hand\n')
for name, lib in self.distribution.libraries:
fh.write(b'#define FN_COMPILE_%b\n' % (name.upper().encode('ascii', )))
if use_fma:
fh.write(b'#define FN_USE_FMA\n')
def compiler_has_flags(self, compiler, name, flags):
cwd = os.getcwd()
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
try:
test_file = 'test-%s.cpp' % (name, )
with open(test_file, 'w') as fd:
fd.write('int main(void) { return 0; }')
try:
compiler.compile([test_file], extra_preargs=flags)
except CCompilerError:
self.warn('Compiler does not support %s flags: %s' %
(name, ' '.join(flags)))
return False
finally:
os.chdir(cwd)
return True
# List classifiers:
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers = """\
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
Intended Audience :: Information Technology
License :: OSI Approved :: BSD License
Programming Language :: Python
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Topic :: Software Development :: Libraries :: Python Modules
Topic :: Multimedia :: Graphics :: 3D Modeling
Operating System :: Microsoft :: Windows
Operating System :: Unix
"""
setup(name = "pyfastnoisesimd",
version = VERSION,
description = 'Python Fast Noise with SIMD',
long_description = long_desc,
long_description_content_type='text/markdown',
classifiers = [c for c in classifiers.split("\n") if c],
author = 'Robert A. McLeod',
author_email = 'robbmcleod@gmail.com',
maintainer = 'Robert A. McLeod',
maintainer_email = 'robbmcleod@gmail.com',
url = 'http://github.com/robbmcleod/pyfastnoisesimd',
license = 'https://opensource.org/licenses/BSD-3-Clause',
platforms = ['any'],
libraries = clibs,
cmdclass = {'build': build},
install_requires=install_requires,
ext_modules = [
Extension( "pyfastnoisesimd.extension",
include_dirs=inc_dirs,
define_macros=def_macros,
sources=sources,
library_dirs=lib_dirs,
libraries=libs,
extra_compile_args=extra_cflags),
],
# tests_require=tests_require,
packages = ['pyfastnoisesimd'],
)