Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 112 lines (96 sloc) 4.094 kb
fef782d @jseabold ENH: Allow automatic Cython building
jseabold authored
1 """
4778b25 @jseabold DOC: Clarify attribution
jseabold authored
2 The cython function was adapted from scikits-image (http://scikits-image.org/)
fef782d @jseabold ENH: Allow automatic Cython building
jseabold authored
3 """
4
5 import sys
6 import os
7 import shutil
8 import subprocess
9 import platform
41d12fd @jseabold ENH: Make building C code dependent on havnig a compiler
jseabold authored
10 from distutils.dist import Distribution
11 from distutils.command.config import config as distutils_config
12 from distutils import log
cef9788 @jseabold ENH: Patch build to work on windows
jseabold authored
13 import optparse # deprecated in 2.7 for argparse
41d12fd @jseabold ENH: Make building C code dependent on havnig a compiler
jseabold authored
14
15 dummy_c_text = r'''
16 /* This file is generated from statsmodels/tools/_build.py to */
17 void do_nothing(void);
18 int main(void) {
19 do_nothing();
20 return 0;
21 }
22 '''
23
cef9788 @jseabold ENH: Patch build to work on windows
jseabold authored
24
41d12fd @jseabold ENH: Make building C code dependent on havnig a compiler
jseabold authored
25 def has_c_compiler():
26 c = distutils_config(Distribution())
cef9788 @jseabold ENH: Patch build to work on windows
jseabold authored
27 if platform.system() == "Windows": # HACK
d387a19 @jseabold STY: Fix indentation. Comment out superfluous code
jseabold authored
28 # this doesn't matter because mingw won't be given at install step
cef9788 @jseabold ENH: Patch build to work on windows
jseabold authored
29 # check if mingw was given in compiler options
d387a19 @jseabold STY: Fix indentation. Comment out superfluous code
jseabold authored
30 #parser = optparse.OptionParser()
31 #parser.add_option('-c', '--compiler', dest='compiler')
32 #options, args = parser.parse_args()
33 #if options.compiler and 'mingw' in options.compiler:
34 # return True
35
cef9788 @jseabold ENH: Patch build to work on windows
jseabold authored
36 # if not, then check to see if compiler is set in disutils.cfg
d387a19 @jseabold STY: Fix indentation. Comment out superfluous code
jseabold authored
37 try: # Josef's code to check the distutils.cfg file
38 c.distribution.parse_config_files(c.distribution.find_config_files())
39 # this will raise a key error if there's not one
40 c.distribution.command_options['build']['compiler'][1]
41 return True
42 except:
43 pass
cef9788 @jseabold ENH: Patch build to work on windows
jseabold authored
44 # just see if there's a system compiler
41d12fd @jseabold ENH: Make building C code dependent on havnig a compiler
jseabold authored
45 try:
46 success = c.try_compile(dummy_c_text)
47 return True
48 except:
49 log.info("No C compiler detected. Not installing Cython version "
50 "of files.")
51 return False
52
fef782d @jseabold ENH: Allow automatic Cython building
jseabold authored
53
54 def cython(pyx_files, working_path=''):
55 """Use Cython to convert the given files to C.
56
57 Parameters
58 ----------
59 pyx_files : list of str
60 The input .pyx files.
61
62 """
63 # Do not build cython files if target is clean
64 if len(sys.argv) >= 2 and sys.argv[1] == 'clean':
65 return
66
67 try:
68 import Cython
69 except ImportError:
70 # If cython is not found, we do nothing -- the build will make use of
71 # the distributed .c files
72 print("Cython not found; falling back to pre-built %s" \
73 % " ".join([f.replace('.pyx', '.c') for f in pyx_files]))
74 else:
75 for pyxfile in [os.path.join(working_path, f) for f in pyx_files]:
76
77 #TODO: replace this with already written hash_funcs once merged
78 # if the .pyx file stayed the same, we don't need to recompile
79 #if not _changed(pyxfile):
80 # continue
81
82 c_file = pyxfile[:-4] + '.c'
83
84 # run cython compiler
85 cmd = 'cython -o %s %s' % (c_file, pyxfile)
86 print(cmd)
87
88 if platform.system() == 'Windows':
e8fb903 @josef-pkt BUG: _build fix windows script name for cython
josef-pkt authored
89 script_path = os.path.join(os.path.dirname(sys.executable),
90 'Scripts', 'cython-script.py')
51300b1 @josef-pkt BUG: fix typo in _build.py
josef-pkt authored
91 if not os.path.exists(script_path):
e8fb903 @josef-pkt BUG: _build fix windows script name for cython
josef-pkt authored
92 script_path = os.path.join(os.path.dirname(sys.executable),
93 'Scripts', 'cython.py')
fef782d @jseabold ENH: Allow automatic Cython building
jseabold authored
94 status = subprocess.call(
e8fb903 @josef-pkt BUG: _build fix windows script name for cython
josef-pkt authored
95 [sys.executable, script_path, '-o', c_file, pyxfile],
96 shell=True)
fef782d @jseabold ENH: Allow automatic Cython building
jseabold authored
97 else:
10751b6 @jseabold BLD: Add fallback for Cython on older Mac OS X
jseabold authored
98 try:
99 status = subprocess.call(['cython', '-o', c_file, pyxfile])
a9ad3f4 @josef-pkt BUG: fix py3 compatibility during setup (before 2to3) closes #288
josef-pkt authored
100 except OSError:
10751b6 @jseabold BLD: Add fallback for Cython on older Mac OS X
jseabold authored
101 # Above is reported to fail on Mac OS X 10.6.8
102 # because cython is a shell alias in the terminal only
103 pkg_path = os.path.abspath(os.path.join(
104 os.path.dirname(Cython.__file__), '..'))
105 script_path = os.path.join(pkg_path, 'cython.py')
106 if not os.path.exists(script_path):
107 script_path = os.path.join(pkg_path,
108 'cython-script.py')
109 status = subprocess.call([sys.executable, script_path,
110 '-o', c_file, pyxfile])
111
Something went wrong with that request. Please try again.