unixccompiler.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. """
  2. unixccompiler - can handle very long argument lists for ar.
  3. """
  4. import os
  5. from distutils.errors import CompileError, DistutilsExecError, LibError
  6. from distutils.unixccompiler import UnixCCompiler
  7. from numpy.distutils.ccompiler import replace_method
  8. from numpy.distutils.misc_util import _commandline_dep_string
  9. from numpy.distutils import log
  10. # Note that UnixCCompiler._compile appeared in Python 2.3
  11. def UnixCCompiler__compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
  12. """Compile a single source files with a Unix-style compiler."""
  13. # HP ad-hoc fix, see ticket 1383
  14. ccomp = self.compiler_so
  15. if ccomp[0] == 'aCC':
  16. # remove flags that will trigger ANSI-C mode for aCC
  17. if '-Ae' in ccomp:
  18. ccomp.remove('-Ae')
  19. if '-Aa' in ccomp:
  20. ccomp.remove('-Aa')
  21. # add flags for (almost) sane C++ handling
  22. ccomp += ['-AA']
  23. self.compiler_so = ccomp
  24. # ensure OPT environment variable is read
  25. if 'OPT' in os.environ:
  26. from distutils.sysconfig import get_config_vars
  27. opt = " ".join(os.environ['OPT'].split())
  28. gcv_opt = " ".join(get_config_vars('OPT')[0].split())
  29. ccomp_s = " ".join(self.compiler_so)
  30. if opt not in ccomp_s:
  31. ccomp_s = ccomp_s.replace(gcv_opt, opt)
  32. self.compiler_so = ccomp_s.split()
  33. llink_s = " ".join(self.linker_so)
  34. if opt not in llink_s:
  35. self.linker_so = llink_s.split() + opt.split()
  36. display = '%s: %s' % (os.path.basename(self.compiler_so[0]), src)
  37. # gcc style automatic dependencies, outputs a makefile (-MF) that lists
  38. # all headers needed by a c file as a side effect of compilation (-MMD)
  39. if getattr(self, '_auto_depends', False):
  40. deps = ['-MMD', '-MF', obj + '.d']
  41. else:
  42. deps = []
  43. try:
  44. self.spawn(self.compiler_so + cc_args + [src, '-o', obj] + deps +
  45. extra_postargs, display = display)
  46. except DistutilsExecError as e:
  47. msg = str(e)
  48. raise CompileError(msg)
  49. # add commandline flags to dependency file
  50. if deps:
  51. with open(obj + '.d', 'a') as f:
  52. f.write(_commandline_dep_string(cc_args, extra_postargs, pp_opts))
  53. replace_method(UnixCCompiler, '_compile', UnixCCompiler__compile)
  54. def UnixCCompiler_create_static_lib(self, objects, output_libname,
  55. output_dir=None, debug=0, target_lang=None):
  56. """
  57. Build a static library in a separate sub-process.
  58. Parameters
  59. ----------
  60. objects : list or tuple of str
  61. List of paths to object files used to build the static library.
  62. output_libname : str
  63. The library name as an absolute or relative (if `output_dir` is used)
  64. path.
  65. output_dir : str, optional
  66. The path to the output directory. Default is None, in which case
  67. the ``output_dir`` attribute of the UnixCCompiler instance.
  68. debug : bool, optional
  69. This parameter is not used.
  70. target_lang : str, optional
  71. This parameter is not used.
  72. Returns
  73. -------
  74. None
  75. """
  76. objects, output_dir = self._fix_object_args(objects, output_dir)
  77. output_filename = \
  78. self.library_filename(output_libname, output_dir=output_dir)
  79. if self._need_link(objects, output_filename):
  80. try:
  81. # previous .a may be screwed up; best to remove it first
  82. # and recreate.
  83. # Also, ar on OS X doesn't handle updating universal archives
  84. os.unlink(output_filename)
  85. except (IOError, OSError):
  86. pass
  87. self.mkpath(os.path.dirname(output_filename))
  88. tmp_objects = objects + self.objects
  89. while tmp_objects:
  90. objects = tmp_objects[:50]
  91. tmp_objects = tmp_objects[50:]
  92. display = '%s: adding %d object files to %s' % (
  93. os.path.basename(self.archiver[0]),
  94. len(objects), output_filename)
  95. self.spawn(self.archiver + [output_filename] + objects,
  96. display = display)
  97. # Not many Unices required ranlib anymore -- SunOS 4.x is, I
  98. # think the only major Unix that does. Maybe we need some
  99. # platform intelligence here to skip ranlib if it's not
  100. # needed -- or maybe Python's configure script took care of
  101. # it for us, hence the check for leading colon.
  102. if self.ranlib:
  103. display = '%s:@ %s' % (os.path.basename(self.ranlib[0]),
  104. output_filename)
  105. try:
  106. self.spawn(self.ranlib + [output_filename],
  107. display = display)
  108. except DistutilsExecError as e:
  109. msg = str(e)
  110. raise LibError(msg)
  111. else:
  112. log.debug("skipping %s (up-to-date)", output_filename)
  113. return
  114. replace_method(UnixCCompiler, 'create_static_lib',
  115. UnixCCompiler_create_static_lib)