m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

805 lines
27 KiB

6 months ago
  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 (
  24. cyg2win32, is_sequence, mingw32, get_num_build_jobs,
  25. _commandline_dep_string, sanitize_cxx_flags
  26. )
  27. # globals for parallel build management
  28. import 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. # Py3k does not have unbound method anymore, MethodType does not work
  74. m = lambda self, *args, **kw: func(self, *args, **kw)
  75. setattr(klass, method_name, m)
  76. ######################################################################
  77. ## Method that subclasses may redefine. But don't call this method,
  78. ## it i private to CCompiler class and may return unexpected
  79. ## results if used elsewhere. So, you have been warned..
  80. def CCompiler_find_executables(self):
  81. """
  82. Does nothing here, but is called by the get_version method and can be
  83. overridden by subclasses. In particular it is redefined in the `FCompiler`
  84. class where more documentation can be found.
  85. """
  86. pass
  87. replace_method(CCompiler, 'find_executables', CCompiler_find_executables)
  88. # Using customized CCompiler.spawn.
  89. def CCompiler_spawn(self, cmd, display=None):
  90. """
  91. Execute a command in a sub-process.
  92. Parameters
  93. ----------
  94. cmd : str
  95. The command to execute.
  96. display : str or sequence of str, optional
  97. The text to add to the log file kept by `numpy.distutils`.
  98. If not given, `display` is equal to `cmd`.
  99. Returns
  100. -------
  101. None
  102. Raises
  103. ------
  104. DistutilsExecError
  105. If the command failed, i.e. the exit status was not 0.
  106. """
  107. if display is None:
  108. display = cmd
  109. if is_sequence(display):
  110. display = ' '.join(list(display))
  111. log.info(display)
  112. try:
  113. if self.verbose:
  114. subprocess.check_output(cmd)
  115. else:
  116. subprocess.check_output(cmd, stderr=subprocess.STDOUT)
  117. except subprocess.CalledProcessError as exc:
  118. o = exc.output
  119. s = exc.returncode
  120. except OSError as e:
  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. # still that would make the end-user lost in translation!
  127. o = f"\n\n{e}\n\n\n"
  128. try:
  129. o = o.encode(sys.stdout.encoding)
  130. except AttributeError:
  131. o = o.encode('utf8')
  132. # status previously used by exec_command() for parent
  133. # of OSError
  134. s = 127
  135. else:
  136. # use a convenience return here so that any kind of
  137. # caught exception will execute the default code after the
  138. # try / except block, which handles various exceptions
  139. return None
  140. if is_sequence(cmd):
  141. cmd = ' '.join(list(cmd))
  142. if self.verbose:
  143. forward_bytes_to_stdout(o)
  144. if re.search(b'Too many open files', o):
  145. msg = '\nTry rerunning setup command until build succeeds.'
  146. else:
  147. msg = ''
  148. raise DistutilsExecError('Command "%s" failed with exit status %d%s' %
  149. (cmd, s, msg))
  150. replace_method(CCompiler, 'spawn', CCompiler_spawn)
  151. def CCompiler_object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
  152. """
  153. Return the name of the object files for the given source files.
  154. Parameters
  155. ----------
  156. source_filenames : list of str
  157. The list of paths to source files. Paths can be either relative or
  158. absolute, this is handled transparently.
  159. strip_dir : bool, optional
  160. Whether to strip the directory from the returned paths. If True,
  161. the file name prepended by `output_dir` is returned. Default is False.
  162. output_dir : str, optional
  163. If given, this path is prepended to the returned paths to the
  164. object files.
  165. Returns
  166. -------
  167. obj_names : list of str
  168. The list of paths to the object files corresponding to the source
  169. files in `source_filenames`.
  170. """
  171. if output_dir is None:
  172. output_dir = ''
  173. obj_names = []
  174. for src_name in source_filenames:
  175. base, ext = os.path.splitext(os.path.normpath(src_name))
  176. base = os.path.splitdrive(base)[1] # Chop off the drive
  177. base = base[os.path.isabs(base):] # If abs, chop off leading /
  178. if base.startswith('..'):
  179. # Resolve starting relative path components, middle ones
  180. # (if any) have been handled by os.path.normpath above.
  181. i = base.rfind('..')+2
  182. d = base[:i]
  183. d = os.path.basename(os.path.abspath(d))
  184. base = d + base[i:]
  185. if ext not in self.src_extensions:
  186. raise UnknownFileError("unknown file type '%s' (from '%s')" % (ext, src_name))
  187. if strip_dir:
  188. base = os.path.basename(base)
  189. obj_name = os.path.join(output_dir, base + self.obj_extension)
  190. obj_names.append(obj_name)
  191. return obj_names
  192. replace_method(CCompiler, 'object_filenames', CCompiler_object_filenames)
  193. def CCompiler_compile(self, sources, output_dir=None, macros=None,
  194. include_dirs=None, debug=0, extra_preargs=None,
  195. extra_postargs=None, depends=None):
  196. """
  197. Compile one or more source files.
  198. Please refer to the Python distutils API reference for more details.
  199. Parameters
  200. ----------
  201. sources : list of str
  202. A list of filenames
  203. output_dir : str, optional
  204. Path to the output directory.
  205. macros : list of tuples
  206. A list of macro definitions.
  207. include_dirs : list of str, optional
  208. The directories to add to the default include file search path for
  209. this compilation only.
  210. debug : bool, optional
  211. Whether or not to output debug symbols in or alongside the object
  212. file(s).
  213. extra_preargs, extra_postargs : ?
  214. Extra pre- and post-arguments.
  215. depends : list of str, optional
  216. A list of file names that all targets depend on.
  217. Returns
  218. -------
  219. objects : list of str
  220. A list of object file names, one per source file `sources`.
  221. Raises
  222. ------
  223. CompileError
  224. If compilation fails.
  225. """
  226. # This method is effective only with Python >=2.3 distutils.
  227. # Any changes here should be applied also to fcompiler.compile
  228. # method to support pre Python 2.3 distutils.
  229. global _job_semaphore
  230. jobs = get_num_build_jobs()
  231. # setup semaphore to not exceed number of compile jobs when parallelized at
  232. # extension level (python >= 3.5)
  233. with _global_lock:
  234. if _job_semaphore is None:
  235. _job_semaphore = threading.Semaphore(jobs)
  236. if not sources:
  237. return []
  238. from numpy.distutils.fcompiler import (FCompiler, is_f_file,
  239. has_f90_header)
  240. if isinstance(self, FCompiler):
  241. display = []
  242. for fc in ['f77', 'f90', 'fix']:
  243. fcomp = getattr(self, 'compiler_'+fc)
  244. if fcomp is None:
  245. continue
  246. display.append("Fortran %s compiler: %s" % (fc, ' '.join(fcomp)))
  247. display = '\n'.join(display)
  248. else:
  249. ccomp = self.compiler_so
  250. display = "C compiler: %s\n" % (' '.join(ccomp),)
  251. log.info(display)
  252. macros, objects, extra_postargs, pp_opts, build = \
  253. self._setup_compile(output_dir, macros, include_dirs, sources,
  254. depends, extra_postargs)
  255. cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
  256. display = "compile options: '%s'" % (' '.join(cc_args))
  257. if extra_postargs:
  258. display += "\nextra options: '%s'" % (' '.join(extra_postargs))
  259. log.info(display)
  260. def single_compile(args):
  261. obj, (src, ext) = args
  262. if not _needs_build(obj, cc_args, extra_postargs, pp_opts):
  263. return
  264. # check if we are currently already processing the same object
  265. # happens when using the same source in multiple extensions
  266. while True:
  267. # need explicit lock as there is no atomic check and add with GIL
  268. with _global_lock:
  269. # file not being worked on, start working
  270. if obj not in _processing_files:
  271. _processing_files.add(obj)
  272. break
  273. # wait for the processing to end
  274. time.sleep(0.1)
  275. try:
  276. # retrieve slot from our #job semaphore and build
  277. with _job_semaphore:
  278. self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
  279. finally:
  280. # register being done processing
  281. with _global_lock:
  282. _processing_files.remove(obj)
  283. if isinstance(self, FCompiler):
  284. objects_to_build = list(build.keys())
  285. f77_objects, other_objects = [], []
  286. for obj in objects:
  287. if obj in objects_to_build:
  288. src, ext = build[obj]
  289. if self.compiler_type=='absoft':
  290. obj = cyg2win32(obj)
  291. src = cyg2win32(src)
  292. if is_f_file(src) and not has_f90_header(src):
  293. f77_objects.append((obj, (src, ext)))
  294. else:
  295. other_objects.append((obj, (src, ext)))
  296. # f77 objects can be built in parallel
  297. build_items = f77_objects
  298. # build f90 modules serial, module files are generated during
  299. # compilation and may be used by files later in the list so the
  300. # ordering is important
  301. for o in other_objects:
  302. single_compile(o)
  303. else:
  304. build_items = build.items()
  305. if len(build) > 1 and jobs > 1:
  306. # build parallel
  307. import multiprocessing.pool
  308. pool = multiprocessing.pool.ThreadPool(jobs)
  309. pool.map(single_compile, build_items)
  310. pool.close()
  311. else:
  312. # build serial
  313. for o in build_items:
  314. single_compile(o)
  315. # Return *all* object filenames, not just the ones we just built.
  316. return objects
  317. replace_method(CCompiler, 'compile', CCompiler_compile)
  318. def CCompiler_customize_cmd(self, cmd, ignore=()):
  319. """
  320. Customize compiler using distutils command.
  321. Parameters
  322. ----------
  323. cmd : class instance
  324. An instance inheriting from `distutils.cmd.Command`.
  325. ignore : sequence of str, optional
  326. List of `CCompiler` commands (without ``'set_'``) that should not be
  327. altered. Strings that are checked for are:
  328. ``('include_dirs', 'define', 'undef', 'libraries', 'library_dirs',
  329. 'rpath', 'link_objects')``.
  330. Returns
  331. -------
  332. None
  333. """
  334. log.info('customize %s using %s' % (self.__class__.__name__,
  335. cmd.__class__.__name__))
  336. if hasattr(self, 'compiler') and 'clang' in self.compiler[0]:
  337. # clang defaults to a non-strict floating error point model.
  338. # Since NumPy and most Python libs give warnings for these, override:
  339. self.compiler.append('-ffp-exception-behavior=strict')
  340. def allow(attr):
  341. return getattr(cmd, attr, None) is not None and attr not in ignore
  342. if allow('include_dirs'):
  343. self.set_include_dirs(cmd.include_dirs)
  344. if allow('define'):
  345. for (name, value) in cmd.define:
  346. self.define_macro(name, value)
  347. if allow('undef'):
  348. for macro in cmd.undef:
  349. self.undefine_macro(macro)
  350. if allow('libraries'):
  351. self.set_libraries(self.libraries + cmd.libraries)
  352. if allow('library_dirs'):
  353. self.set_library_dirs(self.library_dirs + cmd.library_dirs)
  354. if allow('rpath'):
  355. self.set_runtime_library_dirs(cmd.rpath)
  356. if allow('link_objects'):
  357. self.set_link_objects(cmd.link_objects)
  358. replace_method(CCompiler, 'customize_cmd', CCompiler_customize_cmd)
  359. def _compiler_to_string(compiler):
  360. props = []
  361. mx = 0
  362. keys = list(compiler.executables.keys())
  363. for key in ['version', 'libraries', 'library_dirs',
  364. 'object_switch', 'compile_switch',
  365. 'include_dirs', 'define', 'undef', 'rpath', 'link_objects']:
  366. if key not in keys:
  367. keys.append(key)
  368. for key in keys:
  369. if hasattr(compiler, key):
  370. v = getattr(compiler, key)
  371. mx = max(mx, len(key))
  372. props.append((key, repr(v)))
  373. fmt = '%-' + repr(mx+1) + 's = %s'
  374. lines = [fmt % prop for prop in props]
  375. return '\n'.join(lines)
  376. def CCompiler_show_customization(self):
  377. """
  378. Print the compiler customizations to stdout.
  379. Parameters
  380. ----------
  381. None
  382. Returns
  383. -------
  384. None
  385. Notes
  386. -----
  387. Printing is only done if the distutils log threshold is < 2.
  388. """
  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_cxx = cxx.compiler_cxx
  590. cxx.compiler_so = [cxx.compiler_cxx[0]] + \
  591. sanitize_cxx_flags(cxx.compiler_so[1:])
  592. #cxx.compiler_so = [cxx.compiler_cxx[0]] + cxx.compiler_so[1:]
  593. if sys.platform.startswith('aix') and 'ld_so_aix' in cxx.linker_so[0]:
  594. # AIX needs the ld_so_aix script included with Python
  595. cxx.linker_so = [cxx.linker_so[0], cxx.compiler_cxx[0]] \
  596. + cxx.linker_so[2:]
  597. else:
  598. cxx.linker_so = [cxx.compiler_cxx[0]] + cxx.linker_so[1:]
  599. return cxx
  600. replace_method(CCompiler, 'cxx_compiler', CCompiler_cxx_compiler)
  601. compiler_class['intel'] = ('intelccompiler', 'IntelCCompiler',
  602. "Intel C Compiler for 32-bit applications")
  603. compiler_class['intele'] = ('intelccompiler', 'IntelItaniumCCompiler',
  604. "Intel C Itanium Compiler for Itanium-based applications")
  605. compiler_class['intelem'] = ('intelccompiler', 'IntelEM64TCCompiler',
  606. "Intel C Compiler for 64-bit applications")
  607. compiler_class['intelw'] = ('intelccompiler', 'IntelCCompilerW',
  608. "Intel C Compiler for 32-bit applications on Windows")
  609. compiler_class['intelemw'] = ('intelccompiler', 'IntelEM64TCCompilerW',
  610. "Intel C Compiler for 64-bit applications on Windows")
  611. compiler_class['pathcc'] = ('pathccompiler', 'PathScaleCCompiler',
  612. "PathScale Compiler for SiCortex-based applications")
  613. ccompiler._default_compilers += (('linux.*', 'intel'),
  614. ('linux.*', 'intele'),
  615. ('linux.*', 'intelem'),
  616. ('linux.*', 'pathcc'),
  617. ('nt', 'intelw'),
  618. ('nt', 'intelemw'))
  619. if sys.platform == 'win32':
  620. compiler_class['mingw32'] = ('mingw32ccompiler', 'Mingw32CCompiler',
  621. "Mingw32 port of GNU C Compiler for Win32"\
  622. "(for MSC built Python)")
  623. if mingw32():
  624. # On windows platforms, we want to default to mingw32 (gcc)
  625. # because msvc can't build blitz stuff.
  626. log.info('Setting mingw32 as default compiler for nt.')
  627. ccompiler._default_compilers = (('nt', 'mingw32'),) \
  628. + ccompiler._default_compilers
  629. _distutils_new_compiler = new_compiler
  630. def new_compiler (plat=None,
  631. compiler=None,
  632. verbose=None,
  633. dry_run=0,
  634. force=0):
  635. # Try first C compilers from numpy.distutils.
  636. if verbose is None:
  637. verbose = log.get_threshold() <= log.INFO
  638. if plat is None:
  639. plat = os.name
  640. try:
  641. if compiler is None:
  642. compiler = get_default_compiler(plat)
  643. (module_name, class_name, long_description) = compiler_class[compiler]
  644. except KeyError:
  645. msg = "don't know how to compile C/C++ code on platform '%s'" % plat
  646. if compiler is not None:
  647. msg = msg + " with '%s' compiler" % compiler
  648. raise DistutilsPlatformError(msg)
  649. module_name = "numpy.distutils." + module_name
  650. try:
  651. __import__ (module_name)
  652. except ImportError as e:
  653. msg = str(e)
  654. log.info('%s in numpy.distutils; trying from distutils',
  655. str(msg))
  656. module_name = module_name[6:]
  657. try:
  658. __import__(module_name)
  659. except ImportError as e:
  660. msg = str(e)
  661. raise DistutilsModuleError("can't compile C/C++ code: unable to load module '%s'" % \
  662. module_name)
  663. try:
  664. module = sys.modules[module_name]
  665. klass = vars(module)[class_name]
  666. except KeyError:
  667. raise DistutilsModuleError(("can't compile C/C++ code: unable to find class '%s' " +
  668. "in module '%s'") % (class_name, module_name))
  669. compiler = klass(None, dry_run, force)
  670. compiler.verbose = verbose
  671. log.debug('new_compiler returns %s' % (klass))
  672. return compiler
  673. ccompiler.new_compiler = new_compiler
  674. _distutils_gen_lib_options = gen_lib_options
  675. def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
  676. # the version of this function provided by CPython allows the following
  677. # to return lists, which are unpacked automatically:
  678. # - compiler.runtime_library_dir_option
  679. # our version extends the behavior to:
  680. # - compiler.library_dir_option
  681. # - compiler.library_option
  682. # - compiler.find_library_file
  683. r = _distutils_gen_lib_options(compiler, library_dirs,
  684. runtime_library_dirs, libraries)
  685. lib_opts = []
  686. for i in r:
  687. if is_sequence(i):
  688. lib_opts.extend(list(i))
  689. else:
  690. lib_opts.append(i)
  691. return lib_opts
  692. ccompiler.gen_lib_options = gen_lib_options
  693. # Also fix up the various compiler modules, which do
  694. # from distutils.ccompiler import gen_lib_options
  695. # Don't bother with mwerks, as we don't support Classic Mac.
  696. for _cc in ['msvc9', 'msvc', '_msvc', 'bcpp', 'cygwinc', 'emxc', 'unixc']:
  697. _m = sys.modules.get('distutils.' + _cc + 'compiler')
  698. if _m is not None:
  699. setattr(_m, 'gen_lib_options', gen_lib_options)