ccompiler.py 27 KB

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