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