2000-06-27 22:29:09 -03:00
|
|
|
"""distutils.msvccompiler
|
1999-08-29 15:15:07 -03:00
|
|
|
|
|
|
|
Contains MSVCCompiler, an implementation of the abstract CCompiler class
|
2003-05-09 13:06:42 -03:00
|
|
|
for the Microsoft Visual Studio.
|
|
|
|
"""
|
1999-08-29 15:15:07 -03:00
|
|
|
|
2002-11-13 22:25:42 -04:00
|
|
|
# Written by Perry Stoll
|
2000-03-05 23:40:29 -04:00
|
|
|
# hacked by Robin Becker and Thomas Heller to do a better job of
|
|
|
|
# finding DevStudio (through the registry)
|
|
|
|
|
2002-11-19 09:12:28 -04:00
|
|
|
# This module should be kept compatible with Python 1.5.2.
|
|
|
|
|
2000-03-01 21:49:45 -04:00
|
|
|
__revision__ = "$Id$"
|
1999-08-29 15:15:07 -03:00
|
|
|
|
2000-03-05 23:40:29 -04:00
|
|
|
import sys, os, string
|
2000-05-29 23:02:49 -03:00
|
|
|
from distutils.errors import \
|
|
|
|
DistutilsExecError, DistutilsPlatformError, \
|
2000-05-29 22:56:44 -03:00
|
|
|
CompileError, LibError, LinkError
|
2000-05-29 23:02:49 -03:00
|
|
|
from distutils.ccompiler import \
|
|
|
|
CCompiler, gen_preprocess_options, gen_lib_options
|
2002-06-04 17:14:43 -03:00
|
|
|
from distutils import log
|
2000-02-09 22:52:42 -04:00
|
|
|
|
2000-03-31 12:47:40 -04:00
|
|
|
_can_read_reg = 0
|
|
|
|
try:
|
2000-06-30 16:37:59 -03:00
|
|
|
import _winreg
|
2000-06-29 20:04:59 -03:00
|
|
|
|
2000-03-31 12:47:40 -04:00
|
|
|
_can_read_reg = 1
|
2000-06-29 19:59:10 -03:00
|
|
|
hkey_mod = _winreg
|
2000-03-31 15:04:25 -04:00
|
|
|
|
2000-06-29 19:59:10 -03:00
|
|
|
RegOpenKeyEx = _winreg.OpenKeyEx
|
|
|
|
RegEnumKey = _winreg.EnumKey
|
|
|
|
RegEnumValue = _winreg.EnumValue
|
|
|
|
RegError = _winreg.error
|
2000-03-31 15:04:25 -04:00
|
|
|
|
2000-03-31 12:47:40 -04:00
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
import win32api
|
|
|
|
import win32con
|
|
|
|
_can_read_reg = 1
|
2000-03-31 12:53:42 -04:00
|
|
|
hkey_mod = win32con
|
2000-03-31 15:04:25 -04:00
|
|
|
|
|
|
|
RegOpenKeyEx = win32api.RegOpenKeyEx
|
|
|
|
RegEnumKey = win32api.RegEnumKey
|
|
|
|
RegEnumValue = win32api.RegEnumValue
|
|
|
|
RegError = win32api.error
|
|
|
|
|
2000-03-31 12:47:40 -04:00
|
|
|
except ImportError:
|
|
|
|
pass
|
2000-03-31 12:53:42 -04:00
|
|
|
|
|
|
|
if _can_read_reg:
|
2003-05-09 13:06:42 -03:00
|
|
|
HKEYS = (hkey_mod.HKEY_USERS,
|
|
|
|
hkey_mod.HKEY_CURRENT_USER,
|
|
|
|
hkey_mod.HKEY_LOCAL_MACHINE,
|
|
|
|
hkey_mod.HKEY_CLASSES_ROOT)
|
2001-12-06 16:51:35 -04:00
|
|
|
|
2003-05-09 13:06:42 -03:00
|
|
|
def read_keys(base, key):
|
|
|
|
"""Return list of registry keys."""
|
2001-12-06 16:51:35 -04:00
|
|
|
|
2003-05-09 13:06:42 -03:00
|
|
|
try:
|
|
|
|
handle = RegOpenKeyEx(base, key)
|
|
|
|
except RegError:
|
|
|
|
return None
|
2000-02-07 22:39:44 -04:00
|
|
|
L = []
|
2003-05-09 13:06:42 -03:00
|
|
|
i = 0
|
|
|
|
while 1:
|
2000-02-07 22:39:44 -04:00
|
|
|
try:
|
2003-05-09 13:06:42 -03:00
|
|
|
k = RegEnumKey(handle, i)
|
2000-03-31 12:53:42 -04:00
|
|
|
except RegError:
|
2003-05-09 13:06:42 -03:00
|
|
|
break
|
|
|
|
L.append(k)
|
|
|
|
i = i + 1
|
2000-02-07 22:39:44 -04:00
|
|
|
return L
|
|
|
|
|
2003-05-09 13:06:42 -03:00
|
|
|
def read_values(base, key):
|
|
|
|
"""Return dict of registry keys and values.
|
2000-02-07 22:39:44 -04:00
|
|
|
|
2003-05-09 13:06:42 -03:00
|
|
|
All names are converted to lowercase.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
handle = RegOpenKeyEx(base, key)
|
|
|
|
except RegError:
|
|
|
|
return None
|
|
|
|
d = {}
|
|
|
|
i = 0
|
|
|
|
while 1:
|
2000-02-07 22:39:44 -04:00
|
|
|
try:
|
2003-05-09 13:06:42 -03:00
|
|
|
name, value, type = RegEnumValue(handle, i)
|
2000-03-31 12:53:42 -04:00
|
|
|
except RegError:
|
2003-05-09 13:06:42 -03:00
|
|
|
break
|
|
|
|
name = name.lower()
|
|
|
|
d[convert_mbcs(name)] = convert_mbcs(value)
|
|
|
|
i = i + 1
|
|
|
|
return d
|
|
|
|
|
|
|
|
def convert_mbcs(s):
|
|
|
|
enc = getattr(s, "encode", None)
|
|
|
|
if enc is not None:
|
|
|
|
try:
|
|
|
|
s = enc("mbcs")
|
|
|
|
except UnicodeError:
|
2000-02-07 22:39:44 -04:00
|
|
|
pass
|
2003-05-09 13:06:42 -03:00
|
|
|
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
|
|
|
|
|
|
|
|
def load_macros(self, version):
|
|
|
|
vsbase = r"Software\Microsoft\VisualStudio\%s.0" % version
|
|
|
|
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")
|
|
|
|
self.set_macro("FrameworkSDKDir", net, "sdkinstallroot")
|
|
|
|
|
|
|
|
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():
|
|
|
|
s = string.replace(s, k, v)
|
|
|
|
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."
|
|
|
|
i = string.find(sys.version, prefix)
|
|
|
|
if i == -1:
|
|
|
|
return 6
|
2003-05-14 16:48:57 -03:00
|
|
|
i = i + len(prefix)
|
2003-05-09 13:06:42 -03:00
|
|
|
s, rest = sys.version[i:].split(" ", 1)
|
|
|
|
n = int(s[:-2])
|
|
|
|
if n == 12:
|
|
|
|
return 6
|
|
|
|
elif n == 13:
|
|
|
|
return 7
|
|
|
|
# else we don't know what version of the compiler this is
|
|
|
|
return None
|
|
|
|
|
1999-08-29 15:15:07 -03:00
|
|
|
|
1999-09-07 23:36:01 -03:00
|
|
|
class MSVCCompiler (CCompiler) :
|
|
|
|
"""Concrete class that implements an interface to Microsoft Visual C++,
|
|
|
|
as defined by the CCompiler abstract class."""
|
1999-08-29 15:15:07 -03:00
|
|
|
|
1999-09-29 09:29:10 -03:00
|
|
|
compiler_type = 'msvc'
|
|
|
|
|
2000-06-24 23:31:16 -03:00
|
|
|
# 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 = {}
|
|
|
|
|
2000-03-05 23:40:29 -04:00
|
|
|
# Private class data (need to distinguish C from C++ source for compiler)
|
|
|
|
_c_extensions = ['.c']
|
2000-08-30 14:32:24 -03:00
|
|
|
_cpp_extensions = ['.cc', '.cpp', '.cxx']
|
2000-09-19 20:56:43 -03:00
|
|
|
_rc_extensions = ['.rc']
|
|
|
|
_mc_extensions = ['.mc']
|
2000-03-05 23:40:29 -04:00
|
|
|
|
|
|
|
# Needed for the filename generation methods provided by the
|
|
|
|
# base class, CCompiler.
|
2000-09-19 20:56:43 -03:00
|
|
|
src_extensions = (_c_extensions + _cpp_extensions +
|
|
|
|
_rc_extensions + _mc_extensions)
|
|
|
|
res_extension = '.res'
|
2000-03-05 23:40:29 -04:00
|
|
|
obj_extension = '.obj'
|
|
|
|
static_lib_extension = '.lib'
|
|
|
|
shared_lib_extension = '.dll'
|
|
|
|
static_lib_format = shared_lib_format = '%s%s'
|
|
|
|
exe_extension = '.exe'
|
|
|
|
|
2003-05-09 13:06:42 -03:00
|
|
|
def __init__ (self, verbose=0, dry_run=0, force=0):
|
1999-10-03 17:47:52 -03:00
|
|
|
CCompiler.__init__ (self, verbose, dry_run, force)
|
2003-05-09 13:06:42 -03:00
|
|
|
self.__version = get_build_version()
|
|
|
|
if self.__version == 7:
|
|
|
|
self.__root = r"Software\Microsoft\VisualStudio"
|
|
|
|
self.__macros = MacroExpander(self.__version)
|
2000-02-10 22:47:15 -04:00
|
|
|
else:
|
2003-05-09 13:06:42 -03:00
|
|
|
self.__root = r"Software\Microsoft\Devstudio"
|
|
|
|
self.__paths = self.get_msvc_paths("path")
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
# extend the MSVC path with the current path
|
|
|
|
try:
|
|
|
|
for p in string.split(os.environ['path'], ';'):
|
|
|
|
self.__paths.append(p)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
os.environ['path'] = string.join(self.__paths, ';')
|
2000-02-10 22:47:15 -04:00
|
|
|
|
1999-08-29 15:15:07 -03:00
|
|
|
self.preprocess_options = None
|
2002-06-18 16:08:40 -03:00
|
|
|
self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', '/GX' ,
|
|
|
|
'/DNDEBUG']
|
2000-08-30 21:31:07 -03:00
|
|
|
self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GX',
|
|
|
|
'/Z7', '/D_DEBUG']
|
1999-08-29 15:15:07 -03:00
|
|
|
|
2000-02-07 22:39:44 -04:00
|
|
|
self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO']
|
2000-02-09 22:15:52 -04:00
|
|
|
self.ldflags_shared_debug = [
|
|
|
|
'/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG'
|
|
|
|
]
|
1999-08-29 15:15:07 -03:00
|
|
|
self.ldflags_static = [ '/nologo']
|
|
|
|
|
|
|
|
|
|
|
|
# -- Worker methods ------------------------------------------------
|
|
|
|
|
2000-09-19 20:56:43 -03:00
|
|
|
def object_filenames (self,
|
|
|
|
source_filenames,
|
|
|
|
strip_dir=0,
|
|
|
|
output_dir=''):
|
|
|
|
# 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)
|
|
|
|
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
|
|
|
|
|
|
|
|
# object_filenames ()
|
|
|
|
|
|
|
|
|
2002-06-13 14:28:18 -03:00
|
|
|
def compile(self, sources,
|
|
|
|
output_dir=None, macros=None, include_dirs=None, debug=0,
|
|
|
|
extra_preargs=None, extra_postargs=None, depends=None):
|
|
|
|
|
|
|
|
macros, objects, extra_postargs, pp_opts, build = \
|
|
|
|
self._setup_compile(output_dir, macros, include_dirs, sources,
|
|
|
|
depends, extra_postargs)
|
|
|
|
|
2000-03-05 23:40:29 -04:00
|
|
|
compile_opts = extra_preargs or []
|
|
|
|
compile_opts.append ('/c')
|
2000-02-09 22:15:52 -04:00
|
|
|
if debug:
|
2002-06-13 14:28:18 -03:00
|
|
|
compile_opts.extend(self.compile_options_debug)
|
2000-02-09 22:15:52 -04:00
|
|
|
else:
|
2002-06-13 14:28:18 -03:00
|
|
|
compile_opts.extend(self.compile_options)
|
|
|
|
|
|
|
|
for obj, (src, ext) in build.items():
|
|
|
|
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:
|
2003-01-31 16:40:15 -04:00
|
|
|
self.spawn ([self.rc] + pp_opts +
|
2002-06-13 14:28:18 -03:00
|
|
|
[output_opt] + [input_opt])
|
|
|
|
except DistutilsExecError, msg:
|
|
|
|
raise CompileError, msg
|
|
|
|
continue
|
|
|
|
elif ext in self._mc_extensions:
|
1999-08-29 15:15:07 -03:00
|
|
|
|
2002-06-13 14:28:18 -03:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
h_dir = os.path.dirname (src)
|
|
|
|
rc_dir = os.path.dirname (obj)
|
2000-05-29 22:56:44 -03:00
|
|
|
try:
|
2002-06-13 14:28:18 -03:00
|
|
|
# first compile .MC to .RC and .H file
|
|
|
|
self.spawn ([self.mc] +
|
|
|
|
['-h', h_dir, '-r', rc_dir] + [src])
|
|
|
|
base, _ = os.path.splitext (os.path.basename (src))
|
|
|
|
rc_file = os.path.join (rc_dir, base + '.rc')
|
|
|
|
# then compile .RC to .RES file
|
|
|
|
self.spawn ([self.rc] +
|
|
|
|
["/fo" + obj] + [rc_file])
|
|
|
|
|
2000-05-29 22:56:44 -03:00
|
|
|
except DistutilsExecError, msg:
|
|
|
|
raise CompileError, msg
|
2002-06-13 14:28:18 -03:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# how to handle this file?
|
|
|
|
raise CompileError (
|
|
|
|
"Don't know how to compile %s to %s" % \
|
|
|
|
(src, obj))
|
|
|
|
|
|
|
|
output_opt = "/Fo" + obj
|
|
|
|
try:
|
|
|
|
self.spawn ([self.cc] + compile_opts + pp_opts +
|
|
|
|
[input_opt, output_opt] +
|
|
|
|
extra_postargs)
|
|
|
|
except DistutilsExecError, msg:
|
|
|
|
raise CompileError, msg
|
2000-02-09 22:15:52 -04:00
|
|
|
|
2000-03-05 23:40:29 -04:00
|
|
|
return objects
|
1999-08-29 15:15:07 -03:00
|
|
|
|
2000-03-05 23:40:29 -04:00
|
|
|
# compile ()
|
1999-09-07 23:36:01 -03:00
|
|
|
|
|
|
|
|
2000-03-09 21:49:26 -04:00
|
|
|
def create_static_lib (self,
|
|
|
|
objects,
|
|
|
|
output_libname,
|
|
|
|
output_dir=None,
|
|
|
|
debug=0,
|
This patch fixes the following bugs:
[#413582] g++ must be called for c++ extensions
[#454030] distutils cannot link C++ code with GCC
topdir = "Lib/distutils"
* bcppcompiler.py
(BCPPCompiler.create_static_lib): Fixed prototype, removing extra_preargs
and extra_postargs parameters. Included target_lang parameter.
(BCPPCompiler.link): Included target_lang parameter.
* msvccompiler.py
(MSVCCompiler.create_static_lib): Fixed prototype, removing extra_preargs
and extra_postargs parameters. Included target_lang parameter.
(MSVCCompiler.link): Included target_lang parameter.
* ccompiler.py
(CCompiler): New language_map and language_order attributes, used by
CCompiler.detect_language().
(CCompiler.detect_language): New method, will return the language of
a given source, or list of sources. Individual source language is
detected using the language_map dict. When mixed sources are used,
language_order will stablish the language precedence.
(CCompiler.create_static_lib, CCompiler.link, CCompiler.link_executable,
CCompiler.link_shared_object, CCompiler.link_shared_lib):
Inlcuded target_lang parameter.
* cygwinccompiler.py
(CygwinCCompiler.link): Included target_lang parameter.
* emxccompiler.py
(EMXCCompiler.link): Included target_lang parameter.
* mwerkscompiler.py
(MWerksCompiler.link): Included target_lang parameter.
* extension.py
(Extension.__init__): New 'language' parameter/attribute, initialized
to None by default. If provided will overlap the automatic detection
made by CCompiler.detect_language(), in build_ext command.
* sysconfig.py
(customize_compiler): Check Makefile for CXX option, and also the
environment variable CXX. Use the resulting value in the 'compiler_cxx'
parameter of compiler.set_executables().
* unixccompiler.py
(UnixCCompiler): Included 'compiler_cxx' in executables dict, defaulting
to 'cc'.
(UnixCCompiler.create_static_lib): Included target_lang parameter.
(UnixCCompiler.link): Included target_lang parameter, and made
linker command use compiler_cxx, if target_lang is 'c++'.
* command/build_ext.py
(build_ext.build_extension): Pass new ext.language attribute
to compiler.link_shared_object()'s target_lang parameter. If
ext.language is not provided, detect language using
compiler.detect_language(sources) instead.
* command/config.py
(config._link): Pass already available lang parameter as target_lang
parameter of compiler.link_executable().
2002-11-05 12:12:02 -04:00
|
|
|
target_lang=None):
|
1999-09-29 09:29:10 -03:00
|
|
|
|
2000-03-26 17:42:28 -04:00
|
|
|
(objects, output_dir) = self._fix_object_args (objects, output_dir)
|
2000-03-05 23:40:29 -04:00
|
|
|
output_filename = \
|
|
|
|
self.library_filename (output_libname, output_dir=output_dir)
|
|
|
|
|
|
|
|
if self._need_link (objects, output_filename):
|
2000-03-09 21:49:26 -04:00
|
|
|
lib_args = objects + ['/OUT:' + output_filename]
|
2000-03-05 23:40:29 -04:00
|
|
|
if debug:
|
|
|
|
pass # XXX what goes here?
|
2000-05-29 22:56:44 -03:00
|
|
|
try:
|
2000-06-24 23:31:16 -03:00
|
|
|
self.spawn ([self.lib] + lib_args)
|
2000-05-29 22:56:44 -03:00
|
|
|
except DistutilsExecError, msg:
|
|
|
|
raise LibError, msg
|
2001-12-06 16:51:35 -04:00
|
|
|
|
2000-03-05 23:40:29 -04:00
|
|
|
else:
|
2002-06-04 17:14:43 -03:00
|
|
|
log.debug("skipping %s (up-to-date)", output_filename)
|
1999-08-29 15:15:07 -03:00
|
|
|
|
2000-03-09 21:49:26 -04:00
|
|
|
# create_static_lib ()
|
2001-12-06 16:51:35 -04:00
|
|
|
|
2000-09-26 23:08:14 -03:00
|
|
|
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,
|
This patch fixes the following bugs:
[#413582] g++ must be called for c++ extensions
[#454030] distutils cannot link C++ code with GCC
topdir = "Lib/distutils"
* bcppcompiler.py
(BCPPCompiler.create_static_lib): Fixed prototype, removing extra_preargs
and extra_postargs parameters. Included target_lang parameter.
(BCPPCompiler.link): Included target_lang parameter.
* msvccompiler.py
(MSVCCompiler.create_static_lib): Fixed prototype, removing extra_preargs
and extra_postargs parameters. Included target_lang parameter.
(MSVCCompiler.link): Included target_lang parameter.
* ccompiler.py
(CCompiler): New language_map and language_order attributes, used by
CCompiler.detect_language().
(CCompiler.detect_language): New method, will return the language of
a given source, or list of sources. Individual source language is
detected using the language_map dict. When mixed sources are used,
language_order will stablish the language precedence.
(CCompiler.create_static_lib, CCompiler.link, CCompiler.link_executable,
CCompiler.link_shared_object, CCompiler.link_shared_lib):
Inlcuded target_lang parameter.
* cygwinccompiler.py
(CygwinCCompiler.link): Included target_lang parameter.
* emxccompiler.py
(EMXCCompiler.link): Included target_lang parameter.
* mwerkscompiler.py
(MWerksCompiler.link): Included target_lang parameter.
* extension.py
(Extension.__init__): New 'language' parameter/attribute, initialized
to None by default. If provided will overlap the automatic detection
made by CCompiler.detect_language(), in build_ext command.
* sysconfig.py
(customize_compiler): Check Makefile for CXX option, and also the
environment variable CXX. Use the resulting value in the 'compiler_cxx'
parameter of compiler.set_executables().
* unixccompiler.py
(UnixCCompiler): Included 'compiler_cxx' in executables dict, defaulting
to 'cc'.
(UnixCCompiler.create_static_lib): Included target_lang parameter.
(UnixCCompiler.link): Included target_lang parameter, and made
linker command use compiler_cxx, if target_lang is 'c++'.
* command/build_ext.py
(build_ext.build_extension): Pass new ext.language attribute
to compiler.link_shared_object()'s target_lang parameter. If
ext.language is not provided, detect language using
compiler.detect_language(sources) instead.
* command/config.py
(config._link): Pass already available lang parameter as target_lang
parameter of compiler.link_executable().
2002-11-05 12:12:02 -04:00
|
|
|
build_temp=None,
|
|
|
|
target_lang=None):
|
1999-08-29 15:15:07 -03:00
|
|
|
|
2000-03-26 17:42:28 -04:00
|
|
|
(objects, output_dir) = self._fix_object_args (objects, output_dir)
|
|
|
|
(libraries, library_dirs, runtime_library_dirs) = \
|
|
|
|
self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
|
|
|
|
|
2000-04-18 23:16:49 -03:00
|
|
|
if runtime_library_dirs:
|
2000-03-26 17:42:28 -04:00
|
|
|
self.warn ("I don't know what to do with 'runtime_library_dirs': "
|
|
|
|
+ str (runtime_library_dirs))
|
2001-12-06 16:51:35 -04:00
|
|
|
|
2000-03-18 11:19:51 -04:00
|
|
|
lib_opts = gen_lib_options (self,
|
2000-03-26 17:42:28 -04:00
|
|
|
library_dirs, runtime_library_dirs,
|
2000-03-18 11:19:51 -04:00
|
|
|
libraries)
|
2000-03-05 23:40:29 -04:00
|
|
|
if output_dir is not None:
|
|
|
|
output_filename = os.path.join (output_dir, output_filename)
|
|
|
|
|
|
|
|
if self._need_link (objects, output_filename):
|
|
|
|
|
2000-09-26 23:08:14 -03:00
|
|
|
if target_desc == CCompiler.EXECUTABLE:
|
|
|
|
if debug:
|
|
|
|
ldflags = self.ldflags_shared_debug[1:]
|
|
|
|
else:
|
|
|
|
ldflags = self.ldflags_shared[1:]
|
2000-03-05 23:40:29 -04:00
|
|
|
else:
|
2000-09-26 23:08:14 -03:00
|
|
|
if debug:
|
|
|
|
ldflags = self.ldflags_shared_debug
|
|
|
|
else:
|
|
|
|
ldflags = self.ldflags_shared
|
2000-03-05 23:40:29 -04:00
|
|
|
|
2000-05-20 10:23:21 -03:00
|
|
|
export_opts = []
|
|
|
|
for sym in (export_symbols or []):
|
|
|
|
export_opts.append("/EXPORT:" + sym)
|
|
|
|
|
2001-12-06 16:51:35 -04:00
|
|
|
ld_args = (ldflags + lib_opts + export_opts +
|
2000-05-20 10:23:21 -03:00
|
|
|
objects + ['/OUT:' + output_filename])
|
1999-08-29 15:15:07 -03:00
|
|
|
|
2000-08-01 21:00:30 -03:00
|
|
|
# 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.
|
2000-09-26 23:08:14 -03:00
|
|
|
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)
|
2000-08-01 21:00:30 -03:00
|
|
|
|
2000-03-05 23:40:29 -04:00
|
|
|
if extra_preargs:
|
|
|
|
ld_args[:0] = extra_preargs
|
|
|
|
if extra_postargs:
|
2000-08-01 21:00:30 -03:00
|
|
|
ld_args.extend(extra_postargs)
|
1999-08-29 15:15:07 -03:00
|
|
|
|
2000-03-05 23:40:29 -04:00
|
|
|
self.mkpath (os.path.dirname (output_filename))
|
2000-05-29 22:56:44 -03:00
|
|
|
try:
|
2000-09-26 23:08:14 -03:00
|
|
|
self.spawn ([self.linker] + ld_args)
|
2000-05-29 22:56:44 -03:00
|
|
|
except DistutilsExecError, msg:
|
|
|
|
raise LinkError, msg
|
1999-08-29 15:15:07 -03:00
|
|
|
|
2000-03-05 23:40:29 -04:00
|
|
|
else:
|
2002-06-04 17:14:43 -03:00
|
|
|
log.debug("skipping %s (up-to-date)", output_filename)
|
1999-08-29 15:15:07 -03:00
|
|
|
|
2000-09-26 23:08:14 -03:00
|
|
|
# link ()
|
2000-04-18 23:16:49 -03:00
|
|
|
|
|
|
|
|
2000-03-05 23:40:29 -04:00
|
|
|
# -- Miscellaneous methods -----------------------------------------
|
|
|
|
# These are all used by the 'gen_lib_options() function, in
|
|
|
|
# ccompiler.py.
|
1999-10-03 17:47:52 -03:00
|
|
|
|
|
|
|
def library_dir_option (self, dir):
|
|
|
|
return "/LIBPATH:" + dir
|
|
|
|
|
2000-03-18 11:19:51 -04:00
|
|
|
def runtime_library_dir_option (self, dir):
|
|
|
|
raise DistutilsPlatformError, \
|
|
|
|
"don't know how to set runtime library search path for MSVC++"
|
|
|
|
|
1999-10-03 17:47:52 -03:00
|
|
|
def library_option (self, lib):
|
|
|
|
return self.library_filename (lib)
|
|
|
|
|
|
|
|
|
2000-08-03 22:29:27 -03:00
|
|
|
def find_library_file (self, dirs, lib, debug=0):
|
|
|
|
# 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]
|
1999-10-03 17:47:52 -03:00
|
|
|
for dir in dirs:
|
2000-08-03 22:29:27 -03:00
|
|
|
for name in try_names:
|
|
|
|
libfile = os.path.join(dir, self.library_filename (name))
|
|
|
|
if os.path.exists(libfile):
|
|
|
|
return libfile
|
1999-10-03 17:47:52 -03:00
|
|
|
else:
|
|
|
|
# Oops, didn't find it in *any* of 'dirs'
|
|
|
|
return None
|
|
|
|
|
|
|
|
# find_library_file ()
|
|
|
|
|
2003-05-09 13:06:42 -03:00
|
|
|
# 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
|
|
|
|
for p in string.split(os.environ['Path'],';'):
|
|
|
|
fn = os.path.join(os.path.abspath(p),exe)
|
|
|
|
if os.path.isfile(fn):
|
|
|
|
return fn
|
|
|
|
|
|
|
|
return exe
|
|
|
|
|
|
|
|
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"
|
|
|
|
if self.__version == 7:
|
|
|
|
key = (r"%s\7.0\VC\VC_OBJECTS_PLATFORM_INFO\Win32\Directories"
|
|
|
|
% (self.__root,))
|
|
|
|
else:
|
|
|
|
key = (r"%s\6.0\Build System\Components\Platforms"
|
2003-05-09 13:55:28 -03:00
|
|
|
r"\Win32 (%s)\Directories" % (self.__root, platform))
|
2003-05-09 13:06:42 -03:00
|
|
|
|
|
|
|
for base in HKEYS:
|
|
|
|
d = read_values(base, key)
|
|
|
|
if d:
|
|
|
|
if self.__version == 7:
|
|
|
|
return string.split(self.__macros.sub(d[path]), ";")
|
|
|
|
else:
|
|
|
|
return string.split(d[path], ";")
|
|
|
|
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:
|
|
|
|
os.environ[name] = string.join(p, ';')
|
|
|
|
|