merged 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
6 if 'sdist' in sys.argv:
7     # Record the current revision in .hgrev
8     import subprocess # os.popen is cleaner but depricated
9     changset = subprocess.Popen("hg log --rev tip | grep changeset", 
10                                 shell=True,
11                                 stdout=subprocess.PIPE).stdout.read()
12     rev = changset.split(':')[-1].strip()
13     hgrev = open('.hgrev', 'w')
14     hgrev.write(rev)
15     hgrev.close()
16
17 compiler_dir = os.path.join(get_python_lib(prefix=''), 'Cython/Compiler')
18 if sys.platform == "win32":
19     compiler_dir = compiler_dir[len(sys.prefix)+1:]
20
21 if sys.platform == "darwin":
22     # Don't create resource files on OS X tar.
23     os.environ['COPY_EXTENDED_ATTRIBUTES_DISABLE'] = 'true'
24     os.environ['COPYFILE_DISABLE'] = 'true'
25
26 setup_args = {}
27
28 if sys.version_info[0] >= 3:
29     import lib2to3.refactor
30     from distutils.command.build_py \
31          import build_py_2to3 as build_py
32     # need to convert sources to Py3 on installation
33     fixers = [ fix for fix in lib2to3.refactor.get_fixers_from_package("lib2to3.fixes")
34                if fix.split('fix_')[-1] not in ('next',)
35                ]
36     build_py.fixer_names = fixers
37     setup_args['cmdclass'] = {"build_py" : build_py}
38
39
40 if sys.version_info < (2,4):
41     import glob
42     cython_dir = os.path.join(get_python_lib(prefix=''), 'Cython')
43     compiler_dir = os.path.join(cython_dir, 'Compiler')
44     setup_args['data_files'] = [
45         (cython_dir, [ f for pattern in
46                        ['Cython/Includes/*.pxd',
47                         'Cython/Plex/*.pxd',
48                         'Cython/Compiler/*.pxd',
49                         'Cython/Runtime/*.pyx']
50                        for f in glob.glob(pattern) ])]
51 else:
52     setup_args['package_data'] = {'Cython' : ['Includes/*.pxd',
53                                               'Plex/*.pxd',
54                                               'Compiler/*.pxd',
55                                               'Runtime/*.pyx']}
56
57 # This dict is used for passing extra arguments that are setuptools 
58 # specific to setup
59 setuptools_extra_args = {}
60
61 if 'setuptools' in sys.modules:
62     setuptools_extra_args['zip_safe'] = False
63     setuptools_extra_args['entry_points'] = {
64         'console_scripts': [
65             'cython = Cython.Compiler.Main:setuptools_main',
66         ]
67     }
68     scripts = []
69 else:
70     if os.name == "posix":
71         scripts = ["bin/cython"]
72     else:
73         scripts = ["cython.py"]
74
75
76 try:
77     if sys.version_info[0] >= 3:
78         raise ValueError
79     sys.argv.remove("--no-cython-compile")
80 except ValueError:
81     try:
82         from distutils.command.build_ext import build_ext as build_ext_orig
83         class build_ext(build_ext_orig):
84             def build_extension(self, ext, *args, **kargs):
85                 try:
86                     build_ext_orig.build_extension(self, ext, *args, **kargs)
87                 except StandardError:
88                     print("Compilation of '%s' failed" % ext.sources[0])
89         from Cython.Compiler.Main import compile
90         from Cython import Utils
91         source_root = os.path.dirname(__file__)
92         compiled_modules = ["Cython.Plex.Scanners",
93                             "Cython.Compiler.Scanning",
94                             "Cython.Compiler.Parsing",
95                             "Cython.Compiler.Visitor",
96                             "Cython.Runtime.refnanny"]
97         extensions = []
98         for module in compiled_modules:
99             source_file = os.path.join(source_root, *module.split('.'))
100             if os.path.exists(source_file + ".py"):
101                 pyx_source_file = source_file + ".py"
102             else:
103                 pyx_source_file = source_file + ".pyx"
104             c_source_file = source_file + ".c"
105             if not os.path.exists(c_source_file) or \
106                Utils.file_newer_than(pyx_source_file,
107                                      Utils.modification_time(c_source_file)):
108                 print("Compiling module %s ..." % module)
109                 result = compile(pyx_source_file)
110                 c_source_file = result.c_file
111             if c_source_file:
112                 extensions.append(
113                     Extension(module, sources = [c_source_file])
114                     )
115             else:
116                 print("Compilation failed")
117         if extensions:
118             setup_args['ext_modules'] = extensions
119             setup_args['cmdclass'] = {"build_ext" : build_ext}
120     except Exception:
121         print("ERROR: %s" % sys.exc_info()[1])
122         print("Extension module compilation failed, using plain Python implementation")
123
124 setup_args.update(setuptools_extra_args)
125
126 from Cython.Compiler.Version import version
127
128 setup(
129   name = 'Cython',
130   version = version,
131   url = 'http://www.cython.org',
132   author = 'Greg Ewing, Robert Bradshaw, Stefan Behnel, Dag Seljebotn, et al.',
133   author_email = 'cython-dev@codespeak.net',
134   description = "The Cython compiler for writing C extensions for the Python language.",
135   long_description = """\
136   The Cython language makes writing C extensions for the Python language as
137   easy as Python itself.  Cython is a source code translator based on the
138   well-known Pyrex_, but supports more cutting edge functionality and
139   optimizations.
140
141   The Cython language is very close to the Python language (and most Python
142   code is also valid Cython code), but Cython additionally supports calling C
143   functions and declaring C types on variables and class attributes. This
144   allows the compiler to generate very efficient C code from Cython code.
145
146   This makes Cython the ideal language for writing glue code for external C
147   libraries, and for fast C modules that speed up the execution of Python
148   code.
149
150   .. _Pyrex: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
151   """,
152   classifiers = [
153     "Development Status :: 5 - Production/Stable",
154     "Intended Audience :: Developers",
155     "License :: OSI Approved :: Apache Software License",
156     "Operating System :: OS Independent",
157     "Programming Language :: Python",
158     "Programming Language :: Python :: 2",
159     "Programming Language :: Python :: 3",
160     "Programming Language :: C",
161     "Programming Language :: Cython",
162     "Topic :: Software Development :: Code Generators",
163     "Topic :: Software Development :: Compilers",
164     "Topic :: Software Development :: Libraries :: Python Modules"
165   ],
166
167   scripts = scripts,
168   packages=[
169     'Cython',
170     'Cython.Compiler',
171     'Cython.Runtime',
172     'Cython.Distutils',
173     'Cython.Plex',
174
175     'Cython.Tests',
176     'Cython.Compiler.Tests',
177     ],
178
179   # pyximport
180   py_modules = ["pyximport/__init__",
181                 "pyximport/pyximport",
182                 "pyximport/pyxbuild",
183
184                 "cython"],
185
186   **setup_args
187   )