cygwinccompiler.py 12 KB

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