big C++ mergeback
[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         (compiler_dir, ['Cython/Compiler/Lexicon.pickle']),
46         (cython_dir, [ f for pattern in
47                        ['Cython/Includes/*.pxd',
48                         'Cython/Plex/*.pxd',
49                         'Cython/Compiler/*.pxd',
50                         'Cython/Runtime/*.pyx']
51                        for f in glob.glob(pattern) ])]
52 else:
53     setup_args['package_data'] = {'Cython.Compiler' : ['Lexicon.pickle'],
54                                   'Cython' : ['Includes/*.pxd',
55                                               'Plex/*.pxd',
56                                               'Compiler/*.pxd',
57                                               'Runtime/*.pyx']}
58
59 # This dict is used for passing extra arguments that are setuptools 
60 # specific to setup
61 setuptools_extra_args = {}
62
63 if 'setuptools' in sys.modules:
64     setuptools_extra_args['zip_safe'] = False
65     setuptools_extra_args['entry_points'] = {
66         'console_scripts': [
67             'cython = Cython.Compiler.Main:setuptools_main',
68         ]
69     }
70     scripts = []
71 else:
72     if os.name == "posix":
73         scripts = ["bin/cython"]
74     else:
75         scripts = ["cython.py"]
76
77
78 try:
79     if sys.version_info[0] >= 3:
80         raise ValueError
81     sys.argv.remove("--no-cython-compile")
82 except ValueError:
83     try:
84         from distutils.command.build_ext import build_ext as build_ext_orig
85         class build_ext(build_ext_orig):
86             def build_extension(self, ext, *args, **kargs):
87                 try:
88                     build_ext_orig.build_extension(self, ext, *args, **kargs)
89                 except StandardError:
90                     print("Compilation of '%s' failed" % ext.sources[0])
91         from Cython.Compiler.Main import compile
92         from Cython import Utils
93         source_root = os.path.dirname(__file__)
94         compiled_modules = ["Cython.Plex.Scanners",
95                             "Cython.Compiler.Scanning",
96                             "Cython.Compiler.Parsing",
97                             "Cython.Compiler.Visitor",
98                             "Cython.Runtime.refnanny"]
99         extensions = []
100         for module in compiled_modules:
101             source_file = os.path.join(source_root, *module.split('.'))
102             if os.path.exists(source_file + ".py"):
103                 pyx_source_file = source_file + ".py"
104             else:
105                 pyx_source_file = source_file + ".pyx"
106             c_source_file = source_file + ".c"
107             if not os.path.exists(c_source_file) or \
108                Utils.file_newer_than(pyx_source_file,
109                                      Utils.modification_time(c_source_file)):
110                 print("Compiling module %s ..." % module)
111                 result = compile(pyx_source_file)
112                 c_source_file = result.c_file
113             if c_source_file:
114                 extensions.append(
115                     Extension(module, sources = [c_source_file])
116                     )
117             else:
118                 print("Compilation failed")
119         if extensions:
120             setup_args['ext_modules'] = extensions
121             setup_args['cmdclass'] = {"build_ext" : build_ext}
122     except Exception:
123         print("ERROR: %s" % sys.exc_info()[1])
124         print("Extension module compilation failed, using plain Python implementation")
125
126 setup_args.update(setuptools_extra_args)
127
128 from Cython.Compiler.Version import version
129
130 setup(
131   name = 'Cython',
132   version = version,
133   url = 'http://www.cython.org',
134   author = 'Greg Ewing, Robert Bradshaw, Stefan Behnel, Dag Seljebotn, et al.',
135   author_email = 'cython-dev@codespeak.net',
136   description = "The Cython compiler for writing C extensions for the Python language.",
137   long_description = """\
138   The Cython language makes writing C extensions for the Python language as
139   easy as Python itself.  Cython is a source code translator based on the
140   well-known Pyrex_, but supports more cutting edge functionality and
141   optimizations.
142
143   The Cython language is very close to the Python language (and most Python
144   code is also valid Cython code), but Cython additionally supports calling C
145   functions and declaring C types on variables and class attributes. This
146   allows the compiler to generate very efficient C code from Cython code.
147
148   This makes Cython the ideal language for writing glue code for external C
149   libraries, and for fast C modules that speed up the execution of Python
150   code.
151
152   .. _Pyrex: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
153   """,
154   classifiers = [
155     "Development Status :: 5 - Production/Stable",
156     "Intended Audience :: Developers",
157     "License :: OSI Approved :: Apache Software License",
158     "Operating System :: OS Independent",
159     "Programming Language :: Python",
160     "Programming Language :: Python :: 2",
161     "Programming Language :: Python :: 3",
162     "Programming Language :: C",
163     "Programming Language :: Cython",
164     "Topic :: Software Development :: Code Generators",
165     "Topic :: Software Development :: Compilers",
166     "Topic :: Software Development :: Libraries :: Python Modules"
167   ],
168
169   scripts = scripts,
170   packages=[
171     'Cython',
172     'Cython.Compiler',
173     'Cython.Runtime',
174     'Cython.Distutils',
175     'Cython.Mac',
176     'Cython.Unix',
177     'Cython.Plex',
178
179     'Cython.Tests',
180     'Cython.Compiler.Tests',
181     ],
182
183   # pyximport
184   py_modules = ["pyximport/__init__",
185                 "pyximport/pyximport",
186                 "pyximport/pyxbuild",
187
188                 "cython"],
189
190   **setup_args
191   )