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