build_ext.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. self.compiler_opt = new_ccompiler_opt(
  133. compiler=self.compiler, dispatch_hpath=dispatch_hpath,
  134. cpu_baseline=self.cpu_baseline, cpu_dispatch=self.cpu_dispatch,
  135. cache_path=opt_cache_path
  136. )
  137. if not self.compiler_opt.is_cached():
  138. log.info("Detected changes on compiler optimizations, force rebuilding")
  139. self.force = True
  140. import atexit
  141. def report():
  142. log.info("\n########### EXT COMPILER OPTIMIZATION ###########")
  143. log.info(self.compiler_opt.report(full=True))
  144. atexit.register(report)
  145. # Setup directory for storing generated extra DLL files on Windows
  146. self.extra_dll_dir = os.path.join(self.build_temp, '.libs')
  147. if not os.path.isdir(self.extra_dll_dir):
  148. os.makedirs(self.extra_dll_dir)
  149. # Create mapping of libraries built by build_clib:
  150. clibs = {}
  151. if build_clib is not None:
  152. for libname, build_info in build_clib.libraries or []:
  153. if libname in clibs and clibs[libname] != build_info:
  154. log.warn('library %r defined more than once,'
  155. ' overwriting build_info\n%s... \nwith\n%s...'
  156. % (libname, repr(clibs[libname])[:300], repr(build_info)[:300]))
  157. clibs[libname] = build_info
  158. # .. and distribution libraries:
  159. for libname, build_info in self.distribution.libraries or []:
  160. if libname in clibs:
  161. # build_clib libraries have a precedence before distribution ones
  162. continue
  163. clibs[libname] = build_info
  164. # Determine if C++/Fortran 77/Fortran 90 compilers are needed.
  165. # Update extension libraries, library_dirs, and macros.
  166. all_languages = set()
  167. for ext in self.extensions:
  168. ext_languages = set()
  169. c_libs = []
  170. c_lib_dirs = []
  171. macros = []
  172. for libname in ext.libraries:
  173. if libname in clibs:
  174. binfo = clibs[libname]
  175. c_libs += binfo.get('libraries', [])
  176. c_lib_dirs += binfo.get('library_dirs', [])
  177. for m in binfo.get('macros', []):
  178. if m not in macros:
  179. macros.append(m)
  180. for l in clibs.get(libname, {}).get('source_languages', []):
  181. ext_languages.add(l)
  182. if c_libs:
  183. new_c_libs = ext.libraries + c_libs
  184. log.info('updating extension %r libraries from %r to %r'
  185. % (ext.name, ext.libraries, new_c_libs))
  186. ext.libraries = new_c_libs
  187. ext.library_dirs = ext.library_dirs + c_lib_dirs
  188. if macros:
  189. log.info('extending extension %r defined_macros with %r'
  190. % (ext.name, macros))
  191. ext.define_macros = ext.define_macros + macros
  192. # determine extension languages
  193. if has_f_sources(ext.sources):
  194. ext_languages.add('f77')
  195. if has_cxx_sources(ext.sources):
  196. ext_languages.add('c++')
  197. l = ext.language or self.compiler.detect_language(ext.sources)
  198. if l:
  199. ext_languages.add(l)
  200. # reset language attribute for choosing proper linker
  201. if 'c++' in ext_languages:
  202. ext_language = 'c++'
  203. elif 'f90' in ext_languages:
  204. ext_language = 'f90'
  205. elif 'f77' in ext_languages:
  206. ext_language = 'f77'
  207. else:
  208. ext_language = 'c' # default
  209. if l and l != ext_language and ext.language:
  210. log.warn('resetting extension %r language from %r to %r.' %
  211. (ext.name, l, ext_language))
  212. ext.language = ext_language
  213. # global language
  214. all_languages.update(ext_languages)
  215. need_f90_compiler = 'f90' in all_languages
  216. need_f77_compiler = 'f77' in all_languages
  217. need_cxx_compiler = 'c++' in all_languages
  218. # Initialize C++ compiler:
  219. if need_cxx_compiler:
  220. self._cxx_compiler = new_compiler(compiler=compiler_type,
  221. verbose=self.verbose,
  222. dry_run=self.dry_run,
  223. force=self.force)
  224. compiler = self._cxx_compiler
  225. compiler.customize(self.distribution, need_cxx=need_cxx_compiler)
  226. compiler.customize_cmd(self)
  227. compiler.show_customization()
  228. self._cxx_compiler = compiler.cxx_compiler()
  229. else:
  230. self._cxx_compiler = None
  231. # Initialize Fortran 77 compiler:
  232. if need_f77_compiler:
  233. ctype = self.fcompiler
  234. self._f77_compiler = new_fcompiler(compiler=self.fcompiler,
  235. verbose=self.verbose,
  236. dry_run=self.dry_run,
  237. force=self.force,
  238. requiref90=False,
  239. c_compiler=self.compiler)
  240. fcompiler = self._f77_compiler
  241. if fcompiler:
  242. ctype = fcompiler.compiler_type
  243. fcompiler.customize(self.distribution)
  244. if fcompiler and fcompiler.get_version():
  245. fcompiler.customize_cmd(self)
  246. fcompiler.show_customization()
  247. else:
  248. self.warn('f77_compiler=%s is not available.' %
  249. (ctype))
  250. self._f77_compiler = None
  251. else:
  252. self._f77_compiler = None
  253. # Initialize Fortran 90 compiler:
  254. if need_f90_compiler:
  255. ctype = self.fcompiler
  256. self._f90_compiler = new_fcompiler(compiler=self.fcompiler,
  257. verbose=self.verbose,
  258. dry_run=self.dry_run,
  259. force=self.force,
  260. requiref90=True,
  261. c_compiler=self.compiler)
  262. fcompiler = self._f90_compiler
  263. if fcompiler:
  264. ctype = fcompiler.compiler_type
  265. fcompiler.customize(self.distribution)
  266. if fcompiler and fcompiler.get_version():
  267. fcompiler.customize_cmd(self)
  268. fcompiler.show_customization()
  269. else:
  270. self.warn('f90_compiler=%s is not available.' %
  271. (ctype))
  272. self._f90_compiler = None
  273. else:
  274. self._f90_compiler = None
  275. # Build extensions
  276. self.build_extensions()
  277. # Copy over any extra DLL files
  278. # FIXME: In the case where there are more than two packages,
  279. # we blindly assume that both packages need all of the libraries,
  280. # resulting in a larger wheel than is required. This should be fixed,
  281. # but it's so rare that I won't bother to handle it.
  282. pkg_roots = {
  283. self.get_ext_fullname(ext.name).split('.')[0]
  284. for ext in self.extensions
  285. }
  286. for pkg_root in pkg_roots:
  287. shared_lib_dir = os.path.join(pkg_root, '.libs')
  288. if not self.inplace:
  289. shared_lib_dir = os.path.join(self.build_lib, shared_lib_dir)
  290. for fn in os.listdir(self.extra_dll_dir):
  291. if not os.path.isdir(shared_lib_dir):
  292. os.makedirs(shared_lib_dir)
  293. if not fn.lower().endswith('.dll'):
  294. continue
  295. runtime_lib = os.path.join(self.extra_dll_dir, fn)
  296. copy_file(runtime_lib, shared_lib_dir)
  297. def swig_sources(self, sources, extensions=None):
  298. # Do nothing. Swig sources have been handled in build_src command.
  299. return sources
  300. def build_extension(self, ext):
  301. sources = ext.sources
  302. if sources is None or not is_sequence(sources):
  303. raise DistutilsSetupError(
  304. ("in 'ext_modules' option (extension '%s'), " +
  305. "'sources' must be present and must be " +
  306. "a list of source filenames") % ext.name)
  307. sources = list(sources)
  308. if not sources:
  309. return
  310. fullname = self.get_ext_fullname(ext.name)
  311. if self.inplace:
  312. modpath = fullname.split('.')
  313. package = '.'.join(modpath[0:-1])
  314. base = modpath[-1]
  315. build_py = self.get_finalized_command('build_py')
  316. package_dir = build_py.get_package_dir(package)
  317. ext_filename = os.path.join(package_dir,
  318. self.get_ext_filename(base))
  319. else:
  320. ext_filename = os.path.join(self.build_lib,
  321. self.get_ext_filename(fullname))
  322. depends = sources + ext.depends
  323. if not (self.force or newer_group(depends, ext_filename, 'newer')):
  324. log.debug("skipping '%s' extension (up-to-date)", ext.name)
  325. return
  326. else:
  327. log.info("building '%s' extension", ext.name)
  328. extra_args = ext.extra_compile_args or []
  329. macros = ext.define_macros[:]
  330. for undef in ext.undef_macros:
  331. macros.append((undef,))
  332. c_sources, cxx_sources, f_sources, fmodule_sources = \
  333. filter_sources(ext.sources)
  334. if self.compiler.compiler_type == 'msvc':
  335. if cxx_sources:
  336. # Needed to compile kiva.agg._agg extension.
  337. extra_args.append('/Zm1000')
  338. # this hack works around the msvc compiler attributes
  339. # problem, msvc uses its own convention :(
  340. c_sources += cxx_sources
  341. cxx_sources = []
  342. # Set Fortran/C++ compilers for compilation and linking.
  343. if ext.language == 'f90':
  344. fcompiler = self._f90_compiler
  345. elif ext.language == 'f77':
  346. fcompiler = self._f77_compiler
  347. else: # in case ext.language is c++, for instance
  348. fcompiler = self._f90_compiler or self._f77_compiler
  349. if fcompiler is not None:
  350. fcompiler.extra_f77_compile_args = (ext.extra_f77_compile_args or []) if hasattr(
  351. ext, 'extra_f77_compile_args') else []
  352. fcompiler.extra_f90_compile_args = (ext.extra_f90_compile_args or []) if hasattr(
  353. ext, 'extra_f90_compile_args') else []
  354. cxx_compiler = self._cxx_compiler
  355. # check for the availability of required compilers
  356. if cxx_sources and cxx_compiler is None:
  357. raise DistutilsError("extension %r has C++ sources"
  358. "but no C++ compiler found" % (ext.name))
  359. if (f_sources or fmodule_sources) and fcompiler is None:
  360. raise DistutilsError("extension %r has Fortran sources "
  361. "but no Fortran compiler found" % (ext.name))
  362. if ext.language in ['f77', 'f90'] and fcompiler is None:
  363. self.warn("extension %r has Fortran libraries "
  364. "but no Fortran linker found, using default linker" % (ext.name))
  365. if ext.language == 'c++' and cxx_compiler is None:
  366. self.warn("extension %r has C++ libraries "
  367. "but no C++ linker found, using default linker" % (ext.name))
  368. kws = {'depends': ext.depends}
  369. output_dir = self.build_temp
  370. include_dirs = ext.include_dirs + get_numpy_include_dirs()
  371. # filtering C dispatch-table sources when optimization is not disabled,
  372. # otherwise treated as normal sources.
  373. copt_c_sources = []
  374. copt_baseline_flags = []
  375. copt_macros = []
  376. if not self.disable_optimization:
  377. bsrc_dir = self.get_finalized_command("build_src").build_src
  378. dispatch_hpath = os.path.join("numpy", "distutils", "include")
  379. dispatch_hpath = os.path.join(bsrc_dir, dispatch_hpath)
  380. include_dirs.append(dispatch_hpath)
  381. copt_build_src = None if self.inplace else bsrc_dir
  382. copt_c_sources = [
  383. c_sources.pop(c_sources.index(src))
  384. for src in c_sources[:] if src.endswith(".dispatch.c")
  385. ]
  386. copt_baseline_flags = self.compiler_opt.cpu_baseline_flags()
  387. else:
  388. copt_macros.append(("NPY_DISABLE_OPTIMIZATION", 1))
  389. c_objects = []
  390. if copt_c_sources:
  391. log.info("compiling C dispatch-able sources")
  392. c_objects += self.compiler_opt.try_dispatch(copt_c_sources,
  393. output_dir=output_dir,
  394. src_dir=copt_build_src,
  395. macros=macros + copt_macros,
  396. include_dirs=include_dirs,
  397. debug=self.debug,
  398. extra_postargs=extra_args,
  399. **kws)
  400. if c_sources:
  401. log.info("compiling C sources")
  402. c_objects += self.compiler.compile(c_sources,
  403. output_dir=output_dir,
  404. macros=macros + copt_macros,
  405. include_dirs=include_dirs,
  406. debug=self.debug,
  407. extra_postargs=extra_args + copt_baseline_flags,
  408. **kws)
  409. if cxx_sources:
  410. log.info("compiling C++ sources")
  411. c_objects += cxx_compiler.compile(cxx_sources,
  412. output_dir=output_dir,
  413. macros=macros + copt_macros,
  414. include_dirs=include_dirs,
  415. debug=self.debug,
  416. extra_postargs=extra_args + copt_baseline_flags,
  417. **kws)
  418. extra_postargs = []
  419. f_objects = []
  420. if fmodule_sources:
  421. log.info("compiling Fortran 90 module sources")
  422. module_dirs = ext.module_dirs[:]
  423. module_build_dir = os.path.join(
  424. self.build_temp, os.path.dirname(
  425. self.get_ext_filename(fullname)))
  426. self.mkpath(module_build_dir)
  427. if fcompiler.module_dir_switch is None:
  428. existing_modules = glob('*.mod')
  429. extra_postargs += fcompiler.module_options(
  430. module_dirs, module_build_dir)
  431. f_objects += fcompiler.compile(fmodule_sources,
  432. output_dir=self.build_temp,
  433. macros=macros,
  434. include_dirs=include_dirs,
  435. debug=self.debug,
  436. extra_postargs=extra_postargs,
  437. depends=ext.depends)
  438. if fcompiler.module_dir_switch is None:
  439. for f in glob('*.mod'):
  440. if f in existing_modules:
  441. continue
  442. t = os.path.join(module_build_dir, f)
  443. if os.path.abspath(f) == os.path.abspath(t):
  444. continue
  445. if os.path.isfile(t):
  446. os.remove(t)
  447. try:
  448. self.move_file(f, module_build_dir)
  449. except DistutilsFileError:
  450. log.warn('failed to move %r to %r' %
  451. (f, module_build_dir))
  452. if f_sources:
  453. log.info("compiling Fortran sources")
  454. f_objects += fcompiler.compile(f_sources,
  455. output_dir=self.build_temp,
  456. macros=macros,
  457. include_dirs=include_dirs,
  458. debug=self.debug,
  459. extra_postargs=extra_postargs,
  460. depends=ext.depends)
  461. if f_objects and not fcompiler.can_ccompiler_link(self.compiler):
  462. unlinkable_fobjects = f_objects
  463. objects = c_objects
  464. else:
  465. unlinkable_fobjects = []
  466. objects = c_objects + f_objects
  467. if ext.extra_objects:
  468. objects.extend(ext.extra_objects)
  469. extra_args = ext.extra_link_args or []
  470. libraries = self.get_libraries(ext)[:]
  471. library_dirs = ext.library_dirs[:]
  472. linker = self.compiler.link_shared_object
  473. # Always use system linker when using MSVC compiler.
  474. if self.compiler.compiler_type in ('msvc', 'intelw', 'intelemw'):
  475. # expand libraries with fcompiler libraries as we are
  476. # not using fcompiler linker
  477. self._libs_with_msvc_and_fortran(
  478. fcompiler, libraries, library_dirs)
  479. elif ext.language in ['f77', 'f90'] and fcompiler is not None:
  480. linker = fcompiler.link_shared_object
  481. if ext.language == 'c++' and cxx_compiler is not None:
  482. linker = cxx_compiler.link_shared_object
  483. if fcompiler is not None:
  484. objects, libraries = self._process_unlinkable_fobjects(
  485. objects, libraries,
  486. fcompiler, library_dirs,
  487. unlinkable_fobjects)
  488. linker(objects, ext_filename,
  489. libraries=libraries,
  490. library_dirs=library_dirs,
  491. runtime_library_dirs=ext.runtime_library_dirs,
  492. extra_postargs=extra_args,
  493. export_symbols=self.get_export_symbols(ext),
  494. debug=self.debug,
  495. build_temp=self.build_temp,
  496. target_lang=ext.language)
  497. def _add_dummy_mingwex_sym(self, c_sources):
  498. build_src = self.get_finalized_command("build_src").build_src
  499. build_clib = self.get_finalized_command("build_clib").build_clib
  500. objects = self.compiler.compile([os.path.join(build_src,
  501. "gfortran_vs2003_hack.c")],
  502. output_dir=self.build_temp)
  503. self.compiler.create_static_lib(
  504. objects, "_gfortran_workaround", output_dir=build_clib, debug=self.debug)
  505. def _process_unlinkable_fobjects(self, objects, libraries,
  506. fcompiler, library_dirs,
  507. unlinkable_fobjects):
  508. libraries = list(libraries)
  509. objects = list(objects)
  510. unlinkable_fobjects = list(unlinkable_fobjects)
  511. # Expand possible fake static libraries to objects;
  512. # make sure to iterate over a copy of the list as
  513. # "fake" libraries will be removed as they are
  514. # enountered
  515. for lib in libraries[:]:
  516. for libdir in library_dirs:
  517. fake_lib = os.path.join(libdir, lib + '.fobjects')
  518. if os.path.isfile(fake_lib):
  519. # Replace fake static library
  520. libraries.remove(lib)
  521. with open(fake_lib, 'r') as f:
  522. unlinkable_fobjects.extend(f.read().splitlines())
  523. # Expand C objects
  524. c_lib = os.path.join(libdir, lib + '.cobjects')
  525. with open(c_lib, 'r') as f:
  526. objects.extend(f.read().splitlines())
  527. # Wrap unlinkable objects to a linkable one
  528. if unlinkable_fobjects:
  529. fobjects = [os.path.abspath(obj) for obj in unlinkable_fobjects]
  530. wrapped = fcompiler.wrap_unlinkable_objects(
  531. fobjects, output_dir=self.build_temp,
  532. extra_dll_dir=self.extra_dll_dir)
  533. objects.extend(wrapped)
  534. return objects, libraries
  535. def _libs_with_msvc_and_fortran(self, fcompiler, c_libraries,
  536. c_library_dirs):
  537. if fcompiler is None:
  538. return
  539. for libname in c_libraries:
  540. if libname.startswith('msvc'):
  541. continue
  542. fileexists = False
  543. for libdir in c_library_dirs or []:
  544. libfile = os.path.join(libdir, '%s.lib' % (libname))
  545. if os.path.isfile(libfile):
  546. fileexists = True
  547. break
  548. if fileexists:
  549. continue
  550. # make g77-compiled static libs available to MSVC
  551. fileexists = False
  552. for libdir in c_library_dirs:
  553. libfile = os.path.join(libdir, 'lib%s.a' % (libname))
  554. if os.path.isfile(libfile):
  555. # copy libname.a file to name.lib so that MSVC linker
  556. # can find it
  557. libfile2 = os.path.join(self.build_temp, libname + '.lib')
  558. copy_file(libfile, libfile2)
  559. if self.build_temp not in c_library_dirs:
  560. c_library_dirs.append(self.build_temp)
  561. fileexists = True
  562. break
  563. if fileexists:
  564. continue
  565. log.warn('could not find library %r in directories %s'
  566. % (libname, c_library_dirs))
  567. # Always use system linker when using MSVC compiler.
  568. f_lib_dirs = []
  569. for dir in fcompiler.library_dirs:
  570. # correct path when compiling in Cygwin but with normal Win
  571. # Python
  572. if dir.startswith('/usr/lib'):
  573. try:
  574. dir = subprocess.check_output(['cygpath', '-w', dir])
  575. except (OSError, subprocess.CalledProcessError):
  576. pass
  577. else:
  578. dir = filepath_from_subprocess_output(dir)
  579. f_lib_dirs.append(dir)
  580. c_library_dirs.extend(f_lib_dirs)
  581. # make g77-compiled static libs available to MSVC
  582. for lib in fcompiler.libraries:
  583. if not lib.startswith('msvc'):
  584. c_libraries.append(lib)
  585. p = combine_paths(f_lib_dirs, 'lib' + lib + '.a')
  586. if p:
  587. dst_name = os.path.join(self.build_temp, lib + '.lib')
  588. if not os.path.isfile(dst_name):
  589. copy_file(p[0], dst_name)
  590. if self.build_temp not in c_library_dirs:
  591. c_library_dirs.append(self.build_temp)
  592. def get_source_files(self):
  593. self.check_extensions_list(self.extensions)
  594. filenames = []
  595. for ext in self.extensions:
  596. filenames.extend(get_ext_source_files(ext))
  597. return filenames
  598. def get_outputs(self):
  599. self.check_extensions_list(self.extensions)
  600. outputs = []
  601. for ext in self.extensions:
  602. if not ext.sources:
  603. continue
  604. fullname = self.get_ext_fullname(ext.name)
  605. outputs.append(os.path.join(self.build_lib,
  606. self.get_ext_filename(fullname)))
  607. return outputs