Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame^] | 1 | """CCompiler implementations for Cygwin and mingw32 versions of GCC. |
| 2 | |
| 3 | This module contains the CygwinCCompiler class, a subclass of |
| 4 | UnixCCompiler that handles the Cygwin port of the GNU C compiler to |
| 5 | Windows, and the Mingw32CCompiler class which handles the mingw32 port |
| 6 | of GCC (same as cygwin in no-cygwin mode). |
| 7 | """ |
| 8 | |
| 9 | # problems: |
| 10 | # |
| 11 | # * if you use a msvc compiled python version (1.5.2) |
| 12 | # 1. you have to insert a __GNUC__ section in its config.h |
| 13 | # 2. you have to generate a import library for its dll |
| 14 | # - create a def-file for python??.dll |
| 15 | # - create a import library using |
| 16 | # dlltool --dllname python15.dll --def python15.def \ |
| 17 | # --output-lib libpython15.a |
| 18 | # |
| 19 | # see also http://starship.python.net/crew/kernr/mingw32/Notes.html |
| 20 | # |
| 21 | # * We put export_symbols in a def-file, and don't use |
| 22 | # --export-all-symbols because it doesn't worked reliable in some |
| 23 | # tested configurations. And because other windows compilers also |
| 24 | # need their symbols specified this no serious problem. |
| 25 | # |
| 26 | # tested configurations: |
| 27 | # |
| 28 | # * cygwin gcc 2.91.57/ld 2.9.4/dllwrap 0.2.4 works |
| 29 | # (after patching python's config.h and for C++ some other include files) |
| 30 | # see also http://starship.python.net/crew/kernr/mingw32/Notes.html |
| 31 | # * mingw32 gcc 2.95.2/ld 2.9.4/dllwrap 0.2.4 works |
| 32 | # (ld doesn't support -shared, so we use dllwrap) |
| 33 | # * cygwin gcc 2.95.2/ld 2.10.90/dllwrap 2.10.90 works now |
| 34 | # - its dllwrap doesn't work, there is a bug in binutils 2.10.90 |
| 35 | # see also http://sources.redhat.com/ml/cygwin/2000-06/msg01274.html |
| 36 | # - using gcc -mdll instead dllwrap doesn't work without -static because |
| 37 | # it tries to link against dlls instead their import libraries. (If |
| 38 | # it finds the dll first.) |
| 39 | # By specifying -static we force ld to link against the import libraries, |
| 40 | # this is windows standard and there are normally not the necessary symbols |
| 41 | # in the dlls. |
| 42 | # *** only the version of June 2000 shows these problems |
| 43 | # * cygwin gcc 3.2/ld 2.13.90 works |
| 44 | # (ld supports -shared) |
| 45 | # * mingw gcc 3.2/ld 2.13 works |
| 46 | # (ld supports -shared) |
| 47 | |
| 48 | |
| 49 | import os |
| 50 | import sys |
| 51 | import copy |
| 52 | |
| 53 | from packaging import logger |
| 54 | from packaging.compiler.unixccompiler import UnixCCompiler |
| 55 | from packaging.util import write_file |
| 56 | from packaging.errors import PackagingExecError, CompileError, UnknownFileError |
| 57 | from packaging.util import get_compiler_versions |
| 58 | import sysconfig |
| 59 | |
| 60 | |
| 61 | def get_msvcr(): |
| 62 | """Include the appropriate MSVC runtime library if Python was built |
| 63 | with MSVC 7.0 or later. |
| 64 | """ |
| 65 | msc_pos = sys.version.find('MSC v.') |
| 66 | if msc_pos != -1: |
| 67 | msc_ver = sys.version[msc_pos+6:msc_pos+10] |
| 68 | if msc_ver == '1300': |
| 69 | # MSVC 7.0 |
| 70 | return ['msvcr70'] |
| 71 | elif msc_ver == '1310': |
| 72 | # MSVC 7.1 |
| 73 | return ['msvcr71'] |
| 74 | elif msc_ver == '1400': |
| 75 | # VS2005 / MSVC 8.0 |
| 76 | return ['msvcr80'] |
| 77 | elif msc_ver == '1500': |
| 78 | # VS2008 / MSVC 9.0 |
| 79 | return ['msvcr90'] |
| 80 | else: |
| 81 | raise ValueError("Unknown MS Compiler version %s " % msc_ver) |
| 82 | |
| 83 | |
| 84 | class CygwinCCompiler(UnixCCompiler): |
| 85 | """ Handles the Cygwin port of the GNU C compiler to Windows. |
| 86 | """ |
| 87 | name = 'cygwin' |
| 88 | description = 'Cygwin port of GNU C Compiler for Win32' |
| 89 | obj_extension = ".o" |
| 90 | static_lib_extension = ".a" |
| 91 | shared_lib_extension = ".dll" |
| 92 | static_lib_format = "lib%s%s" |
| 93 | shared_lib_format = "%s%s" |
| 94 | exe_extension = ".exe" |
| 95 | |
| 96 | def __init__(self, verbose=0, dry_run=False, force=False): |
| 97 | |
| 98 | UnixCCompiler.__init__(self, verbose, dry_run, force) |
| 99 | |
| 100 | status, details = check_config_h() |
| 101 | logger.debug("Python's GCC status: %s (details: %s)", status, details) |
| 102 | if status is not CONFIG_H_OK: |
| 103 | self.warn( |
| 104 | "Python's pyconfig.h doesn't seem to support your compiler. " |
| 105 | "Reason: %s. " |
| 106 | "Compiling may fail because of undefined preprocessor macros." |
| 107 | % details) |
| 108 | |
| 109 | self.gcc_version, self.ld_version, self.dllwrap_version = \ |
| 110 | get_compiler_versions() |
| 111 | logger.debug(self.name + ": gcc %s, ld %s, dllwrap %s\n", |
| 112 | self.gcc_version, |
| 113 | self.ld_version, |
| 114 | self.dllwrap_version) |
| 115 | |
| 116 | # ld_version >= "2.10.90" and < "2.13" should also be able to use |
| 117 | # gcc -mdll instead of dllwrap |
| 118 | # Older dllwraps had own version numbers, newer ones use the |
| 119 | # same as the rest of binutils ( also ld ) |
| 120 | # dllwrap 2.10.90 is buggy |
| 121 | if self.ld_version >= "2.10.90": |
| 122 | self.linker_dll = "gcc" |
| 123 | else: |
| 124 | self.linker_dll = "dllwrap" |
| 125 | |
| 126 | # ld_version >= "2.13" support -shared so use it instead of |
| 127 | # -mdll -static |
| 128 | if self.ld_version >= "2.13": |
| 129 | shared_option = "-shared" |
| 130 | else: |
| 131 | shared_option = "-mdll -static" |
| 132 | |
| 133 | # Hard-code GCC because that's what this is all about. |
| 134 | # XXX optimization, warnings etc. should be customizable. |
| 135 | self.set_executables(compiler='gcc -mcygwin -O -Wall', |
| 136 | compiler_so='gcc -mcygwin -mdll -O -Wall', |
| 137 | compiler_cxx='g++ -mcygwin -O -Wall', |
| 138 | linker_exe='gcc -mcygwin', |
| 139 | linker_so=('%s -mcygwin %s' % |
| 140 | (self.linker_dll, shared_option))) |
| 141 | |
| 142 | # cygwin and mingw32 need different sets of libraries |
| 143 | if self.gcc_version == "2.91.57": |
| 144 | # cygwin shouldn't need msvcrt, but without the dlls will crash |
| 145 | # (gcc version 2.91.57) -- perhaps something about initialization |
| 146 | self.dll_libraries=["msvcrt"] |
| 147 | self.warn( |
| 148 | "Consider upgrading to a newer version of gcc") |
| 149 | else: |
| 150 | # Include the appropriate MSVC runtime library if Python was built |
| 151 | # with MSVC 7.0 or later. |
| 152 | self.dll_libraries = get_msvcr() |
| 153 | |
| 154 | def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): |
| 155 | """Compile the source by spawning GCC and windres if needed.""" |
| 156 | if ext == '.rc' or ext == '.res': |
| 157 | # gcc needs '.res' and '.rc' compiled to object files !!! |
| 158 | try: |
| 159 | self.spawn(["windres", "-i", src, "-o", obj]) |
| 160 | except PackagingExecError as msg: |
| 161 | raise CompileError(msg) |
| 162 | else: # for other files use the C-compiler |
| 163 | try: |
| 164 | self.spawn(self.compiler_so + cc_args + [src, '-o', obj] + |
| 165 | extra_postargs) |
| 166 | except PackagingExecError as msg: |
| 167 | raise CompileError(msg) |
| 168 | |
| 169 | def link(self, target_desc, objects, output_filename, output_dir=None, |
| 170 | libraries=None, library_dirs=None, runtime_library_dirs=None, |
| 171 | export_symbols=None, debug=False, extra_preargs=None, |
| 172 | extra_postargs=None, build_temp=None, target_lang=None): |
| 173 | """Link the objects.""" |
| 174 | # use separate copies, so we can modify the lists |
| 175 | extra_preargs = copy.copy(extra_preargs or []) |
| 176 | libraries = copy.copy(libraries or []) |
| 177 | objects = copy.copy(objects or []) |
| 178 | |
| 179 | # Additional libraries |
| 180 | libraries.extend(self.dll_libraries) |
| 181 | |
| 182 | # handle export symbols by creating a def-file |
| 183 | # with executables this only works with gcc/ld as linker |
| 184 | if ((export_symbols is not None) and |
| 185 | (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")): |
| 186 | # (The linker doesn't do anything if output is up-to-date. |
| 187 | # So it would probably better to check if we really need this, |
| 188 | # but for this we had to insert some unchanged parts of |
| 189 | # UnixCCompiler, and this is not what we want.) |
| 190 | |
| 191 | # we want to put some files in the same directory as the |
| 192 | # object files are, build_temp doesn't help much |
| 193 | # where are the object files |
| 194 | temp_dir = os.path.dirname(objects[0]) |
| 195 | # name of dll to give the helper files the same base name |
| 196 | dll_name, dll_extension = os.path.splitext( |
| 197 | os.path.basename(output_filename)) |
| 198 | |
| 199 | # generate the filenames for these files |
| 200 | def_file = os.path.join(temp_dir, dll_name + ".def") |
| 201 | lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a") |
| 202 | |
| 203 | # Generate .def file |
| 204 | contents = [ |
| 205 | "LIBRARY %s" % os.path.basename(output_filename), |
| 206 | "EXPORTS"] |
| 207 | for sym in export_symbols: |
| 208 | contents.append(sym) |
| 209 | self.execute(write_file, (def_file, contents), |
| 210 | "writing %s" % def_file) |
| 211 | |
| 212 | # next add options for def-file and to creating import libraries |
| 213 | |
| 214 | # dllwrap uses different options than gcc/ld |
| 215 | if self.linker_dll == "dllwrap": |
| 216 | extra_preargs.extend(("--output-lib", lib_file)) |
| 217 | # for dllwrap we have to use a special option |
| 218 | extra_preargs.extend(("--def", def_file)) |
| 219 | # we use gcc/ld here and can be sure ld is >= 2.9.10 |
| 220 | else: |
| 221 | # doesn't work: bfd_close build\...\libfoo.a: Invalid operation |
| 222 | #extra_preargs.extend(("-Wl,--out-implib,%s" % lib_file)) |
| 223 | # for gcc/ld the def-file is specified as any object files |
| 224 | objects.append(def_file) |
| 225 | |
| 226 | #end: if ((export_symbols is not None) and |
| 227 | # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")): |
| 228 | |
| 229 | # who wants symbols and a many times larger output file |
| 230 | # should explicitly switch the debug mode on |
| 231 | # otherwise we let dllwrap/ld strip the output file |
| 232 | # (On my machine: 10KB < stripped_file < ??100KB |
| 233 | # unstripped_file = stripped_file + XXX KB |
| 234 | # ( XXX=254 for a typical python extension)) |
| 235 | if not debug: |
| 236 | extra_preargs.append("-s") |
| 237 | |
| 238 | UnixCCompiler.link(self, target_desc, objects, output_filename, |
| 239 | output_dir, libraries, library_dirs, |
| 240 | runtime_library_dirs, |
| 241 | None, # export_symbols, we do this in our def-file |
| 242 | debug, extra_preargs, extra_postargs, build_temp, |
| 243 | target_lang) |
| 244 | |
| 245 | # -- Miscellaneous methods ----------------------------------------- |
| 246 | |
| 247 | def object_filenames(self, source_filenames, strip_dir=False, |
| 248 | output_dir=''): |
| 249 | """Adds supports for rc and res files.""" |
| 250 | if output_dir is None: |
| 251 | output_dir = '' |
| 252 | obj_names = [] |
| 253 | for src_name in source_filenames: |
| 254 | # use normcase to make sure '.rc' is really '.rc' and not '.RC' |
| 255 | base, ext = os.path.splitext(os.path.normcase(src_name)) |
| 256 | if ext not in (self.src_extensions + ['.rc','.res']): |
| 257 | raise UnknownFileError("unknown file type '%s' (from '%s')" % (ext, src_name)) |
| 258 | if strip_dir: |
| 259 | base = os.path.basename (base) |
| 260 | if ext in ('.res', '.rc'): |
| 261 | # these need to be compiled to object files |
| 262 | obj_names.append (os.path.join(output_dir, |
| 263 | base + ext + self.obj_extension)) |
| 264 | else: |
| 265 | obj_names.append (os.path.join(output_dir, |
| 266 | base + self.obj_extension)) |
| 267 | return obj_names |
| 268 | |
| 269 | # the same as cygwin plus some additional parameters |
| 270 | class Mingw32CCompiler(CygwinCCompiler): |
| 271 | """ Handles the Mingw32 port of the GNU C compiler to Windows. |
| 272 | """ |
| 273 | name = 'mingw32' |
| 274 | description = 'MinGW32 compiler' |
| 275 | |
| 276 | def __init__(self, verbose=0, dry_run=False, force=False): |
| 277 | |
| 278 | CygwinCCompiler.__init__ (self, verbose, dry_run, force) |
| 279 | |
| 280 | # ld_version >= "2.13" support -shared so use it instead of |
| 281 | # -mdll -static |
| 282 | if self.ld_version >= "2.13": |
| 283 | shared_option = "-shared" |
| 284 | else: |
| 285 | shared_option = "-mdll -static" |
| 286 | |
| 287 | # A real mingw32 doesn't need to specify a different entry point, |
| 288 | # but cygwin 2.91.57 in no-cygwin-mode needs it. |
| 289 | if self.gcc_version <= "2.91.57": |
| 290 | entry_point = '--entry _DllMain@12' |
| 291 | else: |
| 292 | entry_point = '' |
| 293 | |
| 294 | self.set_executables(compiler='gcc -mno-cygwin -O -Wall', |
| 295 | compiler_so='gcc -mno-cygwin -mdll -O -Wall', |
| 296 | compiler_cxx='g++ -mno-cygwin -O -Wall', |
| 297 | linker_exe='gcc -mno-cygwin', |
| 298 | linker_so='%s -mno-cygwin %s %s' |
| 299 | % (self.linker_dll, shared_option, |
| 300 | entry_point)) |
| 301 | # Maybe we should also append -mthreads, but then the finished |
| 302 | # dlls need another dll (mingwm10.dll see Mingw32 docs) |
| 303 | # (-mthreads: Support thread-safe exception handling on `Mingw32') |
| 304 | |
| 305 | # no additional libraries needed |
| 306 | self.dll_libraries=[] |
| 307 | |
| 308 | # Include the appropriate MSVC runtime library if Python was built |
| 309 | # with MSVC 7.0 or later. |
| 310 | self.dll_libraries = get_msvcr() |
| 311 | |
| 312 | # Because these compilers aren't configured in Python's pyconfig.h file by |
| 313 | # default, we should at least warn the user if he is using a unmodified |
| 314 | # version. |
| 315 | |
| 316 | CONFIG_H_OK = "ok" |
| 317 | CONFIG_H_NOTOK = "not ok" |
| 318 | CONFIG_H_UNCERTAIN = "uncertain" |
| 319 | |
| 320 | def check_config_h(): |
| 321 | """Check if the current Python installation appears amenable to building |
| 322 | extensions with GCC. |
| 323 | |
| 324 | Returns a tuple (status, details), where 'status' is one of the following |
| 325 | constants: |
| 326 | |
| 327 | - CONFIG_H_OK: all is well, go ahead and compile |
| 328 | - CONFIG_H_NOTOK: doesn't look good |
| 329 | - CONFIG_H_UNCERTAIN: not sure -- unable to read pyconfig.h |
| 330 | |
| 331 | 'details' is a human-readable string explaining the situation. |
| 332 | |
| 333 | Note there are two ways to conclude "OK": either 'sys.version' contains |
| 334 | the string "GCC" (implying that this Python was built with GCC), or the |
| 335 | installed "pyconfig.h" contains the string "__GNUC__". |
| 336 | """ |
| 337 | |
| 338 | # XXX since this function also checks sys.version, it's not strictly a |
| 339 | # "pyconfig.h" check -- should probably be renamed... |
| 340 | # if sys.version contains GCC then python was compiled with GCC, and the |
| 341 | # pyconfig.h file should be OK |
| 342 | if "GCC" in sys.version: |
| 343 | return CONFIG_H_OK, "sys.version mentions 'GCC'" |
| 344 | |
| 345 | # let's see if __GNUC__ is mentioned in python.h |
| 346 | fn = sysconfig.get_config_h_filename() |
| 347 | try: |
| 348 | with open(fn) as config_h: |
| 349 | if "__GNUC__" in config_h.read(): |
| 350 | return CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn |
| 351 | else: |
| 352 | return CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn |
| 353 | except IOError as exc: |
| 354 | return (CONFIG_H_UNCERTAIN, |
| 355 | "couldn't read '%s': %s" % (fn, exc.strerror)) |