114 kx # Copyright 2015 The Brotli Authors. All rights reserved.
114 kx #
114 kx # Distributed under MIT license.
114 kx # See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
114 kx
114 kx import os
114 kx import platform
114 kx import re
114 kx import unittest
114 kx
114 kx try:
114 kx from setuptools import Extension
114 kx from setuptools import setup
114 kx except:
114 kx from distutils.core import Extension
114 kx from distutils.core import setup
114 kx from distutils.command.build_ext import build_ext
114 kx from distutils import errors
114 kx from distutils import dep_util
114 kx from distutils import log
114 kx
114 kx
114 kx CURR_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
114 kx
114 kx
114 kx def get_version():
114 kx """ Return BROTLI_VERSION string as defined in 'common/version.h' file. """
114 kx version_file_path = os.path.join(CURR_DIR, 'c', 'common', 'version.h')
114 kx version = 0
114 kx with open(version_file_path, 'r') as f:
114 kx for line in f:
114 kx m = re.match(r'#define\sBROTLI_VERSION\s+0x([0-9a-fA-F]+)', line)
114 kx if m:
114 kx version = int(m.group(1), 16)
114 kx if version == 0:
114 kx return ''
114 kx # Semantic version is calculated as (MAJOR << 24) | (MINOR << 12) | PATCH.
114 kx major = version >> 24
114 kx minor = (version >> 12) & 0xFFF
114 kx patch = version & 0xFFF
114 kx return '{0}.{1}.{2}'.format(major, minor, patch)
114 kx
114 kx
114 kx def get_test_suite():
114 kx test_loader = unittest.TestLoader()
114 kx test_suite = test_loader.discover('python', pattern='*_test.py')
114 kx return test_suite
114 kx
114 kx
114 kx class BuildExt(build_ext):
114 kx
114 kx def get_source_files(self):
114 kx filenames = build_ext.get_source_files(self)
114 kx for ext in self.extensions:
114 kx filenames.extend(ext.depends)
114 kx return filenames
114 kx
114 kx def build_extension(self, ext):
114 kx if ext.sources is None or not isinstance(ext.sources, (list, tuple)):
114 kx raise errors.DistutilsSetupError(
114 kx "in 'ext_modules' option (extension '%s'), "
114 kx "'sources' must be present and must be "
114 kx "a list of source filenames" % ext.name)
114 kx
114 kx ext_path = self.get_ext_fullpath(ext.name)
114 kx depends = ext.sources + ext.depends
114 kx if not (self.force or dep_util.newer_group(depends, ext_path, 'newer')):
114 kx log.debug("skipping '%s' extension (up-to-date)", ext.name)
114 kx return
114 kx else:
114 kx log.info("building '%s' extension", ext.name)
114 kx
114 kx c_sources = []
114 kx cxx_sources = []
114 kx for source in ext.sources:
114 kx if source.endswith('.c'):
114 kx c_sources.append(source)
114 kx else:
114 kx cxx_sources.append(source)
114 kx extra_args = ext.extra_compile_args or []
114 kx
114 kx objects = []
114 kx for lang, sources in (('c', c_sources), ('c++', cxx_sources)):
114 kx if lang == 'c++':
114 kx if self.compiler.compiler_type == 'msvc':
114 kx extra_args.append('/EHsc')
114 kx
114 kx macros = ext.define_macros[:]
114 kx if platform.system() == 'Darwin':
114 kx macros.append(('OS_MACOSX', '1'))
114 kx elif self.compiler.compiler_type == 'mingw32':
114 kx # On Windows Python 2.7, pyconfig.h defines "hypot" as "_hypot",
114 kx # This clashes with GCC's cmath, and causes compilation errors when
114 kx # building under MinGW: http://bugs.python.org/issue11566
114 kx macros.append(('_hypot', 'hypot'))
114 kx for undef in ext.undef_macros:
114 kx macros.append((undef,))
114 kx
114 kx objs = self.compiler.compile(
114 kx sources,
114 kx output_dir=self.build_temp,
114 kx macros=macros,
114 kx include_dirs=ext.include_dirs,
114 kx debug=self.debug,
114 kx extra_postargs=extra_args,
114 kx depends=ext.depends)
114 kx objects.extend(objs)
114 kx
114 kx self._built_objects = objects[:]
114 kx if ext.extra_objects:
114 kx objects.extend(ext.extra_objects)
114 kx extra_args = ext.extra_link_args or []
114 kx # when using GCC on Windows, we statically link libgcc and libstdc++,
114 kx # so that we don't need to package extra DLLs
114 kx if self.compiler.compiler_type == 'mingw32':
114 kx extra_args.extend(['-static-libgcc', '-static-libstdc++'])
114 kx
114 kx ext_path = self.get_ext_fullpath(ext.name)
114 kx # Detect target language, if not provided
114 kx language = ext.language or self.compiler.detect_language(sources)
114 kx
114 kx ext_suffix = os.getenv('EXT_SUFFIX')
114 kx if ext_suffix != '':
114 kx dir_name = os.path.dirname(ext_path)
114 kx base_name = os.path.basename(ext_path).split('.', 1)[0]
114 kx ext_path = dir_name + '/' + base_name + ext_suffix
114 kx
114 kx self.compiler.link_shared_object(
114 kx objects,
114 kx ext_path,
114 kx libraries=self.get_libraries(ext),
114 kx library_dirs=ext.library_dirs,
114 kx runtime_library_dirs=ext.runtime_library_dirs,
114 kx extra_postargs=extra_args,
114 kx export_symbols=self.get_export_symbols(ext),
114 kx debug=self.debug,
114 kx build_temp=self.build_temp,
114 kx target_lang=language)
114 kx
114 kx
114 kx NAME = 'Brotli'
114 kx
114 kx VERSION = get_version()
114 kx
114 kx URL = 'https://github.com/google/brotli'
114 kx
114 kx DESCRIPTION = 'Python bindings for the Brotli compression library'
114 kx
114 kx AUTHOR = 'The Brotli Authors'
114 kx
114 kx LICENSE = 'MIT'
114 kx
114 kx PLATFORMS = ['Posix', 'MacOS X', 'Windows']
114 kx
114 kx CLASSIFIERS = [
114 kx 'Development Status :: 4 - Beta',
114 kx 'Environment :: Console',
114 kx 'Intended Audience :: Developers',
114 kx 'License :: OSI Approved :: MIT License',
114 kx 'Operating System :: MacOS :: MacOS X',
114 kx 'Operating System :: Microsoft :: Windows',
114 kx 'Operating System :: POSIX :: Linux',
114 kx 'Programming Language :: C',
114 kx 'Programming Language :: C++',
114 kx 'Programming Language :: Python',
114 kx 'Programming Language :: Python :: 2',
114 kx 'Programming Language :: Python :: 2.7',
114 kx 'Programming Language :: Python :: 3',
114 kx 'Programming Language :: Python :: 3.3',
114 kx 'Programming Language :: Python :: 3.4',
114 kx 'Programming Language :: Python :: 3.5',
114 kx 'Programming Language :: Unix Shell',
114 kx 'Topic :: Software Development :: Libraries',
114 kx 'Topic :: Software Development :: Libraries :: Python Modules',
114 kx 'Topic :: System :: Archiving',
114 kx 'Topic :: System :: Archiving :: Compression',
114 kx 'Topic :: Text Processing :: Fonts',
114 kx 'Topic :: Utilities',
114 kx ]
114 kx
114 kx PACKAGE_DIR = {'': 'python'}
114 kx
114 kx PY_MODULES = ['brotli']
114 kx
114 kx EXT_MODULES = [
114 kx Extension(
114 kx '_brotli',
114 kx sources=[
114 kx 'python/_brotli.cc',
114 kx 'c/common/constants.c',
114 kx 'c/common/context.c',
114 kx 'c/common/dictionary.c',
114 kx 'c/common/platform.c',
114 kx 'c/common/transform.c',
114 kx 'c/dec/bit_reader.c',
114 kx 'c/dec/decode.c',
114 kx 'c/dec/huffman.c',
114 kx 'c/dec/state.c',
114 kx 'c/enc/backward_references.c',
114 kx 'c/enc/backward_references_hq.c',
114 kx 'c/enc/bit_cost.c',
114 kx 'c/enc/block_splitter.c',
114 kx 'c/enc/brotli_bit_stream.c',
114 kx 'c/enc/cluster.c',
114 kx 'c/enc/command.c',
114 kx 'c/enc/compress_fragment.c',
114 kx 'c/enc/compress_fragment_two_pass.c',
114 kx 'c/enc/dictionary_hash.c',
114 kx 'c/enc/encode.c',
114 kx 'c/enc/encoder_dict.c',
114 kx 'c/enc/entropy_encode.c',
114 kx 'c/enc/fast_log.c',
114 kx 'c/enc/histogram.c',
114 kx 'c/enc/literal_cost.c',
114 kx 'c/enc/memory.c',
114 kx 'c/enc/metablock.c',
114 kx 'c/enc/static_dict.c',
114 kx 'c/enc/utf8_util.c',
114 kx ],
114 kx depends=[
114 kx 'c/common/constants.h',
114 kx 'c/common/context.h',
114 kx 'c/common/dictionary.h',
114 kx 'c/common/platform.h',
114 kx 'c/common/transform.h',
114 kx 'c/common/version.h',
114 kx 'c/dec/bit_reader.h',
114 kx 'c/dec/huffman.h',
114 kx 'c/dec/prefix.h',
114 kx 'c/dec/state.h',
114 kx 'c/enc/backward_references.h',
114 kx 'c/enc/backward_references_hq.h',
114 kx 'c/enc/backward_references_inc.h',
114 kx 'c/enc/bit_cost.h',
114 kx 'c/enc/bit_cost_inc.h',
114 kx 'c/enc/block_encoder_inc.h',
114 kx 'c/enc/block_splitter.h',
114 kx 'c/enc/block_splitter_inc.h',
114 kx 'c/enc/brotli_bit_stream.h',
114 kx 'c/enc/cluster.h',
114 kx 'c/enc/cluster_inc.h',
114 kx 'c/enc/command.h',
114 kx 'c/enc/compress_fragment.h',
114 kx 'c/enc/compress_fragment_two_pass.h',
114 kx 'c/enc/dictionary_hash.h',
114 kx 'c/enc/encoder_dict.h',
114 kx 'c/enc/entropy_encode.h',
114 kx 'c/enc/entropy_encode_static.h',
114 kx 'c/enc/fast_log.h',
114 kx 'c/enc/find_match_length.h',
114 kx 'c/enc/hash.h',
114 kx 'c/enc/hash_composite_inc.h',
114 kx 'c/enc/hash_forgetful_chain_inc.h',
114 kx 'c/enc/hash_longest_match64_inc.h',
114 kx 'c/enc/hash_longest_match_inc.h',
114 kx 'c/enc/hash_longest_match_quickly_inc.h',
114 kx 'c/enc/hash_rolling_inc.h',
114 kx 'c/enc/hash_to_binary_tree_inc.h',
114 kx 'c/enc/histogram.h',
114 kx 'c/enc/histogram_inc.h',
114 kx 'c/enc/literal_cost.h',
114 kx 'c/enc/memory.h',
114 kx 'c/enc/metablock.h',
114 kx 'c/enc/metablock_inc.h',
114 kx 'c/enc/params.h',
114 kx 'c/enc/prefix.h',
114 kx 'c/enc/quality.h',
114 kx 'c/enc/ringbuffer.h',
114 kx 'c/enc/static_dict.h',
114 kx 'c/enc/static_dict_lut.h',
114 kx 'c/enc/utf8_util.h',
114 kx 'c/enc/write_bits.h',
114 kx ],
114 kx include_dirs=[
114 kx 'c/include',
114 kx ],
114 kx language='c++'),
114 kx ]
114 kx
114 kx TEST_SUITE = 'setup.get_test_suite'
114 kx
114 kx CMD_CLASS = {
114 kx 'build_ext': BuildExt,
114 kx }
114 kx
114 kx setup(
114 kx name=NAME,
114 kx description=DESCRIPTION,
114 kx version=VERSION,
114 kx url=URL,
114 kx author=AUTHOR,
114 kx license=LICENSE,
114 kx platforms=PLATFORMS,
114 kx classifiers=CLASSIFIERS,
114 kx package_dir=PACKAGE_DIR,
114 kx py_modules=PY_MODULES,
114 kx ext_modules=EXT_MODULES,
114 kx test_suite=TEST_SUITE,
114 kx cmdclass=CMD_CLASS)