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