Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Attributes | Static Private Attributes
distutils.msvccompiler.MSVCCompiler Class Reference
Collaboration diagram for distutils.msvccompiler.MSVCCompiler:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def initialize
def object_filenames
def compile
def create_static_lib
def link
def library_dir_option
def runtime_library_dir_option
def library_option
def find_library_file
def find_exe
def get_msvc_paths
def set_path_env_var

Public Attributes

 initialized
 cc
 linker
 lib
 rc
 mc
 preprocess_options
 compile_options
 compile_options_debug
 ldflags_shared
 ldflags_shared_debug
 ldflags_static

Static Public Attributes

string compiler_type = 'msvc'
dictionary executables = {}
tuple src_extensions
string res_extension = '.res'
string obj_extension = '.obj'
string static_lib_extension = '.lib'
string shared_lib_extension = '.dll'
string static_lib_format = '%s%s'
string exe_extension = '.exe'

Private Attributes

 __version
 __arch
 __root
 __macros
 __product
 __paths

Static Private Attributes

list _c_extensions = ['.c']
list _cpp_extensions = ['.cc', '.cpp', '.cxx']
list _rc_extensions = ['.rc']
list _mc_extensions = ['.mc']

Detailed Description

Concrete class that implements an interface to Microsoft Visual C++,
   as defined by the CCompiler abstract class.

Definition at line 197 of file msvccompiler.py.


Constructor & Destructor Documentation

def distutils.msvccompiler.MSVCCompiler.__init__ (   self,
  verbose = 0,
  dry_run = 0,
  force = 0 
)

Definition at line 227 of file msvccompiler.py.

00227 
00228     def __init__(self, verbose=0, dry_run=0, force=0):
00229         CCompiler.__init__ (self, verbose, dry_run, force)
00230         self.__version = get_build_version()
00231         self.__arch = get_build_architecture()
00232         if self.__arch == "Intel":
00233             # x86
00234             if self.__version >= 7:
00235                 self.__root = r"Software\Microsoft\VisualStudio"
00236                 self.__macros = MacroExpander(self.__version)
00237             else:
00238                 self.__root = r"Software\Microsoft\Devstudio"
00239             self.__product = "Visual Studio version %s" % self.__version
00240         else:
00241             # Win64. Assume this was built with the platform SDK
00242             self.__product = "Microsoft SDK compiler %s" % (self.__version + 6)
00243 
00244         self.initialized = False

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

def distutils.msvccompiler.MSVCCompiler.compile (   self,
  sources,
  output_dir = None,
  macros = None,
  include_dirs = None,
  debug = 0,
  extra_preargs = None,
  extra_postargs = None,
  depends = None 
)

Definition at line 342 of file msvccompiler.py.

