blob: 25626ec7f6b31b98df4e4bb0e121c17e0e725f5a [file] [log] [blame]
Alex Nicksay6f55ee62016-09-28 15:19:49 -04001# Copyright 2015 The Brotli Authors. All rights reserved.
2#
3# Distributed under MIT license.
4# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
6import os
7import platform
8import re
Alex Nicksay89a5b6e2016-12-20 08:40:47 -05009import unittest
10
Cosimo Lupob2f6c6d2015-10-01 12:57:50 +010011try:
Alex Nicksay6f55ee62016-09-28 15:19:49 -040012 from setuptools import Extension
13 from setuptools import setup
Cosimo Lupob2f6c6d2015-10-01 12:57:50 +010014except:
Alex Nicksay6f55ee62016-09-28 15:19:49 -040015 from distutils.core import Extension
16 from distutils.core import setup
Cosimo Lupo77c23992015-03-31 09:30:56 +010017from distutils.command.build_ext import build_ext
Cosimo Lupo4f455ca2017-08-23 19:45:13 +010018from distutils import errors
19from distutils import dep_util
20from distutils import log
Cosimo Lupo77c23992015-03-31 09:30:56 +010021
22
Cosimo Lupod2c8b272015-08-11 10:38:20 +010023CURR_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
24
Cosimo Lupoc3540e22015-08-10 18:01:29 +010025
Cosimo Lupod2c8b272015-08-11 10:38:20 +010026def get_version():
Eugene Kliuchnikov2c2d5572016-08-22 15:44:12 +020027 """ Return BROTLI_VERSION string as defined in 'common/version.h' file. """
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +020028 version_file_path = os.path.join(CURR_DIR, 'c', 'common', 'version.h')
Eugene Kliuchnikov2c2d5572016-08-22 15:44:12 +020029 version = 0
Alex Nicksay6f55ee62016-09-28 15:19:49 -040030 with open(version_file_path, 'r') as f:
Cosimo Lupod2c8b272015-08-11 10:38:20 +010031 for line in f:
Eugene Kliuchnikovcedddde2016-08-22 15:59:08 +020032 m = re.match(r'#define\sBROTLI_VERSION\s+0x([0-9a-fA-F]+)', line)
Cosimo Lupod2c8b272015-08-11 10:38:20 +010033 if m:
Eugene Kliuchnikov2c2d5572016-08-22 15:44:12 +020034 version = int(m.group(1), 16)
35 if version == 0:
Alex Nicksay6f55ee62016-09-28 15:19:49 -040036 return ''
37 # Semantic version is calculated as (MAJOR << 24) | (MINOR << 12) | PATCH.
38 major = version >> 24
39 minor = (version >> 12) & 0xFFF
40 patch = version & 0xFFF
41 return '{0}.{1}.{2}'.format(major, minor, patch)
Cosimo Lupod2c8b272015-08-11 10:38:20 +010042
43
Alex Nicksay89a5b6e2016-12-20 08:40:47 -050044def get_test_suite():
45 test_loader = unittest.TestLoader()
46 test_suite = test_loader.discover('python', pattern='*_test.py')
47 return test_suite
Cosimo Lupo77c23992015-03-31 09:30:56 +010048
49
50class BuildExt(build_ext):
Alex Nicksay6f55ee62016-09-28 15:19:49 -040051
Cosimo Lupo77c23992015-03-31 09:30:56 +010052 def get_source_files(self):
53 filenames = build_ext.get_source_files(self)
54 for ext in self.extensions:
55 filenames.extend(ext.depends)
56 return filenames
57
58 def build_extension(self, ext):
Cosimo Lupo4f455ca2017-08-23 19:45:13 +010059 if ext.sources is None or not isinstance(ext.sources, (list, tuple)):
60 raise errors.DistutilsSetupError(
61 "in 'ext_modules' option (extension '%s'), "
62 "'sources' must be present and must be "
63 "a list of source filenames" % ext.name)
64
65 ext_path = self.get_ext_fullpath(ext.name)
66 depends = ext.sources + ext.depends
67 if not (self.force or dep_util.newer_group(depends, ext_path, 'newer')):
68 log.debug("skipping '%s' extension (up-to-date)", ext.name)
69 return
70 else:
71 log.info("building '%s' extension", ext.name)
72
Cosimo Lupo77c23992015-03-31 09:30:56 +010073 c_sources = []
74 cxx_sources = []
75 for source in ext.sources:
Alex Nicksay6f55ee62016-09-28 15:19:49 -040076 if source.endswith('.c'):
Cosimo Lupo77c23992015-03-31 09:30:56 +010077 c_sources.append(source)
78 else:
79 cxx_sources.append(source)
80 extra_args = ext.extra_compile_args or []
81
82 objects = []
Alex Nicksay6f55ee62016-09-28 15:19:49 -040083 for lang, sources in (('c', c_sources), ('c++', cxx_sources)):
84 if lang == 'c++':
85 if self.compiler.compiler_type == 'msvc':
86 extra_args.append('/EHsc')
Cosimo Lupo77c23992015-03-31 09:30:56 +010087
88 macros = ext.define_macros[:]
Alex Nicksay6f55ee62016-09-28 15:19:49 -040089 if platform.system() == 'Darwin':
90 macros.append(('OS_MACOSX', '1'))
91 elif self.compiler.compiler_type == 'mingw32':
Cosimo Lupo54baf432015-08-10 18:04:30 +010092 # On Windows Python 2.7, pyconfig.h defines "hypot" as "_hypot",
93 # This clashes with GCC's cmath, and causes compilation errors when
94 # building under MinGW: http://bugs.python.org/issue11566
Alex Nicksay6f55ee62016-09-28 15:19:49 -040095 macros.append(('_hypot', 'hypot'))
Cosimo Lupo77c23992015-03-31 09:30:56 +010096 for undef in ext.undef_macros:
97 macros.append((undef,))
98
Alex Nicksay6f55ee62016-09-28 15:19:49 -040099 objs = self.compiler.compile(
100 sources,
101 output_dir=self.build_temp,
102 macros=macros,
103 include_dirs=ext.include_dirs,
104 debug=self.debug,
105 extra_postargs=extra_args,
106 depends=ext.depends)
Cosimo Lupo77c23992015-03-31 09:30:56 +0100107 objects.extend(objs)
108
109 self._built_objects = objects[:]
110 if ext.extra_objects:
111 objects.extend(ext.extra_objects)
112 extra_args = ext.extra_link_args or []
Cosimo Lupo54baf432015-08-10 18:04:30 +0100113 # when using GCC on Windows, we statically link libgcc and libstdc++,
114 # so that we don't need to package extra DLLs
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400115 if self.compiler.compiler_type == 'mingw32':
Cosimo Lupo54baf432015-08-10 18:04:30 +0100116 extra_args.extend(['-static-libgcc', '-static-libstdc++'])
Cosimo Lupo77c23992015-03-31 09:30:56 +0100117
118 ext_path = self.get_ext_fullpath(ext.name)
119 # Detect target language, if not provided
120 language = ext.language or self.compiler.detect_language(sources)
121
122 self.compiler.link_shared_object(
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400123 objects,
124 ext_path,
Cosimo Lupo77c23992015-03-31 09:30:56 +0100125 libraries=self.get_libraries(ext),
126 library_dirs=ext.library_dirs,
127 runtime_library_dirs=ext.runtime_library_dirs,
128 extra_postargs=extra_args,
129 export_symbols=self.get_export_symbols(ext),
130 debug=self.debug,
131 build_temp=self.build_temp,
132 target_lang=language)
133
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400134
135NAME = 'Brotli'
136
137VERSION = get_version()
138
139URL = 'https://github.com/google/brotli'
140
141DESCRIPTION = 'Python bindings for the Brotli compression library'
142
143AUTHOR = 'The Brotli Authors'
144
James Hilliard5c3a9a92019-08-16 08:32:14 -0600145LICENSE = 'MIT'
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400146
147PLATFORMS = ['Posix', 'MacOS X', 'Windows']
148
149CLASSIFIERS = [
150 'Development Status :: 4 - Beta',
151 'Environment :: Console',
152 'Intended Audience :: Developers',
James Hilliard5c3a9a92019-08-16 08:32:14 -0600153 'License :: OSI Approved :: MIT License',
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400154 'Operating System :: MacOS :: MacOS X',
155 'Operating System :: Microsoft :: Windows',
156 'Operating System :: POSIX :: Linux',
157 'Programming Language :: C',
158 'Programming Language :: C++',
159 'Programming Language :: Python',
160 'Programming Language :: Python :: 2',
161 'Programming Language :: Python :: 2.7',
162 'Programming Language :: Python :: 3',
163 'Programming Language :: Python :: 3.3',
164 'Programming Language :: Python :: 3.4',
165 'Programming Language :: Python :: 3.5',
166 'Programming Language :: Unix Shell',
167 'Topic :: Software Development :: Libraries',
168 'Topic :: Software Development :: Libraries :: Python Modules',
169 'Topic :: System :: Archiving',
170 'Topic :: System :: Archiving :: Compression',
171 'Topic :: Text Processing :: Fonts',
172 'Topic :: Utilities',
173]
174
Alex Nicksayf7b5b3d2016-09-28 17:26:00 -0400175PACKAGE_DIR = {'': 'python'}
176
177PY_MODULES = ['brotli']
178
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400179EXT_MODULES = [
180 Extension(
Alex Nicksayf7b5b3d2016-09-28 17:26:00 -0400181 '_brotli',
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400182 sources=[
Alex Nicksayf7b5b3d2016-09-28 17:26:00 -0400183 'python/_brotli.cc',
Eugene Kliuchnikov223d80c2020-08-26 12:32:27 +0200184 'c/common/constants.c',
185 'c/common/context.c',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200186 'c/common/dictionary.c',
Eugene Kliuchnikov223d80c2020-08-26 12:32:27 +0200187 'c/common/platform.c',
Eugene Kliuchnikov35e69fc2018-02-26 09:04:36 -0500188 'c/common/transform.c',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200189 'c/dec/bit_reader.c',
190 'c/dec/decode.c',
191 'c/dec/huffman.c',
192 'c/dec/state.c',
193 'c/enc/backward_references.c',
194 'c/enc/backward_references_hq.c',
195 'c/enc/bit_cost.c',
196 'c/enc/block_splitter.c',
197 'c/enc/brotli_bit_stream.c',
198 'c/enc/cluster.c',
Eugene Kliuchnikov223d80c2020-08-26 12:32:27 +0200199 'c/enc/command.c',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200200 'c/enc/compress_fragment.c',
201 'c/enc/compress_fragment_two_pass.c',
202 'c/enc/dictionary_hash.c',
203 'c/enc/encode.c',
Eugene Kliuchnikov35e69fc2018-02-26 09:04:36 -0500204 'c/enc/encoder_dict.c',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200205 'c/enc/entropy_encode.c',
Eugene Kliuchnikov223d80c2020-08-26 12:32:27 +0200206 'c/enc/fast_log.c',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200207 'c/enc/histogram.c',
208 'c/enc/literal_cost.c',
209 'c/enc/memory.c',
210 'c/enc/metablock.c',
211 'c/enc/static_dict.c',
212 'c/enc/utf8_util.c',
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400213 ],
214 depends=[
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200215 'c/common/constants.h',
Eugene Kliuchnikov35e69fc2018-02-26 09:04:36 -0500216 'c/common/context.h',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200217 'c/common/dictionary.h',
Eugene Kliuchnikov35e69fc2018-02-26 09:04:36 -0500218 'c/common/platform.h',
219 'c/common/transform.h',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200220 'c/common/version.h',
221 'c/dec/bit_reader.h',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200222 'c/dec/huffman.h',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200223 'c/dec/prefix.h',
224 'c/dec/state.h',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200225 'c/enc/backward_references.h',
226 'c/enc/backward_references_hq.h',
227 'c/enc/backward_references_inc.h',
228 'c/enc/bit_cost.h',
229 'c/enc/bit_cost_inc.h',
230 'c/enc/block_encoder_inc.h',
231 'c/enc/block_splitter.h',
232 'c/enc/block_splitter_inc.h',
233 'c/enc/brotli_bit_stream.h',
234 'c/enc/cluster.h',
235 'c/enc/cluster_inc.h',
236 'c/enc/command.h',
237 'c/enc/compress_fragment.h',
Cosimo Lupo4f455ca2017-08-23 19:45:13 +0100238 'c/enc/compress_fragment_two_pass.h',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200239 'c/enc/dictionary_hash.h',
Eugene Kliuchnikov35e69fc2018-02-26 09:04:36 -0500240 'c/enc/encoder_dict.h',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200241 'c/enc/entropy_encode.h',
242 'c/enc/entropy_encode_static.h',
243 'c/enc/fast_log.h',
244 'c/enc/find_match_length.h',
245 'c/enc/hash.h',
William A. Kennington IIIfc4d3452018-07-09 01:40:08 -0700246 'c/enc/hash_composite_inc.h',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200247 'c/enc/hash_forgetful_chain_inc.h',
248 'c/enc/hash_longest_match64_inc.h',
249 'c/enc/hash_longest_match_inc.h',
250 'c/enc/hash_longest_match_quickly_inc.h',
William A. Kennington IIIfc4d3452018-07-09 01:40:08 -0700251 'c/enc/hash_rolling_inc.h',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200252 'c/enc/hash_to_binary_tree_inc.h',
253 'c/enc/histogram.h',
254 'c/enc/histogram_inc.h',
255 'c/enc/literal_cost.h',
256 'c/enc/memory.h',
257 'c/enc/metablock.h',
258 'c/enc/metablock_inc.h',
Eugene Kliuchnikov631fe192018-03-20 17:37:41 +0600259 'c/enc/params.h',
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200260 'c/enc/prefix.h',
261 'c/enc/quality.h',
262 'c/enc/ringbuffer.h',
263 'c/enc/static_dict.h',
264 'c/enc/static_dict_lut.h',
265 'c/enc/utf8_util.h',
266 'c/enc/write_bits.h',
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400267 ],
268 include_dirs=[
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +0200269 'c/include',
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400270 ],
271 language='c++'),
272]
273
Alex Nicksay89a5b6e2016-12-20 08:40:47 -0500274TEST_SUITE = 'setup.get_test_suite'
275
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400276CMD_CLASS = {
277 'build_ext': BuildExt,
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400278}
Cosimo Lupo77c23992015-03-31 09:30:56 +0100279
280setup(
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400281 name=NAME,
282 description=DESCRIPTION,
283 version=VERSION,
284 url=URL,
285 author=AUTHOR,
286 license=LICENSE,
287 platforms=PLATFORMS,
288 classifiers=CLASSIFIERS,
Alex Nicksayf7b5b3d2016-09-28 17:26:00 -0400289 package_dir=PACKAGE_DIR,
290 py_modules=PY_MODULES,
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400291 ext_modules=EXT_MODULES,
Alex Nicksay89a5b6e2016-12-20 08:40:47 -0500292 test_suite=TEST_SUITE,
Alex Nicksay6f55ee62016-09-28 15:19:49 -0400293 cmdclass=CMD_CLASS)