comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/distutils/ccompiler.py @ 87:2a2c65a20a8b

Add Python libs and headers
author Chris Cannam
date Wed, 25 Feb 2015 14:05:22 +0000
parents
children
comparison
equal deleted inserted replaced
86:413a9d26189e 87:2a2c65a20a8b
1 from __future__ import division, absolute_import, print_function
2
3 import re
4 import os
5 import sys
6 import types
7 from copy import copy
8
9 from distutils.ccompiler import *
10 from distutils import ccompiler
11 from distutils.errors import DistutilsExecError, DistutilsModuleError, \
12 DistutilsPlatformError
13 from distutils.sysconfig import customize_compiler
14 from distutils.version import LooseVersion
15
16 from numpy.distutils import log
17 from numpy.distutils.exec_command import exec_command
18 from numpy.distutils.misc_util import cyg2win32, is_sequence, mingw32, \
19 quote_args
20 from numpy.distutils.compat import get_exception
21
22
23 def replace_method(klass, method_name, func):
24 if sys.version_info[0] < 3:
25 m = types.MethodType(func, None, klass)
26 else:
27 # Py3k does not have unbound method anymore, MethodType does not work
28 m = lambda self, *args, **kw: func(self, *args, **kw)
29 setattr(klass, method_name, m)
30
31 # Using customized CCompiler.spawn.
32 def CCompiler_spawn(self, cmd, display=None):
33 """
34 Execute a command in a sub-process.
35
36 Parameters
37 ----------
38 cmd : str
39 The command to execute.
40 display : str or sequence of str, optional
41 The text to add to the log file kept by `numpy.distutils`.
42 If not given, `display` is equal to `cmd`.
43
44 Returns
45 -------
46 None
47
48 Raises
49 ------
50 DistutilsExecError
51 If the command failed, i.e. the exit status was not 0.
52
53 """
54 if display is None:
55 display = cmd
56 if is_sequence(display):
57 display = ' '.join(list(display))
58 log.info(display)
59 s, o = exec_command(cmd)
60 if s:
61 if is_sequence(cmd):
62 cmd = ' '.join(list(cmd))
63 try:
64 print(o)
65 except UnicodeError:
66 # When installing through pip, `o` can contain non-ascii chars
67 pass
68 if re.search('Too many open files', o):
69 msg = '\nTry rerunning setup command until build succeeds.'
70 else:
71 msg = ''
72 raise DistutilsExecError('Command "%s" failed with exit status %d%s' % (cmd, s, msg))
73
74 replace_method(CCompiler, 'spawn', CCompiler_spawn)
75
76 def CCompiler_object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
77 """
78 Return the name of the object files for the given source files.
79
80 Parameters
81 ----------
82 source_filenames : list of str
83 The list of paths to source files. Paths can be either relative or
84 absolute, this is handled transparently.
85 strip_dir : bool, optional
86 Whether to strip the directory from the returned paths. If True,
87 the file name prepended by `output_dir` is returned. Default is False.
88 output_dir : str, optional
89 If given, this path is prepended to the returned paths to the
90 object files.
91
92 Returns
93 -------
94 obj_names : list of str
95 The list of paths to the object files corresponding to the source
96 files in `source_filenames`.
97
98 """
99 if output_dir is None:
100 output_dir = ''
101 obj_names = []
102 for src_name in source_filenames:
103 base, ext = os.path.splitext(os.path.normpath(src_name))
104 base = os.path.splitdrive(base)[1] # Chop off the drive
105 base = base[os.path.isabs(base):] # If abs, chop off leading /
106 if base.startswith('..'):
107 # Resolve starting relative path components, middle ones
108 # (if any) have been handled by os.path.normpath above.
109 i = base.rfind('..')+2
110 d = base[:i]
111 d = os.path.basename(os.path.abspath(d))
112 base = d + base[i:]
113 if ext not in self.src_extensions:
114 raise UnknownFileError("unknown file type '%s' (from '%s')" % (ext, src_name))
115 if strip_dir:
116 base = os.path.basename(base)
117 obj_name = os.path.join(output_dir, base + self.obj_extension)
118 obj_names.append(obj_name)
119 return obj_names
120
121 replace_method(CCompiler, 'object_filenames', CCompiler_object_filenames)
122
123 def CCompiler_compile(self, sources, output_dir=None, macros=None,
124 include_dirs=None, debug=0, extra_preargs=None,
125 extra_postargs=None, depends=None):
126 """
127 Compile one or more source files.
128
129 Please refer to the Python distutils API reference for more details.
130
131 Parameters
132 ----------
133 sources : list of str
134 A list of filenames
135 output_dir : str, optional
136 Path to the output directory.
137 macros : list of tuples
138 A list of macro definitions.
139 include_dirs : list of str, optional
140 The directories to add to the default include file search path for
141 this compilation only.
142 debug : bool, optional
143 Whether or not to output debug symbols in or alongside the object
144 file(s).
145 extra_preargs, extra_postargs : ?
146 Extra pre- and post-arguments.
147 depends : list of str, optional
148 A list of file names that all targets depend on.
149
150 Returns
151 -------
152 objects : list of str
153 A list of object file names, one per source file `sources`.
154
155 Raises
156 ------
157 CompileError
158 If compilation fails.
159
160 """
161 # This method is effective only with Python >=2.3 distutils.
162 # Any changes here should be applied also to fcompiler.compile
163 # method to support pre Python 2.3 distutils.
164 if not sources:
165 return []
166 # FIXME:RELATIVE_IMPORT
167 if sys.version_info[0] < 3:
168 from .fcompiler import FCompiler
169 else:
170 from numpy.distutils.fcompiler import FCompiler
171 if isinstance(self, FCompiler):
172 display = []
173 for fc in ['f77', 'f90', 'fix']:
174 fcomp = getattr(self, 'compiler_'+fc)
175 if fcomp is None:
176 continue
177 display.append("Fortran %s compiler: %s" % (fc, ' '.join(fcomp)))
178 display = '\n'.join(display)
179 else:
180 ccomp = self.compiler_so
181 display = "C compiler: %s\n" % (' '.join(ccomp),)
182 log.info(display)
183 macros, objects, extra_postargs, pp_opts, build = \
184 self._setup_compile(output_dir, macros, include_dirs, sources,
185 depends, extra_postargs)
186 cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
187 display = "compile options: '%s'" % (' '.join(cc_args))
188 if extra_postargs:
189 display += "\nextra options: '%s'" % (' '.join(extra_postargs))
190 log.info(display)
191
192 # build any sources in same order as they were originally specified
193 # especially important for fortran .f90 files using modules
194 if isinstance(self, FCompiler):
195 objects_to_build = list(build.keys())
196 for obj in objects:
197 if obj in objects_to_build:
198 src, ext = build[obj]
199 if self.compiler_type=='absoft':
200 obj = cyg2win32(obj)
201 src = cyg2win32(src)
202 self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
203 else:
204 for obj, (src, ext) in build.items():
205 self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
206
207 # Return *all* object filenames, not just the ones we just built.
208 return objects
209
210 replace_method(CCompiler, 'compile', CCompiler_compile)
211
212 def CCompiler_customize_cmd(self, cmd, ignore=()):
213 """
214 Customize compiler using distutils command.
215
216 Parameters
217 ----------
218 cmd : class instance
219 An instance inheriting from `distutils.cmd.Command`.
220 ignore : sequence of str, optional
221 List of `CCompiler` commands (without ``'set_'``) that should not be
222 altered. Strings that are checked for are:
223 ``('include_dirs', 'define', 'undef', 'libraries', 'library_dirs',
224 'rpath', 'link_objects')``.
225
226 Returns
227 -------
228 None
229
230 """
231 log.info('customize %s using %s' % (self.__class__.__name__,
232 cmd.__class__.__name__))
233 def allow(attr):
234 return getattr(cmd, attr, None) is not None and attr not in ignore
235
236 if allow('include_dirs'):
237 self.set_include_dirs(cmd.include_dirs)
238 if allow('define'):
239 for (name, value) in cmd.define:
240 self.define_macro(name, value)
241 if allow('undef'):
242 for macro in cmd.undef:
243 self.undefine_macro(macro)
244 if allow('libraries'):
245 self.set_libraries(self.libraries + cmd.libraries)
246 if allow('library_dirs'):
247 self.set_library_dirs(self.library_dirs + cmd.library_dirs)
248 if allow('rpath'):
249 self.set_runtime_library_dirs(cmd.rpath)
250 if allow('link_objects'):
251 self.set_link_objects(cmd.link_objects)
252
253 replace_method(CCompiler, 'customize_cmd', CCompiler_customize_cmd)
254
255 def _compiler_to_string(compiler):
256 props = []
257 mx = 0
258 keys = list(compiler.executables.keys())
259 for key in ['version', 'libraries', 'library_dirs',
260 'object_switch', 'compile_switch',
261 'include_dirs', 'define', 'undef', 'rpath', 'link_objects']:
262 if key not in keys:
263 keys.append(key)
264 for key in keys:
265 if hasattr(compiler, key):
266 v = getattr(compiler, key)
267 mx = max(mx, len(key))
268 props.append((key, repr(v)))
269 lines = []
270 format = '%-' + repr(mx+1) + 's = %s'
271 for prop in props:
272 lines.append(format % prop)
273 return '\n'.join(lines)
274
275 def CCompiler_show_customization(self):
276 """
277 Print the compiler customizations to stdout.
278
279 Parameters
280 ----------
281 None
282
283 Returns
284 -------
285 None
286
287 Notes
288 -----
289 Printing is only done if the distutils log threshold is < 2.
290
291 """
292 if 0:
293 for attrname in ['include_dirs', 'define', 'undef',
294 'libraries', 'library_dirs',
295 'rpath', 'link_objects']:
296 attr = getattr(self, attrname, None)
297 if not attr:
298 continue
299 log.info("compiler '%s' is set to %s" % (attrname, attr))
300 try:
301 self.get_version()
302 except:
303 pass
304 if log._global_log.threshold<2:
305 print('*'*80)
306 print(self.__class__)
307 print(_compiler_to_string(self))
308 print('*'*80)
309
310 replace_method(CCompiler, 'show_customization', CCompiler_show_customization)
311
312 def CCompiler_customize(self, dist, need_cxx=0):
313 """
314 Do any platform-specific customization of a compiler instance.
315
316 This method calls `distutils.sysconfig.customize_compiler` for
317 platform-specific customization, as well as optionally remove a flag
318 to suppress spurious warnings in case C++ code is being compiled.
319
320 Parameters
321 ----------
322 dist : object
323 This parameter is not used for anything.
324 need_cxx : bool, optional
325 Whether or not C++ has to be compiled. If so (True), the
326 ``"-Wstrict-prototypes"`` option is removed to prevent spurious
327 warnings. Default is False.
328
329 Returns
330 -------
331 None
332
333 Notes
334 -----
335 All the default options used by distutils can be extracted with::
336
337 from distutils import sysconfig
338 sysconfig.get_config_vars('CC', 'CXX', 'OPT', 'BASECFLAGS',
339 'CCSHARED', 'LDSHARED', 'SO')
340
341 """
342 # See FCompiler.customize for suggested usage.
343 log.info('customize %s' % (self.__class__.__name__))
344 customize_compiler(self)
345 if need_cxx:
346 # In general, distutils uses -Wstrict-prototypes, but this option is
347 # not valid for C++ code, only for C. Remove it if it's there to
348 # avoid a spurious warning on every compilation.
349 try:
350 self.compiler_so.remove('-Wstrict-prototypes')
351 except (AttributeError, ValueError):
352 pass
353
354 if hasattr(self, 'compiler') and 'cc' in self.compiler[0]:
355 if not self.compiler_cxx:
356 if self.compiler[0].startswith('gcc'):
357 a, b = 'gcc', 'g++'
358 else:
359 a, b = 'cc', 'c++'
360 self.compiler_cxx = [self.compiler[0].replace(a, b)]\
361 + self.compiler[1:]
362 else:
363 if hasattr(self, 'compiler'):
364 log.warn("#### %s #######" % (self.compiler,))
365 log.warn('Missing compiler_cxx fix for '+self.__class__.__name__)
366 return
367
368 replace_method(CCompiler, 'customize', CCompiler_customize)
369
370 def simple_version_match(pat=r'[-.\d]+', ignore='', start=''):
371 """
372 Simple matching of version numbers, for use in CCompiler and FCompiler.
373
374 Parameters
375 ----------
376 pat : str, optional
377 A regular expression matching version numbers.
378 Default is ``r'[-.\\d]+'``.
379 ignore : str, optional
380 A regular expression matching patterns to skip.
381 Default is ``''``, in which case nothing is skipped.
382 start : str, optional
383 A regular expression matching the start of where to start looking
384 for version numbers.
385 Default is ``''``, in which case searching is started at the
386 beginning of the version string given to `matcher`.
387
388 Returns
389 -------
390 matcher : callable
391 A function that is appropriate to use as the ``.version_match``
392 attribute of a `CCompiler` class. `matcher` takes a single parameter,
393 a version string.
394
395 """
396 def matcher(self, version_string):
397 # version string may appear in the second line, so getting rid
398 # of new lines:
399 version_string = version_string.replace('\n', ' ')
400 pos = 0
401 if start:
402 m = re.match(start, version_string)
403 if not m:
404 return None
405 pos = m.end()
406 while True:
407 m = re.search(pat, version_string[pos:])
408 if not m:
409 return None
410 if ignore and re.match(ignore, m.group(0)):
411 pos = m.end()
412 continue
413 break
414 return m.group(0)
415 return matcher
416
417 def CCompiler_get_version(self, force=False, ok_status=[0]):
418 """
419 Return compiler version, or None if compiler is not available.
420
421 Parameters
422 ----------
423 force : bool, optional
424 If True, force a new determination of the version, even if the
425 compiler already has a version attribute. Default is False.
426 ok_status : list of int, optional
427 The list of status values returned by the version look-up process
428 for which a version string is returned. If the status value is not
429 in `ok_status`, None is returned. Default is ``[0]``.
430
431 Returns
432 -------
433 version : str or None
434 Version string, in the format of `distutils.version.LooseVersion`.
435
436 """
437 if not force and hasattr(self, 'version'):
438 return self.version
439 self.find_executables()
440 try:
441 version_cmd = self.version_cmd
442 except AttributeError:
443 return None
444 if not version_cmd or not version_cmd[0]:
445 return None
446 try:
447 matcher = self.version_match
448 except AttributeError:
449 try:
450 pat = self.version_pattern
451 except AttributeError:
452 return None
453 def matcher(version_string):
454 m = re.match(pat, version_string)
455 if not m:
456 return None
457 version = m.group('version')
458 return version
459
460 status, output = exec_command(version_cmd, use_tee=0)
461
462 version = None
463 if status in ok_status:
464 version = matcher(output)
465 if version:
466 version = LooseVersion(version)
467 self.version = version
468 return version
469
470 replace_method(CCompiler, 'get_version', CCompiler_get_version)
471
472 def CCompiler_cxx_compiler(self):
473 """
474 Return the C++ compiler.
475
476 Parameters
477 ----------
478 None
479
480 Returns
481 -------
482 cxx : class instance
483 The C++ compiler, as a `CCompiler` instance.
484
485 """
486 if self.compiler_type=='msvc': return self
487 cxx = copy(self)
488 cxx.compiler_so = [cxx.compiler_cxx[0]] + cxx.compiler_so[1:]
489 if sys.platform.startswith('aix') and 'ld_so_aix' in cxx.linker_so[0]:
490 # AIX needs the ld_so_aix script included with Python
491 cxx.linker_so = [cxx.linker_so[0], cxx.compiler_cxx[0]] \
492 + cxx.linker_so[2:]
493 else:
494 cxx.linker_so = [cxx.compiler_cxx[0]] + cxx.linker_so[1:]
495 return cxx
496
497 replace_method(CCompiler, 'cxx_compiler', CCompiler_cxx_compiler)
498
499 compiler_class['intel'] = ('intelccompiler', 'IntelCCompiler',
500 "Intel C Compiler for 32-bit applications")
501 compiler_class['intele'] = ('intelccompiler', 'IntelItaniumCCompiler',
502 "Intel C Itanium Compiler for Itanium-based applications")
503 compiler_class['intelem'] = ('intelccompiler', 'IntelEM64TCCompiler',
504 "Intel C Compiler for 64-bit applications")
505 compiler_class['pathcc'] = ('pathccompiler', 'PathScaleCCompiler',
506 "PathScale Compiler for SiCortex-based applications")
507 ccompiler._default_compilers += (('linux.*', 'intel'),
508 ('linux.*', 'intele'),
509 ('linux.*', 'intelem'),
510 ('linux.*', 'pathcc'))
511
512 if sys.platform == 'win32':
513 compiler_class['mingw32'] = ('mingw32ccompiler', 'Mingw32CCompiler',
514 "Mingw32 port of GNU C Compiler for Win32"\
515 "(for MSC built Python)")
516 if mingw32():
517 # On windows platforms, we want to default to mingw32 (gcc)
518 # because msvc can't build blitz stuff.
519 log.info('Setting mingw32 as default compiler for nt.')
520 ccompiler._default_compilers = (('nt', 'mingw32'),) \
521 + ccompiler._default_compilers
522
523
524 _distutils_new_compiler = new_compiler
525 def new_compiler (plat=None,
526 compiler=None,
527 verbose=0,
528 dry_run=0,
529 force=0):
530 # Try first C compilers from numpy.distutils.
531 if plat is None:
532 plat = os.name
533 try:
534 if compiler is None:
535 compiler = get_default_compiler(plat)
536 (module_name, class_name, long_description) = compiler_class[compiler]
537 except KeyError:
538 msg = "don't know how to compile C/C++ code on platform '%s'" % plat
539 if compiler is not None:
540 msg = msg + " with '%s' compiler" % compiler
541 raise DistutilsPlatformError(msg)
542 module_name = "numpy.distutils." + module_name
543 try:
544 __import__ (module_name)
545 except ImportError:
546 msg = str(get_exception())
547 log.info('%s in numpy.distutils; trying from distutils',
548 str(msg))
549 module_name = module_name[6:]
550 try:
551 __import__(module_name)
552 except ImportError:
553 msg = str(get_exception())
554 raise DistutilsModuleError("can't compile C/C++ code: unable to load module '%s'" % \
555 module_name)
556 try:
557 module = sys.modules[module_name]
558 klass = vars(module)[class_name]
559 except KeyError:
560 raise DistutilsModuleError(("can't compile C/C++ code: unable to find class '%s' " +
561 "in module '%s'") % (class_name, module_name))
562 compiler = klass(None, dry_run, force)
563 log.debug('new_compiler returns %s' % (klass))
564 return compiler
565
566 ccompiler.new_compiler = new_compiler
567
568 _distutils_gen_lib_options = gen_lib_options
569 def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
570 library_dirs = quote_args(library_dirs)
571 runtime_library_dirs = quote_args(runtime_library_dirs)
572 r = _distutils_gen_lib_options(compiler, library_dirs,
573 runtime_library_dirs, libraries)
574 lib_opts = []
575 for i in r:
576 if is_sequence(i):
577 lib_opts.extend(list(i))
578 else:
579 lib_opts.append(i)
580 return lib_opts
581 ccompiler.gen_lib_options = gen_lib_options
582
583 # Also fix up the various compiler modules, which do
584 # from distutils.ccompiler import gen_lib_options
585 # Don't bother with mwerks, as we don't support Classic Mac.
586 for _cc in ['msvc', 'bcpp', 'cygwinc', 'emxc', 'unixc']:
587 _m = sys.modules.get('distutils.'+_cc+'compiler')
588 if _m is not None:
589 setattr(_m, 'gen_lib_options', gen_lib_options)
590
591 _distutils_gen_preprocess_options = gen_preprocess_options
592 def gen_preprocess_options (macros, include_dirs):
593 include_dirs = quote_args(include_dirs)
594 return _distutils_gen_preprocess_options(macros, include_dirs)
595 ccompiler.gen_preprocess_options = gen_preprocess_options
596
597 ##Fix distutils.util.split_quoted:
598 # NOTE: I removed this fix in revision 4481 (see ticket #619), but it appears
599 # that removing this fix causes f2py problems on Windows XP (see ticket #723).
600 # Specifically, on WinXP when gfortran is installed in a directory path, which
601 # contains spaces, then f2py is unable to find it.
602 import re
603 import string
604 _wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace)
605 _squote_re = re.compile(r"'(?:[^'\\]|\\.)*'")
606 _dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"')
607 _has_white_re = re.compile(r'\s')
608 def split_quoted(s):
609 s = s.strip()
610 words = []
611 pos = 0
612
613 while s:
614 m = _wordchars_re.match(s, pos)
615 end = m.end()
616 if end == len(s):
617 words.append(s[:end])
618 break
619
620 if s[end] in string.whitespace: # unescaped, unquoted whitespace: now
621 words.append(s[:end]) # we definitely have a word delimiter
622 s = s[end:].lstrip()
623 pos = 0
624
625 elif s[end] == '\\': # preserve whatever is being escaped;
626 # will become part of the current word
627 s = s[:end] + s[end+1:]
628 pos = end+1
629
630 else:
631 if s[end] == "'": # slurp singly-quoted string
632 m = _squote_re.match(s, end)
633 elif s[end] == '"': # slurp doubly-quoted string
634 m = _dquote_re.match(s, end)
635 else:
636 raise RuntimeError("this can't happen (bad char '%c')" % s[end])
637
638 if m is None:
639 raise ValueError("bad string (mismatched %s quotes?)" % s[end])
640
641 (beg, end) = m.span()
642 if _has_white_re.search(s[beg+1:end-1]):
643 s = s[:beg] + s[beg+1:end-1] + s[end:]
644 pos = m.end() - 2
645 else:
646 # Keeping quotes when a quoted word does not contain
647 # white-space. XXX: send a patch to distutils
648 pos = m.end()
649
650 if pos >= len(s):
651 words.append(s)
652 break
653
654 return words
655 ccompiler.split_quoted = split_quoted
656 ##Fix distutils.util.split_quoted: