msvccompiler.py 23 KB
Newer Older
1
"""distutils.msvccompiler
2 3

Contains MSVCCompiler, an implementation of the abstract CCompiler class
4 5
for the Microsoft Visual Studio.
"""
6

7
# Written by Perry Stoll
8 9 10
# hacked by Robin Becker and Thomas Heller to do a better job of
#   finding DevStudio (through the registry)

11 12 13 14 15 16
import sys, os
from distutils.errors import \
     DistutilsExecError, DistutilsPlatformError, \
     CompileError, LibError, LinkError
from distutils.ccompiler import \
     CCompiler, gen_preprocess_options, gen_lib_options
17
from distutils import log
18

19
_can_read_reg = False
20
try:
21
    import winreg
22

23
    _can_read_reg = True
24
    hkey_mod = winreg
25

26 27 28 29
    RegOpenKeyEx = winreg.OpenKeyEx
    RegEnumKey = winreg.EnumKey
    RegEnumValue = winreg.EnumValue
    RegError = winreg.error
30

31
except ImportError:
32 33 34
    try:
        import win32api
        import win32con
35
        _can_read_reg = True
36
        hkey_mod = win32con
37 38 39 40 41

        RegOpenKeyEx = win32api.RegOpenKeyEx
        RegEnumKey = win32api.RegEnumKey
        RegEnumValue = win32api.RegEnumValue
        RegError = win32api.error
42
    except ImportError:
43 44
        log.info("Warning: Can't read registry to find the "
                 "necessary compiler setting\n"
45
                 "Make sure that Python modules winreg, "
46
                 "win32api or win32con are installed.")
47
        pass
48 49

if _can_read_reg:
50 51 52 53
    HKEYS = (hkey_mod.HKEY_USERS,
             hkey_mod.HKEY_CURRENT_USER,
             hkey_mod.HKEY_LOCAL_MACHINE,
             hkey_mod.HKEY_CLASSES_ROOT)
Fred Drake's avatar
Fred Drake committed
54

55 56 57 58 59 60
def read_keys(base, key):
    """Return list of registry keys."""
    try:
        handle = RegOpenKeyEx(base, key)
    except RegError:
        return None
61
    L = []
62
    i = 0
63
    while True:
64
        try:
65
            k = RegEnumKey(handle, i)
66
        except RegError:
67 68
            break
        L.append(k)
69
        i += 1
70 71
    return L

72 73
def read_values(base, key):
    """Return dict of registry keys and values.
74

75 76 77 78 79 80 81 82
    All names are converted to lowercase.
    """
    try:
        handle = RegOpenKeyEx(base, key)
    except RegError:
        return None
    d = {}
    i = 0
83
    while True:
84
        try:
85
            name, value, type = RegEnumValue(handle, i)
86
        except RegError:
87 88 89
            break
        name = name.lower()
        d[convert_mbcs(name)] = convert_mbcs(value)
90
        i += 1
91 92 93
    return d

def convert_mbcs(s):
94 95
    dec = getattr(s, "decode", None)
    if dec is not None:
96
        try:
97
            s = dec("mbcs")
98
        except UnicodeError:
99
            pass
100 101 102 103 104 105 106 107 108 109 110 111 112
    return s

class MacroExpander:
    def __init__(self, version):
        self.macros = {}
        self.load_macros(version)

    def set_macro(self, macro, path, key):
        for base in HKEYS:
            d = read_values(base, path)
            if d:
                self.macros["$(%s)" % macro] = d[key]
                break
113

114
    def load_macros(self, version):
115
        vsbase = r"Software\Microsoft\VisualStudio\%0.1f" % version
116 117 118 119
        self.set_macro("VCInstallDir", vsbase + r"\Setup\VC", "productdir")
        self.set_macro("VSInstallDir", vsbase + r"\Setup\VS", "productdir")
        net = r"Software\Microsoft\.NETFramework"
        self.set_macro("FrameworkDir", net, "installroot")
