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.

773 lines
30 KiB

6 months ago
  1. """ Build swig and f2py sources.
  2. """
  3. import os
  4. import re
  5. import sys
  6. import shlex
  7. import copy
  8. from distutils.command import build_ext
  9. from distutils.dep_util import newer_group, newer
  10. from distutils.util import get_platform
  11. from distutils.errors import DistutilsError, DistutilsSetupError
  12. # this import can't be done here, as it uses numpy stuff only available
  13. # after it's installed
  14. #import numpy.f2py
  15. from numpy.distutils import log
  16. from numpy.distutils.misc_util import (
  17. fortran_ext_match, appendpath, is_string, is_sequence, get_cmd
  18. )
  19. from numpy.distutils.from_template import process_file as process_f_file
  20. from numpy.distutils.conv_template import process_file as process_c_file
  21. def subst_vars(target, source, d):
  22. """Substitute any occurrence of @foo@ by d['foo'] from source file into
  23. target."""
  24. var = re.compile('@([a-zA-Z_]+)@')
  25. with open(source, 'r') as fs:
  26. with open(target, 'w') as ft:
  27. for l in fs:
  28. m = var.search(l)
  29. if m:
  30. ft.write(l.replace('@%s@' % m.group(1), d[m.group(1)]))
  31. else:
  32. ft.write(l)
  33. class build_src(build_ext.build_ext):
  34. description = "build sources from SWIG, F2PY files or a function"
  35. user_options = [
  36. ('build-src=', 'd', "directory to \"build\" sources to"),
  37. ('f2py-opts=', None, "list of f2py command line options"),
  38. ('swig=', None, "path to the SWIG executable"),
  39. ('swig-opts=', None, "list of SWIG command line options"),
  40. ('swig-cpp', None, "make SWIG create C++ files (default is autodetected from sources)"),
  41. ('f2pyflags=', None, "additional flags to f2py (use --f2py-opts= instead)"), # obsolete
  42. ('swigflags=', None, "additional flags to swig (use --swig-opts= instead)"), # obsolete
  43. ('force', 'f', "forcibly build everything (ignore file timestamps)"),
  44. ('inplace', 'i',
  45. "ignore build-lib and put compiled extensions into the source " +
  46. "directory alongside your pure Python modules"),
  47. ('verbose-cfg', None,
  48. "change logging level from WARN to INFO which will show all " +
  49. "compiler output")
  50. ]
  51. boolean_options = ['force', 'inplace', 'verbose-cfg']
  52. help_options = []
  53. def initialize_options(self):
  54. self.extensions = None
  55. self.package = None
  56. self.py_modules = None
  57. self.py_modules_dict = None
  58. self.build_src = None
  59. self.build_lib = None
  60. self.build_base = None
  61. self.force = None
  62. self.inplace = None
  63. self.package_dir = None
  64. self.f2pyflags = None # obsolete
  65. self.f2py_opts = None
  66. self.swigflags = None # obsolete
  67. self.swig_opts = None
  68. self.swig_cpp = None
  69. self.swig = None
  70. self.verbose_cfg = None
  71. def finalize_options(self):
  72. self.set_undefined_options('build',
  73. ('build_base', 'build_base'),
  74. ('build_lib', 'build_lib'),
  75. ('force', 'force'))
  76. if self.package is None:
  77. self.package = self.distribution.ext_package
  78. self.extensions = self.distribution.ext_modules
  79. self.libraries = self.distribution.libraries or []
  80. self.py_modules = self.distribution.py_modules or []
  81. self.data_files = self.distribution.data_files or []
  82. if self.build_src is None:
  83. plat_specifier = ".{}-{}.{}".format(get_platform(), *sys.version_info[:2])
  84. self.build_src = os.path.join(self.build_base, 'src'+plat_specifier)
  85. # py_modules_dict is used in build_py.find_package_modules
  86. self.py_modules_dict = {}
  87. if self.f2pyflags:
  88. if self.f2py_opts:
  89. log.warn('ignoring --f2pyflags as --f2py-opts already used')
  90. else:
  91. self.f2py_opts = self.f2pyflags
  92. self.f2pyflags = None
  93. if self.f2py_opts is None:
  94. self.f2py_opts = []
  95. else:
  96. self.f2py_opts = shlex.split(self.f2py_opts)
  97. if self.swigflags:
  98. if self.swig_opts:
  99. log.warn('ignoring --swigflags as --swig-opts already used')
  100. else:
  101. self.swig_opts = self.swigflags
  102. self.swigflags = None
  103. if self.swig_opts is None:
  104. self.swig_opts = []
  105. else:
  106. self.swig_opts = shlex.split(self.swig_opts)
  107. # use options from build_ext command
  108. build_ext = self.get_finalized_command('build_ext')
  109. if self.inplace is None:
  110. self.inplace = build_ext.inplace
  111. if self.swig_cpp is None:
  112. self.swig_cpp = build_ext.swig_cpp
  113. for c in ['swig', 'swig_opt']:
  114. o = '--'+c.replace('_', '-')
  115. v = getattr(build_ext, c, None)
  116. if v:
  117. if getattr(self, c):
  118. log.warn('both build_src and build_ext define %s option' % (o))
  119. else:
  120. log.info('using "%s=%s" option from build_ext command' % (o, v))
  121. setattr(self, c, v)
  122. def run(self):
  123. log.info("build_src")
  124. if not (self.extensions or self.libraries):
  125. return
  126. self.build_sources()
  127. def build_sources(self):
  128. if self.inplace:
  129. self.get_package_dir = \
  130. self.get_finalized_command('build_py').get_package_dir
  131. self.build_py_modules_sources()
  132. for libname_info in self.libraries:
  133. self.build_library_sources(*libname_info)
  134. if self.extensions:
  135. self.check_extensions_list(self.extensions)
  136. for ext in self.extensions:
  137. self.build_extension_sources(ext)
  138. self.build_data_files_sources()
  139. self.build_npy_pkg_config()
  140. def build_data_files_sources(self):
  141. if not self.data_files:
  142. return
  143. log.info('building data_files sources')
  144. from numpy.distutils.misc_util import get_data_files
  145. new_data_files = []
  146. for data in self.data_files:
  147. if isinstance(data, str):
  148. new_data_files.append(data)
  149. elif isinstance(data, tuple):
  150. d, files = data
  151. if self.inplace:
  152. build_dir = self.get_package_dir('.'.join(d.split(os.sep)))
  153. else:
  154. build_dir = os.path.join(self.build_src, d)
  155. funcs = [f for f in files if hasattr(f, '__call__')]
  156. files = [f for f in files if not hasattr(f, '__call__')]
  157. for f in funcs:
  158. if f.__code__.co_argcount==1:
  159. s = f(build_dir)
  160. else:
  161. s = f()
  162. if s is not None:
  163. if isinstance(s, list):
  164. files.extend(s)
  165. elif isinstance(s, str):
  166. files.append(s)
  167. else:
  168. raise TypeError(repr(s))
  169. filenames = get_data_files((d, files))
  170. new_data_files.append((d, filenames))
  171. else:
  172. raise TypeError(repr(data))
  173. self.data_files[:] = new_data_files
  174. def _build_npy_pkg_config(self, info, gd):
  175. template, install_dir, subst_dict = info
  176. template_dir = os.path.dirname(template)
  177. for k, v in gd.items():
  178. subst_dict[k] = v
  179. if self.inplace == 1:
  180. generated_dir = os.path.join(template_dir, install_dir)
  181. else:
  182. generated_dir = os.path.join(self.build_src, template_dir,
  183. install_dir)
  184. generated = os.path.basename(os.path.splitext(template)[0])
  185. generated_path = os.path.join(generated_dir, generated)
  186. if not os.path.exists(generated_dir):
  187. os.makedirs(generated_dir)
  188. subst_vars(generated_path, template, subst_dict)
  189. # Where to install relatively to install prefix
  190. full_install_dir = os.path.join(template_dir, install_dir)
  191. return full_install_dir, generated_path
  192. def build_npy_pkg_config(self):
  193. log.info('build_src: building npy-pkg config files')
  194. # XXX: another ugly workaround to circumvent distutils brain damage. We
  195. # need the install prefix here, but finalizing the options of the
  196. # install command when only building sources cause error. Instead, we
  197. # copy the install command instance, and finalize the copy so that it
  198. # does not disrupt how distutils want to do things when with the
  199. # original install command instance.
  200. install_cmd = copy.copy(get_cmd('install'))
  201. if not install_cmd.finalized == 1:
  202. install_cmd.finalize_options()
  203. build_npkg = False
  204. if self.inplace == 1:
  205. top_prefix = '.'
  206. build_npkg = True
  207. elif hasattr(install_cmd, 'install_libbase'):
  208. top_prefix = install_cmd.install_libbase
  209. build_npkg = True
  210. if build_npkg:
  211. for pkg, infos in self.distribution.installed_pkg_config.items():
  212. pkg_path = self.distribution.package_dir[pkg]
  213. prefix = os.path.join(os.path.abspath(top_prefix), pkg_path)
  214. d = {'prefix': prefix}
  215. for info in infos:
  216. install_dir, generated = self._build_npy_pkg_config(info, d)
  217. self.distribution.data_files.append((install_dir,
  218. [generated]))
  219. def build_py_modules_sources(self):
  220. if not self.py_modules:
  221. return
  222. log.info('building py_modules sources')
  223. new_py_modules = []
  224. for source in self.py_modules:
  225. if is_sequence(source) and len(source)==3:
  226. package, module_base, source = source
  227. if self.inplace:
  228. build_dir = self.get_package_dir(package)
  229. else:
  230. build_dir = os.path.join(self.build_src,
  231. os.path.join(*package.split('.')))
  232. if hasattr(source, '__call__'):
  233. target = os.path.join(build_dir, module_base + '.py')
  234. source = source(target)
  235. if source is None:
  236. continue
  237. modules = [(package, module_base, source)]
  238. if package not in self.py_modules_dict:
  239. self.py_modules_dict[package] = []
  240. self.py_modules_dict[package] += modules
  241. else:
  242. new_py_modules.append(source)
  243. self.py_modules[:] = new_py_modules
  244. def build_library_sources(self, lib_name, build_info):
  245. sources = list(build_info.get('sources', []))
  246. if not sources:
  247. return
  248. log.info('building library "%s" sources' % (lib_name))
  249. sources = self.generate_sources(sources, (lib_name, build_info))
  250. sources = self.template_sources(sources, (lib_name, build_info))
  251. sources, h_files = self.filter_h_files(sources)
  252. if h_files:
  253. log.info('%s - nothing done with h_files = %s',
  254. self.package, h_files)
  255. #for f in h_files:
  256. # self.distribution.headers.append((lib_name,f))
  257. build_info['sources'] = sources
  258. return
  259. def build_extension_sources(self, ext):
  260. sources = list(ext.sources)
  261. log.info('building extension "%s" sources' % (ext.name))
  262. fullname = self.get_ext_fullname(ext.name)
  263. modpath = fullname.split('.')
  264. package = '.'.join(modpath[0:-1])
  265. if self.inplace:
  266. self.ext_target_dir = self.get_package_dir(package)
  267. sources = self.generate_sources(sources, ext)
  268. sources = self.template_sources(sources, ext)
  269. sources = self.swig_sources(sources, ext)
  270. sources = self.f2py_sources(sources, ext)
  271. sources = self.pyrex_sources(sources, ext)
  272. sources, py_files = self.filter_py_files(sources)
  273. if package not in self.py_modules_dict:
  274. self.py_modules_dict[package] = []
  275. modules = []
  276. for f in py_files:
  277. module = os.path.splitext(os.path.basename(f))[0]
  278. modules.append((package, module, f))
  279. self.py_modules_dict[package] += modules
  280. sources, h_files = self.filter_h_files(sources)
  281. if h_files:
  282. log.info('%s - nothing done with h_files = %s',
  283. package, h_files)
  284. #for f in h_files:
  285. # self.distribution.headers.append((package,f))
  286. ext.sources = sources
  287. def generate_sources(self, sources, extension):
  288. new_sources = []
  289. func_sources = []
  290. for source in sources:
  291. if is_string(source):
  292. new_sources.append(source)
  293. else:
  294. func_sources.append(source)
  295. if not func_sources:
  296. return new_sources
  297. if self.inplace and not is_sequence(extension):
  298. build_dir = self.ext_target_dir
  299. else:
  300. if is_sequence(extension):
  301. name = extension[0]
  302. # if 'include_dirs' not in extension[1]:
  303. # extension[1]['include_dirs'] = []
  304. # incl_dirs = extension[1]['include_dirs']
  305. else:
  306. name = extension.name
  307. # incl_dirs = extension.include_dirs
  308. #if self.build_src not in incl_dirs:
  309. # incl_dirs.append(self.build_src)
  310. build_dir = os.path.join(*([self.build_src]
  311. +name.split('.')[:-1]))
  312. self.mkpath(build_dir)
  313. if self.verbose_cfg:
  314. new_level = log.INFO
  315. else:
  316. new_level = log.WARN
  317. old_level = log.set_threshold(new_level)
  318. for func in func_sources:
  319. source = func(extension, build_dir)
  320. if not source:
  321. continue
  322. if is_sequence(source):
  323. [log.info(" adding '%s' to sources." % (s,)) for s in source]
  324. new_sources.extend(source)
  325. else:
  326. log.info(" adding '%s' to sources." % (source,))
  327. new_sources.append(source)
  328. log.set_threshold(old_level)
  329. return new_sources
  330. def filter_py_files(self, sources):
  331. return self.filter_files(sources, ['.py'])
  332. def filter_h_files(self, sources):
  333. return self.filter_files(sources, ['.h', '.hpp', '.inc'])
  334. def filter_files(self, sources, exts = []):
  335. new_sources = []
  336. files = []
  337. for source in sources:
  338. (base, ext) = os.path.splitext(source)
  339. if ext in exts:
  340. files.append(source)
  341. else:
  342. new_sources.append(source)
  343. return new_sources, files
  344. def template_sources(self, sources, extension):
  345. new_sources = []
  346. if is_sequence(extension):
  347. depends = extension[1].get('depends')
  348. include_dirs = extension[1].get('include_dirs')
  349. else:
  350. depends = extension.depends
  351. include_dirs = extension.include_dirs
  352. for source in sources:
  353. (base, ext) = os.path.splitext(source)
  354. if ext == '.src': # Template file
  355. if self.inplace:
  356. target_dir = os.path.dirname(base)
  357. else:
  358. target_dir = appendpath(self.build_src, os.path.dirname(base))
  359. self.mkpath(target_dir)
  360. target_file = os.path.join(target_dir, os.path.basename(base))
  361. if (self.force or newer_group([source] + depends, target_file)):
  362. if _f_pyf_ext_match(base):
  363. log.info("from_template:> %s" % (target_file))
  364. outstr = process_f_file(source)
  365. else:
  366. log.info("conv_template:> %s" % (target_file))
  367. outstr = process_c_file(source)
  368. with open(target_file, 'w') as fid:
  369. fid.write(outstr)
  370. if _header_ext_match(target_file):
  371. d = os.path.dirname(target_file)
  372. if d not in include_dirs:
  373. log.info(" adding '%s' to include_dirs." % (d))
  374. include_dirs.append(d)
  375. new_sources.append(target_file)
  376. else:
  377. new_sources.append(source)
  378. return new_sources
  379. def pyrex_sources(self, sources, extension):
  380. """Pyrex not supported; this remains for Cython support (see below)"""
  381. new_sources = []
  382. ext_name = extension.name.split('.')[-1]
  383. for source in sources:
  384. (base, ext) = os.path.splitext(source)
  385. if ext == '.pyx':
  386. target_file = self.generate_a_pyrex_source(base, ext_name,
  387. source,
  388. extension)
  389. new_sources.append(target_file)
  390. else:
  391. new_sources.append(source)
  392. return new_sources
  393. def generate_a_pyrex_source(self, base, ext_name, source, extension):
  394. """Pyrex is not supported, but some projects monkeypatch this method.
  395. That allows compiling Cython code, see gh-6955.
  396. This method will remain here for compatibility reasons.
  397. """
  398. return []
  399. def f2py_sources(self, sources, extension):
  400. new_sources = []
  401. f2py_sources = []
  402. f_sources = []
  403. f2py_targets = {}
  404. target_dirs = []
  405. ext_name = extension.name.split('.')[-1]
  406. skip_f2py = 0
  407. for source in sources:
  408. (base, ext) = os.path.splitext(source)
  409. if ext == '.pyf': # F2PY interface file
  410. if self.inplace:
  411. target_dir = os.path.dirname(base)
  412. else:
  413. target_dir = appendpath(self.build_src, os.path.dirname(base))
  414. if os.path.isfile(source):
  415. name = get_f2py_modulename(source)
  416. if name != ext_name:
  417. raise DistutilsSetupError('mismatch of extension names: %s '
  418. 'provides %r but expected %r' % (
  419. source, name, ext_name))
  420. target_file = os.path.join(target_dir, name+'module.c')
  421. else:
  422. log.debug(' source %s does not exist: skipping f2py\'ing.' \
  423. % (source))
  424. name = ext_name
  425. skip_f2py = 1
  426. target_file = os.path.join(target_dir, name+'module.c')
  427. if not os.path.isfile(target_file):
  428. log.warn(' target %s does not exist:\n '\
  429. 'Assuming %smodule.c was generated with '\
  430. '"build_src --inplace" command.' \
  431. % (target_file, name))
  432. target_dir = os.path.dirname(base)
  433. target_file = os.path.join(target_dir, name+'module.c')
  434. if not os.path.isfile(target_file):
  435. raise DistutilsSetupError("%r missing" % (target_file,))
  436. log.info(' Yes! Using %r as up-to-date target.' \
  437. % (target_file))
  438. target_dirs.append(target_dir)
  439. f2py_sources.append(source)
  440. f2py_targets[source] = target_file
  441. new_sources.append(target_file)
  442. elif fortran_ext_match(ext):
  443. f_sources.append(source)
  444. else:
  445. new_sources.append(source)
  446. if not (f2py_sources or f_sources):
  447. return new_sources
  448. for d in target_dirs:
  449. self.mkpath(d)
  450. f2py_options = extension.f2py_options + self.f2py_opts
  451. if self.distribution.libraries:
  452. for name, build_info in self.distribution.libraries:
  453. if name in extension.libraries:
  454. f2py_options.extend(build_info.get('f2py_options', []))
  455. log.info("f2py options: %s" % (f2py_options))
  456. if f2py_sources:
  457. if len(f2py_sources) != 1:
  458. raise DistutilsSetupError(
  459. 'only one .pyf file is allowed per extension module but got'\
  460. ' more: %r' % (f2py_sources,))
  461. source = f2py_sources[0]
  462. target_file = f2py_targets[source]
  463. target_dir = os.path.dirname(target_file) or '.'
  464. depends = [source] + extension.depends
  465. if (self.force or newer_group(depends, target_file, 'newer')) \
  466. and not skip_f2py:
  467. log.info("f2py: %s" % (source))
  468. import numpy.f2py
  469. numpy.f2py.run_main(f2py_options
  470. + ['--build-dir', target_dir, source])
  471. else:
  472. log.debug(" skipping '%s' f2py interface (up-to-date)" % (source))
  473. else:
  474. #XXX TODO: --inplace support for sdist command
  475. if is_sequence(extension):
  476. name = extension[0]
  477. else: name = extension.name
  478. target_dir = os.path.join(*([self.build_src]
  479. +name.split('.')[:-1]))
  480. target_file = os.path.join(target_dir, ext_name + 'module.c')
  481. new_sources.append(target_file)
  482. depends = f_sources + extension.depends
  483. if (self.force or newer_group(depends, target_file, 'newer')) \
  484. and not skip_f2py:
  485. log.info("f2py:> %s" % (target_file))
  486. self.mkpath(target_dir)
  487. import numpy.f2py
  488. numpy.f2py.run_main(f2py_options + ['--lower',
  489. '--build-dir', target_dir]+\
  490. ['-m', ext_name]+f_sources)
  491. else:
  492. log.debug(" skipping f2py fortran files for '%s' (up-to-date)"\
  493. % (target_file))
  494. if not os.path.isfile(target_file):
  495. raise DistutilsError("f2py target file %r not generated" % (target_file,))
  496. build_dir = os.path.join(self.build_src, target_dir)
  497. target_c = os.path.join(build_dir, 'fortranobject.c')
  498. target_h = os.path.join(build_dir, 'fortranobject.h')
  499. log.info(" adding '%s' to sources." % (target_c))
  500. new_sources.append(target_c)
  501. if build_dir not in extension.include_dirs:
  502. log.info(" adding '%s' to include_dirs." % (build_dir))
  503. extension.include_dirs.append(build_dir)
  504. if not skip_f2py:
  505. import numpy.f2py
  506. d = os.path.dirname(numpy.f2py.__file__)
  507. source_c = os.path.join(d, 'src', 'fortranobject.c')
  508. source_h = os.path.join(d, 'src', 'fortranobject.h')
  509. if newer(source_c, target_c) or newer(source_h, target_h):
  510. self.mkpath(os.path.dirname(target_c))
  511. self.copy_file(source_c, target_c)
  512. self.copy_file(source_h, target_h)
  513. else:
  514. if not os.path.isfile(target_c):
  515. raise DistutilsSetupError("f2py target_c file %r not found" % (target_c,))
  516. if not os.path.isfile(target_h):
  517. raise DistutilsSetupError("f2py target_h file %r not found" % (target_h,))
  518. for name_ext in ['-f2pywrappers.f', '-f2pywrappers2.f90']:
  519. filename = os.path.join(target_dir, ext_name + name_ext)
  520. if os.path.isfile(filename):
  521. log.info(" adding '%s' to sources." % (filename))
  522. f_sources.append(filename)
  523. return new_sources + f_sources
  524. def swig_sources(self, sources, extension):
  525. # Assuming SWIG 1.3.14 or later. See compatibility note in
  526. # http://www.swig.org/Doc1.3/Python.html#Python_nn6
  527. new_sources = []
  528. swig_sources = []
  529. swig_targets = {}
  530. target_dirs = []
  531. py_files = [] # swig generated .py files
  532. target_ext = '.c'
  533. if '-c++' in extension.swig_opts:
  534. typ = 'c++'
  535. is_cpp = True
  536. extension.swig_opts.remove('-c++')
  537. elif self.swig_cpp:
  538. typ = 'c++'
  539. is_cpp = True
  540. else:
  541. typ = None
  542. is_cpp = False
  543. skip_swig = 0
  544. ext_name = extension.name.split('.')[-1]
  545. for source in sources:
  546. (base, ext) = os.path.splitext(source)
  547. if ext == '.i': # SWIG interface file
  548. # the code below assumes that the sources list
  549. # contains not more than one .i SWIG interface file
  550. if self.inplace:
  551. target_dir = os.path.dirname(base)
  552. py_target_dir = self.ext_target_dir
  553. else:
  554. target_dir = appendpath(self.build_src, os.path.dirname(base))
  555. py_target_dir = target_dir
  556. if os.path.isfile(source):
  557. name = get_swig_modulename(source)
  558. if name != ext_name[1:]:
  559. raise DistutilsSetupError(
  560. 'mismatch of extension names: %s provides %r'
  561. ' but expected %r' % (source, name, ext_name[1:]))
  562. if typ is None:
  563. typ = get_swig_target(source)
  564. is_cpp = typ=='c++'
  565. else:
  566. typ2 = get_swig_target(source)
  567. if typ2 is None:
  568. log.warn('source %r does not define swig target, assuming %s swig target' \
  569. % (source, typ))
  570. elif typ!=typ2:
  571. log.warn('expected %r but source %r defines %r swig target' \
  572. % (typ, source, typ2))
  573. if typ2=='c++':
  574. log.warn('resetting swig target to c++ (some targets may have .c extension)')
  575. is_cpp = True
  576. else:
  577. log.warn('assuming that %r has c++ swig target' % (source))
  578. if is_cpp:
  579. target_ext = '.cpp'
  580. target_file = os.path.join(target_dir, '%s_wrap%s' \
  581. % (name, target_ext))
  582. else:
  583. log.warn(' source %s does not exist: skipping swig\'ing.' \
  584. % (source))
  585. name = ext_name[1:]
  586. skip_swig = 1
  587. target_file = _find_swig_target(target_dir, name)
  588. if not os.path.isfile(target_file):
  589. log.warn(' target %s does not exist:\n '\
  590. 'Assuming %s_wrap.{c,cpp} was generated with '\
  591. '"build_src --inplace" command.' \
  592. % (target_file, name))
  593. target_dir = os.path.dirname(base)
  594. target_file = _find_swig_target(target_dir, name)
  595. if not os.path.isfile(target_file):
  596. raise DistutilsSetupError("%r missing" % (target_file,))
  597. log.warn(' Yes! Using %r as up-to-date target.' \
  598. % (target_file))
  599. target_dirs.append(target_dir)
  600. new_sources.append(target_file)
  601. py_files.append(os.path.join(py_target_dir, name+'.py'))
  602. swig_sources.append(source)
  603. swig_targets[source] = new_sources[-1]
  604. else:
  605. new_sources.append(source)
  606. if not swig_sources:
  607. return new_sources
  608. if skip_swig:
  609. return new_sources + py_files
  610. for d in target_dirs:
  611. self.mkpath(d)
  612. swig = self.swig or self.find_swig()
  613. swig_cmd = [swig, "-python"] + extension.swig_opts
  614. if is_cpp:
  615. swig_cmd.append('-c++')
  616. for d in extension.include_dirs:
  617. swig_cmd.append('-I'+d)
  618. for source in swig_sources:
  619. target = swig_targets[source]
  620. depends = [source] + extension.depends
  621. if self.force or newer_group(depends, target, 'newer'):
  622. log.info("%s: %s" % (os.path.basename(swig) \
  623. + (is_cpp and '++' or ''), source))
  624. self.spawn(swig_cmd + self.swig_opts \
  625. + ["-o", target, '-outdir', py_target_dir, source])
  626. else:
  627. log.debug(" skipping '%s' swig interface (up-to-date)" \
  628. % (source))
  629. return new_sources + py_files
  630. _f_pyf_ext_match = re.compile(r'.*\.(f90|f95|f77|for|ftn|f|pyf)\Z', re.I).match
  631. _header_ext_match = re.compile(r'.*\.(inc|h|hpp)\Z', re.I).match
  632. #### SWIG related auxiliary functions ####
  633. _swig_module_name_match = re.compile(r'\s*%module\s*(.*\(\s*package\s*=\s*"(?P<package>[\w_]+)".*\)|)\s*(?P<name>[\w_]+)',
  634. re.I).match
  635. _has_c_header = re.compile(r'-\*-\s*c\s*-\*-', re.I).search
  636. _has_cpp_header = re.compile(r'-\*-\s*c\+\+\s*-\*-', re.I).search
  637. def get_swig_target(source):
  638. with open(source, 'r') as f:
  639. result = None
  640. line = f.readline()
  641. if _has_cpp_header(line):
  642. result = 'c++'
  643. if _has_c_header(line):
  644. result = 'c'
  645. return result
  646. def get_swig_modulename(source):
  647. with open(source, 'r') as f:
  648. name = None
  649. for line in f:
  650. m = _swig_module_name_match(line)
  651. if m:
  652. name = m.group('name')
  653. break
  654. return name
  655. def _find_swig_target(target_dir, name):
  656. for ext in ['.cpp', '.c']:
  657. target = os.path.join(target_dir, '%s_wrap%s' % (name, ext))
  658. if os.path.isfile(target):
  659. break
  660. return target
  661. #### F2PY related auxiliary functions ####
  662. _f2py_module_name_match = re.compile(r'\s*python\s*module\s*(?P<name>[\w_]+)',
  663. re.I).match
  664. _f2py_user_module_name_match = re.compile(r'\s*python\s*module\s*(?P<name>[\w_]*?'
  665. r'__user__[\w_]*)', re.I).match
  666. def get_f2py_modulename(source):
  667. name = None
  668. with open(source) as f:
  669. for line in f:
  670. m = _f2py_module_name_match(line)
  671. if m:
  672. if _f2py_user_module_name_match(line): # skip *__user__* names
  673. continue
  674. name = m.group('name')
  675. break
  676. return name
  677. ##########################################