-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.py
233 lines (205 loc) · 9.13 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
#!/usr/bin/env python3
import fnmatch
import os
import platform
import re
import sys
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
def read_stuff():
ROOT_PATH = os.path.dirname(os.path.realpath(__file__))
# Read README.md
README_PATH = os.path.join(ROOT_PATH, 'README.md')
with open(README_PATH, 'r', encoding='utf-8') as file:
long_description = file.read()
# Read module version
INIT_PATH = os.path.join(ROOT_PATH, 'src', '__init__.py')
with open(INIT_PATH, 'r', encoding='utf-8') as file:
file_content = file.read()
m = re.search(r'''__version__\s*=\s*(['"])(.*?)\1''', file_content)
module_version = m.group(2)
return long_description, module_version
def get_zstd_files_list():
ret = []
for sub_dir in ('common', 'compress', 'decompress', 'dictBuilder'):
directory = 'zstd/lib/' + sub_dir + '/'
dir_list = os.listdir(directory)
# Source files
l = [directory + fn
for fn in dir_list
if fnmatch.fnmatch(fn, '*.[cCsS]')]
ret.extend(l)
return ret
def has_option(option):
if option in sys.argv:
print(' * build pyzstd wheel with option:', option)
sys.argv = [s for s in sys.argv if s != option]
return True
else:
return False
class pyzstd_build_ext(build_ext):
PYZSTD_AVX2 = False
PYZSTD_DEBUG = False
PYZSTD_WARNING_AS_ERROR = False
PYZSTD_CONFIG_MSG = ''
def build_extensions(self):
# Print build config message in actual build
print(self.PYZSTD_CONFIG_MSG)
# Accept assembly files
self.compiler.src_extensions.extend(['.s', '.S'])
# Build debug build
self.debug = self.PYZSTD_DEBUG
for extension in self.extensions:
if self.compiler.compiler_type in ('unix', 'mingw32', 'cygwin'):
# -g0:
# Level 0 produces no debug information at all. This reduces
# the size of GCC wheels. By default CPython won't print any
# C stack trace, so -g0 and -g2 are same for most users.
# -flto:
# This option runs the standard link-time optimizer. To use the
# link-time optimizer, -flto and optimization options should be
# specified at compile time and during the final link.
more_options = ['-g0', '-flto']
if self.PYZSTD_AVX2:
instrs = ['-mavx2', '-mlzcnt', '-mbmi', '-mbmi2']
more_options.extend(instrs)
if self.PYZSTD_WARNING_AS_ERROR:
more_options.append('-Werror')
extension.extra_compile_args.extend(more_options)
extension.extra_link_args.extend(['-g0', '-flto'])
elif self.compiler.compiler_type == 'msvc':
# Remove .S source files, they use gcc/clang syntax.
extension.sources = [i for i in extension.sources
if not fnmatch.fnmatch(i, '*.[sS]')]
# /Ob3: More aggressive inlining than /Ob2.
# /GF: Eliminates duplicate strings.
# /Gy: Does function level linking.
# /Ob3 is a bit faster on the whole. In setuptools v56.1+,
# /GF and /Gy are enabled by default, they reduce the size
# of MSVC wheels.
more_options = ['/Ob3', '/GF', '/Gy']
if self.PYZSTD_AVX2:
more_options.append('/arch:AVX2')
if self.PYZSTD_WARNING_AS_ERROR:
more_options.append('/WX')
extension.extra_compile_args.extend(more_options)
super().build_extensions()
def do_setup():
# Read stuff
long_description, module_version = read_stuff()
# Parse options
pyzstd_build_ext.PYZSTD_AVX2 = has_option('--avx2')
pyzstd_build_ext.PYZSTD_DEBUG = has_option('--debug')
pyzstd_build_ext.PYZSTD_WARNING_AS_ERROR = has_option('--warning-as-error')
DYNAMIC_LINK = has_option('--dynamic-link-zstd')
CFFI = has_option('--cffi') or platform.python_implementation() == 'PyPy'
MULTI_PHASE_INIT = has_option('--multi-phase-init')
NO_MREMAP = has_option('--no-mremap')
# Build config message
pyzstd_build_ext.PYZSTD_CONFIG_MSG = \
('+--------------------------------------------+\n'
'| Pyzstd build config |\n'
'+-------------------------+------------------+\n'
'| Pyzstd version | {!s:<16} |\n'
'+-------------------------+------------------+\n'
'| Implementation | {!s:<16} |\n'
'+-------------------------+------------------+\n'
'| Link to zstd library | {!s:<16} |\n'
'+-------------------------+------------------+\n'
'| Enable AVX2/BMI2 | {!s:<16} |\n'
'+-------------------------+------------------+\n'
'| Debug build | {!s:<16} |\n'
'+-------------------------+------------------+\n'
'| Warning as error | {!s:<16} |\n'
'+-------------------------+------------------+').format(
module_version,
'CFFI' if CFFI else 'C',
'Dynamically link' if DYNAMIC_LINK else 'Statically link',
pyzstd_build_ext.PYZSTD_AVX2,
pyzstd_build_ext.PYZSTD_DEBUG,
pyzstd_build_ext.PYZSTD_WARNING_AS_ERROR)
if DYNAMIC_LINK:
kwargs = {
'include_dirs': [], # .h directory
'library_dirs': [], # .lib directory
'libraries': ['zstd'], # lib name, not filename, for the linker.
'sources': [],
'define_macros': []
}
else: # Statically link to zstd lib
kwargs = {
'include_dirs': ['zstd/lib',
# For zstd 1.4.x:
'zstd/lib/common',
'zstd/lib/dictBuilder'],
'library_dirs': [],
'libraries': [],
'sources': get_zstd_files_list(),
'define_macros': [('PYZSTD_STATIC_LINK', None),
# Enable multi-threaded compression
('ZSTD_MULTITHREAD', None)]
}
if CFFI:
# Packages
packages = ['pyzstd', 'pyzstd.cffi']
# Binary extension
kwargs['module_name'] = 'pyzstd.cffi._cffi_zstd'
sys.path.append('build_script')
import pyzstd_build_cffi
binary_extension = pyzstd_build_cffi.get_extension(**kwargs)
else: # C implementation
# Packages
packages = ['pyzstd', 'pyzstd.c']
# Binary extension
kwargs['name'] = 'pyzstd.c._zstd'
kwargs['sources'].append('src/bin_ext/pyzstd.c')
if MULTI_PHASE_INIT:
# Use multi-phase initialization (PEP-489) on CPython 3.11.
# On CPython 3.12+, it's always enabled.
kwargs['define_macros'].append(('USE_MULTI_PHASE_INIT', None))
if NO_MREMAP:
# Disable mremap output buffer on Linux
kwargs['define_macros'].append(('PYZSTD_NO_MREMAP', None))
binary_extension = Extension(**kwargs)
setup(
name='pyzstd',
version=module_version,
description=("Python bindings to Zstandard (zstd) compression library."),
long_description=long_description,
long_description_content_type='text/markdown',
author='Ma Lin',
author_email='malincns@163.com',
maintainer="Rogdham",
maintainer_email="contact@rogdham.net",
url='https://github.com/Rogdham/pyzstd',
license='BSD-3-Clause',
python_requires='>=3.5',
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Topic :: System :: Archiving :: Compression",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
],
keywords='zstandard zstd zst compress decompress tar file seekable format',
package_dir={'pyzstd': 'src'},
packages=packages,
package_data={'pyzstd': ['__init__.pyi', 'py.typed']},
ext_modules=[binary_extension],
cmdclass={'build_ext': pyzstd_build_ext},
test_suite='tests'
)
if sys.version_info < (3, 8):
print()
print("WARNING")
print(" Python {} has reached end of life.".format(platform.python_version()))
print(" This version of Python will not be supported in future versions of pyzstd.")
print()
if __name__ == '__main__':
do_setup()