Tim Peters's avatar
Tim Peters committed
120 121 122 123 124
        try:
            if version > 7.0:
                self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv1.1")
            else:
                self.set_macro("FrameworkSDKDir", net, "sdkinstallroot")
125
        except KeyError as exc: #
126 127
            raise DistutilsPlatformError(
            """Python was built with Visual Studio 2003;
128 129 130
extensions must be built with a compiler than can generate compatible binaries.
Visual Studio 2003 was not found on this system. If you have Cygwin installed,
you can try compiling with MingW32, by passing "-c mingw32" to setup.py.""")
131 132 133 134 135 136 137 138 139 140 141 142 143

        p = r"Software\Microsoft\NET Framework Setup\Product"
        for base in HKEYS:
            try:
                h = RegOpenKeyEx(base, p)
            except RegError:
                continue
            key = RegEnumKey(h, 0)
            d = read_values(base, r"%s\%s" % (p, key))
            self.macros["$(FrameworkVersion)"] = d["version"]

    def sub(self, s):
        for k, v in self.macros.items():
144
            s = s.replace(k, v)
145 146 147 148 149 150 151 152 153
        return s

def get_build_version():
    """Return the version of MSVC that was used to build Python.

    For Python 2.3 and up, the version number is included in
    sys.version.  For earlier versions, assume the compiler is MSVC 6.
    """
    prefix = "MSC v."
154
    i = sys.version.find(prefix)
155 156
    if i == -1:
        return 6
157
    i = i + len(prefix)
158
    s, rest = sys.version[i:].split(" ", 1)
159
    majorVersion = int(s[:-2]) - 6
160 161 162
    if majorVersion >= 13:
        # v13 was skipped and should be v14
        majorVersion += 1
163 164 165 166 167 168
    minorVersion = int(s[2:3]) / 10.0
    # I don't think paths are affected by minor version in version 6
    if majorVersion == 6:
        minorVersion = 0
    if majorVersion >= 6:
        return majorVersion + minorVersion
169 170
    # else we don't know what version of the compiler this is
    return None
171

Martin v. Löwis's avatar
Martin v. Löwis committed
172 173 174
def get_build_architecture():
    """Return the processor architecture.

175
    Possible results are "Intel" or "AMD64".
Martin v. Löwis's avatar
Martin v. Löwis committed
176 177 178
    """

    prefix = " bit ("
179
    i = sys.version.find(prefix)
Martin v. Löwis's avatar
Martin v. Löwis committed
180 181
    if i == -1:
        return "Intel"
182
    j = sys.version.find(")", i)
Martin v. Löwis's avatar
Martin v. Löwis committed
183
    return sys.version[i+len(prefix):j]
Tim Peters's avatar
Tim Peters committed
184

185 186 187 188 189 190 191 192 193 194 195 196 197
def normalize_and_reduce_paths(paths):
    """Return a list of normalized paths with duplicates removed.

    The current order of paths is maintained.
    """
    # Paths are normalized so things like:  /a and /a/ aren't both preserved.
    reduced_paths = []
    for p in paths:
        np = os.path.normpath(p)
        # XXX(nnorwitz): O(n**2), if reduced_paths gets long perhaps use a set.
        if np not in reduced_paths:
            reduced_paths.append(np)
    return reduced_paths
Martin v. Löwis's avatar
Martin v. Löwis committed
198

199

200
class MSVCCompiler(CCompiler) :
201 202
    """Concrete class that implements an interface to Microsoft Visual C++,
       as defined by the CCompiler abstract class."""
203

204 205
    compiler_type = 'msvc'

206 207 208 209 210 211 212
    # Just set this so CCompiler's constructor doesn't barf.  We currently
    # don't use the 'set_executables()' bureaucracy provided by CCompiler,
    # as it really isn't necessary for this sort of single-compiler class.
    # Would be nice to have a consistent interface with UnixCCompiler,
    # though, so it's worth thinking about.
    executables = {}

