cygwin.py 12 KB

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