setup.py revision 26fa459c
126fa459cSmrg# Copyright 2015 The Brotli Authors. All rights reserved.
226fa459cSmrg#
326fa459cSmrg# Distributed under MIT license.
426fa459cSmrg# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
526fa459cSmrg
626fa459cSmrgimport os
726fa459cSmrgimport platform
826fa459cSmrgimport re
926fa459cSmrgimport unittest
1026fa459cSmrg
1126fa459cSmrgtry:
1226fa459cSmrg    from setuptools import Extension
1326fa459cSmrg    from setuptools import setup
1426fa459cSmrgexcept:
1526fa459cSmrg    from distutils.core import Extension
1626fa459cSmrg    from distutils.core import setup
1726fa459cSmrgfrom distutils.command.build_ext import build_ext
1826fa459cSmrgfrom distutils import errors
1926fa459cSmrgfrom distutils import dep_util
2026fa459cSmrgfrom distutils import log
2126fa459cSmrg
2226fa459cSmrg
2326fa459cSmrgCURR_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
2426fa459cSmrg
2526fa459cSmrg
2626fa459cSmrgdef get_version():
2726fa459cSmrg    """ Return BROTLI_VERSION string as defined in 'common/version.h' file. """
2826fa459cSmrg    version_file_path = os.path.join(CURR_DIR, 'c', 'common', 'version.h')
2926fa459cSmrg    version = 0
3026fa459cSmrg    with open(version_file_path, 'r') as f:
3126fa459cSmrg        for line in f:
3226fa459cSmrg            m = re.match(r'#define\sBROTLI_VERSION\s+0x([0-9a-fA-F]+)', line)
3326fa459cSmrg            if m:
3426fa459cSmrg                version = int(m.group(1), 16)
3526fa459cSmrg    if version == 0:
3626fa459cSmrg        return ''
3726fa459cSmrg    # Semantic version is calculated as (MAJOR << 24) | (MINOR << 12) | PATCH.
3826fa459cSmrg    major = version >> 24
3926fa459cSmrg    minor = (version >> 12) & 0xFFF
4026fa459cSmrg    patch = version & 0xFFF
4126fa459cSmrg    return '{0}.{1}.{2}'.format(major, minor, patch)
4226fa459cSmrg
4326fa459cSmrg
4426fa459cSmrgdef get_test_suite():
4526fa459cSmrg    test_loader = unittest.TestLoader()
4626fa459cSmrg    test_suite = test_loader.discover('python', pattern='*_test.py')
4726fa459cSmrg    return test_suite
4826fa459cSmrg
4926fa459cSmrg
5026fa459cSmrgclass BuildExt(build_ext):
5126fa459cSmrg
5226fa459cSmrg    def get_source_files(self):
5326fa459cSmrg        filenames = build_ext.get_source_files(self)
5426fa459cSmrg        for ext in self.extensions:
5526fa459cSmrg            filenames.extend(ext.depends)
5626fa459cSmrg        return filenames
5726fa459cSmrg
5826fa459cSmrg    def build_extension(self, ext):
5926fa459cSmrg        if ext.sources is None or not isinstance(ext.sources, (list, tuple)):
6026fa459cSmrg            raise errors.DistutilsSetupError(
6126fa459cSmrg                "in 'ext_modules' option (extension '%s'), "
6226fa459cSmrg                "'sources' must be present and must be "
6326fa459cSmrg                "a list of source filenames" % ext.name)
6426fa459cSmrg
6526fa459cSmrg        ext_path = self.get_ext_fullpath(ext.name)
6626fa459cSmrg        depends = ext.sources + ext.depends
6726fa459cSmrg        if not (self.force or dep_util.newer_group(depends, ext_path, 'newer')):
6826fa459cSmrg            log.debug("skipping '%s' extension (up-to-date)", ext.name)
6926fa459cSmrg            return
7026fa459cSmrg        else:
7126fa459cSmrg            log.info("building '%s' extension", ext.name)
7226fa459cSmrg
7326fa459cSmrg        c_sources = []
7426fa459cSmrg        cxx_sources = []
7526fa459cSmrg        for source in ext.sources:
7626fa459cSmrg            if source.endswith('.c'):
7726fa459cSmrg                c_sources.append(source)
7826fa459cSmrg            else:
7926fa459cSmrg                cxx_sources.append(source)
8026fa459cSmrg        extra_args = ext.extra_compile_args or []
8126fa459cSmrg
8226fa459cSmrg        objects = []
8326fa459cSmrg        for lang, sources in (('c', c_sources), ('c++', cxx_sources)):
8426fa459cSmrg            if lang == 'c++':
8526fa459cSmrg                if self.compiler.compiler_type == 'msvc':
8626fa459cSmrg                    extra_args.append('/EHsc')
8726fa459cSmrg
8826fa459cSmrg            macros = ext.define_macros[:]
8926fa459cSmrg            if platform.system() == 'Darwin':
9026fa459cSmrg                macros.append(('OS_MACOSX', '1'))
9126fa459cSmrg            elif self.compiler.compiler_type == 'mingw32':
9226fa459cSmrg                # On Windows Python 2.7, pyconfig.h defines "hypot" as "_hypot",
9326fa459cSmrg                # This clashes with GCC's cmath, and causes compilation errors when
9426fa459cSmrg                # building under MinGW: http://bugs.python.org/issue11566
9526fa459cSmrg                macros.append(('_hypot', 'hypot'))
9626fa459cSmrg            for undef in ext.undef_macros:
9726fa459cSmrg                macros.append((undef,))
9826fa459cSmrg
9926fa459cSmrg            objs = self.compiler.compile(
10026fa459cSmrg                sources,
10126fa459cSmrg                output_dir=self.build_temp,
10226fa459cSmrg                macros=macros,
10326fa459cSmrg                include_dirs=ext.include_dirs,
10426fa459cSmrg                debug=self.debug,
10526fa459cSmrg                extra_postargs=extra_args,
10626fa459cSmrg                depends=ext.depends)
10726fa459cSmrg            objects.extend(objs)
10826fa459cSmrg
10926fa459cSmrg        self._built_objects = objects[:]
11026fa459cSmrg        if ext.extra_objects:
11126fa459cSmrg            objects.extend(ext.extra_objects)
11226fa459cSmrg        extra_args = ext.extra_link_args or []
11326fa459cSmrg        # when using GCC on Windows, we statically link libgcc and libstdc++,
11426fa459cSmrg        # so that we don't need to package extra DLLs
11526fa459cSmrg        if self.compiler.compiler_type == 'mingw32':
11626fa459cSmrg            extra_args.extend(['-static-libgcc', '-static-libstdc++'])
11726fa459cSmrg
11826fa459cSmrg        ext_path = self.get_ext_fullpath(ext.name)
11926fa459cSmrg        # Detect target language, if not provided
12026fa459cSmrg        language = ext.language or self.compiler.detect_language(sources)
12126fa459cSmrg
12226fa459cSmrg        self.compiler.link_shared_object(
12326fa459cSmrg            objects,
12426fa459cSmrg            ext_path,
12526fa459cSmrg            libraries=self.get_libraries(ext),
12626fa459cSmrg            library_dirs=ext.library_dirs,
12726fa459cSmrg            runtime_library_dirs=ext.runtime_library_dirs,
12826fa459cSmrg            extra_postargs=extra_args,
12926fa459cSmrg            export_symbols=self.get_export_symbols(ext),
13026fa459cSmrg            debug=self.debug,
13126fa459cSmrg            build_temp=self.build_temp,
13226fa459cSmrg            target_lang=language)
13326fa459cSmrg
13426fa459cSmrg
13526fa459cSmrgNAME = 'Brotli'
13626fa459cSmrg
13726fa459cSmrgVERSION = get_version()
13826fa459cSmrg
13926fa459cSmrgURL = 'https://github.com/google/brotli'
14026fa459cSmrg
14126fa459cSmrgDESCRIPTION = 'Python bindings for the Brotli compression library'
14226fa459cSmrg
14326fa459cSmrgAUTHOR = 'The Brotli Authors'
14426fa459cSmrg
14526fa459cSmrgLICENSE = 'MIT'
14626fa459cSmrg
14726fa459cSmrgPLATFORMS = ['Posix', 'MacOS X', 'Windows']
14826fa459cSmrg
14926fa459cSmrgCLASSIFIERS = [
15026fa459cSmrg    'Development Status :: 4 - Beta',
15126fa459cSmrg    'Environment :: Console',
15226fa459cSmrg    'Intended Audience :: Developers',
15326fa459cSmrg    'License :: OSI Approved :: MIT License',
15426fa459cSmrg    'Operating System :: MacOS :: MacOS X',
15526fa459cSmrg    'Operating System :: Microsoft :: Windows',
15626fa459cSmrg    'Operating System :: POSIX :: Linux',
15726fa459cSmrg    'Programming Language :: C',
15826fa459cSmrg    'Programming Language :: C++',
15926fa459cSmrg    'Programming Language :: Python',
16026fa459cSmrg    'Programming Language :: Python :: 2',
16126fa459cSmrg    'Programming Language :: Python :: 2.7',
16226fa459cSmrg    'Programming Language :: Python :: 3',
16326fa459cSmrg    'Programming Language :: Python :: 3.3',
16426fa459cSmrg    'Programming Language :: Python :: 3.4',
16526fa459cSmrg    'Programming Language :: Python :: 3.5',
16626fa459cSmrg    'Programming Language :: Unix Shell',
16726fa459cSmrg    'Topic :: Software Development :: Libraries',
16826fa459cSmrg    'Topic :: Software Development :: Libraries :: Python Modules',
16926fa459cSmrg    'Topic :: System :: Archiving',
17026fa459cSmrg    'Topic :: System :: Archiving :: Compression',
17126fa459cSmrg    'Topic :: Text Processing :: Fonts',
17226fa459cSmrg    'Topic :: Utilities',
17326fa459cSmrg]
17426fa459cSmrg
17526fa459cSmrgPACKAGE_DIR = {'': 'python'}
17626fa459cSmrg
17726fa459cSmrgPY_MODULES = ['brotli']
17826fa459cSmrg
17926fa459cSmrgEXT_MODULES = [
18026fa459cSmrg    Extension(
18126fa459cSmrg        '_brotli',
18226fa459cSmrg        sources=[
18326fa459cSmrg            'python/_brotli.cc',
18426fa459cSmrg            'c/common/constants.c',
18526fa459cSmrg            'c/common/context.c',
18626fa459cSmrg            'c/common/dictionary.c',
18726fa459cSmrg            'c/common/platform.c',
18826fa459cSmrg            'c/common/transform.c',
18926fa459cSmrg            'c/dec/bit_reader.c',
19026fa459cSmrg            'c/dec/decode.c',
19126fa459cSmrg            'c/dec/huffman.c',
19226fa459cSmrg            'c/dec/state.c',
19326fa459cSmrg            'c/enc/backward_references.c',
19426fa459cSmrg            'c/enc/backward_references_hq.c',
19526fa459cSmrg            'c/enc/bit_cost.c',
19626fa459cSmrg            'c/enc/block_splitter.c',
19726fa459cSmrg            'c/enc/brotli_bit_stream.c',
19826fa459cSmrg            'c/enc/cluster.c',
19926fa459cSmrg            'c/enc/command.c',
20026fa459cSmrg            'c/enc/compress_fragment.c',
20126fa459cSmrg            'c/enc/compress_fragment_two_pass.c',
20226fa459cSmrg            'c/enc/dictionary_hash.c',
20326fa459cSmrg            'c/enc/encode.c',
20426fa459cSmrg            'c/enc/encoder_dict.c',
20526fa459cSmrg            'c/enc/entropy_encode.c',
20626fa459cSmrg            'c/enc/fast_log.c',
20726fa459cSmrg            'c/enc/histogram.c',
20826fa459cSmrg            'c/enc/literal_cost.c',
20926fa459cSmrg            'c/enc/memory.c',
21026fa459cSmrg            'c/enc/metablock.c',
21126fa459cSmrg            'c/enc/static_dict.c',
21226fa459cSmrg            'c/enc/utf8_util.c',
21326fa459cSmrg        ],
21426fa459cSmrg        depends=[
21526fa459cSmrg            'c/common/constants.h',
21626fa459cSmrg            'c/common/context.h',
21726fa459cSmrg            'c/common/dictionary.h',
21826fa459cSmrg            'c/common/platform.h',
21926fa459cSmrg            'c/common/transform.h',
22026fa459cSmrg            'c/common/version.h',
22126fa459cSmrg            'c/dec/bit_reader.h',
22226fa459cSmrg            'c/dec/huffman.h',
22326fa459cSmrg            'c/dec/prefix.h',
22426fa459cSmrg            'c/dec/state.h',
22526fa459cSmrg            'c/enc/backward_references.h',
22626fa459cSmrg            'c/enc/backward_references_hq.h',
22726fa459cSmrg            'c/enc/backward_references_inc.h',
22826fa459cSmrg            'c/enc/bit_cost.h',
22926fa459cSmrg            'c/enc/bit_cost_inc.h',
23026fa459cSmrg            'c/enc/block_encoder_inc.h',
23126fa459cSmrg            'c/enc/block_splitter.h',
23226fa459cSmrg            'c/enc/block_splitter_inc.h',
23326fa459cSmrg            'c/enc/brotli_bit_stream.h',
23426fa459cSmrg            'c/enc/cluster.h',
23526fa459cSmrg            'c/enc/cluster_inc.h',
23626fa459cSmrg            'c/enc/command.h',
23726fa459cSmrg            'c/enc/compress_fragment.h',
23826fa459cSmrg            'c/enc/compress_fragment_two_pass.h',
23926fa459cSmrg            'c/enc/dictionary_hash.h',
24026fa459cSmrg            'c/enc/encoder_dict.h',
24126fa459cSmrg            'c/enc/entropy_encode.h',
24226fa459cSmrg            'c/enc/entropy_encode_static.h',
24326fa459cSmrg            'c/enc/fast_log.h',
24426fa459cSmrg            'c/enc/find_match_length.h',
24526fa459cSmrg            'c/enc/hash.h',
24626fa459cSmrg            'c/enc/hash_composite_inc.h',
24726fa459cSmrg            'c/enc/hash_forgetful_chain_inc.h',
24826fa459cSmrg            'c/enc/hash_longest_match64_inc.h',
24926fa459cSmrg            'c/enc/hash_longest_match_inc.h',
25026fa459cSmrg            'c/enc/hash_longest_match_quickly_inc.h',
25126fa459cSmrg            'c/enc/hash_rolling_inc.h',
25226fa459cSmrg            'c/enc/hash_to_binary_tree_inc.h',
25326fa459cSmrg            'c/enc/histogram.h',
25426fa459cSmrg            'c/enc/histogram_inc.h',
25526fa459cSmrg            'c/enc/literal_cost.h',
25626fa459cSmrg            'c/enc/memory.h',
25726fa459cSmrg            'c/enc/metablock.h',
25826fa459cSmrg            'c/enc/metablock_inc.h',
25926fa459cSmrg            'c/enc/params.h',
26026fa459cSmrg            'c/enc/prefix.h',
26126fa459cSmrg            'c/enc/quality.h',
26226fa459cSmrg            'c/enc/ringbuffer.h',
26326fa459cSmrg            'c/enc/static_dict.h',
26426fa459cSmrg            'c/enc/static_dict_lut.h',
26526fa459cSmrg            'c/enc/utf8_util.h',
26626fa459cSmrg            'c/enc/write_bits.h',
26726fa459cSmrg        ],
26826fa459cSmrg        include_dirs=[
26926fa459cSmrg            'c/include',
27026fa459cSmrg        ],
27126fa459cSmrg        language='c++'),
27226fa459cSmrg]
27326fa459cSmrg
27426fa459cSmrgTEST_SUITE = 'setup.get_test_suite'
27526fa459cSmrg
27626fa459cSmrgCMD_CLASS = {
27726fa459cSmrg    'build_ext': BuildExt,
27826fa459cSmrg}
27926fa459cSmrg
28026fa459cSmrgsetup(
28126fa459cSmrg    name=NAME,
28226fa459cSmrg    description=DESCRIPTION,
28326fa459cSmrg    version=VERSION,
28426fa459cSmrg    url=URL,
28526fa459cSmrg    author=AUTHOR,
28626fa459cSmrg    license=LICENSE,
28726fa459cSmrg    platforms=PLATFORMS,
28826fa459cSmrg    classifiers=CLASSIFIERS,
28926fa459cSmrg    package_dir=PACKAGE_DIR,
29026fa459cSmrg    py_modules=PY_MODULES,
29126fa459cSmrg    ext_modules=EXT_MODULES,
29226fa459cSmrg    test_suite=TEST_SUITE,
29326fa459cSmrg    cmdclass=CMD_CLASS)
294