build_clib.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. """ Modified version of build_clib that handles fortran source files.
  2. """
  3. from __future__ import division, absolute_import, print_function
  4. import os
  5. from glob import glob
  6. import shutil
  7. from distutils.command.build_clib import build_clib as old_build_clib
  8. from distutils.errors import DistutilsSetupError, DistutilsError, \
  9. DistutilsFileError
  10. from numpy.distutils import log
  11. from distutils.dep_util import newer_group
  12. from numpy.distutils.misc_util import filter_sources, has_f_sources,\
  13. has_cxx_sources, all_strings, get_lib_source_files, is_sequence, \
  14. get_numpy_include_dirs
  15. # Fix Python distutils bug sf #1718574:
  16. _l = old_build_clib.user_options
  17. for _i in range(len(_l)):
  18. if _l[_i][0] in ['build-clib', 'build-temp']:
  19. _l[_i] = (_l[_i][0] + '=',) + _l[_i][1:]
  20. #
  21. class build_clib(old_build_clib):
  22. description = "build C/C++/F libraries used by Python extensions"
  23. user_options = old_build_clib.user_options + [
  24. ('fcompiler=', None,
  25. "specify the Fortran compiler type"),
  26. ('inplace', 'i', 'Build in-place'),
  27. ('parallel=', 'j',
  28. "number of parallel jobs"),
  29. ('warn-error', None,
  30. "turn all warnings into errors (-Werror)"),
  31. ]
  32. boolean_options = old_build_clib.boolean_options + ['inplace', 'warn-error']
  33. def initialize_options(self):
  34. old_build_clib.initialize_options(self)
  35. self.fcompiler = None
  36. self.inplace = 0
  37. self.parallel = None
  38. self.warn_error = None
  39. def finalize_options(self):
  40. if self.parallel:
  41. try:
  42. self.parallel = int(self.parallel)
  43. except ValueError:
  44. raise ValueError("--parallel/-j argument must be an integer")
  45. old_build_clib.finalize_options(self)
  46. self.set_undefined_options('build',
  47. ('parallel', 'parallel'),
  48. ('warn_error', 'warn_error'),
  49. )
  50. def have_f_sources(self):
  51. for (lib_name, build_info) in self.libraries:
  52. if has_f_sources(build_info.get('sources', [])):
  53. return True
  54. return False
  55. def have_cxx_sources(self):
  56. for (lib_name, build_info) in self.libraries:
  57. if has_cxx_sources(build_info.get('sources', [])):
  58. return True
  59. return False
  60. def run(self):
  61. if not self.libraries:
  62. return
  63. # Make sure that library sources are complete.
  64. languages = []
  65. # Make sure that extension sources are complete.
  66. self.run_command('build_src')
  67. for (lib_name, build_info) in self.libraries:
  68. l = build_info.get('language', None)
  69. if l and l not in languages:
  70. languages.append(l)
  71. from distutils.ccompiler import new_compiler
  72. self.compiler = new_compiler(compiler=self.compiler,
  73. dry_run=self.dry_run,
  74. force=self.force)
  75. self.compiler.customize(self.distribution,
  76. need_cxx=self.have_cxx_sources())
  77. if self.warn_error:
  78. self.compiler.compiler.append('-Werror')
  79. self.compiler.compiler_so.append('-Werror')
  80. libraries = self.libraries
  81. self.libraries = None
  82. self.compiler.customize_cmd(self)
  83. self.libraries = libraries
  84. self.compiler.show_customization()
  85. if self.have_f_sources():
  86. from numpy.distutils.fcompiler import new_fcompiler
  87. self._f_compiler = new_fcompiler(compiler=self.fcompiler,
  88. verbose=self.verbose,
  89. dry_run=self.dry_run,
  90. force=self.force,
  91. requiref90='f90' in languages,
  92. c_compiler=self.compiler)
  93. if self._f_compiler is not None:
  94. self._f_compiler.customize(self.distribution)
  95. libraries = self.libraries
  96. self.libraries = None
  97. self._f_compiler.customize_cmd(self)
  98. self.libraries = libraries
  99. self._f_compiler.show_customization()
  100. else:
  101. self._f_compiler = None
  102. self.build_libraries(self.libraries)
  103. if self.inplace:
  104. for l in self.distribution.installed_libraries:
  105. libname = self.compiler.library_filename(l.name)
  106. source = os.path.join(self.build_clib, libname)
  107. target = os.path.join(l.target_dir, libname)
  108. self.mkpath(l.target_dir)
  109. shutil.copy(source, target)
  110. def get_source_files(self):
  111. self.check_library_list(self.libraries)
  112. filenames = []
  113. for lib in self.libraries:
  114. filenames.extend(get_lib_source_files(lib))
  115. return filenames
  116. def build_libraries(self, libraries):
  117. for (lib_name, build_info) in libraries:
  118. self.build_a_library(build_info, lib_name, libraries)
  119. def build_a_library(self, build_info, lib_name, libraries):
  120. # default compilers
  121. compiler = self.compiler
  122. fcompiler = self._f_compiler
  123. sources = build_info.get('sources')
  124. if sources is None or not is_sequence(sources):
  125. raise DistutilsSetupError(("in 'libraries' option (library '%s'), " +
  126. "'sources' must be present and must be " +
  127. "a list of source filenames") % lib_name)
  128. sources = list(sources)
  129. c_sources, cxx_sources, f_sources, fmodule_sources \
  130. = filter_sources(sources)
  131. requiref90 = not not fmodule_sources or \
  132. build_info.get('language', 'c') == 'f90'
  133. # save source type information so that build_ext can use it.
  134. source_languages = []
  135. if c_sources:
  136. source_languages.append('c')
  137. if cxx_sources:
  138. source_languages.append('c++')
  139. if requiref90:
  140. source_languages.append('f90')
  141. elif f_sources:
  142. source_languages.append('f77')
  143. build_info['source_languages'] = source_languages
  144. lib_file = compiler.library_filename(lib_name,
  145. output_dir=self.build_clib)
  146. depends = sources + build_info.get('depends', [])
  147. if not (self.force or newer_group(depends, lib_file, 'newer')):
  148. log.debug("skipping '%s' library (up-to-date)", lib_name)
  149. return
  150. else:
  151. log.info("building '%s' library", lib_name)
  152. config_fc = build_info.get('config_fc', {})
  153. if fcompiler is not None and config_fc:
  154. log.info('using additional config_fc from setup script '
  155. 'for fortran compiler: %s'
  156. % (config_fc,))
  157. from numpy.distutils.fcompiler import new_fcompiler
  158. fcompiler = new_fcompiler(compiler=fcompiler.compiler_type,
  159. verbose=self.verbose,
  160. dry_run=self.dry_run,
  161. force=self.force,
  162. requiref90=requiref90,
  163. c_compiler=self.compiler)
  164. if fcompiler is not None:
  165. dist = self.distribution
  166. base_config_fc = dist.get_option_dict('config_fc').copy()
  167. base_config_fc.update(config_fc)
  168. fcompiler.customize(base_config_fc)
  169. # check availability of Fortran compilers
  170. if (f_sources or fmodule_sources) and fcompiler is None:
  171. raise DistutilsError("library %s has Fortran sources"
  172. " but no Fortran compiler found" % (lib_name))
  173. if fcompiler is not None:
  174. fcompiler.extra_f77_compile_args = build_info.get(
  175. 'extra_f77_compile_args') or []
  176. fcompiler.extra_f90_compile_args = build_info.get(
  177. 'extra_f90_compile_args') or []
  178. macros = build_info.get('macros')
  179. include_dirs = build_info.get('include_dirs')
  180. if include_dirs is None:
  181. include_dirs = []
  182. extra_postargs = build_info.get('extra_compiler_args') or []
  183. include_dirs.extend(get_numpy_include_dirs())
  184. # where compiled F90 module files are:
  185. module_dirs = build_info.get('module_dirs') or []
  186. module_build_dir = os.path.dirname(lib_file)
  187. if requiref90:
  188. self.mkpath(module_build_dir)
  189. if compiler.compiler_type == 'msvc':
  190. # this hack works around the msvc compiler attributes
  191. # problem, msvc uses its own convention :(
  192. c_sources += cxx_sources
  193. cxx_sources = []
  194. objects = []
  195. if c_sources:
  196. log.info("compiling C sources")
  197. objects = compiler.compile(c_sources,
  198. output_dir=self.build_temp,
  199. macros=macros,
  200. include_dirs=include_dirs,
  201. debug=self.debug,
  202. extra_postargs=extra_postargs)
  203. if cxx_sources:
  204. log.info("compiling C++ sources")
  205. cxx_compiler = compiler.cxx_compiler()
  206. cxx_objects = cxx_compiler.compile(cxx_sources,
  207. output_dir=self.build_temp,
  208. macros=macros,
  209. include_dirs=include_dirs,
  210. debug=self.debug,
  211. extra_postargs=extra_postargs)
  212. objects.extend(cxx_objects)
  213. if f_sources or fmodule_sources:
  214. extra_postargs = []
  215. f_objects = []
  216. if requiref90:
  217. if fcompiler.module_dir_switch is None:
  218. existing_modules = glob('*.mod')
  219. extra_postargs += fcompiler.module_options(
  220. module_dirs, module_build_dir)
  221. if fmodule_sources:
  222. log.info("compiling Fortran 90 module sources")
  223. f_objects += fcompiler.compile(fmodule_sources,
  224. output_dir=self.build_temp,
  225. macros=macros,
  226. include_dirs=include_dirs,
  227. debug=self.debug,
  228. extra_postargs=extra_postargs)
  229. if requiref90 and self._f_compiler.module_dir_switch is None:
  230. # move new compiled F90 module files to module_build_dir
  231. for f in glob('*.mod'):
  232. if f in existing_modules:
  233. continue
  234. t = os.path.join(module_build_dir, f)
  235. if os.path.abspath(f) == os.path.abspath(t):
  236. continue
  237. if os.path.isfile(t):
  238. os.remove(t)
  239. try:
  240. self.move_file(f, module_build_dir)
  241. except DistutilsFileError:
  242. log.warn('failed to move %r to %r'
  243. % (f, module_build_dir))
  244. if f_sources:
  245. log.info("compiling Fortran sources")
  246. f_objects += fcompiler.compile(f_sources,
  247. output_dir=self.build_temp,
  248. macros=macros,
  249. include_dirs=include_dirs,
  250. debug=self.debug,
  251. extra_postargs=extra_postargs)
  252. else:
  253. f_objects = []
  254. if f_objects and not fcompiler.can_ccompiler_link(compiler):
  255. # Default linker cannot link Fortran object files, and results
  256. # need to be wrapped later. Instead of creating a real static
  257. # library, just keep track of the object files.
  258. listfn = os.path.join(self.build_clib,
  259. lib_name + '.fobjects')
  260. with open(listfn, 'w') as f:
  261. f.write("\n".join(os.path.abspath(obj) for obj in f_objects))
  262. listfn = os.path.join(self.build_clib,
  263. lib_name + '.cobjects')
  264. with open(listfn, 'w') as f:
  265. f.write("\n".join(os.path.abspath(obj) for obj in objects))
  266. # create empty "library" file for dependency tracking
  267. lib_fname = os.path.join(self.build_clib,
  268. lib_name + compiler.static_lib_extension)
  269. with open(lib_fname, 'wb') as f:
  270. pass
  271. else:
  272. # assume that default linker is suitable for
  273. # linking Fortran object files
  274. objects.extend(f_objects)
  275. compiler.create_static_lib(objects, lib_name,
  276. output_dir=self.build_clib,
  277. debug=self.debug)
  278. # fix library dependencies
  279. clib_libraries = build_info.get('libraries', [])
  280. for lname, binfo in libraries:
  281. if lname in clib_libraries:
  282. clib_libraries.extend(binfo.get('libraries', []))
  283. if clib_libraries:
  284. build_info['libraries'] = clib_libraries