merge in latest cython-devel
[cython.git] / setup.py
1 from distutils.core import setup, Extension
2 from distutils.sysconfig import get_python_lib
3 import os, os.path
4 import sys
5 from Cython.Compiler.Version import version
6
7 compiler_dir = os.path.join(get_python_lib(prefix=''), 'Cython/Compiler')
8 if sys.platform == "win32":
9     compiler_dir = compiler_dir[len(sys.prefix)+1:]
10
11 setup_args = {}
12
13 if sys.version_info[0] >= 3:
14     import lib2to3.refactor
15     from distutils.command.build_py \
16          import build_py_2to3 as build_py
17     # need to convert sources to Py3 on installation
18     fixers = [ fix for fix in lib2to3.refactor.get_fixers_from_package("lib2to3.fixes")
19                if fix.split('fix_')[-1] not in ('next',)
20                ]
21     build_py.fixer_names = fixers
22     setup_args['cmdclass'] = {"build_py" : build_py}
23
24
25 if sys.version_info < (2,4):
26     import glob
27     cython_dir = os.path.join(get_python_lib(prefix=''), 'Cython')
28     compiler_dir = os.path.join(cython_dir, 'Compiler')
29     setup_args['data_files'] = [
30         (compiler_dir, ['Cython/Compiler/Lexicon.pickle']),
31         (cython_dir, [ f for pattern in
32                        ['Cython/Includes/*.pxd',
33                         'Cython/Plex/*.pxd',
34                         'Cython/Compiler/*.pxd',
35                         'Cython/Runtime/*.pyx']
36                        for f in glob.glob(pattern) ])]
37 else:
38     setup_args['package_data'] = {'Cython.Compiler' : ['Lexicon.pickle'],
39                                   'Cython' : ['Includes/*.pxd',
40                                               'Plex/*.pxd',
41                                               'Compiler/*.pxd',
42                                               'Runtime/*.pyx']}
43
44 if os.name == "posix":
45     scripts = ["bin/cython"]
46 else:
47     scripts = ["cython.py"]
48
49 try:
50     if sys.version_info[0] >= 3:
51         raise ValueError
52     sys.argv.remove("--no-cython-compile")
53 except ValueError:
54     try:
55         from distutils.command.build_ext import build_ext as build_ext_orig
56         class build_ext(build_ext_orig):
57             def build_extension(self, ext, *args, **kargs):
58                 try:
59                     build_ext_orig.build_extension(self, ext, *args, **kargs)
60                 except StandardError:
61                     print("Compilation of '%s' failed" % ext.sources[0])
62         from Cython.Compiler.Main import compile
63         source_root = os.path.dirname(__file__)
64         compiled_modules = ["Cython.Plex.Scanners",
65                             "Cython.Compiler.Scanning",
66                             "Cython.Compiler.Parsing",
67                             "Cython.Compiler.Visitor",
68                             "Cython.Runtime.refnanny"]
69         extensions = []
70         for module in compiled_modules:
71             source_file = os.path.join(source_root, *module.split('.'))
72             if os.path.exists(source_file + ".py"):
73                 source_file = source_file + ".py"
74             else:
75                 source_file = source_file + ".pyx"
76             print("Compiling module %s ..." % module)
77             result = compile(source_file)
78             if result.c_file:
79                 extensions.append(
80                     Extension(module, sources = [result.c_file])
81                     )
82             else:
83                 print("Compilation failed")
84         if extensions:
85             setup_args['ext_modules'] = extensions
86             setup_args['cmdclass'] = {"build_ext" : build_ext}
87     except Exception:
88         print("ERROR: %s" % sys.exc_info()[1])
89         print("Extension module compilation failed, using plain Python implementation")
90
91
92 setup(
93   name = 'Cython',
94   version = version,
95   url = 'http://www.cython.org',
96   author = 'Greg Ewing, Robert Bradshaw, Stefan Behnel, Dag Seljebotn, et al.',
97   author_email = 'cython-dev@codespeak.net',
98   description = "The Cython compiler for writing C extensions for the Python language.",
99   long_description = """\
100   The Cython language makes writing C extensions for the Python language as
101   easy as Python itself.  Cython is a source code translator based on the
102   well-known Pyrex_, but supports more cutting edge functionality and
103   optimizations.
104
105   The Cython language is very close to the Python language (and most Python
106   code is also valid Cython code), but Cython additionally supports calling C
107   functions and declaring C types on variables and class attributes. This
108   allows the compiler to generate very efficient C code from Cython code.
109
110   This makes Cython the ideal language for writing glue code for external C
111   libraries, and for fast C modules that speed up the execution of Python
112   code.
113
114   .. _Pyrex: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
115   """,
116   classifiers = [
117     "Development Status :: 5 - Production/Stable",
118     "Intended Audience :: Developers",
119     "License :: OSI Approved :: Apache Software License",
120     "Operating System :: OS Independent",
121     "Programming Language :: Python",
122     "Programming Language :: C",
123     "Programming Language :: Cython",
124     "Topic :: Software Development :: Code Generators",
125     "Topic :: Software Development :: Compilers",
126     "Topic :: Software Development :: Libraries :: Python Modules"
127   ],
128
129   scripts = scripts,
130   packages=[
131     'Cython',
132     'Cython.Compiler',
133     'Cython.Runtime',
134     'Cython.Distutils',
135     'Cython.Mac',
136     'Cython.Unix',
137     'Cython.Plex',
138
139     'Cython.Tests',
140     'Cython.Compiler.Tests',
141     ],
142
143   # pyximport
144   py_modules = ["pyximport/__init__",
145                 "pyximport/pyximport",
146                 "pyximport/pyxbuild",
147
148                 "cython"],
149
150   **setup_args
151   )