Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cygwinccompiler.py 17KB

1 year ago
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. from subprocess import Popen, PIPE, check_output
  51. import re
  52. from distutils.unixccompiler import UnixCCompiler
  53. from distutils.file_util import write_file
  54. from distutils.errors import (DistutilsExecError, CCompilerError,
  55. CompileError, UnknownFileError)
  56. from distutils.version import LooseVersion
  57. from distutils.spawn import find_executable
  58. def get_msvcr():
  59. """Include the appropriate MSVC runtime library if Python was built
  60. with MSVC 7.0 or later.
  61. """
  62. msc_pos = sys.version.find('MSC v.')
  63. if msc_pos != -1:
  64. msc_ver = sys.version[msc_pos+6:msc_pos+10]
  65. if msc_ver == '1300':
  66. # MSVC 7.0
  67. return ['msvcr70']
  68. elif msc_ver == '1310':
  69. # MSVC 7.1
  70. return ['msvcr71']
  71. elif msc_ver == '1400':
  72. # VS2005 / MSVC 8.0
  73. return ['msvcr80']
  74. elif msc_ver == '1500':
  75. # VS2008 / MSVC 9.0
  76. return ['msvcr90']
  77. elif msc_ver == '1600':
  78. # VS2010 / MSVC 10.0
  79. return ['msvcr100']
  80. else:
  81. raise ValueError("Unknown MS Compiler version %s " % msc_ver)
  82. class CygwinCCompiler(UnixCCompiler):
  83. """ Handles the Cygwin port of the GNU C compiler to Windows.
  84. """
  85. compiler_type = 'cygwin'
  86. obj_extension = ".o"
  87. static_lib_extension = ".a"
  88. shared_lib_extension = ".dll"
  89. static_lib_format = "lib%s%s"
  90. shared_lib_format = "%s%s"
  91. exe_extension = ".exe"
  92. def __init__(self, verbose=0, dry_run=0, force=0):
  93. UnixCCompiler.__init__(self, verbose, dry_run, force)
  94. status, details = check_config_h()
  95. self.debug_print("Python's GCC status: %s (details: %s)" %
  96. (status, details))
  97. if status is not CONFIG_H_OK:
  98. self.warn(
  99. "Python's pyconfig.h doesn't seem to support your compiler. "
  100. "Reason: %s. "
  101. "Compiling may fail because of undefined preprocessor macros."
  102. % details)
  103. self.cc = os.environ.get('CC', 'gcc')
  104. self.cxx = os.environ.get('CXX', 'g++')
  105. if ('gcc' in self.cc): # Start gcc workaround
  106. self.gcc_version, self.ld_version, self.dllwrap_version = \
  107. get_versions()
  108. self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
  109. (self.gcc_version,
  110. self.ld_version,
  111. self.dllwrap_version) )
  112. # ld_version >= "2.10.90" and < "2.13" should also be able to use
  113. # gcc -mdll instead of dllwrap
  114. # Older dllwraps had own version numbers, newer ones use the
  115. # same as the rest of binutils ( also ld )
  116. # dllwrap 2.10.90 is buggy
  117. if self.ld_version >= "2.10.90":
  118. self.linker_dll = self.cc
  119. else:
  120. self.linker_dll = "dllwrap"
  121. # ld_version >= "2.13" support -shared so use it instead of
  122. # -mdll -static
  123. if self.ld_version >= "2.13":
  124. shared_option = "-shared"
  125. else:
  126. shared_option = "-mdll -static"
  127. else: # Assume linker is up to date
  128. self.linker_dll = self.cc
  129. shared_option = "-shared"
  130. self.set_executables(compiler='%s -mcygwin -O -Wall' % self.cc,
  131. compiler_so='%s -mcygwin -mdll -O -Wall' % self.cc,
  132. compiler_cxx='%s -mcygwin -O -Wall' % self.cxx,
  133. linker_exe='%s -mcygwin' % self.cc,
  134. linker_so=('%s -mcygwin %s' %
  135. (self.linker_dll, shared_option)))
  136. # cygwin and mingw32 need different sets of libraries
  137. if ('gcc' in self.cc and self.gcc_version == "2.91.57"):
  138. # cygwin shouldn't need msvcrt, but without the dlls will crash
  139. # (gcc version 2.91.57) -- perhaps something about initialization
  140. self.dll_libraries=["msvcrt"]
  141. self.warn(
  142. "Consider upgrading to a newer version of gcc")
  143. else:
  144. # Include the appropriate MSVC runtime library if Python was built
  145. # with MSVC 7.0 or later.
  146. self.dll_libraries = get_msvcr()
  147. def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
  148. """Compiles the source by spawning GCC and windres if needed."""
  149. if ext == '.rc' or ext == '.res':
  150. # gcc needs '.res' and '.rc' compiled to object files !!!
  151. try:
  152. self.spawn(["windres", "-i", src, "-o", obj])
  153. except DistutilsExecError as msg:
  154. raise CompileError(msg)
  155. else: # for other files use the C-compiler
  156. try:
  157. self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
  158. extra_postargs)
  159. except DistutilsExecError as msg:
  160. raise CompileError(msg)
  161. def link(self, target_desc, objects, output_filename, output_dir=None,
  162. libraries=None, library_dirs=None, runtime_library_dirs=None,
  163. export_symbols=None, debug=0, extra_preargs=None,
  164. extra_postargs=None, build_temp=None, target_lang=None):
  165. """Link the objects."""
  166. # use separate copies, so we can modify the lists
  167. extra_preargs = copy.copy(extra_preargs or [])
  168. libraries = copy.copy(libraries or [])
  169. objects = copy.copy(objects or [])
  170. # Additional libraries
  171. libraries.extend(self.dll_libraries)
  172. # handle export symbols by creating a def-file
  173. # with executables this only works with gcc/ld as linker
  174. if ((export_symbols is not None) and
  175. (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
  176. # (The linker doesn't do anything if output is up-to-date.
  177. # So it would probably better to check if we really need this,
  178. # but for this we had to insert some unchanged parts of
  179. # UnixCCompiler, and this is not what we want.)
  180. # we want to put some files in the same directory as the
  181. # object files are, build_temp doesn't help much
  182. # where are the object files
  183. temp_dir = os.path.dirname(objects[0])
  184. # name of dll to give the helper files the same base name
  185. (dll_name, dll_extension) = os.path.splitext(
  186. os.path.basename(output_filename))
  187. # generate the filenames for these files
  188. def_file = os.path.join(temp_dir, dll_name + ".def")
  189. lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a")
  190. # Generate .def file
  191. contents = [
  192. "LIBRARY %s" % os.path.basename(output_filename),
  193. "EXPORTS"]
  194. for sym in export_symbols:
  195. contents.append(sym)
  196. self.execute(write_file, (def_file, contents),
  197. "writing %s" % def_file)
  198. # next add options for def-file and to creating import libraries
  199. # dllwrap uses different options than gcc/ld
  200. if self.linker_dll == "dllwrap":
  201. extra_preargs.extend(["--output-lib", lib_file])
  202. # for dllwrap we have to use a special option
  203. extra_preargs.extend(["--def", def_file])
  204. # we use gcc/ld here and can be sure ld is >= 2.9.10
  205. else:
  206. # doesn't work: bfd_close build\...\libfoo.a: Invalid operation
  207. #extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
  208. # for gcc/ld the def-file is specified as any object files
  209. objects.append(def_file)
  210. #end: if ((export_symbols is not None) and
  211. # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
  212. # who wants symbols and a many times larger output file
  213. # should explicitly switch the debug mode on
  214. # otherwise we let dllwrap/ld strip the output file
  215. # (On my machine: 10KiB < stripped_file < ??100KiB
  216. # unstripped_file = stripped_file + XXX KiB
  217. # ( XXX=254 for a typical python extension))
  218. if not debug:
  219. extra_preargs.append("-s")
  220. UnixCCompiler.link(self, target_desc, objects, output_filename,
  221. output_dir, libraries, library_dirs,
  222. runtime_library_dirs,
  223. None, # export_symbols, we do this in our def-file
  224. debug, extra_preargs, extra_postargs, build_temp,
  225. target_lang)
  226. # -- Miscellaneous methods -----------------------------------------
  227. def object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
  228. """Adds supports for rc and res files."""
  229. if output_dir is None:
  230. output_dir = ''
  231. obj_names = []
  232. for src_name in source_filenames:
  233. # use normcase to make sure '.rc' is really '.rc' and not '.RC'
  234. base, ext = os.path.splitext(os.path.normcase(src_name))
  235. if ext not in (self.src_extensions + ['.rc','.res']):
  236. raise UnknownFileError("unknown file type '%s' (from '%s')" % \
  237. (ext, src_name))
  238. if strip_dir:
  239. base = os.path.basename (base)
  240. if ext in ('.res', '.rc'):
  241. # these need to be compiled to object files
  242. obj_names.append (os.path.join(output_dir,
  243. base + ext + self.obj_extension))
  244. else:
  245. obj_names.append (os.path.join(output_dir,
  246. base + self.obj_extension))
  247. return obj_names
  248. # the same as cygwin plus some additional parameters
  249. class Mingw32CCompiler(CygwinCCompiler):
  250. """ Handles the Mingw32 port of the GNU C compiler to Windows.
  251. """
  252. compiler_type = 'mingw32'
  253. def __init__(self, verbose=0, dry_run=0, force=0):
  254. CygwinCCompiler.__init__ (self, verbose, dry_run, force)
  255. # ld_version >= "2.13" support -shared so use it instead of
  256. # -mdll -static
  257. if ('gcc' in self.cc and self.ld_version < "2.13"):
  258. shared_option = "-mdll -static"
  259. else:
  260. shared_option = "-shared"
  261. # A real mingw32 doesn't need to specify a different entry point,
  262. # but cygwin 2.91.57 in no-cygwin-mode needs it.
  263. if ('gcc' in self.cc and self.gcc_version <= "2.91.57"):
  264. entry_point = '--entry _DllMain@12'
  265. else:
  266. entry_point = ''
  267. if is_cygwincc(self.cc):
  268. raise CCompilerError(
  269. 'Cygwin gcc cannot be used with --compiler=mingw32')
  270. self.set_executables(compiler='%s -O -Wall' % self.cc,
  271. compiler_so='%s -mdll -O -Wall' % self.cc,
  272. compiler_cxx='%s -O -Wall' % self.cxx,
  273. linker_exe='%s' % self.cc,
  274. linker_so='%s %s %s'
  275. % (self.linker_dll, shared_option,
  276. entry_point))
  277. # Maybe we should also append -mthreads, but then the finished
  278. # dlls need another dll (mingwm10.dll see Mingw32 docs)
  279. # (-mthreads: Support thread-safe exception handling on `Mingw32')
  280. # no additional libraries needed
  281. self.dll_libraries=[]
  282. # Include the appropriate MSVC runtime library if Python was built
  283. # with MSVC 7.0 or later.
  284. self.dll_libraries = get_msvcr()
  285. # Because these compilers aren't configured in Python's pyconfig.h file by
  286. # default, we should at least warn the user if he is using an unmodified
  287. # version.
  288. CONFIG_H_OK = "ok"
  289. CONFIG_H_NOTOK = "not ok"
  290. CONFIG_H_UNCERTAIN = "uncertain"
  291. def check_config_h():
  292. """Check if the current Python installation appears amenable to building
  293. extensions with GCC.
  294. Returns a tuple (status, details), where 'status' is one of the following
  295. constants:
  296. - CONFIG_H_OK: all is well, go ahead and compile
  297. - CONFIG_H_NOTOK: doesn't look good
  298. - CONFIG_H_UNCERTAIN: not sure -- unable to read pyconfig.h
  299. 'details' is a human-readable string explaining the situation.
  300. Note there are two ways to conclude "OK": either 'sys.version' contains
  301. the string "GCC" (implying that this Python was built with GCC), or the
  302. installed "pyconfig.h" contains the string "__GNUC__".
  303. """
  304. # XXX since this function also checks sys.version, it's not strictly a
  305. # "pyconfig.h" check -- should probably be renamed...
  306. from distutils import sysconfig
  307. # if sys.version contains GCC then python was compiled with GCC, and the
  308. # pyconfig.h file should be OK
  309. if "GCC" in sys.version:
  310. return CONFIG_H_OK, "sys.version mentions 'GCC'"
  311. # Clang would also work
  312. if "Clang" in sys.version:
  313. return CONFIG_H_OK, "sys.version mentions 'Clang'"
  314. # let's see if __GNUC__ is mentioned in python.h
  315. fn = sysconfig.get_config_h_filename()
  316. try:
  317. config_h = open(fn)
  318. try:
  319. if "__GNUC__" in config_h.read():
  320. return CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn
  321. else:
  322. return CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn
  323. finally:
  324. config_h.close()
  325. except OSError as exc:
  326. return (CONFIG_H_UNCERTAIN,
  327. "couldn't read '%s': %s" % (fn, exc.strerror))
  328. RE_VERSION = re.compile(br'(\d+\.\d+(\.\d+)*)')
  329. def _find_exe_version(cmd):
  330. """Find the version of an executable by running `cmd` in the shell.
  331. If the command is not found, or the output does not match
  332. `RE_VERSION`, returns None.
  333. """
  334. executable = cmd.split()[0]
  335. if find_executable(executable) is None:
  336. return None
  337. out = Popen(cmd, shell=True, stdout=PIPE).stdout
  338. try:
  339. out_string = out.read()
  340. finally:
  341. out.close()
  342. result = RE_VERSION.search(out_string)
  343. if result is None:
  344. return None
  345. # LooseVersion works with strings
  346. # so we need to decode our bytes
  347. return LooseVersion(result.group(1).decode())
  348. def get_versions():
  349. """ Try to find out the versions of gcc, ld and dllwrap.
  350. If not possible it returns None for it.
  351. """
  352. commands = ['gcc -dumpversion', 'ld -v', 'dllwrap --version']
  353. return tuple([_find_exe_version(cmd) for cmd in commands])
  354. def is_cygwincc(cc):
  355. '''Try to determine if the compiler that would be used is from cygwin.'''
  356. out_string = check_output([cc, '-dumpmachine'])
  357. return out_string.strip().endswith(b'cygwin')