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