1+ import distutils
12from distutils .core import setup , Extension
23from distutils .command .build_ext import build_ext
34from distutils .cmd import Command
45import 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
737class 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
154192setup (
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