home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / distutils / msvccompiler.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  15.3 KB  |  596 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. __revision__ = '$Id: msvccompiler.py 62197 2008-04-07 01:53:39Z mark.hammond $'
  5. import sys
  6. import os
  7. import string
  8. from distutils.errors import DistutilsExecError, DistutilsPlatformError, CompileError, LibError, LinkError
  9. from distutils.ccompiler import CCompiler, gen_preprocess_options, gen_lib_options
  10. from distutils import log
  11. _can_read_reg = 0
  12.  
  13. try:
  14.     import _winreg
  15.     _can_read_reg = 1
  16.     hkey_mod = _winreg
  17.     RegOpenKeyEx = _winreg.OpenKeyEx
  18.     RegEnumKey = _winreg.EnumKey
  19.     RegEnumValue = _winreg.EnumValue
  20.     RegError = _winreg.error
  21. except ImportError:
  22.     
  23.     try:
  24.         import win32api
  25.         import win32con
  26.         _can_read_reg = 1
  27.         hkey_mod = win32con
  28.         RegOpenKeyEx = win32api.RegOpenKeyEx
  29.         RegEnumKey = win32api.RegEnumKey
  30.         RegEnumValue = win32api.RegEnumValue
  31.         RegError = win32api.error
  32.     except ImportError:
  33.         log.info("Warning: Can't read registry to find the necessary compiler setting\nMake sure that Python modules _winreg, win32api or win32con are installed.")
  34.  
  35.  
  36. if _can_read_reg:
  37.     HKEYS = (hkey_mod.HKEY_USERS, hkey_mod.HKEY_CURRENT_USER, hkey_mod.HKEY_LOCAL_MACHINE, hkey_mod.HKEY_CLASSES_ROOT)
  38.  
  39.  
  40. def read_keys(base, key):
  41.     
  42.     try:
  43.         handle = RegOpenKeyEx(base, key)
  44.     except RegError:
  45.         return None
  46.  
  47.     L = []
  48.     i = 0
  49.     while None:
  50.         
  51.         try:
  52.             k = RegEnumKey(handle, i)
  53.         except RegError:
  54.             break
  55.  
  56.         i = i + 1
  57.         continue
  58.         return L
  59.  
  60.  
  61. def read_values(base, key):
  62.     
  63.     try:
  64.         handle = RegOpenKeyEx(base, key)
  65.     except RegError:
  66.         return None
  67.  
  68.     d = { }
  69.     i = 0
  70.     while None:
  71.         
  72.         try:
  73.             (name, value, type) = RegEnumValue(handle, i)
  74.         except RegError:
  75.             break
  76.  
  77.         name = name.lower()
  78.         d[convert_mbcs(name)] = convert_mbcs(value)
  79.         i = i + 1
  80.         continue
  81.         return d
  82.  
  83.  
  84. def convert_mbcs(s):
  85.     enc = getattr(s, 'encode', None)
  86.     if enc is not None:
  87.         
  88.         try:
  89.             s = enc('mbcs')
  90.         except UnicodeError:
  91.             pass
  92.         except:
  93.             None<EXCEPTION MATCH>UnicodeError
  94.         
  95.  
  96.     None<EXCEPTION MATCH>UnicodeError
  97.     return s
  98.  
  99.  
  100. class MacroExpander:
  101.     
  102.     def __init__(self, version):
  103.         self.macros = { }
  104.         self.load_macros(version)
  105.  
  106.     
  107.     def set_macro(self, macro, path, key):
  108.         for base in HKEYS:
  109.             d = read_values(base, path)
  110.             if d:
  111.                 self.macros['$(%s)' % macro] = d[key]
  112.                 break
  113.                 continue
  114.         
  115.  
  116.     
  117.     def load_macros(self, version):
  118.         vsbase = 'Software\\Microsoft\\VisualStudio\\%0.1f' % version
  119.         self.set_macro('VCInstallDir', vsbase + '\\Setup\\VC', 'productdir')
  120.         self.set_macro('VSInstallDir', vsbase + '\\Setup\\VS', 'productdir')
  121.         net = 'Software\\Microsoft\\.NETFramework'
  122.         self.set_macro('FrameworkDir', net, 'installroot')
  123.         
  124.         try:
  125.             if version > 7:
  126.                 self.set_macro('FrameworkSDKDir', net, 'sdkinstallrootv1.1')
  127.             else:
  128.                 self.set_macro('FrameworkSDKDir', net, 'sdkinstallroot')
  129.         except KeyError:
  130.             exc = None
  131.             raise DistutilsPlatformError, 'Python was built with Visual Studio 2003;\nextensions must be built with a compiler than can generate compatible binaries.\nVisual Studio 2003 was not found on this system. If you have Cygwin installed,\nyou can try compiling with MingW32, by passing "-c mingw32" to setup.py.'
  132.  
  133.         p = 'Software\\Microsoft\\NET Framework Setup\\Product'
  134.         for base in HKEYS:
  135.             
  136.             try:
  137.                 h = RegOpenKeyEx(base, p)
  138.             except RegError:
  139.                 continue
  140.  
  141.             key = RegEnumKey(h, 0)
  142.             d = read_values(base, '%s\\%s' % (p, key))
  143.             self.macros['$(FrameworkVersion)'] = d['version']
  144.         
  145.  
  146.     
  147.     def sub(self, s):
  148.         for k, v in self.macros.items():
  149.             s = string.replace(s, k, v)
  150.         
  151.         return s
  152.  
  153.  
  154.  
  155. def get_build_version():
  156.     prefix = 'MSC v.'
  157.     i = string.find(sys.version, prefix)
  158.     if i == -1:
  159.         return 6
  160.     i = i + len(prefix)
  161.     (s, rest) = sys.version[i:].split(' ', 1)
  162.     majorVersion = int(s[:-2]) - 6
  163.     minorVersion = int(s[2:3]) / 10
  164.     if majorVersion == 6:
  165.         minorVersion = 0
  166.     
  167.     if majorVersion >= 6:
  168.         return majorVersion + minorVersion
  169.  
  170.  
  171. def get_build_architecture():
  172.     prefix = ' bit ('
  173.     i = string.find(sys.version, prefix)
  174.     if i == -1:
  175.         return 'Intel'
  176.     j = string.find(sys.version, ')', i)
  177.     return sys.version[i + len(prefix):j]
  178.  
  179.  
  180. def normalize_and_reduce_paths(paths):
  181.     reduced_paths = []
  182.     for p in paths:
  183.         np = os.path.normpath(p)
  184.         if np not in reduced_paths:
  185.             reduced_paths.append(np)
  186.             continue
  187.     
  188.     return reduced_paths
  189.  
  190.  
  191. class MSVCCompiler(CCompiler):
  192.     compiler_type = 'msvc'
  193.     executables = { }
  194.     _c_extensions = [
  195.         '.c']
  196.     _cpp_extensions = [
  197.         '.cc',
  198.         '.cpp',
  199.         '.cxx']
  200.     _rc_extensions = [
  201.         '.rc']
  202.     _mc_extensions = [
  203.         '.mc']
  204.     src_extensions = _c_extensions + _cpp_extensions + _rc_extensions + _mc_extensions
  205.     res_extension = '.res'
  206.     obj_extension = '.obj'
  207.     static_lib_extension = '.lib'
  208.     shared_lib_extension = '.dll'
  209.     static_lib_format = shared_lib_format = '%s%s'
  210.     exe_extension = '.exe'
  211.     
  212.     def __init__(self, verbose = 0, dry_run = 0, force = 0):
  213.         CCompiler.__init__(self, verbose, dry_run, force)
  214.         self._MSVCCompiler__version = get_build_version()
  215.         self._MSVCCompiler__arch = get_build_architecture()
  216.         if self._MSVCCompiler__arch == 'Intel':
  217.             if self._MSVCCompiler__version >= 7:
  218.                 self._MSVCCompiler__root = 'Software\\Microsoft\\VisualStudio'
  219.                 self._MSVCCompiler__macros = MacroExpander(self._MSVCCompiler__version)
  220.             else:
  221.                 self._MSVCCompiler__root = 'Software\\Microsoft\\Devstudio'
  222.             self._MSVCCompiler__product = 'Visual Studio version %s' % self._MSVCCompiler__version
  223.         else:
  224.             self._MSVCCompiler__product = 'Microsoft SDK compiler %s' % (self._MSVCCompiler__version + 6)
  225.         self.initialized = False
  226.  
  227.     
  228.     def initialize(self):
  229.         self._MSVCCompiler__paths = []
  230.         if 'DISTUTILS_USE_SDK' in os.environ and 'MSSdk' in os.environ and self.find_exe('cl.exe'):
  231.             self.cc = 'cl.exe'
  232.             self.linker = 'link.exe'
  233.             self.lib = 'lib.exe'
  234.             self.rc = 'rc.exe'
  235.             self.mc = 'mc.exe'
  236.         else:
  237.             self._MSVCCompiler__paths = self.get_msvc_paths('path')
  238.             if len(self._MSVCCompiler__paths) == 0:
  239.                 raise DistutilsPlatformError, "Python was built with %s, and extensions need to be built with the same version of the compiler, but it isn't installed." % self._MSVCCompiler__product
  240.             len(self._MSVCCompiler__paths) == 0
  241.             self.cc = self.find_exe('cl.exe')
  242.             self.linker = self.find_exe('link.exe')
  243.             self.lib = self.find_exe('lib.exe')
  244.             self.rc = self.find_exe('rc.exe')
  245.             self.mc = self.find_exe('mc.exe')
  246.             self.set_path_env_var('lib')
  247.             self.set_path_env_var('include')
  248.         
  249.         try:
  250.             for p in string.split(os.environ['path'], ';'):
  251.                 self._MSVCCompiler__paths.append(p)
  252.         except KeyError:
  253.             pass
  254.  
  255.         self._MSVCCompiler__paths = normalize_and_reduce_paths(self._MSVCCompiler__paths)
  256.         os.environ['path'] = string.join(self._MSVCCompiler__paths, ';')
  257.         self.preprocess_options = None
  258.         if self._MSVCCompiler__arch == 'Intel':
  259.             self.compile_options = [
  260.                 '/nologo',
  261.                 '/Ox',
  262.                 '/MD',
  263.                 '/W3',
  264.                 '/GX',
  265.                 '/DNDEBUG']
  266.             self.compile_options_debug = [
  267.                 '/nologo',
  268.                 '/Od',
  269.                 '/MDd',
  270.                 '/W3',
  271.                 '/GX',
  272.                 '/Z7',
  273.                 '/D_DEBUG']
  274.         else:
  275.             self.compile_options = [
  276.                 '/nologo',
  277.                 '/Ox',
  278.                 '/MD',
  279.                 '/W3',
  280.                 '/GS-',
  281.                 '/DNDEBUG']
  282.             self.compile_options_debug = [
  283.                 '/nologo',
  284.                 '/Od',
  285.                 '/MDd',
  286.                 '/W3',
  287.                 '/GS-',
  288.                 '/Z7',
  289.                 '/D_DEBUG']
  290.         self.ldflags_shared = [
  291.             '/DLL',
  292.             '/nologo',
  293.             '/INCREMENTAL:NO']
  294.         if self._MSVCCompiler__version >= 7:
  295.             self.ldflags_shared_debug = [
  296.                 '/DLL',
  297.                 '/nologo',
  298.                 '/INCREMENTAL:no',
  299.                 '/DEBUG']
  300.         else:
  301.             self.ldflags_shared_debug = [
  302.                 '/DLL',
  303.                 '/nologo',
  304.                 '/INCREMENTAL:no',
  305.                 '/pdb:None',
  306.                 '/DEBUG']
  307.         self.ldflags_static = [
  308.             '/nologo']
  309.         self.initialized = True
  310.  
  311.     
  312.     def object_filenames(self, source_filenames, strip_dir = 0, output_dir = ''):
  313.         if output_dir is None:
  314.             output_dir = ''
  315.         
  316.         obj_names = []
  317.         for src_name in source_filenames:
  318.             (base, ext) = os.path.splitext(src_name)
  319.             base = os.path.splitdrive(base)[1]
  320.             base = base[os.path.isabs(base):]
  321.             if ext not in self.src_extensions:
  322.                 raise CompileError("Don't know how to compile %s" % src_name)
  323.             ext not in self.src_extensions
  324.             if strip_dir:
  325.                 base = os.path.basename(base)
  326.             
  327.             if ext in self._rc_extensions:
  328.                 obj_names.append(os.path.join(output_dir, base + self.res_extension))
  329.                 continue
  330.             if ext in self._mc_extensions:
  331.                 obj_names.append(os.path.join(output_dir, base + self.res_extension))
  332.                 continue
  333.             obj_names.append(os.path.join(output_dir, base + self.obj_extension))
  334.         
  335.         return obj_names
  336.  
  337.     
  338.     def compile(self, sources, output_dir = None, macros = None, include_dirs = None, debug = 0, extra_preargs = None, extra_postargs = None, depends = None):
  339.         if not self.initialized:
  340.             self.initialize()
  341.         
  342.         (macros, objects, extra_postargs, pp_opts, build) = self._setup_compile(output_dir, macros, include_dirs, sources, depends, extra_postargs)
  343.         if not extra_preargs:
  344.             pass
  345.         compile_opts = []
  346.         compile_opts.append('/c')
  347.         if debug:
  348.             compile_opts.extend(self.compile_options_debug)
  349.         else:
  350.             compile_opts.extend(self.compile_options)
  351.         for obj in objects:
  352.             
  353.             try:
  354.                 (src, ext) = build[obj]
  355.             except KeyError:
  356.                 continue
  357.  
  358.             if debug:
  359.                 src = os.path.abspath(src)
  360.             
  361.             if ext in self._c_extensions:
  362.                 input_opt = '/Tc' + src
  363.             elif ext in self._cpp_extensions:
  364.                 input_opt = '/Tp' + src
  365.             elif ext in self._rc_extensions:
  366.                 input_opt = src
  367.                 output_opt = '/fo' + obj
  368.                 
  369.                 try:
  370.                     self.spawn([
  371.                         self.rc] + pp_opts + [
  372.                         output_opt] + [
  373.                         input_opt])
  374.                 continue
  375.                 except DistutilsExecError:
  376.                     msg = None
  377.                     raise CompileError, msg
  378.                     continue
  379.                 
  380.  
  381.             elif ext in self._mc_extensions:
  382.                 h_dir = os.path.dirname(src)
  383.                 rc_dir = os.path.dirname(obj)
  384.                 
  385.                 try:
  386.                     self.spawn([
  387.                         self.mc] + [
  388.                         '-h',
  389.                         h_dir,
  390.                         '-r',
  391.                         rc_dir] + [
  392.                         src])
  393.                     (base, _) = os.path.splitext(os.path.basename(src))
  394.                     rc_file = os.path.join(rc_dir, base + '.rc')
  395.                     self.spawn([
  396.                         self.rc] + [
  397.                         '/fo' + obj] + [
  398.                         rc_file])
  399.                 continue
  400.                 except DistutilsExecError:
  401.                     msg = None
  402.                     raise CompileError, msg
  403.                     continue
  404.                 
  405.  
  406.             else:
  407.                 raise CompileError("Don't know how to compile %s to %s" % (src, obj))
  408.             output_opt = (None<EXCEPTION MATCH>DistutilsExecError) + obj
  409.             
  410.             try:
  411.                 self.spawn([
  412.                     self.cc] + compile_opts + pp_opts + [
  413.                     input_opt,
  414.                     output_opt] + extra_postargs)
  415.             continue
  416.             except DistutilsExecError:
  417.                 msg = None
  418.                 raise CompileError, msg
  419.                 continue
  420.             
  421.  
  422.         
  423.         return objects
  424.  
  425.     
  426.     def create_static_lib(self, objects, output_libname, output_dir = None, debug = 0, target_lang = None):
  427.         if not self.initialized:
  428.             self.initialize()
  429.         
  430.         (objects, output_dir) = self._fix_object_args(objects, output_dir)
  431.         output_filename = self.library_filename(output_libname, output_dir = output_dir)
  432.         if self._need_link(objects, output_filename):
  433.             lib_args = objects + [
  434.                 '/OUT:' + output_filename]
  435.             if debug:
  436.                 pass
  437.             
  438.             
  439.             try:
  440.                 self.spawn([
  441.                     self.lib] + lib_args)
  442.             except DistutilsExecError:
  443.                 msg = None
  444.                 raise LibError, msg
  445.             except:
  446.                 None<EXCEPTION MATCH>DistutilsExecError
  447.             
  448.  
  449.         None<EXCEPTION MATCH>DistutilsExecError
  450.         log.debug('skipping %s (up-to-date)', output_filename)
  451.  
  452.     
  453.     def link(self, target_desc, objects, output_filename, output_dir = None, libraries = None, library_dirs = None, runtime_library_dirs = None, export_symbols = None, debug = 0, extra_preargs = None, extra_postargs = None, build_temp = None, target_lang = None):
  454.         if not self.initialized:
  455.             self.initialize()
  456.         
  457.         (objects, output_dir) = self._fix_object_args(objects, output_dir)
  458.         (libraries, library_dirs, runtime_library_dirs) = self._fix_lib_args(libraries, library_dirs, runtime_library_dirs)
  459.         if runtime_library_dirs:
  460.             self.warn("I don't know what to do with 'runtime_library_dirs': " + str(runtime_library_dirs))
  461.         
  462.         lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs, libraries)
  463.         if output_dir is not None:
  464.             output_filename = os.path.join(output_dir, output_filename)
  465.         
  466.         if self._need_link(objects, output_filename):
  467.             if target_desc == CCompiler.EXECUTABLE:
  468.                 if debug:
  469.                     ldflags = self.ldflags_shared_debug[1:]
  470.                 else:
  471.                     ldflags = self.ldflags_shared[1:]
  472.             elif debug:
  473.                 ldflags = self.ldflags_shared_debug
  474.             else:
  475.                 ldflags = self.ldflags_shared
  476.             export_opts = []
  477.             for sym in []:
  478.                 export_opts.append('/EXPORT:' + sym)
  479.             ld_args = ldflags + lib_opts + export_opts + objects + [
  480.                 '/OUT:' + output_filename]
  481.             if export_symbols is not None:
  482.                 (dll_name, dll_ext) = os.path.splitext(os.path.basename(output_filename))
  483.                 implib_file = os.path.join(os.path.dirname(objects[0]), self.library_filename(dll_name))
  484.                 ld_args.append('/IMPLIB:' + implib_file)
  485.             
  486.             if extra_preargs:
  487.                 ld_args[:0] = extra_preargs
  488.             
  489.             if extra_postargs:
  490.                 ld_args.extend(extra_postargs)
  491.             
  492.             self.mkpath(os.path.dirname(output_filename))
  493.             
  494.             try:
  495.                 self.spawn([
  496.                     self.linker] + ld_args)
  497.             except DistutilsExecError:
  498.                 msg = None
  499.                 raise LinkError, msg
  500.             except:
  501.                 None<EXCEPTION MATCH>DistutilsExecError
  502.             
  503.  
  504.         None<EXCEPTION MATCH>DistutilsExecError
  505.         log.debug('skipping %s (up-to-date)', output_filename)
  506.  
  507.     
  508.     def library_dir_option(self, dir):
  509.         return '/LIBPATH:' + dir
  510.  
  511.     
  512.     def runtime_library_dir_option(self, dir):
  513.         raise DistutilsPlatformError, "don't know how to set runtime library search path for MSVC++"
  514.  
  515.     
  516.     def library_option(self, lib):
  517.         return self.library_filename(lib)
  518.  
  519.     
  520.     def find_library_file(self, dirs, lib, debug = 0):
  521.         if debug:
  522.             try_names = [
  523.                 lib + '_d',
  524.                 lib]
  525.         else:
  526.             try_names = [
  527.                 lib]
  528.         for dir in dirs:
  529.             for name in try_names:
  530.                 libfile = os.path.join(dir, self.library_filename(name))
  531.                 if os.path.exists(libfile):
  532.                     return libfile
  533.             
  534.         else:
  535.             return None
  536.         return os.path.exists(libfile)
  537.  
  538.     
  539.     def find_exe(self, exe):
  540.         for p in self._MSVCCompiler__paths:
  541.             fn = os.path.join(os.path.abspath(p), exe)
  542.             if os.path.isfile(fn):
  543.                 return fn
  544.         
  545.         for p in string.split(os.environ['Path'], ';'):
  546.             fn = os.path.join(os.path.abspath(p), exe)
  547.             if os.path.isfile(fn):
  548.                 return fn
  549.         
  550.         return exe
  551.  
  552.     
  553.     def get_msvc_paths(self, path, platform = 'x86'):
  554.         if not _can_read_reg:
  555.             return []
  556.         path = path + ' dirs'
  557.         if self._MSVCCompiler__version >= 7:
  558.             key = '%s\\%0.1f\\VC\\VC_OBJECTS_PLATFORM_INFO\\Win32\\Directories' % (self._MSVCCompiler__root, self._MSVCCompiler__version)
  559.         else:
  560.             key = '%s\\6.0\\Build System\\Components\\Platforms\\Win32 (%s)\\Directories' % (self._MSVCCompiler__root, platform)
  561.         for base in HKEYS:
  562.             d = read_values(base, key)
  563.             if d:
  564.                 if self._MSVCCompiler__version >= 7:
  565.                     return string.split(self._MSVCCompiler__macros.sub(d[path]), ';')
  566.                 return string.split(d[path], ';')
  567.             d
  568.         
  569.         if self._MSVCCompiler__version == 6:
  570.             for base in HKEYS:
  571.                 if read_values(base, '%s\\6.0' % self._MSVCCompiler__root) is not None:
  572.                     self.warn('It seems you have Visual Studio 6 installed, but the expected registry settings are not present.\nYou must at least run the Visual Studio GUI once so that these entries are created.')
  573.                     break
  574.                     continue
  575.             
  576.         
  577.         return []
  578.  
  579.     
  580.     def set_path_env_var(self, name):
  581.         if name == 'lib':
  582.             p = self.get_msvc_paths('library')
  583.         else:
  584.             p = self.get_msvc_paths(name)
  585.         if p:
  586.             os.environ[name] = string.join(p, ';')
  587.         
  588.  
  589.  
  590. if get_build_version() >= 8:
  591.     log.debug('Importing new compiler from distutils.msvc9compiler')
  592.     OldMSVCCompiler = MSVCCompiler
  593.     from distutils.msvc9compiler import MSVCCompiler
  594.     from distutils.msvc9compiler import MacroExpander
  595.  
  596.