unixccompiler.py 5.3 KB

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