+++ /dev/null
-"""distutils.cygwinccompiler\r
-\r
-Provides the CygwinCCompiler class, a subclass of UnixCCompiler that\r
-handles the Cygwin port of the GNU C compiler to Windows. It also contains\r
-the Mingw32CCompiler class which handles the mingw32 port of GCC (same as\r
-cygwin in no-cygwin mode).\r
-"""\r
-\r
-# problems:\r
-#\r
-# * if you use a msvc compiled python version (1.5.2)\r
-# 1. you have to insert a __GNUC__ section in its config.h\r
-# 2. you have to generate a import library for its dll\r
-# - create a def-file for python??.dll\r
-# - create a import library using\r
-# dlltool --dllname python15.dll --def python15.def \\r
-# --output-lib libpython15.a\r
-#\r
-# see also http://starship.python.net/crew/kernr/mingw32/Notes.html\r
-#\r
-# * We put export_symbols in a def-file, and don't use\r
-# --export-all-symbols because it doesn't worked reliable in some\r
-# tested configurations. And because other windows compilers also\r
-# need their symbols specified this no serious problem.\r
-#\r
-# tested configurations:\r
-#\r
-# * cygwin gcc 2.91.57/ld 2.9.4/dllwrap 0.2.4 works\r
-# (after patching python's config.h and for C++ some other include files)\r
-# see also http://starship.python.net/crew/kernr/mingw32/Notes.html\r
-# * mingw32 gcc 2.95.2/ld 2.9.4/dllwrap 0.2.4 works\r
-# (ld doesn't support -shared, so we use dllwrap)\r
-# * cygwin gcc 2.95.2/ld 2.10.90/dllwrap 2.10.90 works now\r
-# - its dllwrap doesn't work, there is a bug in binutils 2.10.90\r
-# see also http://sources.redhat.com/ml/cygwin/2000-06/msg01274.html\r
-# - using gcc -mdll instead dllwrap doesn't work without -static because\r
-# it tries to link against dlls instead their import libraries. (If\r
-# it finds the dll first.)\r
-# By specifying -static we force ld to link against the import libraries,\r
-# this is windows standard and there are normally not the necessary symbols\r
-# in the dlls.\r
-# *** only the version of June 2000 shows these problems\r
-# * cygwin gcc 3.2/ld 2.13.90 works\r
-# (ld supports -shared)\r
-# * mingw gcc 3.2/ld 2.13 works\r
-# (ld supports -shared)\r
-\r
-# This module should be kept compatible with Python 2.1.\r
-\r
-__revision__ = "$Id$"\r
-\r
-import os,sys,copy\r
-from distutils.ccompiler import gen_preprocess_options, gen_lib_options\r
-from distutils.unixccompiler import UnixCCompiler\r
-from distutils.file_util import write_file\r
-from distutils.errors import DistutilsExecError, CompileError, UnknownFileError\r
-from distutils import log\r
-\r
-def get_msvcr():\r
- """Include the appropriate MSVC runtime library if Python was built\r
- with MSVC 7.0 or later.\r
- """\r
- msc_pos = sys.version.find('MSC v.')\r
- if msc_pos != -1:\r
- msc_ver = sys.version[msc_pos+6:msc_pos+10]\r
- if msc_ver == '1300':\r
- # MSVC 7.0\r
- return ['msvcr70']\r
- elif msc_ver == '1310':\r
- # MSVC 7.1\r
- return ['msvcr71']\r
- elif msc_ver == '1400':\r
- # VS2005 / MSVC 8.0\r
- return ['msvcr80']\r
- elif msc_ver == '1500':\r
- # VS2008 / MSVC 9.0\r
- return ['msvcr90']\r
- else:\r
- raise ValueError("Unknown MS Compiler version %s " % msc_ver)\r
-\r
-\r
-class CygwinCCompiler (UnixCCompiler):\r
-\r
- compiler_type = 'cygwin'\r
- obj_extension = ".o"\r
- static_lib_extension = ".a"\r
- shared_lib_extension = ".dll"\r
- static_lib_format = "lib%s%s"\r
- shared_lib_format = "%s%s"\r
- exe_extension = ".exe"\r
-\r
- def __init__ (self, verbose=0, dry_run=0, force=0):\r
-\r
- UnixCCompiler.__init__ (self, verbose, dry_run, force)\r
-\r
- (status, details) = check_config_h()\r
- self.debug_print("Python's GCC status: %s (details: %s)" %\r
- (status, details))\r
- if status is not CONFIG_H_OK:\r
- self.warn(\r
- "Python's pyconfig.h doesn't seem to support your compiler. "\r
- "Reason: %s. "\r
- "Compiling may fail because of undefined preprocessor macros."\r
- % details)\r
-\r
- self.gcc_version, self.ld_version, self.dllwrap_version = \\r
- get_versions()\r
- self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %\r
- (self.gcc_version,\r
- self.ld_version,\r
- self.dllwrap_version) )\r
-\r
- # ld_version >= "2.10.90" and < "2.13" should also be able to use\r
- # gcc -mdll instead of dllwrap\r
- # Older dllwraps had own version numbers, newer ones use the\r
- # same as the rest of binutils ( also ld )\r
- # dllwrap 2.10.90 is buggy\r
- if self.ld_version >= "2.10.90":\r
- self.linker_dll = "gcc"\r
- else:\r
- self.linker_dll = "dllwrap"\r
-\r
- # ld_version >= "2.13" support -shared so use it instead of\r
- # -mdll -static\r
- if self.ld_version >= "2.13":\r
- shared_option = "-shared"\r
- else:\r
- shared_option = "-mdll -static"\r
-\r
- # Hard-code GCC because that's what this is all about.\r
- # XXX optimization, warnings etc. should be customizable.\r
- self.set_executables(compiler='gcc -mcygwin -O -Wall',\r
- compiler_so='gcc -mcygwin -mdll -O -Wall',\r
- compiler_cxx='g++ -mcygwin -O -Wall',\r
- linker_exe='gcc -mcygwin',\r
- linker_so=('%s -mcygwin %s' %\r
- (self.linker_dll, shared_option)))\r
-\r
- # cygwin and mingw32 need different sets of libraries\r
- if self.gcc_version == "2.91.57":\r
- # cygwin shouldn't need msvcrt, but without the dlls will crash\r
- # (gcc version 2.91.57) -- perhaps something about initialization\r
- self.dll_libraries=["msvcrt"]\r
- self.warn(\r
- "Consider upgrading to a newer version of gcc")\r
- else:\r
- # Include the appropriate MSVC runtime library if Python was built\r
- # with MSVC 7.0 or later.\r
- self.dll_libraries = get_msvcr()\r
-\r
- # __init__ ()\r
-\r
-\r
- def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):\r
- if ext == '.rc' or ext == '.res':\r
- # gcc needs '.res' and '.rc' compiled to object files !!!\r
- try:\r
- self.spawn(["windres", "-i", src, "-o", obj])\r
- except DistutilsExecError, msg:\r
- raise CompileError, msg\r
- else: # for other files use the C-compiler\r
- try:\r
- self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +\r
- extra_postargs)\r
- except DistutilsExecError, msg:\r
- raise CompileError, msg\r
-\r
- def link (self,\r
- target_desc,\r
- objects,\r
- output_filename,\r
- output_dir=None,\r
- libraries=None,\r
- library_dirs=None,\r
- runtime_library_dirs=None,\r
- export_symbols=None,\r
- debug=0,\r
- extra_preargs=None,\r
- extra_postargs=None,\r
- build_temp=None,\r
- target_lang=None):\r
-\r
- # use separate copies, so we can modify the lists\r
- extra_preargs = copy.copy(extra_preargs or [])\r
- libraries = copy.copy(libraries or [])\r
- objects = copy.copy(objects or [])\r
-\r
- # Additional libraries\r
- libraries.extend(self.dll_libraries)\r
-\r
- # handle export symbols by creating a def-file\r
- # with executables this only works with gcc/ld as linker\r
- if ((export_symbols is not None) and\r
- (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):\r
- # (The linker doesn't do anything if output is up-to-date.\r
- # So it would probably better to check if we really need this,\r
- # but for this we had to insert some unchanged parts of\r
- # UnixCCompiler, and this is not what we want.)\r
-\r
- # we want to put some files in the same directory as the\r
- # object files are, build_temp doesn't help much\r
- # where are the object files\r
- temp_dir = os.path.dirname(objects[0])\r
- # name of dll to give the helper files the same base name\r
- (dll_name, dll_extension) = os.path.splitext(\r
- os.path.basename(output_filename))\r
-\r
- # generate the filenames for these files\r
- def_file = os.path.join(temp_dir, dll_name + ".def")\r
- lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a")\r
-\r
- # Generate .def file\r
- contents = [\r
- "LIBRARY %s" % os.path.basename(output_filename),\r
- "EXPORTS"]\r
- for sym in export_symbols:\r
- contents.append(sym)\r
- self.execute(write_file, (def_file, contents),\r
- "writing %s" % def_file)\r
-\r
- # next add options for def-file and to creating import libraries\r
-\r
- # dllwrap uses different options than gcc/ld\r
- if self.linker_dll == "dllwrap":\r
- extra_preargs.extend(["--output-lib", lib_file])\r
- # for dllwrap we have to use a special option\r
- extra_preargs.extend(["--def", def_file])\r
- # we use gcc/ld here and can be sure ld is >= 2.9.10\r
- else:\r
- # doesn't work: bfd_close build\...\libfoo.a: Invalid operation\r
- #extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])\r
- # for gcc/ld the def-file is specified as any object files\r
- objects.append(def_file)\r
-\r
- #end: if ((export_symbols is not None) and\r
- # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):\r
-\r
- # who wants symbols and a many times larger output file\r
- # should explicitly switch the debug mode on\r
- # otherwise we let dllwrap/ld strip the output file\r
- # (On my machine: 10KB < stripped_file < ??100KB\r
- # unstripped_file = stripped_file + XXX KB\r
- # ( XXX=254 for a typical python extension))\r
- if not debug:\r
- extra_preargs.append("-s")\r
-\r
- UnixCCompiler.link(self,\r
- target_desc,\r
- objects,\r
- output_filename,\r
- output_dir,\r
- libraries,\r
- library_dirs,\r
- runtime_library_dirs,\r
- None, # export_symbols, we do this in our def-file\r
- debug,\r
- extra_preargs,\r
- extra_postargs,\r
- build_temp,\r
- target_lang)\r
-\r
- # link ()\r
-\r
- # -- Miscellaneous methods -----------------------------------------\r
-\r
- # overwrite the one from CCompiler to support rc and res-files\r
- def object_filenames (self,\r
- source_filenames,\r
- strip_dir=0,\r
- output_dir=''):\r
- if output_dir is None: output_dir = ''\r
- obj_names = []\r
- for src_name in source_filenames:\r
- # use normcase to make sure '.rc' is really '.rc' and not '.RC'\r
- (base, ext) = os.path.splitext (os.path.normcase(src_name))\r
- if ext not in (self.src_extensions + ['.rc','.res']):\r
- raise UnknownFileError, \\r
- "unknown file type '%s' (from '%s')" % \\r
- (ext, src_name)\r
- if strip_dir:\r
- base = os.path.basename (base)\r
- if ext == '.res' or ext == '.rc':\r
- # these need to be compiled to object files\r
- obj_names.append (os.path.join (output_dir,\r
- base + ext + self.obj_extension))\r
- else:\r
- obj_names.append (os.path.join (output_dir,\r
- base + self.obj_extension))\r
- return obj_names\r
-\r
- # object_filenames ()\r
-\r
-# class CygwinCCompiler\r
-\r
-\r
-# the same as cygwin plus some additional parameters\r
-class Mingw32CCompiler (CygwinCCompiler):\r
-\r
- compiler_type = 'mingw32'\r
-\r
- def __init__ (self,\r
- verbose=0,\r
- dry_run=0,\r
- force=0):\r
-\r
- CygwinCCompiler.__init__ (self, verbose, dry_run, force)\r
-\r
- # ld_version >= "2.13" support -shared so use it instead of\r
- # -mdll -static\r
- if self.ld_version >= "2.13":\r
- shared_option = "-shared"\r
- else:\r
- shared_option = "-mdll -static"\r
-\r
- # A real mingw32 doesn't need to specify a different entry point,\r
- # but cygwin 2.91.57 in no-cygwin-mode needs it.\r
- if self.gcc_version <= "2.91.57":\r
- entry_point = '--entry _DllMain@12'\r
- else:\r
- entry_point = ''\r
-\r
- self.set_executables(compiler='gcc -mno-cygwin -O -Wall',\r
- compiler_so='gcc -mno-cygwin -mdll -O -Wall',\r
- compiler_cxx='g++ -mno-cygwin -O -Wall',\r
- linker_exe='gcc -mno-cygwin',\r
- linker_so='%s -mno-cygwin %s %s'\r
- % (self.linker_dll, shared_option,\r
- entry_point))\r
- # Maybe we should also append -mthreads, but then the finished\r
- # dlls need another dll (mingwm10.dll see Mingw32 docs)\r
- # (-mthreads: Support thread-safe exception handling on `Mingw32')\r
-\r
- # no additional libraries needed\r
- self.dll_libraries=[]\r
-\r
- # Include the appropriate MSVC runtime library if Python was built\r
- # with MSVC 7.0 or later.\r
- self.dll_libraries = get_msvcr()\r
-\r
- # __init__ ()\r
-\r
-# class Mingw32CCompiler\r
-\r
-# Because these compilers aren't configured in Python's pyconfig.h file by\r
-# default, we should at least warn the user if he is using a unmodified\r
-# version.\r
-\r
-CONFIG_H_OK = "ok"\r
-CONFIG_H_NOTOK = "not ok"\r
-CONFIG_H_UNCERTAIN = "uncertain"\r
-\r
-def check_config_h():\r
-\r
- """Check if the current Python installation (specifically, pyconfig.h)\r
- appears amenable to building extensions with GCC. Returns a tuple\r
- (status, details), where 'status' is one of the following constants:\r
- CONFIG_H_OK\r
- all is well, go ahead and compile\r
- CONFIG_H_NOTOK\r
- doesn't look good\r
- CONFIG_H_UNCERTAIN\r
- not sure -- unable to read pyconfig.h\r
- 'details' is a human-readable string explaining the situation.\r
-\r
- Note there are two ways to conclude "OK": either 'sys.version' contains\r
- the string "GCC" (implying that this Python was built with GCC), or the\r
- installed "pyconfig.h" contains the string "__GNUC__".\r
- """\r
-\r
- # XXX since this function also checks sys.version, it's not strictly a\r
- # "pyconfig.h" check -- should probably be renamed...\r
-\r
- from distutils import sysconfig\r
- import string\r
- # if sys.version contains GCC then python was compiled with\r
- # GCC, and the pyconfig.h file should be OK\r
- if string.find(sys.version,"GCC") >= 0:\r
- return (CONFIG_H_OK, "sys.version mentions 'GCC'")\r
-\r
- fn = sysconfig.get_config_h_filename()\r
- try:\r
- # It would probably better to read single lines to search.\r
- # But we do this only once, and it is fast enough\r
- f = open(fn)\r
- try:\r
- s = f.read()\r
- finally:\r
- f.close()\r
-\r
- except IOError, exc:\r
- # if we can't read this file, we cannot say it is wrong\r
- # the compiler will complain later about this file as missing\r
- return (CONFIG_H_UNCERTAIN,\r
- "couldn't read '%s': %s" % (fn, exc.strerror))\r
-\r
- else:\r
- # "pyconfig.h" contains an "#ifdef __GNUC__" or something similar\r
- if string.find(s,"__GNUC__") >= 0:\r
- return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn)\r
- else:\r
- return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn)\r
-\r
-\r
-\r
-def get_versions():\r
- """ Try to find out the versions of gcc, ld and dllwrap.\r
- If not possible it returns None for it.\r
- """\r
- from distutils.version import LooseVersion\r
- from distutils.spawn import find_executable\r
- import re\r
-\r
- gcc_exe = find_executable('gcc')\r
- if gcc_exe:\r
- out = os.popen(gcc_exe + ' -dumpversion','r')\r
- out_string = out.read()\r
- out.close()\r
- result = re.search('(\d+\.\d+(\.\d+)*)',out_string)\r
- if result:\r
- gcc_version = LooseVersion(result.group(1))\r
- else:\r
- gcc_version = None\r
- else:\r
- gcc_version = None\r
- ld_exe = find_executable('ld')\r
- if ld_exe:\r
- out = os.popen(ld_exe + ' -v','r')\r
- out_string = out.read()\r
- out.close()\r
- result = re.search('(\d+\.\d+(\.\d+)*)',out_string)\r
- if result:\r
- ld_version = LooseVersion(result.group(1))\r
- else:\r
- ld_version = None\r
- else:\r
- ld_version = None\r
- dllwrap_exe = find_executable('dllwrap')\r
- if dllwrap_exe:\r
- out = os.popen(dllwrap_exe + ' --version','r')\r
- out_string = out.read()\r
- out.close()\r
- result = re.search(' (\d+\.\d+(\.\d+)*)',out_string)\r
- if result:\r
- dllwrap_version = LooseVersion(result.group(1))\r
- else:\r
- dllwrap_version = None\r
- else:\r
- dllwrap_version = None\r
- return (gcc_version, ld_version, dllwrap_version)\r