213 214
    # Private class data (need to distinguish C from C++ source for compiler)
    _c_extensions = ['.c']
215
    _cpp_extensions = ['.cc', '.cpp', '.cxx']
216 217
    _rc_extensions = ['.rc']
    _mc_extensions = ['.mc']
218 219 220

    # Needed for the filename generation methods provided by the
    # base class, CCompiler.
221 222 223
    src_extensions = (_c_extensions + _cpp_extensions +
                      _rc_extensions + _mc_extensions)
    res_extension = '.res'
224 225 226 227 228 229
    obj_extension = '.obj'
    static_lib_extension = '.lib'
    shared_lib_extension = '.dll'
    static_lib_format = shared_lib_format = '%s%s'
    exe_extension = '.exe'

230
    def __init__(self, verbose=0, dry_run=0, force=0):
231
        CCompiler.__init__ (self, verbose, dry_run, force)
232
        self.__version = get_build_version()
Martin v. Löwis's avatar
Martin v. Löwis committed
233 234 235 236 237 238 239 240 241
        self.__arch = get_build_architecture()
        if self.__arch == "Intel":
            # x86
            if self.__version >= 7:
                self.__root = r"Software\Microsoft\VisualStudio"
                self.__macros = MacroExpander(self.__version)
            else:
                self.__root = r"Software\Microsoft\Devstudio"
            self.__product = "Visual Studio version %s" % self.__version
242
        else:
Martin v. Löwis's avatar
Martin v. Löwis committed
243 244
            # Win64. Assume this was built with the platform SDK
            self.__product = "Microsoft SDK compiler %s" % (self.__version + 6)
Tim Peters's avatar
Tim Peters committed
245

246 247 248
        self.initialized = False

    def initialize(self):
249
        self.__paths = []
250
        if "DISTUTILS_USE_SDK" in os.environ and "MSSdk" in os.environ and self.find_exe("cl.exe"):
251 252 253 254 255 256 257 258 259 260
            # Assume that the SDK set up everything alright; don't try to be
            # smarter
            self.cc = "cl.exe"
            self.linker = "link.exe"
            self.lib = "lib.exe"
            self.rc = "rc.exe"
            self.mc = "mc.exe"
        else:
            self.__paths = self.get_msvc_paths("path")

261 262
            if len(self.__paths) == 0:
                raise DistutilsPlatformError("Python was built with %s, "
263
                       "and extensions need to be built with the same "
264 265
                       "version of the compiler, but it isn't installed."
                       % self.__product)
266 267 268 269 270 271 272 273

            self.cc = self.find_exe("cl.exe")
            self.linker = self.find_exe("link.exe")
            self.lib = self.find_exe("lib.exe")
            self.rc = self.find_exe("rc.exe")   # resource compiler
            self.mc = self.find_exe("mc.exe")   # message compiler
            self.set_path_env_var('lib')
            self.set_path_env_var('include')
274 275 276

        # extend the MSVC path with the current path
        try:
277
            for p in os.environ['path'].split(';'):
278 279 280
                self.__paths.append(p)
        except KeyError:
            pass
281 282
        self.__paths = normalize_and_reduce_paths(self.__paths)
        os.environ['path'] = ";".join(self.__paths)
283

284
        self.preprocess_options = None
Martin v. Löwis's avatar
Martin v. Löwis committed
285 286 287 288 289 290 291 292 293 294
        if self.__arch == "Intel":
            self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', '/GX' ,
                                     '/DNDEBUG']
            self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GX',
                                          '/Z7', '/D_DEBUG']
        else:
            # Win64
            self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', '/GS-' ,
                                     '/DNDEBUG']
            self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GS-',
Tim Peters's avatar
Tim Peters committed
295
                                          '/Z7', '/D_DEBUG']
296

297
        self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO']