00342 
00343                 extra_preargs=None, extra_postargs=None, depends=None):
00344 
00345         if not self.initialized:
00346             self.initialize()
00347         compile_info = self._setup_compile(output_dir, macros, include_dirs,
00348                                            sources, depends, extra_postargs)
00349         macros, objects, extra_postargs, pp_opts, build = compile_info
00350 
00351         compile_opts = extra_preargs or []
00352         compile_opts.append ('/c')
00353         if debug:
00354             compile_opts.extend(self.compile_options_debug)
00355         else:
00356             compile_opts.extend(self.compile_options)
00357 
00358         for obj in objects:
00359             try:
00360                 src, ext = build[obj]
00361             except KeyError:
00362                 continue
00363             if debug:
00364                 # pass the full pathname to MSVC in debug mode,
00365                 # this allows the debugger to find the source file
00366                 # without asking the user to browse for it
00367                 src = os.path.abspath(src)
00368 
00369             if ext in self._c_extensions:
00370                 input_opt = "/Tc" + src
00371             elif ext in self._cpp_extensions:
00372                 input_opt = "/Tp" + src
00373             elif ext in self._rc_extensions:
00374                 # compile .RC to .RES file
00375                 input_opt = src
00376                 output_opt = "/fo" + obj
00377                 try:
00378                     self.spawn([self.rc] + pp_opts +
00379                                [output_opt] + [input_opt])
00380                 except DistutilsExecError as msg:
00381                     raise CompileError(msg)
00382                 continue
00383             elif ext in self._mc_extensions:
00384                 # Compile .MC to .RC file to .RES file.
00385                 #   * '-h dir' specifies the directory for the
00386                 #     generated include file
00387                 #   * '-r dir' specifies the target directory of the
00388                 #     generated RC file and the binary message resource
00389                 #     it includes
00390                 #
00391                 # For now (since there are no options to change this),
00392                 # we use the source-directory for the include file and
00393                 # the build directory for the RC file and message
00394                 # resources. This works at least for win32all.
00395                 h_dir = os.path.dirname(src)
00396                 rc_dir = os.path.dirname(obj)
00397                 try:
00398                     # first compile .MC to .RC and .H file
00399                     self.spawn([self.mc] +
00400                                ['-h', h_dir, '-r', rc_dir] + [src])
00401                     base, _ = os.path.splitext (os.path.basename (src))
00402                     rc_file = os.path.join (rc_dir, base + '.rc')
00403                     # then compile .RC to .RES file
00404                     self.spawn([self.rc] +
00405                                ["/fo" + obj] + [rc_file])
00406 
00407                 except DistutilsExecError as msg:
00408                     raise CompileError(msg)
00409                 continue
00410             else:
00411                 # how to handle this file?
00412                 raise CompileError("Don't know how to compile %s to %s"
00413                                    % (src, obj))
00414 
00415             output_opt = "/Fo" + obj
00416             try:
00417                 self.spawn([self.cc] + compile_opts + pp_opts +
00418                            [input_opt, output_opt] +
00419                            extra_postargs)
00420             except DistutilsExecError as msg:
00421                 raise CompileError(msg)
00422 
00423         return objects
00424 

Here is the call graph for this function:

def distutils.msvccompiler.MSVCCompiler.create_static_lib (   self,
  objects,
  output_libname,
  output_dir = None,
  debug = 0,
  target_lang = None 
)

Definition at line 430 of file msvccompiler.py.

00430 
00431                           target_lang=None):
00432 
00433         if not self.initialized:
00434             self.initialize()
00435         (objects, output_dir) = self._fix_object_args(objects, output_dir)
00436         output_filename = self.library_filename(output_libname,
00437                                                 output_dir=output_dir)
00438 
00439         if self._need_link(objects, output_filename):
00440             lib_args = objects + ['/OUT:' + output_filename]
00441             if debug:
00442                 pass # XXX what goes here?
00443             try:
00444                 self.spawn([self.lib] + lib_args)
00445             except DistutilsExecError as msg:
00446                 raise LibError(msg)
00447         else:
00448             log.debug("skipping %s (up-to-date)", output_filename)
00449 

Here is the call graph for this function:

Return path to an MSVC executable program.

Tries to find the program in several places: first, one of the
MSVC program search paths from the registry; next, the directories
in the PATH environment variable.  If any of those work, return an
absolute path that is known to exist.  If none of them work, just
return the original program name, 'exe'.

Definition at line 562 of file msvccompiler.py.

00562 
00563     def find_exe(self, exe):
00564         """Return path to an MSVC executable program.
00565 
00566         Tries to find the program in several places: first, one of the
00567         MSVC program search paths from the registry; next, the directories
00568         in the PATH environment variable.  If any of those work, return an
00569         absolute path that is known to exist.  If none of them work, just
00570         return the original program name, 'exe'.
00571         """
00572         for p in self.__paths:
00573             fn = os.path.join(os.path.abspath(p), exe)
00574             if os.path.isfile(fn):
00575                 return fn
00576 
00577         # didn't find it; try existing path
00578         for p in os.environ['Path'].split(';'):
00579             fn = os.path.join(os.path.abspath(p),exe)
00580             if os.path.isfile(fn):
00581                 return fn
00582 
00583         return exe

Here is the call graph for this function:

def distutils.msvccompiler.MSVCCompiler.find_library_file (   self,
  dirs,
  lib,
  debug = 0 
)

Definition at line 544 of file msvccompiler.py.

