1
0

build_ext.py 26 KB

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