| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import os |
| 7 |
import platform |
| 8 |
import re |
| 9 |
import unittest |
| 10 |
|
| 11 |
try: |
| 12 |
from setuptools import Extension |
| 13 |
from setuptools import setup |
| 14 |
except: |
| 15 |
from distutils.core import Extension |
| 16 |
from distutils.core import setup |
| 17 |
from distutils.command.build_ext import build_ext |
| 18 |
from distutils import errors |
| 19 |
from distutils import dep_util |
| 20 |
from distutils import log |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
CURR_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 25 |
|
| 26 |
|
| 27 |
def bool_from_environ(key): |
| 28 |
value = os.environ.get(key) |
| 29 |
if not value: |
| 30 |
return False |
| 31 |
if value == "1": |
| 32 |
return True |
| 33 |
if value == "0": |
| 34 |
return False |
| 35 |
raise ValueError("Environment variable {} has invalid value {}. Please set it to 1, 0 or an empty string".format(key, value)) |
| 36 |
|
| 37 |
|
| 38 |
def read_define(path, macro): |
| 39 |
"""Return macro value from the given file.""" |
| 40 |
with open(path, "r") as f: |
| 41 |
for line in f: |
| 42 |
m = re.match(r"#define\s{}\s+(.+)".format(macro), line) |
| 43 |
if m: |
| 44 |
return m.group(1) |
| 45 |
return "" |
| 46 |
|
| 47 |
|
| 48 |
def get_version(): |
| 49 |
"""Return library version string from 'common/version.h' file.""" |
| 50 |
version_file_path = os.path.join(CURR_DIR, "c", "common", "version.h") |
| 51 |
major = read_define(version_file_path, "BROTLI_VERSION_MAJOR") |
| 52 |
minor = read_define(version_file_path, "BROTLI_VERSION_MINOR") |
| 53 |
patch = read_define(version_file_path, "BROTLI_VERSION_PATCH") |
| 54 |
if not major or not minor or not patch: |
| 55 |
return "" |
| 56 |
return "{}.{}.{}".format(major, minor, patch) |
| 57 |
|
| 58 |
|
| 59 |
def get_test_suite(): |
| 60 |
test_loader = unittest.TestLoader() |
| 61 |
test_suite = test_loader.discover("python", pattern="*_test.py") |
| 62 |
return test_suite |
| 63 |
|
| 64 |
|
| 65 |
class BuildExt(build_ext): |
| 66 |
def get_source_files(self): |
| 67 |
filenames = build_ext.get_source_files(self) |
| 68 |
for ext in self.extensions: |
| 69 |
filenames.extend(ext.depends) |
| 70 |
return filenames |
| 71 |
|
| 72 |
def build_extension(self, ext): |
| 73 |
if ext.sources is None or not isinstance(ext.sources, (list, tuple)): |
| 74 |
raise errors.DistutilsSetupError( |
| 75 |
"in 'ext_modules' option (extension '%s'), " |
| 76 |
"'sources' must be present and must be " |
| 77 |
"a list of source filenames" % ext.name |
| 78 |
) |
| 79 |
|
| 80 |
ext_path = self.get_ext_fullpath(ext.name) |
| 81 |
depends = ext.sources + ext.depends |
| 82 |
if not (self.force or dep_util.newer_group(depends, ext_path, "newer")): |
| 83 |
log.debug("skipping '%s' extension (up-to-date)", ext.name) |
| 84 |
return |
| 85 |
else: |
| 86 |
log.info("building '%s' extension", ext.name) |
| 87 |
|
| 88 |
c_sources = [] |
| 89 |
for source in ext.sources: |
| 90 |
if source.endswith(".c"): |
| 91 |
c_sources.append(source) |
| 92 |
extra_args = ext.extra_compile_args or [] |
| 93 |
|
| 94 |
objects = [] |
| 95 |
|
| 96 |
macros = ext.define_macros[:] |
| 97 |
if self.compiler.compiler_type == "mingw32": |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
macros.append(("_hypot", "hypot")) |
| 102 |
for undef in ext.undef_macros: |
| 103 |
macros.append((undef,)) |
| 104 |
|
| 105 |
objs = self.compiler.compile( |
| 106 |
c_sources, |
| 107 |
output_dir=self.build_temp, |
| 108 |
macros=macros, |
| 109 |
include_dirs=ext.include_dirs, |
| 110 |
debug=self.debug, |
| 111 |
extra_postargs=extra_args, |
| 112 |
depends=ext.depends, |
| 113 |
) |
| 114 |
objects.extend(objs) |
| 115 |
|
| 116 |
self._built_objects = objects[:] |
| 117 |
if ext.extra_objects: |
| 118 |
objects.extend(ext.extra_objects) |
| 119 |
extra_args = ext.extra_link_args or [] |
| 120 |
|
| 121 |
|
| 122 |
if self.compiler.compiler_type == "mingw32": |
| 123 |
extra_args.extend(["-static-libgcc", "-static-libstdc++"]) |
| 124 |
|
| 125 |
ext_path = self.get_ext_fullpath(ext.name) |
| 126 |
|
| 127 |
language = ext.language or self.compiler.detect_language(c_sources) |
| 128 |
|
| 129 |
self.compiler.link_shared_object( |
| 130 |
objects, |
| 131 |
ext_path, |
| 132 |
libraries=self.get_libraries(ext), |
| 133 |
library_dirs=ext.library_dirs, |
| 134 |
runtime_library_dirs=ext.runtime_library_dirs, |
| 135 |
extra_postargs=extra_args, |
| 136 |
export_symbols=self.get_export_symbols(ext), |
| 137 |
debug=self.debug, |
| 138 |
build_temp=self.build_temp, |
| 139 |
target_lang=language, |
| 140 |
) |
| 141 |
|
| 142 |
|
| 143 |
NAME = "Brotli" |
| 144 |
|
| 145 |
VERSION = get_version() |
| 146 |
|
| 147 |
URL = "https://github.com/google/brotli" |
| 148 |
|
| 149 |
DESCRIPTION = "Python bindings for the Brotli compression library" |
| 150 |
|
| 151 |
AUTHOR = "The Brotli Authors" |
| 152 |
|
| 153 |
LICENSE = "MIT" |
| 154 |
|
| 155 |
PLATFORMS = ["Posix", "MacOS X", "Windows"] |
| 156 |
|
| 157 |
CLASSIFIERS = [ |
| 158 |
"Development Status :: 4 - Beta", |
| 159 |
"Environment :: Console", |
| 160 |
"Intended Audience :: Developers", |
| 161 |
|
| 162 |
|
| 163 |
"Operating System :: MacOS :: MacOS X", |
| 164 |
"Operating System :: Microsoft :: Windows", |
| 165 |
"Operating System :: POSIX :: Linux", |
| 166 |
"Programming Language :: C", |
| 167 |
"Programming Language :: C++", |
| 168 |
"Programming Language :: Python", |
| 169 |
"Programming Language :: Python :: 2", |
| 170 |
"Programming Language :: Python :: 2.7", |
| 171 |
"Programming Language :: Python :: 3", |
| 172 |
"Programming Language :: Python :: 3.3", |
| 173 |
"Programming Language :: Python :: 3.4", |
| 174 |
"Programming Language :: Python :: 3.5", |
| 175 |
"Programming Language :: Unix Shell", |
| 176 |
"Topic :: Software Development :: Libraries", |
| 177 |
"Topic :: Software Development :: Libraries :: Python Modules", |
| 178 |
"Topic :: System :: Archiving", |
| 179 |
"Topic :: System :: Archiving :: Compression", |
| 180 |
"Topic :: Text Processing :: Fonts", |
| 181 |
"Topic :: Utilities", |
| 182 |
] |
| 183 |
|
| 184 |
PACKAGE_DIR = {"": "python"} |
| 185 |
|
| 186 |
PY_MODULES = ["brotli"] |
| 187 |
|
| 188 |
USE_SYSTEM_BROTLI = bool_from_environ("USE_SYSTEM_BROTLI") |
| 189 |
|
| 190 |
if USE_SYSTEM_BROTLI: |
| 191 |
import pkgconfig |
| 192 |
|
| 193 |
REQUIRED_BROTLI_SYSTEM_LIBRARIES = ["libbrotlicommon", "libbrotlienc", "libbrotlidec"] |
| 194 |
|
| 195 |
define_macros = [] |
| 196 |
include_dirs = [] |
| 197 |
libraries = [] |
| 198 |
library_dirs = [] |
| 199 |
|
| 200 |
for required_system_library in REQUIRED_BROTLI_SYSTEM_LIBRARIES: |
| 201 |
package_configuration = pkgconfig.parse(required_system_library) |
| 202 |
|
| 203 |
define_macros += package_configuration["define_macros"] |
| 204 |
include_dirs += package_configuration["include_dirs"] |
| 205 |
libraries += package_configuration["libraries"] |
| 206 |
library_dirs += package_configuration["library_dirs"] |
| 207 |
|
| 208 |
brotli_extension = Extension( |
| 209 |
'_brotli', |
| 210 |
sources=[ |
| 211 |
'python/_brotli.c' |
| 212 |
], |
| 213 |
include_dirs=include_dirs, |
| 214 |
define_macros=define_macros, |
| 215 |
libraries=libraries, |
| 216 |
library_dirs=library_dirs |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
EXT_MODULES = [brotli_extension] |
| 221 |
else: |
| 222 |
EXT_MODULES = [ |
| 223 |
Extension( |
| 224 |
"_brotli", |
| 225 |
sources=[ |
| 226 |
"python/_brotli.c", |
| 227 |
"c/common/constants.c", |
| 228 |
"c/common/context.c", |
| 229 |
"c/common/dictionary.c", |
| 230 |
"c/common/platform.c", |
| 231 |
"c/common/shared_dictionary.c", |
| 232 |
"c/common/transform.c", |
| 233 |
"c/dec/bit_reader.c", |
| 234 |
"c/dec/decode.c", |
| 235 |
"c/dec/huffman.c", |
| 236 |
"c/dec/prefix.c", |
| 237 |
"c/dec/state.c", |
| 238 |
"c/dec/static_init.c", |
| 239 |
"c/enc/backward_references.c", |
| 240 |
"c/enc/backward_references_hq.c", |
| 241 |
"c/enc/bit_cost.c", |
| 242 |
"c/enc/block_splitter.c", |
| 243 |
"c/enc/brotli_bit_stream.c", |
| 244 |
"c/enc/cluster.c", |
| 245 |
"c/enc/command.c", |
| 246 |
"c/enc/compound_dictionary.c", |
| 247 |
"c/enc/compress_fragment.c", |
| 248 |
"c/enc/compress_fragment_two_pass.c", |
| 249 |
"c/enc/dictionary_hash.c", |
| 250 |
"c/enc/encode.c", |
| 251 |
"c/enc/encoder_dict.c", |
| 252 |
"c/enc/entropy_encode.c", |
| 253 |
"c/enc/fast_log.c", |
| 254 |
"c/enc/histogram.c", |
| 255 |
"c/enc/literal_cost.c", |
| 256 |
"c/enc/memory.c", |
| 257 |
"c/enc/metablock.c", |
| 258 |
"c/enc/static_dict.c", |
| 259 |
"c/enc/static_dict_lut.c", |
| 260 |
"c/enc/static_init.c", |
| 261 |
"c/enc/utf8_util.c", |
| 262 |
], |
| 263 |
depends=[ |
| 264 |
"c/common/constants.h", |
| 265 |
"c/common/context.h", |
| 266 |
"c/common/dictionary.h", |
| 267 |
"c/common/platform.h", |
| 268 |
"c/common/shared_dictionary_internal.h", |
| 269 |
"c/common/static_init.h", |
| 270 |
"c/common/transform.h", |
| 271 |
"c/common/version.h", |
| 272 |
"c/dec/bit_reader.h", |
| 273 |
"c/dec/huffman.h", |
| 274 |
"c/dec/prefix.h", |
| 275 |
"c/dec/prefix_inc.h", |
| 276 |
"c/dec/state.h", |
| 277 |
"c/dec/static_init.h", |
| 278 |
"c/enc/backward_references.h", |
| 279 |
"c/enc/backward_references_hq.h", |
| 280 |
"c/enc/backward_references_inc.h", |
| 281 |
"c/enc/bit_cost.h", |
| 282 |
"c/enc/bit_cost_inc.h", |
| 283 |
"c/enc/block_encoder_inc.h", |
| 284 |
"c/enc/block_splitter.h", |
| 285 |
"c/enc/block_splitter_inc.h", |
| 286 |
"c/enc/brotli_bit_stream.h", |
| 287 |
"c/enc/cluster.h", |
| 288 |
"c/enc/cluster_inc.h", |
| 289 |
"c/enc/command.h", |
| 290 |
"c/enc/compound_dictionary.h", |
| 291 |
"c/enc/compress_fragment.h", |
| 292 |
"c/enc/compress_fragment_two_pass.h", |
| 293 |
"c/enc/dictionary_hash.h", |
| 294 |
"c/enc/dictionary_hash_inc.h", |
| 295 |
"c/enc/encoder_dict.h", |
| 296 |
"c/enc/entropy_encode.h", |
| 297 |
"c/enc/entropy_encode_static.h", |
| 298 |
"c/enc/fast_log.h", |
| 299 |
"c/enc/find_match_length.h", |
| 300 |
"c/enc/hash.h", |
| 301 |
"c/enc/hash_composite_inc.h", |
| 302 |
"c/enc/hash_forgetful_chain_inc.h", |
| 303 |
"c/enc/hash_longest_match64_inc.h", |
| 304 |
"c/enc/hash_longest_match_inc.h", |
| 305 |
"c/enc/hash_longest_match_quickly_inc.h", |
| 306 |
"c/enc/hash_rolling_inc.h", |
| 307 |
"c/enc/hash_to_binary_tree_inc.h", |
| 308 |
"c/enc/histogram.h", |
| 309 |
"c/enc/histogram_inc.h", |
| 310 |
"c/enc/literal_cost.h", |
| 311 |
"c/enc/memory.h", |
| 312 |
"c/enc/metablock.h", |
| 313 |
"c/enc/metablock_inc.h", |
| 314 |
"c/enc/params.h", |
| 315 |
"c/enc/prefix.h", |
| 316 |
"c/enc/quality.h", |
| 317 |
"c/enc/ringbuffer.h", |
| 318 |
"c/enc/static_dict.h", |
| 319 |
"c/enc/static_dict_lut.h", |
| 320 |
"c/enc/static_init.h", |
| 321 |
"c/enc/utf8_util.h", |
| 322 |
"c/enc/write_bits.h", |
| 323 |
], |
| 324 |
include_dirs=[ |
| 325 |
"c/include", |
| 326 |
]), |
| 327 |
] |
| 328 |
|
| 329 |
TEST_SUITE = "setup.get_test_suite" |
| 330 |
|
| 331 |
CMD_CLASS = { |
| 332 |
"build_ext": BuildExt, |
| 333 |
} |
| 334 |
|
| 335 |
with open("README.md", "r") as f: |
| 336 |
README = f.read() |
| 337 |
|
| 338 |
setup( |
| 339 |
name=NAME, |
| 340 |
description=DESCRIPTION, |
| 341 |
long_description=README, |
| 342 |
long_description_content_type="text/markdown", |
| 343 |
version=VERSION, |
| 344 |
url=URL, |
| 345 |
author=AUTHOR, |
| 346 |
license=LICENSE, |
| 347 |
platforms=PLATFORMS, |
| 348 |
classifiers=CLASSIFIERS, |
| 349 |
package_dir=PACKAGE_DIR, |
| 350 |
py_modules=PY_MODULES, |
| 351 |
ext_modules=EXT_MODULES, |
| 352 |
test_suite=TEST_SUITE, |
| 353 |
cmdclass=CMD_CLASS, |
| 354 |
) |