cygwinccompiler.py 17 KB

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