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 | # |
Greg Ward | 7483d68 | 2000-09-01 01:24:31 +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: |
| 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 |
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 |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +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. |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 42 | # *** only the version of June 2000 shows these problems |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 43 | |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +0000 | [diff] [blame] | 44 | # created 2000/05/05, Rene Liebscher |
| 45 | |
| 46 | __revision__ = "$Id$" |
| 47 | |
Greg Ward | b1dceae | 2000-08-13 00:43:56 +0000 | [diff] [blame] | 48 | import os,sys,copy |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 49 | from distutils.ccompiler import gen_preprocess_options, gen_lib_options |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +0000 | [diff] [blame] | 50 | from distutils.unixccompiler import UnixCCompiler |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 51 | from distutils.file_util import write_file |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 52 | from distutils.errors import DistutilsExecError, CompileError, UnknownFileError |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +0000 | [diff] [blame] | 53 | |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +0000 | [diff] [blame] | 54 | class CygwinCCompiler (UnixCCompiler): |
| 55 | |
| 56 | compiler_type = 'cygwin' |
Greg Ward | b1dceae | 2000-08-13 00:43:56 +0000 | [diff] [blame] | 57 | obj_extension = ".o" |
| 58 | static_lib_extension = ".a" |
| 59 | shared_lib_extension = ".dll" |
| 60 | static_lib_format = "lib%s%s" |
| 61 | shared_lib_format = "%s%s" |
| 62 | exe_extension = ".exe" |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +0000 | [diff] [blame] | 63 | |
| 64 | def __init__ (self, |
| 65 | verbose=0, |
| 66 | dry_run=0, |
| 67 | force=0): |
| 68 | |
| 69 | UnixCCompiler.__init__ (self, verbose, dry_run, force) |
| 70 | |
Greg Ward | e8e9d11 | 2000-08-13 01:18:55 +0000 | [diff] [blame] | 71 | (status, details) = check_config_h() |
| 72 | self.debug_print("Python's GCC status: %s (details: %s)" % |
| 73 | (status, details)) |
| 74 | if status is not CONFIG_H_OK: |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 75 | self.warn( |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 76 | "Python's pyconfig.h doesn't seem to support your compiler. " + |
Greg Ward | e8e9d11 | 2000-08-13 01:18:55 +0000 | [diff] [blame] | 77 | ("Reason: %s." % details) + |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 78 | "Compiling may fail because of undefined preprocessor macros.") |
| 79 | |
| 80 | (self.gcc_version, self.ld_version, self.dllwrap_version) = \ |
| 81 | get_versions() |
Greg Ward | b1dceae | 2000-08-13 00:43:56 +0000 | [diff] [blame] | 82 | self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" % |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 83 | (self.gcc_version, |
| 84 | self.ld_version, |
| 85 | self.dllwrap_version) ) |
| 86 | |
| 87 | # ld_version >= "2.10.90" should also be able to use |
| 88 | # gcc -mdll instead of dllwrap |
| 89 | # Older dllwraps had own version numbers, newer ones use the |
| 90 | # same as the rest of binutils ( also ld ) |
| 91 | # dllwrap 2.10.90 is buggy |
| 92 | if self.ld_version >= "2.10.90": |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 93 | self.linker_dll = "gcc" |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 94 | else: |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 95 | self.linker_dll = "dllwrap" |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 96 | |
Greg Ward | f34506a | 2000-06-29 22:57:55 +0000 | [diff] [blame] | 97 | # Hard-code GCC because that's what this is all about. |
| 98 | # XXX optimization, warnings etc. should be customizable. |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 99 | self.set_executables(compiler='gcc -mcygwin -O -Wall', |
| 100 | compiler_so='gcc -mcygwin -mdll -O -Wall', |
| 101 | linker_exe='gcc -mcygwin', |
| 102 | linker_so=('%s -mcygwin -mdll -static' % |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 103 | self.linker_dll)) |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +0000 | [diff] [blame] | 104 | |
Greg Ward | f34506a | 2000-06-29 22:57:55 +0000 | [diff] [blame] | 105 | # cygwin and mingw32 need different sets of libraries |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 106 | if self.gcc_version == "2.91.57": |
| 107 | # cygwin shouldn't need msvcrt, but without the dlls will crash |
| 108 | # (gcc version 2.91.57) -- perhaps something about initialization |
| 109 | self.dll_libraries=["msvcrt"] |
| 110 | self.warn( |
| 111 | "Consider upgrading to a newer version of gcc") |
| 112 | else: |
| 113 | self.dll_libraries=[] |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +0000 | [diff] [blame] | 114 | |
| 115 | # __init__ () |
| 116 | |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 117 | # not much different of the compile method in UnixCCompiler, |
| 118 | # but we have to insert some lines in the middle of it, so |
| 119 | # we put here a adapted version of it. |
| 120 | # (If we would call compile() in the base class, it would do some |
| 121 | # initializations a second time, this is why all is done here.) |
| 122 | def compile (self, |
| 123 | sources, |
| 124 | output_dir=None, |
| 125 | macros=None, |
| 126 | include_dirs=None, |
| 127 | debug=0, |
| 128 | extra_preargs=None, |
| 129 | extra_postargs=None): |
| 130 | |
| 131 | (output_dir, macros, include_dirs) = \ |
| 132 | self._fix_compile_args (output_dir, macros, include_dirs) |
| 133 | (objects, skip_sources) = self._prep_compile (sources, output_dir) |
| 134 | |
| 135 | # Figure out the options for the compiler command line. |
| 136 | pp_opts = gen_preprocess_options (macros, include_dirs) |
| 137 | cc_args = pp_opts + ['-c'] |
| 138 | if debug: |
| 139 | cc_args[:0] = ['-g'] |
| 140 | if extra_preargs: |
| 141 | cc_args[:0] = extra_preargs |
| 142 | if extra_postargs is None: |
| 143 | extra_postargs = [] |
| 144 | |
| 145 | # Compile all source files that weren't eliminated by |
| 146 | # '_prep_compile()'. |
| 147 | for i in range (len (sources)): |
| 148 | src = sources[i] ; obj = objects[i] |
| 149 | ext = (os.path.splitext (src))[1] |
| 150 | if skip_sources[src]: |
| 151 | self.announce ("skipping %s (%s up-to-date)" % (src, obj)) |
| 152 | else: |
| 153 | self.mkpath (os.path.dirname (obj)) |
| 154 | if ext == '.rc' or ext == '.res': |
| 155 | # gcc needs '.res' and '.rc' compiled to object files !!! |
| 156 | try: |
| 157 | self.spawn (["windres","-i",src,"-o",obj]) |
| 158 | except DistutilsExecError, msg: |
| 159 | raise CompileError, msg |
| 160 | else: # for other files use the C-compiler |
| 161 | try: |
| 162 | self.spawn (self.compiler_so + cc_args + |
| 163 | [src, '-o', obj] + |
| 164 | extra_postargs) |
| 165 | except DistutilsExecError, msg: |
| 166 | raise CompileError, msg |
| 167 | |
| 168 | # Return *all* object filenames, not just the ones we just built. |
| 169 | return objects |
| 170 | |
| 171 | # compile () |
| 172 | |
| 173 | |
| 174 | def link (self, |
| 175 | target_desc, |
| 176 | objects, |
| 177 | output_filename, |
| 178 | output_dir=None, |
| 179 | libraries=None, |
| 180 | library_dirs=None, |
| 181 | runtime_library_dirs=None, |
| 182 | export_symbols=None, |
| 183 | debug=0, |
| 184 | extra_preargs=None, |
| 185 | extra_postargs=None, |
| 186 | build_temp=None): |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +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 []) |
| 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, |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +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 | |
| 205 | # we want to put some files in the same directory as the |
| 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 | exp_file = os.path.join(temp_dir, dll_name + ".exp") |
| 216 | lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a") |
| 217 | |
| 218 | # Generate .def file |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 219 | contents = [ |
| 220 | "LIBRARY %s" % os.path.basename(output_filename), |
| 221 | "EXPORTS"] |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +0000 | [diff] [blame] | 222 | for sym in export_symbols: |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 223 | contents.append(sym) |
| 224 | self.execute(write_file, (def_file, contents), |
| 225 | "writing %s" % def_file) |
| 226 | |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 227 | # next add options for def-file and to creating import libraries |
| 228 | |
| 229 | # dllwrap uses different options than gcc/ld |
| 230 | if self.linker_dll == "dllwrap": |
| 231 | extra_preargs.extend([#"--output-exp",exp_file, |
| 232 | "--output-lib",lib_file, |
| 233 | ]) |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 234 | # for dllwrap we have to use a special option |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 235 | extra_preargs.extend(["--def", def_file]) |
| 236 | # we use gcc/ld here and can be sure ld is >= 2.9.10 |
| 237 | else: |
| 238 | # doesn't work: bfd_close build\...\libfoo.a: Invalid operation |
| 239 | #extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file]) |
| 240 | # for gcc/ld the def-file is specified as any other object files |
| 241 | objects.append(def_file) |
| 242 | |
| 243 | #end: if ((export_symbols is not None) and |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 244 | # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")): |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 245 | |
Greg Ward | f34506a | 2000-06-29 22:57:55 +0000 | [diff] [blame] | 246 | # who wants symbols and a many times larger output file |
Greg Ward | 612eb9f | 2000-07-27 02:13:20 +0000 | [diff] [blame] | 247 | # should explicitly switch the debug mode on |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 248 | # otherwise we let dllwrap/ld strip the output file |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 249 | # (On my machine: 10KB < stripped_file < ??100KB |
| 250 | # unstripped_file = stripped_file + XXX KB |
| 251 | # ( XXX=254 for a typical python extension)) |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +0000 | [diff] [blame] | 252 | if not debug: |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 253 | extra_preargs.append("-s") |
Greg Ward | f34506a | 2000-06-29 22:57:55 +0000 | [diff] [blame] | 254 | |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 255 | UnixCCompiler.link(self, |
| 256 | target_desc, |
| 257 | objects, |
| 258 | output_filename, |
| 259 | output_dir, |
| 260 | libraries, |
| 261 | library_dirs, |
| 262 | runtime_library_dirs, |
| 263 | None, # export_symbols, we do this in our def-file |
| 264 | debug, |
| 265 | extra_preargs, |
| 266 | extra_postargs, |
| 267 | build_temp) |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +0000 | [diff] [blame] | 268 | |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 269 | # link () |
| 270 | |
| 271 | # -- Miscellaneous methods ----------------------------------------- |
| 272 | |
| 273 | # overwrite the one from CCompiler to support rc and res-files |
| 274 | def object_filenames (self, |
| 275 | source_filenames, |
| 276 | strip_dir=0, |
| 277 | output_dir=''): |
| 278 | if output_dir is None: output_dir = '' |
| 279 | obj_names = [] |
| 280 | for src_name in source_filenames: |
| 281 | # use normcase to make sure '.rc' is really '.rc' and not '.RC' |
| 282 | (base, ext) = os.path.splitext (os.path.normcase(src_name)) |
| 283 | if ext not in (self.src_extensions + ['.rc','.res']): |
| 284 | raise UnknownFileError, \ |
| 285 | "unknown file type '%s' (from '%s')" % \ |
| 286 | (ext, src_name) |
| 287 | if strip_dir: |
| 288 | base = os.path.basename (base) |
| 289 | if ext == '.res' or ext == '.rc': |
| 290 | # these need to be compiled to object files |
| 291 | obj_names.append (os.path.join (output_dir, |
| 292 | base + ext + self.obj_extension)) |
| 293 | else: |
| 294 | obj_names.append (os.path.join (output_dir, |
| 295 | base + self.obj_extension)) |
| 296 | return obj_names |
| 297 | |
| 298 | # object_filenames () |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +0000 | [diff] [blame] | 299 | |
| 300 | # class CygwinCCompiler |
| 301 | |
Greg Ward | f34506a | 2000-06-29 22:57:55 +0000 | [diff] [blame] | 302 | |
Greg Ward | 7c6395a | 2000-06-21 03:33:03 +0000 | [diff] [blame] | 303 | # the same as cygwin plus some additional parameters |
| 304 | class Mingw32CCompiler (CygwinCCompiler): |
| 305 | |
| 306 | compiler_type = 'mingw32' |
| 307 | |
| 308 | def __init__ (self, |
| 309 | verbose=0, |
| 310 | dry_run=0, |
| 311 | force=0): |
| 312 | |
| 313 | CygwinCCompiler.__init__ (self, verbose, dry_run, force) |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 314 | |
| 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', |
Greg Ward | f34506a | 2000-06-29 22:57:55 +0000 | [diff] [blame] | 324 | linker_exe='gcc -mno-cygwin', |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 325 | linker_so='%s -mno-cygwin -mdll -static %s' |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 326 | % (self.linker_dll, entry_point)) |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +0000 | [diff] [blame] | 327 | # Maybe we should also append -mthreads, but then the finished |
| 328 | # dlls need another dll (mingwm10.dll see Mingw32 docs) |
| 329 | # (-mthreads: Support thread-safe exception handling on `Mingw32') |
| 330 | |
| 331 | # no additional libraries needed |
| 332 | self.dll_libraries=[] |
| 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'") |
Greg Ward | bf5c709 | 2000-08-02 01:31:56 +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. |
| 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() |
| 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 |
| 404 | |
| 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() |
| 410 | result = re.search('(\d+\.\d+\.\d+)',out_string) |
| 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() |
| 422 | result = re.search('(\d+\.\d+\.\d+)',out_string) |
| 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() |
| 434 | result = re.search(' (\d+\.\d+\.\d+)',out_string) |
| 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) |
| 442 | |