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.

708 lines
31 KiB

6 months ago
  1. """ Modified version of build_ext that handles fortran source files.
  2. """
  3. import os
  4. import subprocess
  5. from glob import glob
  6. from distutils.dep_util import newer_group
  7. from distutils.command.build_ext import build_ext as old_build_ext
  8. from distutils.errors import DistutilsFileError, DistutilsSetupError,\
  9. DistutilsError
  10. from distutils.file_util import copy_file
  11. from numpy.distutils import log
  12. from numpy.distutils.exec_command import filepath_from_subprocess_output
  13. from numpy.distutils.system_info import combine_paths
  14. from numpy.distutils.misc_util import (
  15. filter_sources, get_ext_source_files, get_numpy_include_dirs,
  16. has_cxx_sources, has_f_sources, is_sequence
  17. )
  18. from numpy.distutils.command.config_compiler import show_fortran_compilers
  19. from numpy.distutils.ccompiler_opt import new_ccompiler_opt, CCompilerOpt
  20. class build_ext (old_build_ext):
  21. description = "build C/C++/F extensions (compile/link to build directory)"
  22. user_options = old_build_ext.user_options + [
  23. ('fcompiler=', None,
  24. "specify the Fortran compiler type"),
  25. ('parallel=', 'j',
  26. "number of parallel jobs"),
  27. ('warn-error', None,
  28. "turn all warnings into errors (-Werror)"),
  29. ('cpu-baseline=', None,
  30. "specify a list of enabled baseline CPU optimizations"),
  31. ('cpu-dispatch=', None,
  32. "specify a list of dispatched CPU optimizations"),
  33. ('disable-optimization', None,
  34. "disable CPU optimized code(dispatch,simd,fast...)"),
  35. ('simd-test=', None,
  36. "specify a list of CPU optimizations to be tested against NumPy SIMD interface"),
  37. ]
  38. help_options = old_build_ext.help_options + [
  39. ('help-fcompiler', None, "list available Fortran compilers",
  40. show_fortran_compilers),
  41. ]
  42. boolean_options = old_build_ext.boolean_options + ['warn-error', 'disable-optimization']
  43. def initialize_options(self):
  44. old_build_ext.initialize_options(self)
  45. self.fcompiler = None
  46. self.parallel = None
  47. self.warn_error = None
  48. self.cpu_baseline = None
  49. self.cpu_dispatch = None
  50. self.disable_optimization = None
  51. self.simd_test = None
  52. def finalize_options(self):
  53. if self.parallel:
  54. try:
  55. self.parallel = int(self.parallel)
  56. except ValueError as e:
  57. raise ValueError("--parallel/-j argument must be an integer") from e
  58. # Ensure that self.include_dirs and self.distribution.include_dirs
  59. # refer to the same list object. finalize_options will modify
  60. # self.include_dirs, but self.distribution.include_dirs is used
  61. # during the actual build.
  62. # self.include_dirs is None unless paths are specified with
  63. # --include-dirs.
  64. # The include paths will be passed to the compiler in the order:
  65. # numpy paths, --include-dirs paths, Python include path.
  66. if isinstance(self.include_dirs, str):
  67. self.include_dirs = self.include_dirs.split(os.pathsep)
  68. incl_dirs = self.include_dirs or []
  69. if self.distribution.include_dirs is None:
  70. self.distribution.include_dirs = []
  71. self.include_dirs = self.distribution.include_dirs
  72. self.include_dirs.extend(incl_dirs)
  73. old_build_ext.finalize_options(self)
  74. self.set_undefined_options('build',
  75. ('parallel', 'parallel'),
  76. ('warn_error', 'warn_error'),
  77. ('cpu_baseline', 'cpu_baseline'),
  78. ('cpu_dispatch', 'cpu_dispatch'),
  79. ('disable_optimization', 'disable_optimization'),
  80. ('simd_test', 'simd_test')
  81. )
  82. CCompilerOpt.conf_target_groups["simd_test"] = self.simd_test
  83. def run(self):
  84. if not self.extensions:
  85. return
  86. # Make sure that extension sources are complete.
  87. self.run_command('build_src')
  88. if self.distribution.has_c_libraries():
  89. if self.inplace:
  90. if self.distribution.have_run.get('build_clib'):
  91. log.warn('build_clib already run, it is too late to '
  92. 'ensure in-place build of build_clib')
  93. build_clib = self.distribution.get_command_obj(
  94. 'build_clib')
  95. else:
  96. build_clib = self.distribution.get_command_obj(
  97. 'build_clib')
  98. build_clib.inplace = 1
  99. build_clib.ensure_finalized()
  100. build_clib.run()
  101. self.distribution.have_run['build_clib'] = 1
  102. else:
  103. self.run_command('build_clib')
  104. build_clib = self.get_finalized_command('build_clib')
  105. self.library_dirs.append(build_clib.build_clib)
  106. else:
  107. build_clib = None
  108. # Not including C libraries to the list of
  109. # extension libraries automatically to prevent
  110. # bogus linking commands. Extensions must
  111. # explicitly specify the C libraries that they use.
  112. from distutils.ccompiler import new_compiler
  113. from numpy.distutils.fcompiler import new_fcompiler
  114. compiler_type = self.compiler
  115. # Initialize C compiler:
  116. self.compiler = new_compiler(compiler=compiler_type,
  117. verbose=self.verbose,
  118. dry_run=self.dry_run,
  119. force=self.force)
  120. self.compiler.customize(self.distribution)
  121. self.compiler.customize_cmd(self)
  122. if self.warn_error:
  123. self.compiler.compiler.append('-Werror')
  124. self.compiler.compiler_so.append('-Werror')
  125. self.compiler.show_customization()
  126. if not self.disable_optimization:
  127. dispatch_hpath = os.path.join("numpy", "distutils", "include", "npy_cpu_dispatch_config.h")
  128. dispatch_hpath = os.path.join(self.get_finalized_command("build_src").build_src, dispatch_hpath)
  129. opt_cache_path = os.path.abspath(
  130. os.path.join(self.build_temp, 'ccompiler_opt_cache_ext.py')
  131. )
  132. if hasattr(self, "compiler_opt"):
  133. # By default `CCompilerOpt` update the cache at the exit of
  134. # the process, which may lead to duplicate building
  135. # (see build_extension()/force_rebuild) if run() called
  136. # multiple times within the same os process/thread without
  137. # giving the chance the previous instances of `CCompilerOpt`
  138. # to update the cache.
  139. self.compiler_opt.cache_flush()
  140. self.compiler_opt = new_ccompiler_opt(
  141. compiler=self.compiler, dispatch_hpath=dispatch_hpath,
  142. cpu_baseline=self.cpu_baseline, cpu_dispatch=self.cpu_dispatch,
  143. cache_path=opt_cache_path
  144. )
  145. def report(copt):
  146. log.info("\n########### EXT COMPILER OPTIMIZATION ###########")
  147. log.info(copt.report(full=True))
  148. import atexit
  149. atexit.register(report, self.compiler_opt)
  150. # Setup directory for storing generated extra DLL files on Windows
  151. self.extra_dll_dir = os.path.join(self.build_temp, '.libs')
  152. if not os.path.isdir(self.extra_dll_dir):
  153. os.makedirs(self.extra_dll_dir)
  154. # Create mapping of libraries built by build_clib:
  155. clibs = {}
  156. if build_clib is not None:
  157. for libname, build_info in build_clib.libraries or []:
  158. if libname in clibs and clibs[libname] != build_info:
  159. log.warn('library %r defined more than once,'
  160. ' overwriting build_info\n%s... \nwith\n%s...'
  161. % (libname, repr(clibs[libname])[:300], repr(build_info)[:300]))
  162. clibs[libname] = build_info
  163. # .. and distribution libraries:
  164. for libname, build_info in self.distribution.libraries or []:
  165. if libname in clibs:
  166. # build_clib libraries have a precedence before distribution ones
  167. continue
  168. clibs[libname] = build_info
  169. # Determine if C++/Fortran 77/Fortran 90 compilers are needed.
  170. # Update extension libraries, library_dirs, and macros.
  171. all_languages = set()
  172. for ext in self.extensions:
  173. ext_languages = set()
  174. c_libs = []
  175. c_lib_dirs = []
  176. macros = []
  177. for libname in ext.libraries:
  178. if libname in clibs:
  179. binfo = clibs[libname]
  180. c_libs += binfo.get('libraries', [])
  181. c_lib_dirs += binfo.get('library_dirs', [])
  182. for m in binfo.get('macros', []):
  183. if m not in macros:
  184. macros.append(m)
  185. for l in clibs.get(libname, {}).get('source_languages', []):
  186. ext_languages.add(l)
  187. if c_libs:
  188. new_c_libs = ext.libraries + c_libs
  189. log.info('updating extension %r libraries from %r to %r'
  190. % (ext.name, ext.libraries, new_c_libs))
  191. ext.libraries = new_c_libs
  192. ext.library_dirs = ext.library_dirs + c_lib_dirs
  193. if macros:
  194. log.info('extending extension %r defined_macros with %r'
  195. % (ext.name, macros))
  196. ext.define_macros = ext.define_macros + macros
  197. # determine extension languages
  198. if has_f_sources(ext.sources):
  199. ext_languages.add('f77')
  200. if has_cxx_sources(ext.sources):
  201. ext_languages.add('c++')
  202. l = ext.language or self.compiler.detect_language(ext.sources)
  203. if l:
  204. ext_languages.add(l)
  205. # reset language attribute for choosing proper linker
  206. if 'c++' in ext_languages:
  207. ext_language = 'c++'
  208. elif 'f90' in ext_languages:
  209. ext_language = 'f90'
  210. elif 'f77' in ext_languages:
  211. ext_language = 'f77'
  212. else:
  213. ext_language = 'c' # default
  214. if l and l != ext_language and ext.language:
  215. log.warn('resetting extension %r language from %r to %r.' %
  216. (ext.name, l, ext_language))
  217. ext.language = ext_language
  218. # global language
  219. all_languages.update(ext_languages)
  220. need_f90_compiler = 'f90' in all_languages
  221. need_f77_compiler = 'f77' in all_languages
  222. need_cxx_compiler = 'c++' in all_languages
  223. # Initialize C++ compiler:
  224. if need_cxx_compiler:
  225. self._cxx_compiler = new_compiler(compiler=compiler_type,
  226. verbose=self.verbose,
  227. dry_run=self.dry_run,
  228. force=self.force)
  229. compiler = self._cxx_compiler
  230. compiler.customize(self.distribution, need_cxx=need_cxx_compiler)
  231. compiler.customize_cmd(self)
  232. compiler.show_customization()
  233. self._cxx_compiler = compiler.cxx_compiler()
  234. else:
  235. self._cxx_compiler = None
  236. # Initialize Fortran 77 compiler:
  237. if need_f77_compiler:
  238. ctype = self.fcompiler
  239. self._f77_compiler = new_fcompiler(compiler=self.fcompiler,
  240. verbose=self.verbose,
  241. dry_run=self.dry_run,
  242. force=self.force,
  243. requiref90=False,
  244. c_compiler=self.compiler)
  245. fcompiler = self._f77_compiler
  246. if fcompiler:
  247. ctype = fcompiler.compiler_type
  248. fcompiler.customize(self.distribution)
  249. if fcompiler and fcompiler.get_version():
  250. fcompiler.customize_cmd(self)
  251. fcompiler.show_customization()
  252. else:
  253. self.warn('f77_compiler=%s is not available.' %
  254. (ctype))
  255. self._f77_compiler = None
  256. else:
  257. self._f77_compiler = None
  258. # Initialize Fortran 90 compiler:
  259. if need_f90_compiler:
  260. ctype = self.fcompiler
  261. self._f90_compiler = new_fcompiler(compiler=self.fcompiler,
  262. verbose=self.verbose,
  263. dry_run=self.dry_run,
  264. force=self.force,
  265. requiref90=True,
  266. c_compiler=self.compiler)
  267. fcompiler = self._f90_compiler
  268. if fcompiler:
  269. ctype = fcompiler.compiler_type
  270. fcompiler.customize(self.distribution)
  271. if fcompiler and fcompiler.get_version():
  272. fcompiler.customize_cmd(self)
  273. fcompiler.show_customization()
  274. else:
  275. self.warn('f90_compiler=%s is not available.' %
  276. (ctype))
  277. self._f90_compiler = None
  278. else:
  279. self._f90_compiler = None
  280. # Build extensions
  281. self.build_extensions()
  282. # Copy over any extra DLL files
  283. # FIXME: In the case where there are more than two packages,
  284. # we blindly assume that both packages need all of the libraries,
  285. # resulting in a larger wheel than is required. This should be fixed,
  286. # but it's so rare that I won't bother to handle it.
  287. pkg_roots = {
  288. self.get_ext_fullname(ext.name).split('.')[0]
  289. for ext in self.extensions
  290. }
  291. for pkg_root in pkg_roots:
  292. shared_lib_dir = os.path.join(pkg_root, '.libs')
  293. if not self.inplace:
  294. shared_lib_dir = os.path.join(self.build_lib, shared_lib_dir)
  295. for fn in os.listdir(self.extra_dll_dir):
  296. if not os.path.isdir(shared_lib_dir):
  297. os.makedirs(shared_lib_dir)
  298. if not fn.lower().endswith('.dll'):
  299. continue
  300. runtime_lib = os.path.join(self.extra_dll_dir, fn)
  301. copy_file(runtime_lib, shared_lib_dir)
  302. def swig_sources(self, sources, extensions=None):
  303. # Do nothing. Swig sources have been handled in build_src command.
  304. return sources
  305. def build_extension(self, ext):
  306. sources = ext.sources
  307. if sources is None or not is_sequence(sources):
  308. raise DistutilsSetupError(
  309. ("in 'ext_modules' option (extension '%s'), " +
  310. "'sources' must be present and must be " +
  311. "a list of source filenames") % ext.name)
  312. sources = list(sources)
  313. if not sources:
  314. return
  315. fullname = self.get_ext_fullname(ext.name)
  316. if self.inplace:
  317. modpath = fullname.split('.')
  318. package = '.'.join(modpath[0:-1])
  319. base = modpath[-1]
  320. build_py = self.get_finalized_command('build_py')
  321. package_dir = build_py.get_package_dir(package)
  322. ext_filename = os.path.join(package_dir,
  323. self.get_ext_filename(base))
  324. else:
  325. ext_filename = os.path.join(self.build_lib,
  326. self.get_ext_filename(fullname))
  327. depends = sources + ext.depends
  328. force_rebuild = self.force
  329. if not self.disable_optimization and not self.compiler_opt.is_cached():
  330. log.debug("Detected changes on compiler optimizations")
  331. force_rebuild = True
  332. if not (force_rebuild or newer_group(depends, ext_filename, 'newer')):
  333. log.debug("skipping '%s' extension (up-to-date)", ext.name)
  334. return
  335. else:
  336. log.info("building '%s' extension", ext.name)
  337. extra_args = ext.extra_compile_args or []
  338. macros = ext.define_macros[:]
  339. for undef in ext.undef_macros:
  340. macros.append((undef,))
  341. c_sources, cxx_sources, f_sources, fmodule_sources = \
  342. filter_sources(ext.sources)
  343. if self.compiler.compiler_type == 'msvc':
  344. if cxx_sources:
  345. # Needed to compile kiva.agg._agg extension.
  346. extra_args.append('/Zm1000')
  347. # this hack works around the msvc compiler attributes
  348. # problem, msvc uses its own convention :(
  349. c_sources += cxx_sources
  350. cxx_sources = []
  351. # Set Fortran/C++ compilers for compilation and linking.
  352. if ext.language == 'f90':
  353. fcompiler = self._f90_compiler
  354. elif ext.language == 'f77':
  355. fcompiler = self._f77_compiler
  356. else: # in case ext.language is c++, for instance
  357. fcompiler = self._f90_compiler or self._f77_compiler
  358. if fcompiler is not None:
  359. fcompiler.extra_f77_compile_args = (ext.extra_f77_compile_args or []) if hasattr(
  360. ext, 'extra_f77_compile_args') else []
  361. fcompiler.extra_f90_compile_args = (ext.extra_f90_compile_args or []) if hasattr(
  362. ext, 'extra_f90_compile_args') else []
  363. cxx_compiler = self._cxx_compiler
  364. # check for the availability of required compilers
  365. if cxx_sources and cxx_compiler is None:
  366. raise DistutilsError("extension %r has C++ sources"
  367. "but no C++ compiler found" % (ext.name))
  368. if (f_sources or fmodule_sources) and fcompiler is None:
  369. raise DistutilsError("extension %r has Fortran sources "
  370. "but no Fortran compiler found" % (ext.name))
  371. if ext.language in ['f77', 'f90'] and fcompiler is None:
  372. self.warn("extension %r has Fortran libraries "
  373. "but no Fortran linker found, using default linker" % (ext.name))
  374. if ext.language == 'c++' and cxx_compiler is None:
  375. self.warn("extension %r has C++ libraries "
  376. "but no C++ linker found, using default linker" % (ext.name))
  377. kws = {'depends': ext.depends}
  378. output_dir = self.build_temp
  379. include_dirs = ext.include_dirs + get_numpy_include_dirs()
  380. # filtering C dispatch-table sources when optimization is not disabled,
  381. # otherwise treated as normal sources.
  382. copt_c_sources = []
  383. copt_cxx_sources = []
  384. copt_baseline_flags = []
  385. copt_macros = []
  386. if not self.disable_optimization:
  387. bsrc_dir = self.get_finalized_command("build_src").build_src
  388. dispatch_hpath = os.path.join("numpy", "distutils", "include")
  389. dispatch_hpath = os.path.join(bsrc_dir, dispatch_hpath)
  390. include_dirs.append(dispatch_hpath)
  391. copt_build_src = None if self.inplace else bsrc_dir
  392. for _srcs, _dst, _ext in (
  393. ((c_sources,), copt_c_sources, ('.dispatch.c',)),
  394. ((c_sources, cxx_sources), copt_cxx_sources,
  395. ('.dispatch.cpp', '.dispatch.cxx'))
  396. ):
  397. for _src in _srcs:
  398. _dst += [
  399. _src.pop(_src.index(s))
  400. for s in _src[:] if s.endswith(_ext)
  401. ]
  402. copt_baseline_flags = self.compiler_opt.cpu_baseline_flags()
  403. else:
  404. copt_macros.append(("NPY_DISABLE_OPTIMIZATION", 1))
  405. c_objects = []
  406. if copt_cxx_sources:
  407. log.info("compiling C++ dispatch-able sources")
  408. c_objects += self.compiler_opt.try_dispatch(
  409. copt_cxx_sources,
  410. output_dir=output_dir,
  411. src_dir=copt_build_src,
  412. macros=macros + copt_macros,
  413. include_dirs=include_dirs,
  414. debug=self.debug,
  415. extra_postargs=extra_args,
  416. ccompiler=cxx_compiler,
  417. **kws
  418. )
  419. if copt_c_sources:
  420. log.info("compiling C dispatch-able sources")
  421. c_objects += self.compiler_opt.try_dispatch(copt_c_sources,
  422. output_dir=output_dir,
  423. src_dir=copt_build_src,
  424. macros=macros + copt_macros,
  425. include_dirs=include_dirs,
  426. debug=self.debug,
  427. extra_postargs=extra_args,
  428. **kws)
  429. if c_sources:
  430. log.info("compiling C sources")
  431. c_objects += self.compiler.compile(c_sources,
  432. output_dir=output_dir,
  433. macros=macros + copt_macros,
  434. include_dirs=include_dirs,
  435. debug=self.debug,
  436. extra_postargs=extra_args + copt_baseline_flags,
  437. **kws)
  438. if cxx_sources:
  439. log.info("compiling C++ sources")
  440. c_objects += cxx_compiler.compile(cxx_sources,
  441. output_dir=output_dir,
  442. macros=macros + copt_macros,
  443. include_dirs=include_dirs,
  444. debug=self.debug,
  445. extra_postargs=extra_args + copt_baseline_flags,
  446. **kws)
  447. extra_postargs = []
  448. f_objects = []
  449. if fmodule_sources:
  450. log.info("compiling Fortran 90 module sources")
  451. module_dirs = ext.module_dirs[:]
  452. module_build_dir = os.path.join(
  453. self.build_temp, os.path.dirname(
  454. self.get_ext_filename(fullname)))
  455. self.mkpath(module_build_dir)
  456. if fcompiler.module_dir_switch is None:
  457. existing_modules = glob('*.mod')
  458. extra_postargs += fcompiler.module_options(
  459. module_dirs, module_build_dir)
  460. f_objects += fcompiler.compile(fmodule_sources,
  461. output_dir=self.build_temp,
  462. macros=macros,
  463. include_dirs=include_dirs,
  464. debug=self.debug,
  465. extra_postargs=extra_postargs,
  466. depends=ext.depends)
  467. if fcompiler.module_dir_switch is None:
  468. for f in glob('*.mod'):
  469. if f in existing_modules:
  470. continue
  471. t = os.path.join(module_build_dir, f)
  472. if os.path.abspath(f) == os.path.abspath(t):
  473. continue
  474. if os.path.isfile(t):
  475. os.remove(t)
  476. try:
  477. self.move_file(f, module_build_dir)
  478. except DistutilsFileError:
  479. log.warn('failed to move %r to %r' %
  480. (f, module_build_dir))
  481. if f_sources:
  482. log.info("compiling Fortran sources")
  483. f_objects += fcompiler.compile(f_sources,
  484. output_dir=self.build_temp,
  485. macros=macros,
  486. include_dirs=include_dirs,
  487. debug=self.debug,
  488. extra_postargs=extra_postargs,
  489. depends=ext.depends)
  490. if f_objects and not fcompiler.can_ccompiler_link(self.compiler):
  491. unlinkable_fobjects = f_objects
  492. objects = c_objects
  493. else:
  494. unlinkable_fobjects = []
  495. objects = c_objects + f_objects
  496. if ext.extra_objects:
  497. objects.extend(ext.extra_objects)
  498. extra_args = ext.extra_link_args or []
  499. libraries = self.get_libraries(ext)[:]
  500. library_dirs = ext.library_dirs[:]
  501. linker = self.compiler.link_shared_object
  502. # Always use system linker when using MSVC compiler.
  503. if self.compiler.compiler_type in ('msvc', 'intelw', 'intelemw'):
  504. # expand libraries with fcompiler libraries as we are
  505. # not using fcompiler linker
  506. self._libs_with_msvc_and_fortran(
  507. fcompiler, libraries, library_dirs)
  508. elif ext.language in ['f77', 'f90'] and fcompiler is not None:
  509. linker = fcompiler.link_shared_object
  510. if ext.language == 'c++' and cxx_compiler is not None:
  511. linker = cxx_compiler.link_shared_object
  512. if fcompiler is not None:
  513. objects, libraries = self._process_unlinkable_fobjects(
  514. objects, libraries,
  515. fcompiler, library_dirs,
  516. unlinkable_fobjects)
  517. linker(objects, ext_filename,
  518. libraries=libraries,
  519. library_dirs=library_dirs,
  520. runtime_library_dirs=ext.runtime_library_dirs,
  521. extra_postargs=extra_args,
  522. export_symbols=self.get_export_symbols(ext),
  523. debug=self.debug,
  524. build_temp=self.build_temp,
  525. target_lang=ext.language)
  526. def _add_dummy_mingwex_sym(self, c_sources):
  527. build_src = self.get_finalized_command("build_src").build_src
  528. build_clib = self.get_finalized_command("build_clib").build_clib
  529. objects = self.compiler.compile([os.path.join(build_src,
  530. "gfortran_vs2003_hack.c")],
  531. output_dir=self.build_temp)
  532. self.compiler.create_static_lib(
  533. objects, "_gfortran_workaround", output_dir=build_clib, debug=self.debug)
  534. def _process_unlinkable_fobjects(self, objects, libraries,
  535. fcompiler, library_dirs,
  536. unlinkable_fobjects):
  537. libraries = list(libraries)
  538. objects = list(objects)
  539. unlinkable_fobjects = list(unlinkable_fobjects)
  540. # Expand possible fake static libraries to objects;
  541. # make sure to iterate over a copy of the list as
  542. # "fake" libraries will be removed as they are
  543. # enountered
  544. for lib in libraries[:]:
  545. for libdir in library_dirs:
  546. fake_lib = os.path.join(libdir, lib + '.fobjects')
  547. if os.path.isfile(fake_lib):
  548. # Replace fake static library
  549. libraries.remove(lib)
  550. with open(fake_lib, 'r') as f:
  551. unlinkable_fobjects.extend(f.read().splitlines())
  552. # Expand C objects
  553. c_lib = os.path.join(libdir, lib + '.cobjects')
  554. with open(c_lib, 'r') as f:
  555. objects.extend(f.read().splitlines())
  556. # Wrap unlinkable objects to a linkable one
  557. if unlinkable_fobjects:
  558. fobjects = [os.path.abspath(obj) for obj in unlinkable_fobjects]
  559. wrapped = fcompiler.wrap_unlinkable_objects(
  560. fobjects, output_dir=self.build_temp,
  561. extra_dll_dir=self.extra_dll_dir)
  562. objects.extend(wrapped)
  563. return objects, libraries
  564. def _libs_with_msvc_and_fortran(self, fcompiler, c_libraries,
  565. c_library_dirs):
  566. if fcompiler is None:
  567. return
  568. for libname in c_libraries:
  569. if libname.startswith('msvc'):
  570. continue
  571. fileexists = False
  572. for libdir in c_library_dirs or []:
  573. libfile = os.path.join(libdir, '%s.lib' % (libname))
  574. if os.path.isfile(libfile):
  575. fileexists = True
  576. break
  577. if fileexists:
  578. continue
  579. # make g77-compiled static libs available to MSVC
  580. fileexists = False
  581. for libdir in c_library_dirs:
  582. libfile = os.path.join(libdir, 'lib%s.a' % (libname))
  583. if os.path.isfile(libfile):
  584. # copy libname.a file to name.lib so that MSVC linker
  585. # can find it
  586. libfile2 = os.path.join(self.build_temp, libname + '.lib')
  587. copy_file(libfile, libfile2)
  588. if self.build_temp not in c_library_dirs:
  589. c_library_dirs.append(self.build_temp)
  590. fileexists = True
  591. break
  592. if fileexists:
  593. continue
  594. log.warn('could not find library %r in directories %s'
  595. % (libname, c_library_dirs))
  596. # Always use system linker when using MSVC compiler.
  597. f_lib_dirs = []
  598. for dir in fcompiler.library_dirs:
  599. # correct path when compiling in Cygwin but with normal Win
  600. # Python
  601. if dir.startswith('/usr/lib'):
  602. try:
  603. dir = subprocess.check_output(['cygpath', '-w', dir])
  604. except (OSError, subprocess.CalledProcessError):
  605. pass
  606. else:
  607. dir = filepath_from_subprocess_output(dir)
  608. f_lib_dirs.append(dir)
  609. c_library_dirs.extend(f_lib_dirs)
  610. # make g77-compiled static libs available to MSVC
  611. for lib in fcompiler.libraries:
  612. if not lib.startswith('msvc'):
  613. c_libraries.append(lib)
  614. p = combine_paths(f_lib_dirs, 'lib' + lib + '.a')
  615. if p:
  616. dst_name = os.path.join(self.build_temp, lib + '.lib')
  617. if not os.path.isfile(dst_name):
  618. copy_file(p[0], dst_name)
  619. if self.build_temp not in c_library_dirs:
  620. c_library_dirs.append(self.build_temp)
  621. def get_source_files(self):
  622. self.check_extensions_list(self.extensions)
  623. filenames = []
  624. for ext in self.extensions:
  625. filenames.extend(get_ext_source_files(ext))
  626. return filenames
  627. def get_outputs(self):
  628. self.check_extensions_list(self.extensions)
  629. outputs = []
  630. for ext in self.extensions:
  631. if not ext.sources:
  632. continue
  633. fullname = self.get_ext_fullname(ext.name)
  634. outputs.append(os.path.join(self.build_lib,
  635. self.get_ext_filename(fullname)))
  636. return outputs