ccompiler.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. try:
  382. self.get_version()
  383. except Exception:
  384. pass
  385. if log._global_log.threshold<2:
  386. print('*'*80)
  387. print(self.__class__)
  388. print(_compiler_to_string(self))
  389. print('*'*80)
  390. replace_method(CCompiler, 'show_customization', CCompiler_show_customization)
  391. def CCompiler_customize(self, dist, need_cxx=0):
  392. """
  393. Do any platform-specific customization of a compiler instance.
  394. This method calls `distutils.sysconfig.customize_compiler` for
  395. platform-specific customization, as well as optionally remove a flag
  396. to suppress spurious warnings in case C++ code is being compiled.
  397. Parameters
  398. ----------
  399. dist : object
  400. This parameter is not used for anything.
  401. need_cxx : bool, optional
  402. Whether or not C++ has to be compiled. If so (True), the
  403. ``"-Wstrict-prototypes"`` option is removed to prevent spurious
  404. warnings. Default is False.
  405. Returns
  406. -------
  407. None
  408. Notes
  409. -----
  410. All the default options used by distutils can be extracted with::
  411. from distutils import sysconfig
  412. sysconfig.get_config_vars('CC', 'CXX', 'OPT', 'BASECFLAGS',
  413. 'CCSHARED', 'LDSHARED', 'SO')
  414. """
  415. # See FCompiler.customize for suggested usage.
  416. log.info('customize %s' % (self.__class__.__name__))
  417. customize_compiler(self)
  418. if need_cxx:
  419. # In general, distutils uses -Wstrict-prototypes, but this option is
  420. # not valid for C++ code, only for C. Remove it if it's there to
  421. # avoid a spurious warning on every compilation.
  422. try:
  423. self.compiler_so.remove('-Wstrict-prototypes')
  424. except (AttributeError, ValueError):
  425. pass
  426. if hasattr(self, 'compiler') and 'cc' in self.compiler[0]:
  427. if not self.compiler_cxx:
  428. if self.compiler[0].startswith('gcc'):
  429. a, b = 'gcc', 'g++'
  430. else:
  431. a, b = 'cc', 'c++'
  432. self.compiler_cxx = [self.compiler[0].replace(a, b)]\
  433. + self.compiler[1:]
  434. else:
  435. if hasattr(self, 'compiler'):
  436. log.warn("#### %s #######" % (self.compiler,))
  437. if not hasattr(self, 'compiler_cxx'):
  438. log.warn('Missing compiler_cxx fix for ' + self.__class__.__name__)
  439. # check if compiler supports gcc style automatic dependencies
  440. # run on every extension so skip for known good compilers
  441. if hasattr(self, 'compiler') and ('gcc' in self.compiler[0] or
  442. 'g++' in self.compiler[0] or
  443. 'clang' in self.compiler[0]):
  444. self._auto_depends = True
  445. elif os.name == 'posix':
  446. import tempfile
  447. import shutil
  448. tmpdir = tempfile.mkdtemp()
  449. try:
  450. fn = os.path.join(tmpdir, "file.c")
  451. with open(fn, "w") as f:
  452. f.write("int a;\n")
  453. self.compile([fn], output_dir=tmpdir,
  454. extra_preargs=['-MMD', '-MF', fn + '.d'])
  455. self._auto_depends = True
  456. except CompileError:
  457. self._auto_depends = False
  458. finally:
  459. shutil.rmtree(tmpdir)
  460. return
  461. replace_method(CCompiler, 'customize', CCompiler_customize)
  462. def simple_version_match(pat=r'[-.\d]+', ignore='', start=''):
  463. """
  464. Simple matching of version numbers, for use in CCompiler and FCompiler.
  465. Parameters
  466. ----------
  467. pat : str, optional
  468. A regular expression matching version numbers.
  469. Default is ``r'[-.\\d]+'``.
  470. ignore : str, optional
  471. A regular expression matching patterns to skip.
  472. Default is ``''``, in which case nothing is skipped.
  473. start : str, optional
  474. A regular expression matching the start of where to start looking
  475. for version numbers.
  476. Default is ``''``, in which case searching is started at the
  477. beginning of the version string given to `matcher`.
  478. Returns
  479. -------
  480. matcher : callable
  481. A function that is appropriate to use as the ``.version_match``
  482. attribute of a `CCompiler` class. `matcher` takes a single parameter,
  483. a version string.
  484. """
  485. def matcher(self, version_string):
  486. # version string may appear in the second line, so getting rid
  487. # of new lines:
  488. version_string = version_string.replace('\n', ' ')
  489. pos = 0
  490. if start:
  491. m = re.match(start, version_string)
  492. if not m:
  493. return None
  494. pos = m.end()
  495. while True:
  496. m = re.search(pat, version_string[pos:])
  497. if not m:
  498. return None
  499. if ignore and re.match(ignore, m.group(0)):
  500. pos = m.end()
  501. continue
  502. break
  503. return m.group(0)
  504. return matcher
  505. def CCompiler_get_version(self, force=False, ok_status=[0]):
  506. """
  507. Return compiler version, or None if compiler is not available.
  508. Parameters
  509. ----------
  510. force : bool, optional
  511. If True, force a new determination of the version, even if the
  512. compiler already has a version attribute. Default is False.
  513. ok_status : list of int, optional
  514. The list of status values returned by the version look-up process
  515. for which a version string is returned. If the status value is not
  516. in `ok_status`, None is returned. Default is ``[0]``.
  517. Returns
  518. -------
  519. version : str or None
  520. Version string, in the format of `distutils.version.LooseVersion`.
  521. """
  522. if not force and hasattr(self, 'version'):
  523. return self.version
  524. self.find_executables()
  525. try:
  526. version_cmd = self.version_cmd
  527. except AttributeError:
  528. return None
  529. if not version_cmd or not version_cmd[0]:
  530. return None
  531. try:
  532. matcher = self.version_match
  533. except AttributeError:
  534. try:
  535. pat = self.version_pattern
  536. except AttributeError:
  537. return None
  538. def matcher(version_string):
  539. m = re.match(pat, version_string)
  540. if not m:
  541. return None
  542. version = m.group('version')
  543. return version
  544. try:
  545. output = subprocess.check_output(version_cmd, stderr=subprocess.STDOUT)
  546. except subprocess.CalledProcessError as exc:
  547. output = exc.output
  548. status = exc.returncode
  549. except OSError:
  550. # match the historical returns for a parent
  551. # exception class caught by exec_command()
  552. status = 127
  553. output = b''
  554. else:
  555. # output isn't actually a filepath but we do this
  556. # for now to match previous distutils behavior
  557. output = filepath_from_subprocess_output(output)
  558. status = 0
  559. version = None
  560. if status in ok_status:
  561. version = matcher(output)
  562. if version:
  563. version = LooseVersion(version)
  564. self.version = version
  565. return version
  566. replace_method(CCompiler, 'get_version', CCompiler_get_version)
  567. def CCompiler_cxx_compiler(self):
  568. """
  569. Return the C++ compiler.
  570. Parameters
  571. ----------
  572. None
  573. Returns
  574. -------
  575. cxx : class instance
  576. The C++ compiler, as a `CCompiler` instance.
  577. """
  578. if self.compiler_type in ('msvc', 'intelw', 'intelemw'):
  579. return self
  580. cxx = copy(self)
  581. cxx.compiler_so = [cxx.compiler_cxx[0]] + cxx.compiler_so[1:]
  582. if sys.platform.startswith('aix') and 'ld_so_aix' in cxx.linker_so[0]:
  583. # AIX needs the ld_so_aix script included with Python
  584. cxx.linker_so = [cxx.linker_so[0], cxx.compiler_cxx[0]] \
  585. + cxx.linker_so[2:]
  586. else:
  587. cxx.linker_so = [cxx.compiler_cxx[0]] + cxx.linker_so[1:]
  588. return cxx
  589. replace_method(CCompiler, 'cxx_compiler', CCompiler_cxx_compiler)
  590. compiler_class['intel'] = ('intelccompiler', 'IntelCCompiler',
  591. "Intel C Compiler for 32-bit applications")
  592. compiler_class['intele'] = ('intelccompiler', 'IntelItaniumCCompiler',
  593. "Intel C Itanium Compiler for Itanium-based applications")
  594. compiler_class['intelem'] = ('intelccompiler', 'IntelEM64TCCompiler',
  595. "Intel C Compiler for 64-bit applications")
  596. compiler_class['intelw'] = ('intelccompiler', 'IntelCCompilerW',
  597. "Intel C Compiler for 32-bit applications on Windows")
  598. compiler_class['intelemw'] = ('intelccompiler', 'IntelEM64TCCompilerW',
  599. "Intel C Compiler for 64-bit applications on Windows")
  600. compiler_class['pathcc'] = ('pathccompiler', 'PathScaleCCompiler',
  601. "PathScale Compiler for SiCortex-based applications")
  602. ccompiler._default_compilers += (('linux.*', 'intel'),
  603. ('linux.*', 'intele'),
  604. ('linux.*', 'intelem'),
  605. ('linux.*', 'pathcc'),
  606. ('nt', 'intelw'),
  607. ('nt', 'intelemw'))
  608. if sys.platform == 'win32':
  609. compiler_class['mingw32'] = ('mingw32ccompiler', 'Mingw32CCompiler',
  610. "Mingw32 port of GNU C Compiler for Win32"\
  611. "(for MSC built Python)")
  612. if mingw32():
  613. # On windows platforms, we want to default to mingw32 (gcc)
  614. # because msvc can't build blitz stuff.
  615. log.info('Setting mingw32 as default compiler for nt.')
  616. ccompiler._default_compilers = (('nt', 'mingw32'),) \
  617. + ccompiler._default_compilers
  618. _distutils_new_compiler = new_compiler
  619. def new_compiler (plat=None,
  620. compiler=None,
  621. verbose=None,
  622. dry_run=0,
  623. force=0):
  624. # Try first C compilers from numpy.distutils.
  625. if verbose is None:
  626. verbose = log.get_threshold() <= log.INFO
  627. if plat is None:
  628. plat = os.name
  629. try:
  630. if compiler is None:
  631. compiler = get_default_compiler(plat)
  632. (module_name, class_name, long_description) = compiler_class[compiler]
  633. except KeyError:
  634. msg = "don't know how to compile C/C++ code on platform '%s'" % plat
  635. if compiler is not None:
  636. msg = msg + " with '%s' compiler" % compiler
  637. raise DistutilsPlatformError(msg)
  638. module_name = "numpy.distutils." + module_name
  639. try:
  640. __import__ (module_name)
  641. except ImportError as e:
  642. msg = str(e)
  643. log.info('%s in numpy.distutils; trying from distutils',
  644. str(msg))
  645. module_name = module_name[6:]
  646. try:
  647. __import__(module_name)
  648. except ImportError as e:
  649. msg = str(e)
  650. raise DistutilsModuleError("can't compile C/C++ code: unable to load module '%s'" % \
  651. module_name)
  652. try:
  653. module = sys.modules[module_name]
  654. klass = vars(module)[class_name]
  655. except KeyError:
  656. raise DistutilsModuleError(("can't compile C/C++ code: unable to find class '%s' " +
  657. "in module '%s'") % (class_name, module_name))
  658. compiler = klass(None, dry_run, force)
  659. compiler.verbose = verbose
  660. log.debug('new_compiler returns %s' % (klass))
  661. return compiler
  662. ccompiler.new_compiler = new_compiler
  663. _distutils_gen_lib_options = gen_lib_options
  664. def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
  665. # the version of this function provided by CPython allows the following
  666. # to return lists, which are unpacked automatically:
  667. # - compiler.runtime_library_dir_option
  668. # our version extends the behavior to:
  669. # - compiler.library_dir_option
  670. # - compiler.library_option
  671. # - compiler.find_library_file
  672. r = _distutils_gen_lib_options(compiler, library_dirs,
  673. runtime_library_dirs, libraries)
  674. lib_opts = []
  675. for i in r:
  676. if is_sequence(i):
  677. lib_opts.extend(list(i))
  678. else:
  679. lib_opts.append(i)
  680. return lib_opts
  681. ccompiler.gen_lib_options = gen_lib_options
  682. # Also fix up the various compiler modules, which do
  683. # from distutils.ccompiler import gen_lib_options
  684. # Don't bother with mwerks, as we don't support Classic Mac.
  685. for _cc in ['msvc9', 'msvc', '_msvc', 'bcpp', 'cygwinc', 'emxc', 'unixc']:
  686. _m = sys.modules.get('distutils.' + _cc + 'compiler')
  687. if _m is not None:
  688. setattr(_m, 'gen_lib_options', gen_lib_options)