cygwinccompiler.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. """distutils.cygwinccompiler
  2. Provides the CygwinCCompiler class, a subclass of UnixCCompiler that
  3. handles the Cygwin port of the GNU C compiler to Windows. It also contains
  4. the Mingw32CCompiler class which handles the mingw32 port of GCC (same as
  5. cygwin in no-cygwin mode).
  6. """
  7. import os
  8. import re
  9. import sys
  10. import copy
  11. import shlex
  12. import warnings
  13. from subprocess import check_output
  14. from .unixccompiler import UnixCCompiler
  15. from .file_util import write_file
  16. from .errors import (
  17. DistutilsExecError,
  18. DistutilsPlatformError,
  19. CCompilerError,
  20. CompileError,
  21. )
  22. from .version import LooseVersion, suppress_known_deprecation
  23. from ._collections import RangeMap
  24. _msvcr_lookup = RangeMap.left(
  25. {
  26. # MSVC 7.0
  27. 1300: ['msvcr70'],
  28. # MSVC 7.1
  29. 1310: ['msvcr71'],
  30. # VS2005 / MSVC 8.0
  31. 1400: ['msvcr80'],
  32. # VS2008 / MSVC 9.0
  33. 1500: ['msvcr90'],
  34. # VS2010 / MSVC 10.0
  35. 1600: ['msvcr100'],
  36. # VS2012 / MSVC 11.0
  37. 1700: ['msvcr110'],
  38. # VS2013 / MSVC 12.0
  39. 1800: ['msvcr120'],
  40. # VS2015 / MSVC 14.0
  41. 1900: ['vcruntime140'],
  42. 2000: RangeMap.undefined_value,
  43. },
  44. )
  45. def get_msvcr():
  46. """Include the appropriate MSVC runtime library if Python was built
  47. with MSVC 7.0 or later.
  48. """
  49. match = re.search(r'MSC v\.(\d{4})', sys.version)
  50. try:
  51. msc_ver = int(match.group(1))
  52. except AttributeError:
  53. return
  54. try:
  55. return _msvcr_lookup[msc_ver]
  56. except KeyError:
  57. raise ValueError("Unknown MS Compiler version %s " % msc_ver)
  58. _runtime_library_dirs_msg = (
  59. "Unable to set runtime library search path on Windows, "
  60. "usually indicated by `runtime_library_dirs` parameter to Extension"
  61. )
  62. class CygwinCCompiler(UnixCCompiler):
  63. """Handles the Cygwin port of the GNU C compiler to Windows."""
  64. compiler_type = 'cygwin'
  65. obj_extension = ".o"
  66. static_lib_extension = ".a"
  67. shared_lib_extension = ".dll.a"
  68. dylib_lib_extension = ".dll"
  69. static_lib_format = "lib%s%s"
  70. shared_lib_format = "lib%s%s"
  71. dylib_lib_format = "cyg%s%s"
  72. exe_extension = ".exe"
  73. def __init__(self, verbose=0, dry_run=0, force=0):
  74. super().__init__(verbose, dry_run, force)
  75. status, details = check_config_h()
  76. self.debug_print(
  77. "Python's GCC status: {} (details: {})".format(status, details)
  78. )
  79. if status is not CONFIG_H_OK:
  80. self.warn(
  81. "Python's pyconfig.h doesn't seem to support your compiler. "
  82. "Reason: %s. "
  83. "Compiling may fail because of undefined preprocessor macros." % details
  84. )
  85. self.cc = os.environ.get('CC', 'gcc')
  86. self.cxx = os.environ.get('CXX', 'g++')
  87. self.linker_dll = self.cc
  88. shared_option = "-shared"
  89. self.set_executables(
  90. compiler='%s -mcygwin -O -Wall' % self.cc,
  91. compiler_so='%s -mcygwin -mdll -O -Wall' % self.cc,
  92. compiler_cxx='%s -mcygwin -O -Wall' % self.cxx,
  93. linker_exe='%s -mcygwin' % self.cc,
  94. linker_so=('{} -mcygwin {}'.format(self.linker_dll, shared_option)),
  95. )
  96. # Include the appropriate MSVC runtime library if Python was built
  97. # with MSVC 7.0 or later.
  98. self.dll_libraries = get_msvcr()
  99. @property
  100. def gcc_version(self):
  101. # Older numpy depended on this existing to check for ancient
  102. # gcc versions. This doesn't make much sense with clang etc so
  103. # just hardcode to something recent.
  104. # https://github.com/numpy/numpy/pull/20333
  105. warnings.warn(
  106. "gcc_version attribute of CygwinCCompiler is deprecated. "
  107. "Instead of returning actual gcc version a fixed value 11.2.0 is returned.",
  108. DeprecationWarning,
  109. stacklevel=2,
  110. )
  111. with suppress_known_deprecation():
  112. return LooseVersion("11.2.0")
  113. def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
  114. """Compiles the source by spawning GCC and windres if needed."""
  115. if ext in ('.rc', '.res'):
  116. # gcc needs '.res' and '.rc' compiled to object files !!!
  117. try:
  118. self.spawn(["windres", "-i", src, "-o", obj])
  119. except DistutilsExecError as msg:
  120. raise CompileError(msg)
  121. else: # for other files use the C-compiler
  122. try:
  123. self.spawn(
  124. self.compiler_so + cc_args + [src, '-o', obj] + extra_postargs
  125. )
  126. except DistutilsExecError as msg:
  127. raise CompileError(msg)
  128. def link(
  129. self,
  130. target_desc,
  131. objects,
  132. output_filename,
  133. output_dir=None,
  134. libraries=None,
  135. library_dirs=None,
  136. runtime_library_dirs=None,
  137. export_symbols=None,
  138. debug=0,
  139. extra_preargs=None,
  140. extra_postargs=None,
  141. build_temp=None,
  142. target_lang=None,
  143. ):
  144. """Link the objects."""
  145. # use separate copies, so we can modify the lists
  146. extra_preargs = copy.copy(extra_preargs or [])
  147. libraries = copy.copy(libraries or [])
  148. objects = copy.copy(objects or [])
  149. if runtime_library_dirs:
  150. self.warn(_runtime_library_dirs_msg)
  151. # Additional libraries
  152. libraries.extend(self.dll_libraries)
  153. # handle export symbols by creating a def-file
  154. # with executables this only works with gcc/ld as linker
  155. if (export_symbols is not None) and (
  156. target_desc != self.EXECUTABLE or self.linker_dll == "gcc"
  157. ):
  158. # (The linker doesn't do anything if output is up-to-date.
  159. # So it would probably better to check if we really need this,
  160. # but for this we had to insert some unchanged parts of
  161. # UnixCCompiler, and this is not what we want.)
  162. # we want to put some files in the same directory as the
  163. # object files are, build_temp doesn't help much
  164. # where are the object files
  165. temp_dir = os.path.dirname(objects[0])
  166. # name of dll to give the helper files the same base name
  167. (dll_name, dll_extension) = os.path.splitext(
  168. os.path.basename(output_filename)
  169. )
  170. # generate the filenames for these files
  171. def_file = os.path.join(temp_dir, dll_name + ".def")
  172. # Generate .def file
  173. contents = ["LIBRARY %s" % os.path.basename(output_filename), "EXPORTS"]
  174. for sym in export_symbols:
  175. contents.append(sym)
  176. self.execute(write_file, (def_file, contents), "writing %s" % def_file)
  177. # next add options for def-file
  178. # for gcc/ld the def-file is specified as any object files
  179. objects.append(def_file)
  180. # end: if ((export_symbols is not None) and
  181. # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
  182. # who wants symbols and a many times larger output file
  183. # should explicitly switch the debug mode on
  184. # otherwise we let ld strip the output file
  185. # (On my machine: 10KiB < stripped_file < ??100KiB
  186. # unstripped_file = stripped_file + XXX KiB
  187. # ( XXX=254 for a typical python extension))
  188. if not debug:
  189. extra_preargs.append("-s")
  190. UnixCCompiler.link(
  191. self,
  192. target_desc,
  193. objects,
  194. output_filename,
  195. output_dir,
  196. libraries,
  197. library_dirs,
  198. runtime_library_dirs,
  199. None, # export_symbols, we do this in our def-file
  200. debug,
  201. extra_preargs,
  202. extra_postargs,
  203. build_temp,
  204. target_lang,
  205. )
  206. def runtime_library_dir_option(self, dir):
  207. # cygwin doesn't support rpath. While in theory we could error
  208. # out like MSVC does, code might expect it to work like on Unix, so
  209. # just warn and hope for the best.
  210. self.warn(_runtime_library_dirs_msg)
  211. return []
  212. # -- Miscellaneous methods -----------------------------------------
  213. def _make_out_path(self, output_dir, strip_dir, src_name):
  214. # use normcase to make sure '.rc' is really '.rc' and not '.RC'
  215. norm_src_name = os.path.normcase(src_name)
  216. return super()._make_out_path(output_dir, strip_dir, norm_src_name)
  217. @property
  218. def out_extensions(self):
  219. """
  220. Add support for rc and res files.
  221. """
  222. return {
  223. **super().out_extensions,
  224. **{ext: ext + self.obj_extension for ext in ('.res', '.rc')},
  225. }
  226. # the same as cygwin plus some additional parameters
  227. class Mingw32CCompiler(CygwinCCompiler):
  228. """Handles the Mingw32 port of the GNU C compiler to Windows."""
  229. compiler_type = 'mingw32'
  230. def __init__(self, verbose=0, dry_run=0, force=0):
  231. super().__init__(verbose, dry_run, force)
  232. shared_option = "-shared"
  233. if is_cygwincc(self.cc):
  234. raise CCompilerError('Cygwin gcc cannot be used with --compiler=mingw32')
  235. self.set_executables(
  236. compiler='%s -O -Wall' % self.cc,
  237. compiler_so='%s -mdll -O -Wall' % self.cc,
  238. compiler_cxx='%s -O -Wall' % self.cxx,
  239. linker_exe='%s' % self.cc,
  240. linker_so='{} {}'.format(self.linker_dll, shared_option),
  241. )
  242. def runtime_library_dir_option(self, dir):
  243. raise DistutilsPlatformError(_runtime_library_dirs_msg)
  244. # Because these compilers aren't configured in Python's pyconfig.h file by
  245. # default, we should at least warn the user if he is using an unmodified
  246. # version.
  247. CONFIG_H_OK = "ok"
  248. CONFIG_H_NOTOK = "not ok"
  249. CONFIG_H_UNCERTAIN = "uncertain"
  250. def check_config_h():
  251. """Check if the current Python installation appears amenable to building
  252. extensions with GCC.
  253. Returns a tuple (status, details), where 'status' is one of the following
  254. constants:
  255. - CONFIG_H_OK: all is well, go ahead and compile
  256. - CONFIG_H_NOTOK: doesn't look good
  257. - CONFIG_H_UNCERTAIN: not sure -- unable to read pyconfig.h
  258. 'details' is a human-readable string explaining the situation.
  259. Note there are two ways to conclude "OK": either 'sys.version' contains
  260. the string "GCC" (implying that this Python was built with GCC), or the
  261. installed "pyconfig.h" contains the string "__GNUC__".
  262. """
  263. # XXX since this function also checks sys.version, it's not strictly a
  264. # "pyconfig.h" check -- should probably be renamed...
  265. from distutils import sysconfig
  266. # if sys.version contains GCC then python was compiled with GCC, and the
  267. # pyconfig.h file should be OK
  268. if "GCC" in sys.version:
  269. return CONFIG_H_OK, "sys.version mentions 'GCC'"
  270. # Clang would also work
  271. if "Clang" in sys.version:
  272. return CONFIG_H_OK, "sys.version mentions 'Clang'"
  273. # let's see if __GNUC__ is mentioned in python.h
  274. fn = sysconfig.get_config_h_filename()
  275. try:
  276. config_h = open(fn)
  277. try:
  278. if "__GNUC__" in config_h.read():
  279. return CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn
  280. else:
  281. return CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn
  282. finally:
  283. config_h.close()
  284. except OSError as exc:
  285. return (CONFIG_H_UNCERTAIN, "couldn't read '{}': {}".format(fn, exc.strerror))
  286. def is_cygwincc(cc):
  287. '''Try to determine if the compiler that would be used is from cygwin.'''
  288. out_string = check_output(shlex.split(cc) + ['-dumpmachine'])
  289. return out_string.strip().endswith(b'cygwin')
  290. get_versions = None
  291. """
  292. A stand-in for the previous get_versions() function to prevent failures
  293. when monkeypatched. See pypa/setuptools#2969.
  294. """