Skip to content

Commit 670bb3c

Browse files
committed
Use C++ compiler on win32.
1 parent 7c03f32 commit 670bb3c

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

setup.py

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,81 @@
44

55
import os
66
import sys
7+
import shutil
78
from glob import glob
89
from distutils.command.sdist import sdist
910
from setuptools import setup, Extension
1011

12+
from distutils.command.build_ext import build_ext
13+
1114
try:
12-
from Cython.Distutils import build_ext
1315
import Cython.Compiler.Main as cython_compiler
1416
have_cython = True
1517
except 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
2057
f = 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

2564
version_str = '.'.join(str(x) for x in version[:3])
2665
if len(version) > 3 and version[3] != 'final':
2766
version_str += version[3]
2867

2968
# take care of extension modules.
3069
if 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)
4075
else:
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']
4979
libraries = []
50-
language = 'c'
5180
if sys.platform == 'win32':
5281
libraries.append('ws2_32')
53-
language = 'c++'
5482

5583
if 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

7097
desc = '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

Comments
 (0)