ccompiler.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. from __future__ import division, absolute_import, print_function
  2. import os
  3. import re
  4. import sys
  5. import types
  6. import shlex
  7. import time
  8. import subprocess
  9. from copy import copy
  10. from distutils import ccompiler
  11. from distutils.ccompiler import *
  12. from distutils.errors import DistutilsExecError, DistutilsModuleError, \
  13. DistutilsPlatformError, CompileError
  14. from distutils.sysconfig import customize_compiler
  15. from distutils.version import LooseVersion
  16. from numpy.distutils import log
  17. from numpy.distutils.compat import get_exception
  18. from numpy.distutils.exec_command import (
  19. filepath_from_subprocess_output, forward_bytes_to_stdout
  20. )
  21. from numpy.distutils.misc_util import cyg2win32, is_sequence, mingw32, \
  22. get_num_build_jobs, \
  23. _commandline_dep_string
  24. # globals for parallel build management
  25. try:
  26. import threading
  27. except ImportError:
  28. import dummy_threading as threading
  29. _job_semaphore = None
  30. _global_lock = threading.Lock()
  31. _processing_files = set()
  32. def _needs_build(obj, cc_args, extra_postargs, pp_opts):
  33. """
  34. Check if an objects needs to be rebuild based on its dependencies
  35. Parameters
  36. ----------
  37. obj : str
  38. object file
  39. Returns
  40. -------
  41. bool
  42. """
  43. # defined in unixcompiler.py
  44. dep_file = obj + '.d'
  45. if not os.path.exists(dep_file):
  46. return True
  47. # dep_file is a makefile containing 'object: dependencies'
  48. # formatted like posix shell (spaces escaped, \ line continuations)
  49. # the last line contains the compiler commandline arguments as some
  50. # projects may compile an extension multiple times with different
  51. # arguments
  52. with open(dep_file, "r") as f:
  53. lines = f.readlines()
  54. cmdline =_commandline_dep_string(cc_args, extra_postargs, pp_opts)
  55. last_cmdline = lines[-1]
  56. if last_cmdline != cmdline:
  57. return True
  58. contents = ''.join(lines[:-1])
  59. deps = [x for x in shlex.split(contents, posix=True)
  60. if x != "\n" and not x.endswith(":")]
  61. try:
  62. t_obj = os.stat(obj).st_mtime
  63. # check if any of the dependencies is newer than the object
  64. # the dependencies includes the source used to create the object
  65. for f in deps:
  66. if os.stat(f).st_mtime > t_obj:
  67. return True
  68. except OSError:
  69. # no object counts as newer (shouldn't happen if dep_file exists)
  70. return True
  71. return False
  72. def replace_method(klass, method_name, func):
  73. if sys.version_info[0] < 3:
  74. m = types.MethodType(func, None, klass)
  75. else:
  76. # Py3k does not have unbound method anymore, MethodType does not work
  77. m = lambda self, *args, **kw: func(self, *args, **kw)
  78. setattr(klass, method_name, m)
  79. ######################################################################
  80. ## Method that subclasses may redefine. But don't call this method,
  81. ## it i private to CCompiler class and may return unexpected
  82. ## results if used elsewhere. So, you have been warned..
  83. def CCompiler_find_executables(self):
  84. """
  85. Does nothing here, but is called by the get_version method and can be
  86. overridden by subclasses. In particular it is redefined in the `FCompiler`
  87. class where more documentation can be found.
  88. """
  89. pass
  90. replace_method(CCompiler, 'find_executables', CCompiler_find_executables)
  91. # Using customized CCompiler.spawn.
  92. def CCompiler_spawn(self, cmd, display=None):
  93. """
  94. Execute a command in a sub-process.
  95. Parameters
  96. ----------
  97. cmd : str
  98. The command to execute.
  99. display : str or sequence of str, optional
  100. The text to add to the log file kept by `numpy.distutils`.
  101. If not given, `display` is equal to `cmd`.
  102. Returns
  103. -------
  104. None
  105. Raises
  106. ------
  107. DistutilsExecError
  108. If the command failed, i.e. the exit status was not 0.
  109. """
  110. if display is None:
  111. display = cmd
  112. if is_sequence(display):
  113. display = ' '.join(list(display))
  114. log.info(display)
  115. try:
  116. subprocess.check_output(cmd)
  117. except subprocess.CalledProcessError as exc:
  118. o = exc.output
  119. s = exc.returncode
  120. except OSError:
  121. # OSError doesn't have the same hooks for the exception
  122. # output, but exec_command() historically would use an
  123. # empty string for EnvironmentError (base class for
  124. # OSError)
  125. o = b''
  126. # status previously used by exec_command() for parent
  127. # of OSError
  128. s = 127
  129. else:
  130. # use a convenience return here so that any kind of
  131. # caught exception will execute the default code after the
  132. # try / except block, which handles various exceptions
  133. return None
  134. if is_sequence(cmd):
  135. cmd = ' '.join(list(cmd))
  136. forward_bytes_to_stdout(o)
  137. if re.search(b'Too many open files', o):
  138. msg = '\nTry rerunning setup command until build succeeds.'
  139. else:
  140. msg = ''
  141. raise DistutilsExecError('Command "%s" failed with exit status %d%s' %
  142. (cmd, s, msg))
  143. replace_method(CCompiler, 'spawn', CCompiler_spawn)
  144. def CCompiler_object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
  145. """
  146. Return the name of the object files for the given source files.
  147. Parameters
  148. ----------
  149. source_filenames : list of str
  150. The list of paths to source files. Paths can be either relative or
  151. absolute, this is handled transparently.
  152. strip_dir : bool, optional
  153. Whether to strip the directory from the returned paths. If True,
  154. the file name prepended by `output_dir` is returned. Default is False.
  155. output_dir : str, optional
  156. If given, this path is prepended to the returned paths to the
  157. object files.
  158. Returns
  159. -------
  160. obj_names : list of str
  161. The list of paths to the object files corresponding to the source
  162. files in `source_filenames`.
  163. """
  164. if output_dir is None:
  165. output_dir = ''
  166. obj_names = []
  167. for src_name in source_filenames:
  168. base, ext = os.path.splitext(os.path.normpath(src_name))
  169. base = os.path.splitdrive(base)[1] # Chop off the drive
  170. base = base[os.path.isabs(base):] # If abs, chop off leading /
  171. if base.startswith('..'):
  172. # Resolve starting relative path components, middle ones
  173. # (if any) have been handled by os.path.normpath above.
  174. i = base.rfind('..')+2
  175. d = base[:i]
  176. d = os.path.basename(os.path.abspath(d))
  177. base = d + base[i:]
  178. if ext not in self.src_extensions:
  179. raise UnknownFileError("unknown file type '%s' (from '%s')" % (ext, src_name))
  180. if strip_dir:
  181. base = os.path.basename(base)
  182. obj_name = os.path.join(output_dir, base + self.obj_extension)
  183. obj_names.append(obj_name)
  184. return obj_names
  185. replace_method(CCompiler, 'object_filenames', CCompiler_object_filenames)
  186. def CCompiler_compile(self, sources, output_dir=None, macros=None,
  187. include_dirs=None, debug=0, extra_preargs=None,
  188. extra_postargs=None, depends=None):
  189. """
  190. Compile one or more source files.
  191. Please refer to the Python distutils API reference for more details.
  192. Parameters
  193. ----------
  194. sources : list of str
  195. A list of filenames
  196. output_dir : str, optional
  197. Path to the output directory.
  198. macros : list of tuples
  199. A list of macro definitions.
  200. include_dirs : list of str, optional
  201. The directories to add to the default include file search path for
  202. this compilation only.
  203. debug : bool, optional
  204. Whether or not to output debug symbols in or alongside the object
  205. file(s).
  206. extra_preargs, extra_postargs : ?
  207. Extra pre- and post-arguments.
  208. depends : list of str, optional
  209. A list of file names that all targets depend on.
  210. Returns
  211. -------
  212. objects : list of str
  213. A list of object file names, one per source file `sources`.
  214. Raises
  215. ------
  216. CompileError
  217. If compilation fails.
  218. """
  219. # This method is effective only with Python >=2.3 distutils.
  220. # Any changes here should be applied also to fcompiler.compile
  221. # method to support pre Python 2.3 distutils.
  222. global _job_semaphore
  223. jobs = get_num_build_jobs()
  224. # setup semaphore to not exceed number of compile jobs when parallelized at
  225. # extension level (python >= 3.5)
  226. with _global_lock:
  227. if _job_semaphore is None:
  228. _job_semaphore = threading.Semaphore(jobs)
  229. if not sources:
  230. return []
  231. # FIXME:RELATIVE_IMPORT
  232. if sys.version_info[0] < 3:
  233. from .fcompiler import FCompiler, is_f_file, has_f90_header
  234. else:
  235. from numpy.distutils.fcompiler import (FCompiler, is_f_file,
  236. has_f90_header)
  237. if isinstance(self, FCompiler):
  238. display = []
  239. for fc in ['f77', 'f90', 'fix']:
  240. fcomp = getattr(self, 'compiler_'+fc)
  241. if fcomp is None:
  242. continue
  243. display.append("Fortran %s compiler: %s" % (fc, ' '.join(fcomp)))
  244. display = '\n'.join(display)
  245. else:
  246. ccomp = self.compiler_so
  247. display = "C compiler: %s\n" % (' '.join(ccomp),)
  248. log.info(display)
  249. macros, objects, extra_postargs, pp_opts, build = \
  250. self._setup_compile(output_dir, macros, include_dirs, sources,
  251. depends, extra_postargs)
  252. cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
  253. display = "compile options: '%s'" % (' '.join(cc_args))
  254. if extra_postargs:
  255. display += "\nextra options: '%s'" % (' '.join(extra_postargs))
  256. log.info(display)
  257. def single_compile(args):
  258. obj, (src, ext) = args
  259. if not _needs_build(obj, cc_args, extra_postargs, pp_opts):
  260. return
  261. # check if we are currently already processing the same object
  262. # happens when using the same source in multiple extensions
  263. while True:
  264. # need explicit lock as there is no atomic check and add with GIL
  265. with _global_lock:
  266. # file not being worked on, start working
  267. if obj not in _processing_files:
  268. _processing_files.add(obj)
  269. break
  270. # wait for the processing to end
  271. time.sleep(0.1)
  272. try:
  273. # retrieve slot from our #job semaphore and build
  274. with _job_semaphore:
  275. self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
  276. finally:
  277. # register being done processing
  278. with _global_lock:
  279. _processing_files.remove(obj)
  280. if isinstance(self, FCompiler):
  281. objects_to_build = list(build.keys())
  282. f77_objects, other_objects = [], []
  283. for obj in objects:
  284. if obj in objects_to_build:
  285. src, ext = build[obj]
  286. if self.compiler_type=='absoft':
  287. obj = cyg2win32(obj)
  288. src = cyg2win32(src)
  289. if is_f_file(src) and not has_f90_header(src):
  290. f77_objects.append((obj, (src, ext)))
  291. else:
  292. other_objects.append((obj, (src, ext)))
  293. # f77 objects can be built in parallel
  294. build_items = f77_objects
  295. # build f90 modules serial, module files are generated during
  296. # compilation and may be used by files later in the list so the
  297. # ordering is important
  298. for o in other_objects:
  299. single_compile(o)
  300. else:
  301. build_items = build.items()
  302. if len(build) > 1 and jobs > 1:
  303. # build parallel
  304. import multiprocessing.pool
  305. pool = multiprocessing.pool.ThreadPool(jobs)
  306. pool.map(single_compile, build_items)
  307. pool.close()
  308. else:
  309. # build serial
  310. for o in build_items:
  311. single_compile(o)
  312. # Return *all* object filenames, not just the ones we just built.
  313. return objects
  314. replace_method(CCompiler, 'compile', CCompiler_compile)
  315. def CCompiler_customize_cmd(self, cmd, ignore=()):
  316. """
  317. Customize compiler using distutils command.
  318. Parameters
  319. ----------
  320. cmd : class instance
  321. An instance inheriting from `distutils.cmd.Command`.
  322. ignore : sequence of str, optional
  323. List of `CCompiler` commands (without ``'set_'``) that should not be
  324. altered. Strings that are checked for are:
  325. ``('include_dirs', 'define', 'undef', 'libraries', 'library_dirs',
  326. 'rpath', 'link_objects')``.
  327. Returns
  328. -------
  329. None
  330. """
  331. log.info('customize %s using %s' % (self.__class__.__name__,
  332. cmd.__class__.__name__))
  333. def allow(attr):
  334. return getattr(cmd, attr, None) is not None and attr not in ignore
  335. if allow('include_dirs'):
  336. self.set_include_dirs(cmd.include_dirs)
  337. if allow('define'):
  338. for (name, value) in cmd.define:
  339. self.define_macro(name, value)
  340. if allow('undef'):
  341. for macro in cmd.undef:
  342. self.undefine_macro(macro)
  343. if allow('libraries'):
  344. self.set_libraries(self.libraries + cmd.libraries)
  345. if allow('library_dirs'):
  346. self.set_library_dirs(self.library_dirs + cmd.library_dirs)
  347. if allow('rpath'):
  348. self.set_runtime_library_dirs(cmd.rpath)
  349. if allow('link_objects'):
  350. self.set_link_objects(cmd.link_objects)
  351. replace_method(CCompiler, 'customize_cmd', CCompiler_customize_cmd)
  352. def _compiler_to_string(compiler):
  353. props = []
  354. mx = 0
  355. keys = list(compiler.executables.keys())
  356. for key in ['version', 'libraries', 'library_dirs',
  357. 'object_switch', 'compile_switch',
  358. 'include_dirs', 'define', 'undef', 'rpath', 'link_objects']:
  359. if key not in keys:
  360. keys.append(key)
  361. for key in keys:
  362. if hasattr(compiler, key):
  363. v = getattr(compiler, key)
  364. mx = max(mx, len(key))
  365. props.append((key, repr(v)))
  366. fmt = '%-' + repr(mx+1) + 's = %s'
  367. lines = [fmt % prop for prop in props]
  368. return '\n'.join(lines)
  369. def CCompiler_show_customization(self):
  370. """
  371. Print the compiler customizations to stdout.
  372. Parameters
  373. ----------
  374. None
  375. Returns
  376. -------
  377. None
  378. Notes
  379. -----
  380. Printing is only done if the distutils log threshold is < 2.
  381. """
  382. if 0:
  383. for attrname in ['include_dirs', 'define', 'undef',
  384. 'libraries', 'library_dirs',
  385. 'rpath', 'link_objects']:
  386. attr = getattr(self, attrname, None)
  387. if not attr:
  388. continue
  389. log.info("compiler '%s' is set to %s" % (attrname, attr))
  390. try:
  391. self.get_version()
  392. except Exception:
  393. pass
  394. if log._global_log.threshold<2:
  395. print('*'*80)
  396. print(self.__class__)
  397. print(_compiler_to_string(self))
  398. print('*'*80)
  399. replace_method(CCompiler, 'show_customization', CCompiler_show_customization)
  400. def CCompiler_customize(self, dist, need_cxx=0):
  401. """
  402. Do any platform-specific customization of a compiler instance.
  403. This method calls `distutils.sysconfig.customize_compiler` for
  404. platform-specific customization, as well as optionally remove a flag
  405. to suppress spurious warnings in case C++ code is being compiled.
  406. Parameters
  407. ----------
  408. dist : object
  409. This parameter is not used for anything.
  410. need_cxx : bool, optional
  411. Whether or not C++ has to be compiled. If so (True), the
  412. ``"-Wstrict-prototypes"`` option is removed to prevent spurious
  413. warnings. Default is False.
  414. Returns
  415. -------
  416. None
  417. Notes
  418. -----
  419. All the default options used by distutils can be extracted with::
  420. from distutils import sysconfig
  421. sysconfig.get_config_vars('CC', 'CXX', 'OPT', 'BASECFLAGS',
  422. 'CCSHARED', 'LDSHARED', 'SO')
  423. """
  424. # See FCompiler.customize for suggested usage.
  425. log.info('customize %s' % (self.__class__.__name__))
  426. customize_compiler(self)
  427. if need_cxx:
  428. # In general, distutils uses -Wstrict-prototypes, but this option is
  429. # not valid for C++ code, only for C. Remove it if it's there to
  430. # avoid a spurious warning on every compilation.
  431. try:
  432. self.compiler_so.remove('-Wstrict-prototypes')
  433. except (AttributeError, ValueError):
  434. pass
  435. if hasattr(self, 'compiler') and 'cc' in self.compiler[0]:
  436. if not self.compiler_cxx:
  437. if self.compiler[0].startswith('gcc'):
  438. a, b = 'gcc', 'g++'
  439. else:
  440. a, b = 'cc', 'c++'
  441. self.compiler_cxx = [self.compiler[0].replace(a, b)]\
  442. + self.compiler[1:]
  443. else:
  444. if hasattr(self, 'compiler'):
  445. log.warn("#### %s #######" % (self.compiler,))
  446. if not hasattr(self, 'compiler_cxx'):
  447. log.warn('Missing compiler_cxx fix for ' + self.__class__.__name__)
  448. # check if compiler supports gcc style automatic dependencies
  449. # run on every extension so skip for known good compilers
  450. if hasattr(self, 'compiler') and ('gcc' in self.compiler[0] or
  451. 'g++' in self.compiler[0] or
  452. 'clang' in self.compiler[0]):
  453. self._auto_depends = True
  454. elif os.name == 'posix':
  455. import tempfile
  456. import shutil
  457. tmpdir = tempfile.mkdtemp()
  458. try:
  459. fn = os.path.join(tmpdir, "file.c")
  460. with open(fn, "w") as f:
  461. f.write("int a;\n")
  462. self.compile([fn], output_dir=tmpdir,
  463. extra_preargs=['-MMD', '-MF', fn + '.d'])
  464. self._auto_depends = True
  465. except CompileError:
  466. self._auto_depends = False
  467. finally:
  468. shutil.rmtree(tmpdir)
  469. return
  470. replace_method(CCompiler, 'customize', CCompiler_customize)
  471. def simple_version_match(pat=r'[-.\d]+', ignore='', start=''):
  472. """
  473. Simple matching of version numbers, for use in CCompiler and FCompiler.
  474. Parameters
  475. ----------
  476. pat : str, optional
  477. A regular expression matching version numbers.
  478. Default is ``r'[-.\\d]+'``.
  479. ignore : str, optional
  480. A regular expression matching patterns to skip.
  481. Default is ``''``, in which case nothing is skipped.
  482. start : str, optional
  483. A regular expression matching the start of where to start looking
  484. for version numbers.
  485. Default is ``''``, in which case searching is started at the
  486. beginning of the version string given to `matcher`.
  487. Returns
  488. -------
  489. matcher : callable
  490. A function that is appropriate to use as the ``.version_match``
  491. attribute of a `CCompiler` class. `matcher` takes a single parameter,
  492. a version string.
  493. """
  494. def matcher(self, version_string):
  495. # version string may appear in the second line, so getting rid
  496. # of new lines:
  497. version_string = version_string.replace('\n', ' ')
  498. pos = 0
  499. if start:
  500. m = re.match(start, version_string)
  501. if not m:
  502. return None
  503. pos = m.end()
  504. while True:
  505. m = re.search(pat, version_string[pos:])
  506. if not m:
  507. return None
  508. if ignore and re.match(ignore, m.group(0)):
  509. pos = m.end()
  510. continue
  511. break
  512. return m.group(0)
  513. return matcher
  514. def CCompiler_get_version(self, force=False, ok_status=[0]):
  515. """
  516. Return compiler version, or None if compiler is not available.
  517. Parameters
  518. ----------
  519. force : bool, optional
  520. If True, force a new determination of the version, even if the
  521. compiler already has a version attribute. Default is False.
  522. ok_status : list of int, optional
  523. The list of status values returned by the version look-up process
  524. for which a version string is returned. If the status value is not
  525. in `ok_status`, None is returned. Default is ``[0]``.
  526. Returns
  527. -------
  528. version : str or None
  529. Version string, in the format of `distutils.version.LooseVersion`.
  530. """
  531. if not force and hasattr(self, 'version'):
  532. return self.version
  533. self.find_executables()
  534. try:
  535. version_cmd = self.version_cmd
  536. except AttributeError:
  537. return None
  538. if not version_cmd or not version_cmd[0]:
  539. return None
  540. try:
  541. matcher = self.version_match
  542. except AttributeError:
  543. try:
  544. pat = self.version_pattern
  545. except AttributeError:
  546. return None
  547. def matcher(version_string):
  548. m = re.match(pat, version_string)
  549. if not m:
  550. return None
  551. version = m.group('version')
  552. return version
  553. try:
  554. output = subprocess.check_output(version_cmd, stderr=subprocess.STDOUT)
  555. except subprocess.CalledProcessError as exc:
  556. output = exc.output
  557. status = exc.returncode
  558. except OSError:
  559. # match the historical returns for a parent
  560. # exception class caught by exec_command()
  561. status = 127
  562. output = b''
  563. else:
  564. # output isn't actually a filepath but we do this
  565. # for now to match previous distutils behavior
  566. output = filepath_from_subprocess_output(output)
  567. status = 0
  568. version = None
  569. if status in ok_status:
  570. version = matcher(output)
  571. if version:
  572. version = LooseVersion(version)
  573. self.version = version
  574. return version
  575. replace_method(CCompiler, 'get_version', CCompiler_get_version)
  576. def CCompiler_cxx_compiler(self):
  577. """
  578. Return the C++ compiler.
  579. Parameters
  580. ----------
  581. None
  582. Returns
  583. -------
  584. cxx : class instance
  585. The C++ compiler, as a `CCompiler` instance.
  586. """
  587. if self.compiler_type in ('msvc', 'intelw', 'intelemw'):
  588. return self
  589. cxx = copy(self)
  590. cxx.compiler_so = [cxx.compiler_cxx[0]] + cxx.compiler_so[1:]
  591. if sys.platform.startswith('aix') and 'ld_so_aix' in cxx.linker_so[0]:
  592. # AIX needs the ld_so_aix script included with Python
  593. cxx.linker_so = [cxx.linker_so[0], cxx.compiler_cxx[0]] \
  594. + cxx.linker_so[2:]
  595. else:
  596. cxx.linker_so = [cxx.compiler_cxx[0]] + cxx.linker_so[1:]
  597. return cxx
  598. replace_method(CCompiler, 'cxx_compiler', CCompiler_cxx_compiler)
  599. compiler_class['intel'] = ('intelccompiler', 'IntelCCompiler',
  600. "Intel C Compiler for 32-bit applications")
  601. compiler_class['intele'] = ('intelccompiler', 'IntelItaniumCCompiler',
  602. "Intel C Itanium Compiler for Itanium-based applications")
  603. compiler_class['intelem'] = ('intelccompiler', 'IntelEM64TCCompiler',
  604. "Intel C Compiler for 64-bit applications")
  605. compiler_class['intelw'] = ('intelccompiler', 'IntelCCompilerW',
  606. "Intel C Compiler for 32-bit applications on Windows")
  607. compiler_class['intelemw'] = ('intelccompiler', 'IntelEM64TCCompilerW',
  608. "Intel C Compiler for 64-bit applications on Windows")
  609. compiler_class['pathcc'] = ('pathccompiler', 'PathScaleCCompiler',
  610. "PathScale Compiler for SiCortex-based applications")
  611. ccompiler._default_compilers += (('linux.*', 'intel'),
  612. ('linux.*', 'intele'),
  613. ('linux.*', 'intelem'),
  614. ('linux.*', 'pathcc'),
  615. ('nt', 'intelw'),
  616. ('nt', 'intelemw'))
  617. if sys.platform == 'win32':
  618. compiler_class['mingw32'] = ('mingw32ccompiler', 'Mingw32CCompiler',
  619. "Mingw32 port of GNU C Compiler for Win32"\
  620. "(for MSC built Python)")
  621. if mingw32():
  622. # On windows platforms, we want to default to mingw32 (gcc)
  623. # because msvc can't build blitz stuff.
  624. log.info('Setting mingw32 as default compiler for nt.')
  625. ccompiler._default_compilers = (('nt', 'mingw32'),) \
  626. + ccompiler._default_compilers
  627. _distutils_new_compiler = new_compiler
  628. def new_compiler (plat=None,
  629. compiler=None,
  630. verbose=0,
  631. dry_run=0,
  632. force=0):
  633. # Try first C compilers from numpy.distutils.
  634. if plat is None:
  635. plat = os.name
  636. try:
  637. if compiler is None:
  638. compiler = get_default_compiler(plat)
  639. (module_name, class_name, long_description) = compiler_class[compiler]
  640. except KeyError:
  641. msg = "don't know how to compile C/C++ code on platform '%s'" % plat
  642. if compiler is not None:
  643. msg = msg + " with '%s' compiler" % compiler
  644. raise DistutilsPlatformError(msg)
  645. module_name = "numpy.distutils." + module_name
  646. try:
  647. __import__ (module_name)
  648. except ImportError:
  649. msg = str(get_exception())
  650. log.info('%s in numpy.distutils; trying from distutils',
  651. str(msg))
  652. module_name = module_name[6:]
  653. try:
  654. __import__(module_name)
  655. except ImportError:
  656. msg = str(get_exception())
  657. raise DistutilsModuleError("can't compile C/C++ code: unable to load module '%s'" % \
  658. module_name)
  659. try:
  660. module = sys.modules[module_name]
  661. klass = vars(module)[class_name]
  662. except KeyError:
  663. raise DistutilsModuleError(("can't compile C/C++ code: unable to find class '%s' " +
  664. "in module '%s'") % (class_name, module_name))
  665. compiler = klass(None, dry_run, force)
  666. log.debug('new_compiler returns %s' % (klass))
  667. return compiler
  668. ccompiler.new_compiler = new_compiler
  669. _distutils_gen_lib_options = gen_lib_options
  670. def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
  671. # the version of this function provided by CPython allows the following
  672. # to return lists, which are unpacked automatically:
  673. # - compiler.runtime_library_dir_option
  674. # our version extends the behavior to:
  675. # - compiler.library_dir_option
  676. # - compiler.library_option
  677. # - compiler.find_library_file
  678. r = _distutils_gen_lib_options(compiler, library_dirs,
  679. runtime_library_dirs, libraries)
  680. lib_opts = []
  681. for i in r:
  682. if is_sequence(i):
  683. lib_opts.extend(list(i))
  684. else:
  685. lib_opts.append(i)
  686. return lib_opts
  687. ccompiler.gen_lib_options = gen_lib_options
  688. # Also fix up the various compiler modules, which do
  689. # from distutils.ccompiler import gen_lib_options
  690. # Don't bother with mwerks, as we don't support Classic Mac.
  691. for _cc in ['msvc9', 'msvc', '_msvc', 'bcpp', 'cygwinc', 'emxc', 'unixc']:
  692. _m = sys.modules.get('distutils.' + _cc + 'compiler')
  693. if _m is not None:
  694. setattr(_m, 'gen_lib_options', gen_lib_options)