00544 
00545     def find_library_file(self, dirs, lib, debug=0):
00546         # Prefer a debugging library if found (and requested), but deal
00547         # with it if we don't have one.
00548         if debug:
00549             try_names = [lib + "_d", lib]
00550         else:
00551             try_names = [lib]
00552         for dir in dirs:
00553             for name in try_names:
00554                 libfile = os.path.join(dir, self.library_filename (name))
00555                 if os.path.exists(libfile):
00556                     return libfile
00557         else:
00558             # Oops, didn't find it in *any* of 'dirs'
00559             return None

Here is the call graph for this function:

def distutils.msvccompiler.MSVCCompiler.get_msvc_paths (   self,
  path,
  platform = 'x86' 
)
Get a list of devstudio directories (include, lib or path).

Return a list of strings.  The list will be empty if unable to
access the registry or appropriate registry keys not found.

Definition at line 584 of file msvccompiler.py.

00584 
00585     def get_msvc_paths(self, path, platform='x86'):
00586         """Get a list of devstudio directories (include, lib or path).
00587 
00588         Return a list of strings.  The list will be empty if unable to
00589         access the registry or appropriate registry keys not found.
00590         """
00591         if not _can_read_reg:
00592             return []
00593 
00594         path = path + " dirs"
00595         if self.__version >= 7:
00596             key = (r"%s\%0.1f\VC\VC_OBJECTS_PLATFORM_INFO\Win32\Directories"
00597                    % (self.__root, self.__version))
00598         else:
00599             key = (r"%s\6.0\Build System\Components\Platforms"
00600                    r"\Win32 (%s)\Directories" % (self.__root, platform))
00601 
00602         for base in HKEYS:
00603             d = read_values(base, key)
00604             if d:
00605                 if self.__version >= 7:
00606                     return self.__macros.sub(d[path]).split(";")
00607                 else:
00608                     return d[path].split(";")
00609         # MSVC 6 seems to create the registry entries we need only when
00610         # the GUI is run.
00611         if self.__version == 6:
00612             for base in HKEYS:
00613                 if read_values(base, r"%s\6.0" % self.__root) is not None:
00614                     self.warn("It seems you have Visual Studio 6 installed, "
00615                         "but the expected registry settings are not present.\n"
00616                         "You must at least run the Visual Studio GUI once "
00617                         "so that these entries are created.")
00618                     break
00619         return []

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 245 of file msvccompiler.py.

00245 
00246     def initialize(self):
00247         self.__paths = []
00248         if "DISTUTILS_USE_SDK" in os.environ and "MSSdk" in os.environ and self.find_exe("cl.exe"):
00249             # Assume that the SDK set up everything alright; don't try to be
00250             # smarter
00251             self.cc = "cl.exe"
00252             self.linker = "link.exe"
00253             self.lib = "lib.exe"
00254             self.rc = "rc.exe"
00255             self.mc = "mc.exe"
00256         else:
00257             self.__paths = self.get_msvc_paths("path")
00258 
00259             if len(self.__paths) == 0:
00260                 raise DistutilsPlatformError("Python was built with %s, "
00261                        "and extensions need to be built with the same "
00262                        "version of the compiler, but it isn't installed."
00263                        % self.__product)
00264 
00265             self.cc = self.find_exe("cl.exe")
00266             self.linker = self.find_exe("link.exe")
00267             self.lib = self.find_exe("lib.exe")
00268             self.rc = self.find_exe("rc.exe")   # resource compiler
00269             self.mc = self.find_exe("mc.exe")   # message compiler
00270             self.set_path_env_var('lib')
00271             self.set_path_env_var('include')
00272 
00273         # extend the MSVC path with the current path
00274         try:
00275             for p in os.environ['path'].split(';'):
00276                 self.__paths.append(p)
00277         except KeyError:
00278             pass
00279         self.__paths = normalize_and_reduce_paths(self.__paths)
00280         os.environ['path'] = ";".join(self.__paths)
00281 
00282         self.preprocess_options = None
00283         if self.__arch == "Intel":
00284             self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', '/GX' ,
00285                                      '/DNDEBUG']
00286             self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GX',
00287                                           '/Z7', '/D_DEBUG']
00288         else:
00289             # Win64
00290             self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', '/GS-' ,
00291                                      '/DNDEBUG']
00292             self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GS-',
00293                                           '/Z7', '/D_DEBUG']
00294 
00295         self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO']
00296         if self.__version >= 7:
00297             self.ldflags_shared_debug = [
00298                 '/DLL', '/nologo', '/INCREMENTAL:no', '/DEBUG'
00299                 ]
00300         else:
00301             self.ldflags_shared_debug = [
00302                 '/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG'
00303                 ]
00304         self.ldflags_static = [ '/nologo']
00305 
00306         self.initialized = True

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 533 of file msvccompiler.py.

