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