298 299 300 301 302 303 304 305
        if self.__version >= 7:
            self.ldflags_shared_debug = [
                '/DLL', '/nologo', '/INCREMENTAL:no', '/DEBUG'
                ]
        else:
            self.ldflags_shared_debug = [
                '/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG'
                ]
306 307
        self.ldflags_static = [ '/nologo']

Tim Peters's avatar
Tim Peters committed
308
        self.initialized = True
309 310 311

    # -- Worker methods ------------------------------------------------

312 313 314 315
    def object_filenames(self,
                         source_filenames,
                         strip_dir=0,
                         output_dir=''):
316 317 318 319 320 321
        # Copied from ccompiler.py, extended to return .res as 'object'-file
        # for .rc input file
        if output_dir is None: output_dir = ''
        obj_names = []
        for src_name in source_filenames:
            (base, ext) = os.path.splitext (src_name)
322 323
            base = os.path.splitdrive(base)[1] # Chop off the drive
            base = base[os.path.isabs(base):]  # If abs, chop off leading /
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
            if ext not in self.src_extensions:
                # Better to raise an exception instead of silently continuing
                # and later complain about sources and targets having
                # different lengths
                raise CompileError ("Don't know how to compile %s" % src_name)
            if strip_dir:
                base = os.path.basename (base)
            if ext in self._rc_extensions:
                obj_names.append (os.path.join (output_dir,
                                                base + self.res_extension))
            elif ext in self._mc_extensions:
                obj_names.append (os.path.join (output_dir,
                                                base + self.res_extension))
            else:
                obj_names.append (os.path.join (output_dir,
                                                base + self.obj_extension))
        return obj_names


343 344 345 346
    def compile(self, sources,
                output_dir=None, macros=None, include_dirs=None, debug=0,
                extra_preargs=None, extra_postargs=None, depends=None):

347 348 349 350 351
        if not self.initialized:
            self.initialize()
        compile_info = self._setup_compile(output_dir, macros, include_dirs,
                                           sources, depends, extra_postargs)
        macros, objects, extra_postargs, pp_opts, build = compile_info
352

353 354
        compile_opts = extra_preargs or []
        compile_opts.append ('/c')
Greg Ward's avatar
Greg Ward committed
355
        if debug:
356
            compile_opts.extend(self.compile_options_debug)
Greg Ward's avatar
Greg Ward committed
357
        else:
358 359
            compile_opts.extend(self.compile_options)

360 361 362 363 364
        for obj in objects:
            try:
                src, ext = build[obj]
            except KeyError:
                continue
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
            if debug:
                # pass the full pathname to MSVC in debug mode,
                # this allows the debugger to find the source file
                # without asking the user to browse for it
                src = os.path.abspath(src)

            if ext in self._c_extensions:
                input_opt = "/Tc" + src
            elif ext in self._cpp_extensions:
                input_opt = "/Tp" + src
            elif ext in self._rc_extensions:
                # compile .RC to .RES file
                input_opt = src
                output_opt = "/fo" + obj
                try:
380 381
                    self.spawn([self.rc] + pp_opts +
                               [output_opt] + [input_opt])
382
                except DistutilsExecError as msg:
383
                    raise CompileError(msg)
384 385 386 387 388 389 390 391 392 393 394 395 396
                continue
            elif ext in self._mc_extensions:
                # Compile .MC to .RC file to .RES file.
                #   * '-h dir' specifies the directory for the
                #     generated include file
                #   * '-r dir' specifies the target directory of the
                #     generated RC file and the binary message resource
                #     it includes
                #
                # For now (since there are no options to change this),
                # we use the source-directory for the include file and
                # the build directory for the RC file and message
                # resources. This works at least for win32all.
397 398
                h_dir = os.path.dirname(src)
                rc_dir = os.path.dirname(obj)
399
                try:
400
                    # first compile .MC to .RC and .H file
