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