cygwinccompiler.py 15 KB

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