44
55import os
66import sys
7+ import shutil
78from glob import glob
89from distutils .command .sdist import sdist
910from setuptools import setup , Extension
1011
12+ from distutils .command .build_ext import build_ext
13+
1114try :
12- from Cython .Distutils import build_ext
1315 import Cython .Compiler .Main as cython_compiler
1416 have_cython = True
1517except ImportError :
16- from distutils .command .build_ext import build_ext
1718 have_cython = False
1819
20+
21+ def cythonize (src ):
22+ sys .stderr .write ("cythonize: %r\n " % (src ,))
23+ cython_compiler .compile ([src ])
24+
25+ def ensure_source (src ):
26+ pyx = os .path .splitext (src )[0 ] + '.pyx'
27+
28+ if not os .path .exists (src ):
29+ if not have_cython :
30+ raise Exception ("""\
31+ Cython is required for building extension from checkout.
32+ Install Cython >= 0.16 or install msgpack from PyPI.
33+ """ )
34+ cythonize (src )
35+ elif (os .path .exists (pyx ) and
36+ os .stat (src ).st_mtime < os .stat (pyx ).st_mtime and
37+ have_cython ):
38+ cythonize (src )
39+
40+ # Use C++ compiler on win32.
41+ # MSVC9 doesn't provide stdint.h when using C Compiler.
42+ if sys .platform == 'win32' :
43+ cpp = src + 'pp'
44+ shutil .copy (src , cpp )
45+ return cpp
46+ else :
47+ return src
48+
49+
50+ class BuildExt (build_ext ):
51+ def build_extension (self , ext ):
52+ ext .sources = map (ensure_source , ext .sources )
53+ return build_ext .build_extension (self , ext )
54+
55+
1956# make msgpack/__verison__.py
2057f = open ('msgpack/__version__.py' , 'w' )
21- f .write ("version = %r\n " % (version ,))
22- f .close ()
23- del f
58+ try :
59+ f .write ("version = %r\n " % (version ,))
60+ finally :
61+ f .close ()
62+ del f
2463
2564version_str = '.' .join (str (x ) for x in version [:3 ])
2665if len (version ) > 3 and version [3 ] != 'final' :
2766 version_str += version [3 ]
2867
2968# take care of extension modules.
3069if have_cython :
31- sources = ['msgpack/_msgpack.pyx' ]
32-
3370 class Sdist (sdist ):
3471 def __init__ (self , * args , ** kwargs ):
35- cy_opt = cython_compiler .default_options .copy ()
36- #cy_opt['cplus'] = True
3772 for src in glob ('msgpack/*.pyx' ):
38- cython_compiler . compile ( glob ( 'msgpack/*.pyx' ), cy_opt )
73+ cythonize ( src )
3974 sdist .__init__ (self , * args , ** kwargs )
4075else :
41- sources = ['msgpack/_msgpack.c' ]
42-
43- for f in sources :
44- if not os .path .exists (f ):
45- raise ImportError ("Building msgpack from VCS needs Cython. Install Cython or use sdist package." )
46-
4776 Sdist = sdist
4877
78+ sources = ['msgpack/_msgpack.c' ]
4979libraries = []
50- language = 'c'
5180if sys .platform == 'win32' :
5281 libraries .append ('ws2_32' )
53- language = 'c++'
5482
5583if sys .byteorder == 'big' :
5684 macros = [('__BIG_ENDIAN__' , '1' )]
@@ -61,10 +89,9 @@ def __init__(self, *args, **kwargs):
6189 sources = sources ,
6290 libraries = libraries ,
6391 include_dirs = ['.' ],
64- language = language ,
6592 define_macros = macros ,
6693 )
67- del sources , libraries , language , macros
94+ del sources , libraries , macros
6895
6996
7097desc = 'MessagePack (de)serializer.'
@@ -77,7 +104,7 @@ def __init__(self, *args, **kwargs):
77104 author = 'INADA Naoki' ,
78105 author_email = 'songofacandy@gmail.com' ,
79106 version = version_str ,
80- cmdclass = {'build_ext' : build_ext , 'sdist' : Sdist },
107+ cmdclass = {'build_ext' : BuildExt , 'sdist' : Sdist },
81108 ext_modules = [msgpack_mod ],
82109 packages = ['msgpack' ],
83110 description = desc ,
0 commit comments