401 402
                    self.spawn([self.mc] +
                               ['-h', h_dir, '-r', rc_dir] + [src])
403 404 405
                    base, _ = os.path.splitext (os.path.basename (src))
                    rc_file = os.path.join (rc_dir, base + '.rc')
                    # then compile .RC to .RES file
406 407
                    self.spawn([self.rc] +
                               ["/fo" + obj] + [rc_file])
408

409
                except DistutilsExecError as msg:
410
                    raise CompileError(msg)
411 412 413
                continue
            else:
                # how to handle this file?
414 415
                raise CompileError("Don't know how to compile %s to %s"
                                   % (src, obj))
416 417 418

            output_opt = "/Fo" + obj
            try:
419 420 421
                self.spawn([self.cc] + compile_opts + pp_opts +
                           [input_opt, output_opt] +
                           extra_postargs)
422
            except DistutilsExecError as msg:
423
                raise CompileError(msg)
Greg Ward's avatar
Greg Ward committed
424

425
        return objects
426

427

428 429 430 431 432 433
    def create_static_lib(self,
                          objects,
                          output_libname,
                          output_dir=None,
                          debug=0,
                          target_lang=None):
434

435 436 437 438 439
        if not self.initialized:
            self.initialize()
        (objects, output_dir) = self._fix_object_args(objects, output_dir)
        output_filename = self.library_filename(output_libname,
                                                output_dir=output_dir)
440

441
        if self._need_link(objects, output_filename):
442
            lib_args = objects + ['/OUT:' + output_filename]
443
            if debug:
444
                pass # XXX what goes here?
445
            try:
446
                self.spawn([self.lib] + lib_args)
447
            except DistutilsExecError as msg:
448
                raise LibError(msg)
449
        else:
450
            log.debug("skipping %s (up-to-date)", output_filename)
451

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

    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):

        if not self.initialized:
            self.initialize()
        (objects, output_dir) = self._fix_object_args(objects, output_dir)
        fixed_args = self._fix_lib_args(libraries, library_dirs,
                                        runtime_library_dirs)
        (libraries, library_dirs, runtime_library_dirs) = fixed_args
474

475
        if runtime_library_dirs:
476 477
            self.warn ("I don't know what to do with 'runtime_library_dirs': "
                       + str (runtime_library_dirs))
Fred Drake's avatar
Fred Drake committed
478

479 480 481
        lib_opts = gen_lib_options(self,
                                   library_dirs, runtime_library_dirs,
                                   libraries)
482
        if output_dir is not None:
483
            output_filename = os.path.join(output_dir, output_filename)
484

485
        if self._need_link(objects, output_filename):
486 487 488 489 490
            if target_desc == CCompiler.EXECUTABLE:
                if debug:
                    ldflags = self.ldflags_shared_debug[1:]
                else:
                    ldflags = self.ldflags_shared[1:]
491
            else:
492 493 494 495
                if debug:
                    ldflags = self.ldflags_shared_debug
                else:
                    ldflags = self.ldflags_shared
496

497 498 499 500
            export_opts = []
            for sym in (export_symbols or []):
                export_opts.append("/EXPORT:" + sym)

Fred Drake's avatar
Fred Drake committed
501
            ld_args = (ldflags + lib_opts + export_opts +
502
                       objects + ['/OUT:' + output_filename])
503

504 505 506 507 508
            # The MSVC linker generates .lib and .exp files, which cannot be
            # suppressed by any linker switches. The .lib files may even be
            # needed! Make sure they are generated in the temporary build
            # directory. Since they have different names for debug and release
            # builds, they can go into the same directory.
509 510 511 512 513 514 515
            if export_symbols is not None:
                (dll_name, dll_ext) = os.path.splitext(
                    os.path.basename(output_filename))
                implib_file = os.path.join(
                    os.path.dirname(objects[0]),
                    self.library_filename(dll_name))
                ld_args.append ('/IMPLIB:' + implib_file)
516