00533 
00534     def library_dir_option(self, dir):
00535         return "/LIBPATH:" + dir

Here is the call graph for this function:

Definition at line 540 of file msvccompiler.py.

00540 
00541     def library_option(self, lib):
00542         return self.library_filename(lib)
00543 

Here is the call graph for this function:

def distutils.msvccompiler.MSVCCompiler.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 
)

Definition at line 463 of file msvccompiler.py.

00463 
00464              target_lang=None):
00465 
00466         if not self.initialized:
00467             self.initialize()
00468         (objects, output_dir) = self._fix_object_args(objects, output_dir)
00469         fixed_args = self._fix_lib_args(libraries, library_dirs,
00470                                         runtime_library_dirs)
00471         (libraries, library_dirs, runtime_library_dirs) = fixed_args
00472 
00473         if runtime_library_dirs:
00474             self.warn ("I don't know what to do with 'runtime_library_dirs': "
00475                        + str (runtime_library_dirs))
00476 
00477         lib_opts = gen_lib_options(self,
00478                                    library_dirs, runtime_library_dirs,
00479                                    libraries)
00480         if output_dir is not None:
00481             output_filename = os.path.join(output_dir, output_filename)
00482 
00483         if self._need_link(objects, output_filename):
00484             if target_desc == CCompiler.EXECUTABLE:
00485                 if debug:
00486                     ldflags = self.ldflags_shared_debug[1:]
00487                 else:
00488                     ldflags = self.ldflags_shared[1:]
00489             else:
00490                 if debug:
00491                     ldflags = self.ldflags_shared_debug
00492                 else:
00493                     ldflags = self.ldflags_shared
00494 
00495             export_opts = []
00496             for sym in (export_symbols or []):
00497                 export_opts.append("/EXPORT:" + sym)
00498 
00499             ld_args = (ldflags + lib_opts + export_opts +
00500                        objects + ['/OUT:' + output_filename])
00501 
00502             # The MSVC linker generates .lib and .exp files, which cannot be
00503             # suppressed by any linker switches. The .lib files may even be
00504             # needed! Make sure they are generated in the temporary build
00505             # directory. Since they have different names for debug and release
00506             # builds, they can go into the same directory.
00507             if export_symbols is not None:
00508                 (dll_name, dll_ext) = os.path.splitext(
00509                     os.path.basename(output_filename))
00510                 implib_file = os.path.join(
00511                     os.path.dirname(objects[0]),
00512                     self.library_filename(dll_name))
00513                 ld_args.append ('/IMPLIB:' + implib_file)
00514 
00515             if extra_preargs:
00516                 ld_args[:0] = extra_preargs
00517             if extra_postargs:
00518                 ld_args.extend(extra_postargs)
00519 
00520             self.mkpath(os.path.dirname(output_filename))
00521             try:
00522                 self.spawn([self.linker] + ld_args)
00523             except DistutilsExecError as msg:
00524                 raise LinkError(msg)
00525 
00526         else:
00527             log.debug("skipping %s (up-to-date)", output_filename)
00528 

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.msvccompiler.MSVCCompiler.object_filenames (   self,
  source_filenames,
  strip_dir = 0,
  output_dir = '' 
)

Definition at line 312 of file msvccompiler.py.

