Skip to content

Commit d811b18

Browse files
committed
Merge pull request #133 from anthrotype/py27win
fix compilation on Windows Python 2.7 + support for MINGW32 and Cygwin
2 parents 55a40ce + d2c8b27 commit d811b18

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

python/bro.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
import platform
1010

1111

12-
__version__ = '1.0'
13-
14-
1512
# default values of encoder parameters
1613
DEFAULT_PARAMS = {
1714
'mode': brotli.MODE_GENERIC,
@@ -54,7 +51,7 @@ def main():
5451
parser = argparse.ArgumentParser(
5552
prog='bro.py',
5653
description="Compression/decompression utility using the Brotli algorithm.")
57-
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
54+
parser.add_argument('--version', action='version', version=brotli.__version__)
5855
parser.add_argument('-i', '--input', metavar='FILE', type=str, dest='infile',
5956
help='Input file', default=None)
6057
parser.add_argument('-o', '--output', metavar='FILE', type=str, dest='outfile',

python/brotlimodule.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#define PyInt_AsLong PyLong_AsLong
1010
#endif
1111

12+
#define BROTLI_VERSION "0.1.0"
13+
1214
using namespace brotli;
1315

1416
static PyObject *BrotliError;
@@ -110,7 +112,7 @@ static PyObject* brotli_compress(PyObject *self, PyObject *args, PyObject *keywd
110112
int lgblock = -1;
111113
int ok;
112114

113-
static const char *kwlist[] = {"string", "mode", "quality", "lgwin", "lgblock"};
115+
static const char *kwlist[] = {"string", "mode", "quality", "lgwin", "lgblock", NULL};
114116

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

248+
PyModule_AddStringConstant(m, "__version__", BROTLI_VERSION);
249+
246250
RETURN_BROTLI;
247251
}

setup.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1+
import distutils
12
from distutils.core import setup, Extension
23
from distutils.command.build_ext import build_ext
34
from distutils.cmd import Command
45
import platform
6+
import os
7+
import re
8+
9+
10+
CURR_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
11+
12+
# when compiling for Windows Python 2.7, force distutils to use Visual Studio
13+
# 2010 instead of 2008, as the latter doesn't support c++0x
14+
if platform.system() == 'Windows':
15+
try:
16+
import distutils.msvc9compiler
17+
except distutils.errors.DistutilsPlatformError:
18+
pass # importing msvc9compiler raises when running under MinGW
19+
else:
20+
orig_find_vcvarsall = distutils.msvc9compiler.find_vcvarsall
21+
def patched_find_vcvarsall(version):
22+
return orig_find_vcvarsall(version if version != 9.0 else 10.0)
23+
distutils.msvc9compiler.find_vcvarsall = patched_find_vcvarsall
24+
25+
26+
def get_version():
27+
""" Return BROTLI_VERSION string as defined in 'brotlimodule.cc' file. """
28+
brotlimodule = os.path.join(CURR_DIR, 'python', 'brotlimodule.cc')
29+
with open(brotlimodule, 'r') as f:
30+
for line in f:
31+
m = re.match(r'#define\sBROTLI_VERSION\s"(.*)"', line)
32+
if m:
33+
return m.group(1)
34+
return ""
535

636

737
class TestCommand(Command):
@@ -18,10 +48,9 @@ def finalize_options(self):
1848
pass
1949

2050
def run(self):
21-
import sys, os, subprocess, glob
51+
import sys, subprocess, glob
2252

23-
curr_dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
24-
test_dir = os.path.join(curr_dir, 'python', 'tests')
53+
test_dir = os.path.join(CURR_DIR, 'python', 'tests')
2554
os.chdir(test_dir)
2655

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

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

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

154192
setup(
155193
name="Brotli",
156-
version="0.1",
194+
version=get_version(),
157195
url="https://github.com/google/brotli",
158196
description="Python binding of the Brotli compression library",
159197
author="Khaled Hosny",

0 commit comments

Comments
 (0)