517 518 519
            if extra_preargs:
                ld_args[:0] = extra_preargs
            if extra_postargs:
520
                ld_args.extend(extra_postargs)
521

522
            self.mkpath(os.path.dirname(output_filename))
523
            try:
524
                self.spawn([self.linker] + ld_args)
525
            except DistutilsExecError as msg:
526
                raise LinkError(msg)
527

528
        else:
529
            log.debug("skipping %s (up-to-date)", output_filename)
530

531

532 533 534
    # -- Miscellaneous methods -----------------------------------------
    # These are all used by the 'gen_lib_options() function, in
    # ccompiler.py.
535

536
    def library_dir_option(self, dir):
537 538
        return "/LIBPATH:" + dir

539 540 541
    def runtime_library_dir_option(self, dir):
        raise DistutilsPlatformError(
              "don't know how to set runtime library search path for MSVC++")
542

543 544
    def library_option(self, lib):
        return self.library_filename(lib)
545 546


547
    def find_library_file(self, dirs, lib, debug=0):
548 549 550 551 552 553
        # Prefer a debugging library if found (and requested), but deal
        # with it if we don't have one.
        if debug:
            try_names = [lib + "_d", lib]
        else:
            try_names = [lib]
554
        for dir in dirs:
555 556 557 558
            for name in try_names:
                libfile = os.path.join(dir, self.library_filename (name))
                if os.path.exists(libfile):
                    return libfile
559 560 561 562
        else:
            # Oops, didn't find it in *any* of 'dirs'
            return None

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    # Helper methods for using the MSVC registry settings

    def find_exe(self, exe):
        """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'.
        """
        for p in self.__paths:
            fn = os.path.join(os.path.abspath(p), exe)
            if os.path.isfile(fn):
                return fn

        # didn't find it; try existing path
580
        for p in os.environ['Path'].split(';'):
581 582 583 584 585
            fn = os.path.join(os.path.abspath(p),exe)
            if os.path.isfile(fn):
                return fn

        return exe
586

587 588 589 590 591 592 593 594 595 596
    def 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.
        """
        if not _can_read_reg:
            return []

        path = path + " dirs"
597 598 599
        if self.__version >= 7:
            key = (r"%s\%0.1f\VC\VC_OBJECTS_PLATFORM_INFO\Win32\Directories"
                   % (self.__root, self.__version))
600 601
        else:
            key = (r"%s\6.0\Build System\Components\Platforms"
602
                   r"\Win32 (%s)\Directories" % (self.__root, platform))
603 604 605 606

        for base in HKEYS:
            d = read_values(base, key)
            if d:
607
                if self.__version >= 7:
608
                    return self.__macros.sub(d[path]).split(";")
609
                else:
610
                    return d[path].split(";")
611 612 613 614 615 616 617 618 619
        # MSVC 6 seems to create the registry entries we need only when
        # the GUI is run.
        if self.__version == 6:
            for base in HKEYS:
                if read_values(base, r"%s\6.0" % self.__root) is not None:
                    self.warn("It seems you have Visual Studio 6 installed, "
                        "but the expected registry settings are not present.\n"
                        "You must at least run the Visual Studio GUI once "
                        "so that these entries are created.")
620
                    break
621 622 623 624 625 626 627 628 629 630 631 632 633 634
        return []

    def 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.
        """

        if name == "lib":
            p = self.get_msvc_paths("library")
        else:
            p = self.get_msvc_paths(name)
        if p:
635
            os.environ[name] = ';'.join(p)
636 637 638 639 640 641


if get_build_version() >= 8.0:
    log.debug("Importing new compiler from distutils.msvc9compiler")
    OldMSVCCompiler = MSVCCompiler
    from distutils.msvc9compiler import MSVCCompiler
Christian Heimes's avatar
Christian Heimes committed
642
    # get_build_architecture not really relevant now we support cross-compile
643
    from distutils.msvc9compiler import MacroExpander