00312 
00313                          output_dir=''):
00314         # Copied from ccompiler.py, extended to return .res as 'object'-file
00315         # for .rc input file
00316         if output_dir is None: output_dir = ''
00317         obj_names = []
00318         for src_name in source_filenames:
00319             (base, ext) = os.path.splitext (src_name)
00320             base = os.path.splitdrive(base)[1] # Chop off the drive
00321             base = base[os.path.isabs(base):]  # If abs, chop off leading /
00322             if ext not in self.src_extensions:
00323                 # Better to raise an exception instead of silently continuing
00324                 # and later complain about sources and targets having
00325                 # different lengths
00326                 raise CompileError ("Don't know how to compile %s" % src_name)
00327             if strip_dir:
00328                 base = os.path.basename (base)
00329             if ext in self._rc_extensions:
00330                 obj_names.append (os.path.join (output_dir,
00331                                                 base + self.res_extension))
00332             elif ext in self._mc_extensions:
00333                 obj_names.append (os.path.join (output_dir,
00334                                                 base + self.res_extension))
00335             else:
00336                 obj_names.append (os.path.join (output_dir,
00337                                                 base + self.obj_extension))
00338         return obj_names
00339 

Here is the call graph for this function:

Definition at line 536 of file msvccompiler.py.

00536 
00537     def runtime_library_dir_option(self, dir):
00538         raise DistutilsPlatformError(
00539               "don't know how to set runtime library search path for MSVC++")

Here is the call graph for this function:

def distutils.msvccompiler.MSVCCompiler.set_path_env_var (   self,
  name 
)
Set environment variable 'name' to an MSVC path type value.

This is equivalent to a SET command prior to execution of spawned
commands.

Definition at line 620 of file msvccompiler.py.

00620 
00621     def set_path_env_var(self, name):
00622         """Set environment variable 'name' to an MSVC path type value.
00623 
00624         This is equivalent to a SET command prior to execution of spawned
00625         commands.
00626         """
00627 
00628         if name == "lib":
00629             p = self.get_msvc_paths("library")
00630         else:
00631             p = self.get_msvc_paths(name)
00632         if p:
00633             os.environ[name] = ';'.join(p)
00634 

Here is the call graph for this function:


Member Data Documentation

Definition at line 230 of file msvccompiler.py.

distutils.msvccompiler.MSVCCompiler.__macros [private]

Definition at line 235 of file msvccompiler.py.

Definition at line 246 of file msvccompiler.py.

distutils.msvccompiler.MSVCCompiler.__product [private]

Definition at line 238 of file msvccompiler.py.

Definition at line 234 of file msvccompiler.py.

Definition at line 229 of file msvccompiler.py.

Definition at line 211 of file msvccompiler.py.

list distutils.msvccompiler.MSVCCompiler._cpp_extensions = ['.cc', '.cpp', '.cxx'] [static, private]

Definition at line 212 of file msvccompiler.py.

Definition at line 214 of file msvccompiler.py.

Definition at line 213 of file msvccompiler.py.

Definition at line 250 of file msvccompiler.py.

Definition at line 283 of file msvccompiler.py.

Definition at line 285 of file msvccompiler.py.

Definition at line 201 of file msvccompiler.py.

Definition at line 225 of file msvccompiler.py.

Definition at line 208 of file msvccompiler.py.

Definition at line 243 of file msvccompiler.py.

Definition at line 294 of file msvccompiler.py.

Definition at line 296 of file msvccompiler.py.

Definition at line 303 of file msvccompiler.py.

Definition at line 252 of file msvccompiler.py.

Definition at line 251 of file msvccompiler.py.

Definition at line 254 of file msvccompiler.py.

Definition at line 221 of file msvccompiler.py.

Definition at line 281 of file msvccompiler.py.

Definition at line 253 of file msvccompiler.py.

Definition at line 220 of file msvccompiler.py.

Definition at line 223 of file msvccompiler.py.

Initial value:
(_c_extensions + _cpp_extensions +
                      _rc_extensions + _mc_extensions)

Definition at line 218 of file msvccompiler.py.

Definition at line 222 of file msvccompiler.py.

Definition at line 224 of file msvccompiler.py.


The documentation for this class was generated from the following file: