Cosimo Lupo | 77c2399 | 2015-03-31 09:30:56 +0100 | [diff] [blame^] | 1 | from distutils.core import setup, Extension |
| 2 | from distutils.command.build_ext import build_ext |
| 3 | from distutils.cmd import Command |
| 4 | import platform |
| 5 | |
| 6 | |
| 7 | class TestCommand(Command): |
| 8 | """ Run all *_test.py scripts in 'tests' folder with the same Python |
| 9 | interpreter used to run setup.py. |
| 10 | """ |
| 11 | |
| 12 | user_options = [] |
| 13 | |
| 14 | def initialize_options(self): |
| 15 | pass |
| 16 | |
| 17 | def finalize_options(self): |
| 18 | pass |
| 19 | |
| 20 | def run(self): |
| 21 | import sys, os, subprocess, glob |
| 22 | |
| 23 | curr_dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 24 | test_dir = os.path.join(curr_dir, 'python', 'tests') |
| 25 | os.chdir(test_dir) |
| 26 | |
| 27 | for test in glob.glob("*_test.py"): |
| 28 | try: |
| 29 | subprocess.check_call([sys.executable, test]) |
| 30 | except subprocess.CalledProcessError: |
| 31 | raise SystemExit(1) |
| 32 | |
| 33 | |
| 34 | class BuildExt(build_ext): |
| 35 | def get_source_files(self): |
| 36 | filenames = build_ext.get_source_files(self) |
| 37 | for ext in self.extensions: |
| 38 | filenames.extend(ext.depends) |
| 39 | return filenames |
| 40 | |
| 41 | def build_extension(self, ext): |
| 42 | c_sources = [] |
| 43 | cxx_sources = [] |
| 44 | for source in ext.sources: |
| 45 | if source.endswith(".c"): |
| 46 | c_sources.append(source) |
| 47 | else: |
| 48 | cxx_sources.append(source) |
| 49 | extra_args = ext.extra_compile_args or [] |
| 50 | |
| 51 | objects = [] |
| 52 | for lang, sources in (("c", c_sources), ("c++", cxx_sources)): |
| 53 | if lang == "c++": |
| 54 | if self.compiler.compiler_type in ["unix", "cygwin", "mingw32"]: |
| 55 | extra_args.append("-std=c++0x") |
| 56 | elif self.compiler.compiler_type == "msvc": |
| 57 | extra_args.append("/EHsc") |
| 58 | |
| 59 | macros = ext.define_macros[:] |
| 60 | if platform.system() == "Darwin": |
| 61 | macros.append(("OS_MACOSX", "1")) |
| 62 | for undef in ext.undef_macros: |
| 63 | macros.append((undef,)) |
| 64 | |
| 65 | objs = self.compiler.compile(sources, |
| 66 | output_dir=self.build_temp, |
| 67 | macros=macros, |
| 68 | include_dirs=ext.include_dirs, |
| 69 | debug=self.debug, |
| 70 | extra_postargs=extra_args, |
| 71 | depends=ext.depends) |
| 72 | objects.extend(objs) |
| 73 | |
| 74 | self._built_objects = objects[:] |
| 75 | if ext.extra_objects: |
| 76 | objects.extend(ext.extra_objects) |
| 77 | extra_args = ext.extra_link_args or [] |
| 78 | |
| 79 | ext_path = self.get_ext_fullpath(ext.name) |
| 80 | # Detect target language, if not provided |
| 81 | language = ext.language or self.compiler.detect_language(sources) |
| 82 | |
| 83 | self.compiler.link_shared_object( |
| 84 | objects, ext_path, |
| 85 | libraries=self.get_libraries(ext), |
| 86 | library_dirs=ext.library_dirs, |
| 87 | runtime_library_dirs=ext.runtime_library_dirs, |
| 88 | extra_postargs=extra_args, |
| 89 | export_symbols=self.get_export_symbols(ext), |
| 90 | debug=self.debug, |
| 91 | build_temp=self.build_temp, |
| 92 | target_lang=language) |
| 93 | |
| 94 | brotli = Extension("brotli", |
| 95 | sources=[ |
| 96 | "python/brotlimodule.cc", |
| 97 | "enc/backward_references.cc", |
| 98 | "enc/block_splitter.cc", |
| 99 | "enc/brotli_bit_stream.cc", |
| 100 | "enc/encode.cc", |
| 101 | "enc/entropy_encode.cc", |
| 102 | "enc/histogram.cc", |
| 103 | "enc/literal_cost.cc", |
| 104 | "enc/metablock.cc", |
| 105 | "dec/bit_reader.c", |
| 106 | "dec/decode.c", |
| 107 | "dec/huffman.c", |
| 108 | "dec/safe_malloc.c", |
| 109 | "dec/streams.c", |
| 110 | "dec/state.c", |
| 111 | ], |
| 112 | depends=[ |
| 113 | "enc/backward_references.h", |
| 114 | "enc/bit_cost.h", |
| 115 | "enc/block_splitter.h", |
| 116 | "enc/brotli_bit_stream.h", |
| 117 | "enc/cluster.h", |
| 118 | "enc/command.h", |
| 119 | "enc/context.h", |
| 120 | "enc/dictionary.h", |
| 121 | "enc/encode.h", |
| 122 | "enc/entropy_encode.h", |
| 123 | "enc/fast_log.h", |
| 124 | "enc/find_match_length.h", |
| 125 | "enc/hash.h", |
| 126 | "enc/histogram.h", |
| 127 | "enc/literal_cost.h", |
| 128 | "enc/metablock.h", |
| 129 | "enc/port.h", |
| 130 | "enc/prefix.h", |
| 131 | "enc/ringbuffer.h", |
| 132 | "enc/static_dict.h", |
| 133 | "enc/transform.h", |
| 134 | "enc/write_bits.h", |
| 135 | "dec/bit_reader.h", |
| 136 | "dec/context.h", |
| 137 | "dec/decode.h", |
| 138 | "dec/dictionary.h", |
| 139 | "dec/huffman.h", |
| 140 | "dec/prefix.h", |
| 141 | "dec/safe_malloc.h", |
| 142 | "dec/streams.h", |
| 143 | "dec/transform.h", |
| 144 | "dec/types.h", |
| 145 | "dec/state.h", |
| 146 | ], |
| 147 | language="c++", |
| 148 | ) |
| 149 | |
| 150 | setup( |
| 151 | name="Brotli", |
| 152 | version="0.1", |
| 153 | url="https://github.com/google/brotli", |
| 154 | description="Python binding of the Brotli compression library", |
| 155 | author="Khaled Hosny", |
| 156 | author_email="khaledhosny@eglug.org", |
| 157 | license="Apache 2.0", |
| 158 | ext_modules=[brotli], |
| 159 | cmdclass={ |
| 160 | 'build_ext': BuildExt, |
| 161 | 'test': TestCommand |
| 162 | }, |
| 163 | ) |