unixccompiler.py 5.3 KB

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