Skip to content

Commit

Permalink
Merge pull request #133 from anthrotype/py27win
Browse files Browse the repository at this point in the history
fix compilation on Windows Python 2.7 + support for MINGW32 and Cygwin
  • Loading branch information
szabadka committed Aug 11, 2015
2 parents 55a40ce + d2c8b27 commit d811b18
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
5 changes: 1 addition & 4 deletions python/bro.py
Expand Up @@ -9,9 +9,6 @@
import platform


__version__ = '1.0'


# default values of encoder parameters
DEFAULT_PARAMS = {
'mode': brotli.MODE_GENERIC,
Expand Down Expand Up @@ -54,7 +51,7 @@ def main():
parser = argparse.ArgumentParser(
prog='bro.py',
description="Compression/decompression utility using the Brotli algorithm.")
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
parser.add_argument('--version', action='version', version=brotli.__version__)
parser.add_argument('-i', '--input', metavar='FILE', type=str, dest='infile',
help='Input file', default=None)
parser.add_argument('-o', '--output', metavar='FILE', type=str, dest='outfile',
Expand Down
6 changes: 5 additions & 1 deletion python/brotlimodule.cc
Expand Up @@ -9,6 +9,8 @@
#define PyInt_AsLong PyLong_AsLong
#endif

#define BROTLI_VERSION "0.1.0"

using namespace brotli;

static PyObject *BrotliError;
Expand Down Expand Up @@ -110,7 +112,7 @@ static PyObject* brotli_compress(PyObject *self, PyObject *args, PyObject *keywd
int lgblock = -1;
int ok;

static const char *kwlist[] = {"string", "mode", "quality", "lgwin", "lgblock"};
static const char *kwlist[] = {"string", "mode", "quality", "lgwin", "lgblock", NULL};

ok = PyArg_ParseTupleAndKeywords(args, keywds, "s#|O&O&O&O&:compress",
const_cast<char **>(kwlist),
Expand Down Expand Up @@ -243,5 +245,7 @@ PyMODINIT_FUNC INIT_BROTLI(void) {
PyModule_AddIntConstant(m, "MODE_TEXT", (int) BrotliParams::Mode::MODE_TEXT);
PyModule_AddIntConstant(m, "MODE_FONT", (int) BrotliParams::Mode::MODE_FONT);

PyModule_AddStringConstant(m, "__version__", BROTLI_VERSION);

RETURN_BROTLI;
}
46 changes: 42 additions & 4 deletions setup.py
@@ -1,7 +1,37 @@
import distutils
from distutils.core import setup, Extension
from distutils.command.build_ext import build_ext
from distutils.cmd import Command
import platform
import os
import re


CURR_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))

# when compiling for Windows Python 2.7, force distutils to use Visual Studio
# 2010 instead of 2008, as the latter doesn't support c++0x
if platform.system() == 'Windows':
try:
import distutils.msvc9compiler
except distutils.errors.DistutilsPlatformError:
pass # importing msvc9compiler raises when running under MinGW
else:
orig_find_vcvarsall = distutils.msvc9compiler.find_vcvarsall
def patched_find_vcvarsall(version):
return orig_find_vcvarsall(version if version != 9.0 else 10.0)
distutils.msvc9compiler.find_vcvarsall = patched_find_vcvarsall


def get_version():
""" Return BROTLI_VERSION string as defined in 'brotlimodule.cc' file. """
brotlimodule = os.path.join(CURR_DIR, 'python', 'brotlimodule.cc')
with open(brotlimodule, 'r') as f:
for line in f:
m = re.match(r'#define\sBROTLI_VERSION\s"(.*)"', line)
if m:
return m.group(1)
return ""


class TestCommand(Command):
Expand All @@ -18,10 +48,9 @@ def finalize_options(self):
pass

def run(self):
import sys, os, subprocess, glob
import sys, subprocess, glob

curr_dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
test_dir = os.path.join(curr_dir, 'python', 'tests')
test_dir = os.path.join(CURR_DIR, 'python', 'tests')
os.chdir(test_dir)

for test in glob.glob("*_test.py"):
Expand Down Expand Up @@ -59,6 +88,11 @@ def build_extension(self, ext):
macros = ext.define_macros[:]
if platform.system() == "Darwin":
macros.append(("OS_MACOSX", "1"))
elif self.compiler.compiler_type == "mingw32":
# On Windows Python 2.7, pyconfig.h defines "hypot" as "_hypot",
# This clashes with GCC's cmath, and causes compilation errors when
# building under MinGW: http://bugs.python.org/issue11566
macros.append(("_hypot", "hypot"))
for undef in ext.undef_macros:
macros.append((undef,))

Expand All @@ -75,6 +109,10 @@ def build_extension(self, ext):
if ext.extra_objects:
objects.extend(ext.extra_objects)
extra_args = ext.extra_link_args or []
# when using GCC on Windows, we statically link libgcc and libstdc++,
# so that we don't need to package extra DLLs
if self.compiler.compiler_type == "mingw32":
extra_args.extend(['-static-libgcc', '-static-libstdc++'])

ext_path = self.get_ext_fullpath(ext.name)
# Detect target language, if not provided
Expand Down Expand Up @@ -153,7 +191,7 @@ def build_extension(self, ext):

setup(
name="Brotli",
version="0.1",
version=get_version(),
url="https://github.com/google/brotli",
description="Python binding of the Brotli compression library",
author="Khaled Hosny",
Expand Down

0 comments on commit d811b18

Please sign in to comment.