blob: 85d515b20dbc38ea5d2a7467b02c4ed74dda21ae [file] [log] [blame]
Greg Wardbfc79d62000-06-28 01:29:09 +00001"""distutils.msvccompiler
Greg Warddbd12761999-08-29 18:15:07 +00002
3Contains MSVCCompiler, an implementation of the abstract CCompiler class
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +00004for the Microsoft Visual Studio.
5"""
Greg Warddbd12761999-08-29 18:15:07 +00006
Andrew M. Kuchlinga6483d22002-11-14 02:25:42 +00007# Written by Perry Stoll
Greg Ward32c4a8a2000-03-06 03:40:29 +00008# hacked by Robin Becker and Thomas Heller to do a better job of
9# finding DevStudio (through the registry)
10
Martin v. Löwis5a6601c2004-11-10 22:23:15 +000011# This module should be kept compatible with Python 2.1.
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +000012
Greg Ward3ce77fd2000-03-02 01:49:45 +000013__revision__ = "$Id$"
Greg Warddbd12761999-08-29 18:15:07 +000014
Greg Ward32c4a8a2000-03-06 03:40:29 +000015import sys, os, string
Greg Ward3add77f2000-05-30 02:02:49 +000016from distutils.errors import \
17 DistutilsExecError, DistutilsPlatformError, \
Greg Wardd1517112000-05-30 01:56:44 +000018 CompileError, LibError, LinkError
Greg Ward3add77f2000-05-30 02:02:49 +000019from distutils.ccompiler import \
20 CCompiler, gen_preprocess_options, gen_lib_options
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000021from distutils import log
Greg Ward62e33932000-02-10 02:52:42 +000022
Greg Ward7642f5c2000-03-31 16:47:40 +000023_can_read_reg = 0
24try:
Greg Ward1b5ec762000-06-30 19:37:59 +000025 import _winreg
Greg Ward83c38702000-06-29 23:04:59 +000026
Greg Ward7642f5c2000-03-31 16:47:40 +000027 _can_read_reg = 1
Greg Wardcd079c42000-06-29 22:59:10 +000028 hkey_mod = _winreg
Greg Ward19ce1662000-03-31 19:04:25 +000029
Greg Wardcd079c42000-06-29 22:59:10 +000030 RegOpenKeyEx = _winreg.OpenKeyEx
31 RegEnumKey = _winreg.EnumKey
32 RegEnumValue = _winreg.EnumValue
33 RegError = _winreg.error
Greg Ward19ce1662000-03-31 19:04:25 +000034
Greg Ward7642f5c2000-03-31 16:47:40 +000035except ImportError:
36 try:
37 import win32api
38 import win32con
Greg Ward7642f5c2000-03-31 16:47:40 +000039 _can_read_reg = 1
Greg Ward1027e3f2000-03-31 16:53:42 +000040 hkey_mod = win32con
Greg Ward19ce1662000-03-31 19:04:25 +000041
42 RegOpenKeyEx = win32api.RegOpenKeyEx
43 RegEnumKey = win32api.RegEnumKey
44 RegEnumValue = win32api.RegEnumValue
45 RegError = win32api.error
46
Greg Ward7642f5c2000-03-31 16:47:40 +000047 except ImportError:
Jeremy Hyltone9a92aa2003-07-17 14:41:07 +000048 log.info("Warning: Can't read registry to find the "
49 "necessary compiler setting\n"
50 "Make sure that Python modules _winreg, "
51 "win32api or win32con are installed.")
Greg Ward7642f5c2000-03-31 16:47:40 +000052 pass
Greg Ward1027e3f2000-03-31 16:53:42 +000053
54if _can_read_reg:
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +000055 HKEYS = (hkey_mod.HKEY_USERS,
56 hkey_mod.HKEY_CURRENT_USER,
57 hkey_mod.HKEY_LOCAL_MACHINE,
58 hkey_mod.HKEY_CLASSES_ROOT)
Fred Drakeb94b8492001-12-06 20:51:35 +000059
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +000060def read_keys(base, key):
61 """Return list of registry keys."""
Fred Drakeb94b8492001-12-06 20:51:35 +000062
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +000063 try:
64 handle = RegOpenKeyEx(base, key)
65 except RegError:
66 return None
Greg Ward1b9c6f72000-02-08 02:39:44 +000067 L = []
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +000068 i = 0
69 while 1:
Greg Ward1b9c6f72000-02-08 02:39:44 +000070 try:
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +000071 k = RegEnumKey(handle, i)
Greg Ward1027e3f2000-03-31 16:53:42 +000072 except RegError:
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +000073 break
74 L.append(k)
75 i = i + 1
Greg Ward1b9c6f72000-02-08 02:39:44 +000076 return L
77
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +000078def read_values(base, key):
79 """Return dict of registry keys and values.
Greg Ward62e33932000-02-10 02:52:42 +000080
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +000081 All names are converted to lowercase.
82 """
83 try:
84 handle = RegOpenKeyEx(base, key)
85 except RegError:
86 return None
87 d = {}
88 i = 0
89 while 1:
Greg Ward1b9c6f72000-02-08 02:39:44 +000090 try:
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +000091 name, value, type = RegEnumValue(handle, i)
Greg Ward1027e3f2000-03-31 16:53:42 +000092 except RegError:
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +000093 break
94 name = name.lower()
95 d[convert_mbcs(name)] = convert_mbcs(value)
96 i = i + 1
97 return d
98
99def convert_mbcs(s):
100 enc = getattr(s, "encode", None)
101 if enc is not None:
102 try:
103 s = enc("mbcs")
104 except UnicodeError:
Greg Ward1b9c6f72000-02-08 02:39:44 +0000105 pass
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000106 return s
Greg Ward1b9c6f72000-02-08 02:39:44 +0000107
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000108class MacroExpander:
Greg Ward62e33932000-02-10 02:52:42 +0000109
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000110 def __init__(self, version):
111 self.macros = {}
112 self.load_macros(version)
Greg Ward62e33932000-02-10 02:52:42 +0000113
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000114 def set_macro(self, macro, path, key):
115 for base in HKEYS:
116 d = read_values(base, path)
117 if d:
118 self.macros["$(%s)" % macro] = d[key]
119 break
Tim Peters182b5ac2004-07-18 06:16:08 +0000120
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000121 def load_macros(self, version):
Jeremy Hyltone9a92aa2003-07-17 14:41:07 +0000122 vsbase = r"Software\Microsoft\VisualStudio\%0.1f" % version
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000123 self.set_macro("VCInstallDir", vsbase + r"\Setup\VC", "productdir")
124 self.set_macro("VSInstallDir", vsbase + r"\Setup\VS", "productdir")
125 net = r"Software\Microsoft\.NETFramework"
126 self.set_macro("FrameworkDir", net, "installroot")
Tim Peters26be2062004-11-28 01:10:01 +0000127 try:
128 if version > 7.0:
129 self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv1.1")
130 else:
131 self.set_macro("FrameworkSDKDir", net, "sdkinstallroot")
132 except KeyError, exc: #
Fredrik Lundhcb328f32004-11-24 22:31:11 +0000133 raise DistutilsPlatformError, \
134 ("The .NET Framework SDK needs to be installed before "
135 "building extensions for Python.")
Greg Ward1b9c6f72000-02-08 02:39:44 +0000136
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000137 p = r"Software\Microsoft\NET Framework Setup\Product"
138 for base in HKEYS:
139 try:
140 h = RegOpenKeyEx(base, p)
141 except RegError:
142 continue
143 key = RegEnumKey(h, 0)
144 d = read_values(base, r"%s\%s" % (p, key))
145 self.macros["$(FrameworkVersion)"] = d["version"]
Greg Ward69988092000-02-11 02:47:15 +0000146
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000147 def sub(self, s):
148 for k, v in self.macros.items():
149 s = string.replace(s, k, v)
150 return s
Greg Ward69988092000-02-11 02:47:15 +0000151
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000152def get_build_version():
153 """Return the version of MSVC that was used to build Python.
Greg Ward1b9c6f72000-02-08 02:39:44 +0000154
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000155 For Python 2.3 and up, the version number is included in
156 sys.version. For earlier versions, assume the compiler is MSVC 6.
157 """
Greg Ward62e33932000-02-10 02:52:42 +0000158
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000159 prefix = "MSC v."
160 i = string.find(sys.version, prefix)
161 if i == -1:
162 return 6
Marc-André Lemburgf0b5d172003-05-14 19:48:57 +0000163 i = i + len(prefix)
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000164 s, rest = sys.version[i:].split(" ", 1)
Jeremy Hyltone9a92aa2003-07-17 14:41:07 +0000165 majorVersion = int(s[:-2]) - 6
166 minorVersion = int(s[2:3]) / 10.0
167 # I don't think paths are affected by minor version in version 6
168 if majorVersion == 6:
169 minorVersion = 0
170 if majorVersion >= 6:
171 return majorVersion + minorVersion
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000172 # else we don't know what version of the compiler this is
173 return None
Tim Peters182b5ac2004-07-18 06:16:08 +0000174
Greg Warddbd12761999-08-29 18:15:07 +0000175
Greg Ward3d50b901999-09-08 02:36:01 +0000176class MSVCCompiler (CCompiler) :
177 """Concrete class that implements an interface to Microsoft Visual C++,
178 as defined by the CCompiler abstract class."""
Greg Warddbd12761999-08-29 18:15:07 +0000179
Greg Warddf178f91999-09-29 12:29:10 +0000180 compiler_type = 'msvc'
181
Greg Ward992c8f92000-06-25 02:31:16 +0000182 # Just set this so CCompiler's constructor doesn't barf. We currently
183 # don't use the 'set_executables()' bureaucracy provided by CCompiler,
184 # as it really isn't necessary for this sort of single-compiler class.
185 # Would be nice to have a consistent interface with UnixCCompiler,
186 # though, so it's worth thinking about.
187 executables = {}
188
Greg Ward32c4a8a2000-03-06 03:40:29 +0000189 # Private class data (need to distinguish C from C++ source for compiler)
190 _c_extensions = ['.c']
Greg Ward408e9ae2000-08-30 17:32:24 +0000191 _cpp_extensions = ['.cc', '.cpp', '.cxx']
Greg Ward9c0ea132000-09-19 23:56:43 +0000192 _rc_extensions = ['.rc']
193 _mc_extensions = ['.mc']
Greg Ward32c4a8a2000-03-06 03:40:29 +0000194
195 # Needed for the filename generation methods provided by the
196 # base class, CCompiler.
Greg Ward9c0ea132000-09-19 23:56:43 +0000197 src_extensions = (_c_extensions + _cpp_extensions +
198 _rc_extensions + _mc_extensions)
199 res_extension = '.res'
Greg Ward32c4a8a2000-03-06 03:40:29 +0000200 obj_extension = '.obj'
201 static_lib_extension = '.lib'
202 shared_lib_extension = '.dll'
203 static_lib_format = shared_lib_format = '%s%s'
204 exe_extension = '.exe'
205
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000206 def __init__ (self, verbose=0, dry_run=0, force=0):
Greg Wardc74138d1999-10-03 20:47:52 +0000207 CCompiler.__init__ (self, verbose, dry_run, force)
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000208 self.__version = get_build_version()
Jeremy Hyltone9a92aa2003-07-17 14:41:07 +0000209 if self.__version >= 7:
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000210 self.__root = r"Software\Microsoft\VisualStudio"
211 self.__macros = MacroExpander(self.__version)
Greg Ward69988092000-02-11 02:47:15 +0000212 else:
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000213 self.__root = r"Software\Microsoft\Devstudio"
Martin v. Löwisc72dd382005-03-04 13:50:17 +0000214 self.initialized = False
215
216 def initialize(self):
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000217 self.__paths = self.get_msvc_paths("path")
218
Jeremy Hyltone9a92aa2003-07-17 14:41:07 +0000219 if len (self.__paths) == 0:
220 raise DistutilsPlatformError, \
221 ("Python was built with version %s of Visual Studio, "
222 "and extensions need to be built with the same "
223 "version of the compiler, but it isn't installed." % self.__version)
224
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000225 self.cc = self.find_exe("cl.exe")
226 self.linker = self.find_exe("link.exe")
227 self.lib = self.find_exe("lib.exe")
228 self.rc = self.find_exe("rc.exe") # resource compiler
229 self.mc = self.find_exe("mc.exe") # message compiler
230 self.set_path_env_var('lib')
231 self.set_path_env_var('include')
232
233 # extend the MSVC path with the current path
234 try:
235 for p in string.split(os.environ['path'], ';'):
236 self.__paths.append(p)
237 except KeyError:
238 pass
239 os.environ['path'] = string.join(self.__paths, ';')
Greg Ward69988092000-02-11 02:47:15 +0000240
Greg Warddbd12761999-08-29 18:15:07 +0000241 self.preprocess_options = None
Jeremy Hylton2683ac72002-06-18 19:08:40 +0000242 self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', '/GX' ,
243 '/DNDEBUG']
Greg Ward8a98cd92000-08-31 00:31:07 +0000244 self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GX',
245 '/Z7', '/D_DEBUG']
Greg Warddbd12761999-08-29 18:15:07 +0000246
Greg Ward1b9c6f72000-02-08 02:39:44 +0000247 self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO']
Thomas Heller41f70382004-11-10 09:01:41 +0000248 if self.__version >= 7:
249 self.ldflags_shared_debug = [
250 '/DLL', '/nologo', '/INCREMENTAL:no', '/DEBUG'
251 ]
252 else:
253 self.ldflags_shared_debug = [
254 '/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG'
255 ]
Greg Warddbd12761999-08-29 18:15:07 +0000256 self.ldflags_static = [ '/nologo']
257
Tim Petersa733bd92005-03-12 19:05:58 +0000258 self.initialized = True
Greg Warddbd12761999-08-29 18:15:07 +0000259
260 # -- Worker methods ------------------------------------------------
Greg Warddbd12761999-08-29 18:15:07 +0000261
Greg Ward9c0ea132000-09-19 23:56:43 +0000262 def object_filenames (self,
263 source_filenames,
264 strip_dir=0,
265 output_dir=''):
266 # Copied from ccompiler.py, extended to return .res as 'object'-file
267 # for .rc input file
268 if output_dir is None: output_dir = ''
269 obj_names = []
270 for src_name in source_filenames:
271 (base, ext) = os.path.splitext (src_name)
Martin v. Löwisb813c532005-08-07 20:51:04 +0000272 base = os.path.splitdrive(base)[1] # Chop off the drive
273 base = base[os.path.isabs(base):] # If abs, chop off leading /
Greg Ward9c0ea132000-09-19 23:56:43 +0000274 if ext not in self.src_extensions:
275 # Better to raise an exception instead of silently continuing
276 # and later complain about sources and targets having
277 # different lengths
278 raise CompileError ("Don't know how to compile %s" % src_name)
279 if strip_dir:
280 base = os.path.basename (base)
281 if ext in self._rc_extensions:
282 obj_names.append (os.path.join (output_dir,
283 base + self.res_extension))
284 elif ext in self._mc_extensions:
285 obj_names.append (os.path.join (output_dir,
286 base + self.res_extension))
287 else:
288 obj_names.append (os.path.join (output_dir,
289 base + self.obj_extension))
290 return obj_names
291
292 # object_filenames ()
293
294
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000295 def compile(self, sources,
296 output_dir=None, macros=None, include_dirs=None, debug=0,
297 extra_preargs=None, extra_postargs=None, depends=None):
Greg Warddbd12761999-08-29 18:15:07 +0000298
Brett Cannon3304a142005-03-05 05:28:45 +0000299 if not self.initialized: self.initialize()
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000300 macros, objects, extra_postargs, pp_opts, build = \
301 self._setup_compile(output_dir, macros, include_dirs, sources,
302 depends, extra_postargs)
Greg Warddbd12761999-08-29 18:15:07 +0000303
Greg Ward32c4a8a2000-03-06 03:40:29 +0000304 compile_opts = extra_preargs or []
305 compile_opts.append ('/c')
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000306 if debug:
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000307 compile_opts.extend(self.compile_options_debug)
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000308 else:
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000309 compile_opts.extend(self.compile_options)
Fred Drakeb94b8492001-12-06 20:51:35 +0000310
Thomas Heller9436a752003-12-05 20:12:23 +0000311 for obj in objects:
312 try:
313 src, ext = build[obj]
314 except KeyError:
315 continue
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000316 if debug:
317 # pass the full pathname to MSVC in debug mode,
318 # this allows the debugger to find the source file
319 # without asking the user to browse for it
320 src = os.path.abspath(src)
Greg Warddbd12761999-08-29 18:15:07 +0000321
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000322 if ext in self._c_extensions:
323 input_opt = "/Tc" + src
324 elif ext in self._cpp_extensions:
325 input_opt = "/Tp" + src
326 elif ext in self._rc_extensions:
327 # compile .RC to .RES file
328 input_opt = src
329 output_opt = "/fo" + obj
Greg Wardd1517112000-05-30 01:56:44 +0000330 try:
Thomas Heller95827942003-01-31 20:40:15 +0000331 self.spawn ([self.rc] + pp_opts +
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000332 [output_opt] + [input_opt])
Greg Wardd1517112000-05-30 01:56:44 +0000333 except DistutilsExecError, msg:
334 raise CompileError, msg
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000335 continue
336 elif ext in self._mc_extensions:
337
338 # Compile .MC to .RC file to .RES file.
339 # * '-h dir' specifies the directory for the
340 # generated include file
341 # * '-r dir' specifies the target directory of the
342 # generated RC file and the binary message resource
343 # it includes
344 #
345 # For now (since there are no options to change this),
346 # we use the source-directory for the include file and
347 # the build directory for the RC file and message
348 # resources. This works at least for win32all.
349
350 h_dir = os.path.dirname (src)
351 rc_dir = os.path.dirname (obj)
352 try:
353 # first compile .MC to .RC and .H file
354 self.spawn ([self.mc] +
355 ['-h', h_dir, '-r', rc_dir] + [src])
356 base, _ = os.path.splitext (os.path.basename (src))
357 rc_file = os.path.join (rc_dir, base + '.rc')
358 # then compile .RC to .RES file
359 self.spawn ([self.rc] +
360 ["/fo" + obj] + [rc_file])
361
362 except DistutilsExecError, msg:
363 raise CompileError, msg
364 continue
365 else:
366 # how to handle this file?
367 raise CompileError (
368 "Don't know how to compile %s to %s" % \
369 (src, obj))
370
371 output_opt = "/Fo" + obj
372 try:
373 self.spawn ([self.cc] + compile_opts + pp_opts +
374 [input_opt, output_opt] +
375 extra_postargs)
376 except DistutilsExecError, msg:
377 raise CompileError, msg
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000378
Greg Ward32c4a8a2000-03-06 03:40:29 +0000379 return objects
Greg Warddbd12761999-08-29 18:15:07 +0000380
Greg Ward32c4a8a2000-03-06 03:40:29 +0000381 # compile ()
Greg Ward3d50b901999-09-08 02:36:01 +0000382
383
Greg Ward09fc5422000-03-10 01:49:26 +0000384 def create_static_lib (self,
385 objects,
386 output_libname,
387 output_dir=None,
388 debug=0,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000389 target_lang=None):
Greg Warddbd12761999-08-29 18:15:07 +0000390
Brett Cannon1bfd85b2005-03-05 05:32:14 +0000391 if not self.initialized: self.initialize()
Greg Ward2f557a22000-03-26 21:42:28 +0000392 (objects, output_dir) = self._fix_object_args (objects, output_dir)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000393 output_filename = \
394 self.library_filename (output_libname, output_dir=output_dir)
Greg Warddbd12761999-08-29 18:15:07 +0000395
Greg Ward32c4a8a2000-03-06 03:40:29 +0000396 if self._need_link (objects, output_filename):
Greg Ward09fc5422000-03-10 01:49:26 +0000397 lib_args = objects + ['/OUT:' + output_filename]
Greg Ward32c4a8a2000-03-06 03:40:29 +0000398 if debug:
399 pass # XXX what goes here?
Greg Wardd1517112000-05-30 01:56:44 +0000400 try:
Greg Ward992c8f92000-06-25 02:31:16 +0000401 self.spawn ([self.lib] + lib_args)
Greg Wardd1517112000-05-30 01:56:44 +0000402 except DistutilsExecError, msg:
403 raise LibError, msg
Fred Drakeb94b8492001-12-06 20:51:35 +0000404
Greg Ward32c4a8a2000-03-06 03:40:29 +0000405 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000406 log.debug("skipping %s (up-to-date)", output_filename)
Greg Warddbd12761999-08-29 18:15:07 +0000407
Greg Ward09fc5422000-03-10 01:49:26 +0000408 # create_static_lib ()
Fred Drakeb94b8492001-12-06 20:51:35 +0000409
Greg Ward42406482000-09-27 02:08:14 +0000410 def link (self,
411 target_desc,
412 objects,
413 output_filename,
414 output_dir=None,
415 libraries=None,
416 library_dirs=None,
417 runtime_library_dirs=None,
418 export_symbols=None,
419 debug=0,
420 extra_preargs=None,
421 extra_postargs=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000422 build_temp=None,
423 target_lang=None):
Greg Ward32c4a8a2000-03-06 03:40:29 +0000424
Brett Cannon1bfd85b2005-03-05 05:32:14 +0000425 if not self.initialized: self.initialize()
Greg Ward2f557a22000-03-26 21:42:28 +0000426 (objects, output_dir) = self._fix_object_args (objects, output_dir)
427 (libraries, library_dirs, runtime_library_dirs) = \
428 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
429
Greg Wardf70c6032000-04-19 02:16:49 +0000430 if runtime_library_dirs:
Greg Ward2f557a22000-03-26 21:42:28 +0000431 self.warn ("I don't know what to do with 'runtime_library_dirs': "
432 + str (runtime_library_dirs))
Fred Drakeb94b8492001-12-06 20:51:35 +0000433
Greg Wardd03f88a2000-03-18 15:19:51 +0000434 lib_opts = gen_lib_options (self,
Greg Ward2f557a22000-03-26 21:42:28 +0000435 library_dirs, runtime_library_dirs,
Greg Wardd03f88a2000-03-18 15:19:51 +0000436 libraries)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000437 if output_dir is not None:
438 output_filename = os.path.join (output_dir, output_filename)
Greg Warddbd12761999-08-29 18:15:07 +0000439
Greg Ward32c4a8a2000-03-06 03:40:29 +0000440 if self._need_link (objects, output_filename):
441
Greg Ward42406482000-09-27 02:08:14 +0000442 if target_desc == CCompiler.EXECUTABLE:
443 if debug:
444 ldflags = self.ldflags_shared_debug[1:]
445 else:
446 ldflags = self.ldflags_shared[1:]
Greg Ward32c4a8a2000-03-06 03:40:29 +0000447 else:
Greg Ward42406482000-09-27 02:08:14 +0000448 if debug:
449 ldflags = self.ldflags_shared_debug
450 else:
451 ldflags = self.ldflags_shared
Greg Ward32c4a8a2000-03-06 03:40:29 +0000452
Greg Ward5299b6a2000-05-20 13:23:21 +0000453 export_opts = []
454 for sym in (export_symbols or []):
455 export_opts.append("/EXPORT:" + sym)
456
Fred Drakeb94b8492001-12-06 20:51:35 +0000457 ld_args = (ldflags + lib_opts + export_opts +
Greg Ward5299b6a2000-05-20 13:23:21 +0000458 objects + ['/OUT:' + output_filename])
Greg Ward32c4a8a2000-03-06 03:40:29 +0000459
Greg Ward159eb922000-08-02 00:00:30 +0000460 # The MSVC linker generates .lib and .exp files, which cannot be
461 # suppressed by any linker switches. The .lib files may even be
462 # needed! Make sure they are generated in the temporary build
463 # directory. Since they have different names for debug and release
464 # builds, they can go into the same directory.
Greg Ward42406482000-09-27 02:08:14 +0000465 if export_symbols is not None:
466 (dll_name, dll_ext) = os.path.splitext(
467 os.path.basename(output_filename))
468 implib_file = os.path.join(
469 os.path.dirname(objects[0]),
470 self.library_filename(dll_name))
471 ld_args.append ('/IMPLIB:' + implib_file)
Greg Ward159eb922000-08-02 00:00:30 +0000472
Greg Ward32c4a8a2000-03-06 03:40:29 +0000473 if extra_preargs:
474 ld_args[:0] = extra_preargs
475 if extra_postargs:
Greg Ward159eb922000-08-02 00:00:30 +0000476 ld_args.extend(extra_postargs)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000477
478 self.mkpath (os.path.dirname (output_filename))
Greg Wardd1517112000-05-30 01:56:44 +0000479 try:
Greg Ward42406482000-09-27 02:08:14 +0000480 self.spawn ([self.linker] + ld_args)
Greg Wardd1517112000-05-30 01:56:44 +0000481 except DistutilsExecError, msg:
482 raise LinkError, msg
Greg Ward32c4a8a2000-03-06 03:40:29 +0000483
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000484 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000485 log.debug("skipping %s (up-to-date)", output_filename)
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000486
Greg Ward42406482000-09-27 02:08:14 +0000487 # link ()
Greg Wardf70c6032000-04-19 02:16:49 +0000488
489
Greg Ward32c4a8a2000-03-06 03:40:29 +0000490 # -- Miscellaneous methods -----------------------------------------
491 # These are all used by the 'gen_lib_options() function, in
492 # ccompiler.py.
Greg Wardc74138d1999-10-03 20:47:52 +0000493
494 def library_dir_option (self, dir):
495 return "/LIBPATH:" + dir
496
Greg Wardd03f88a2000-03-18 15:19:51 +0000497 def runtime_library_dir_option (self, dir):
498 raise DistutilsPlatformError, \
499 "don't know how to set runtime library search path for MSVC++"
500
Greg Wardc74138d1999-10-03 20:47:52 +0000501 def library_option (self, lib):
502 return self.library_filename (lib)
503
504
Greg Wardd1425642000-08-04 01:29:27 +0000505 def find_library_file (self, dirs, lib, debug=0):
506 # Prefer a debugging library if found (and requested), but deal
507 # with it if we don't have one.
508 if debug:
509 try_names = [lib + "_d", lib]
510 else:
511 try_names = [lib]
Greg Wardc74138d1999-10-03 20:47:52 +0000512 for dir in dirs:
Greg Wardd1425642000-08-04 01:29:27 +0000513 for name in try_names:
514 libfile = os.path.join(dir, self.library_filename (name))
515 if os.path.exists(libfile):
516 return libfile
Greg Wardc74138d1999-10-03 20:47:52 +0000517 else:
518 # Oops, didn't find it in *any* of 'dirs'
519 return None
520
521 # find_library_file ()
522
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000523 # Helper methods for using the MSVC registry settings
524
525 def find_exe(self, exe):
526 """Return path to an MSVC executable program.
527
528 Tries to find the program in several places: first, one of the
529 MSVC program search paths from the registry; next, the directories
530 in the PATH environment variable. If any of those work, return an
531 absolute path that is known to exist. If none of them work, just
532 return the original program name, 'exe'.
533 """
534
535 for p in self.__paths:
536 fn = os.path.join(os.path.abspath(p), exe)
537 if os.path.isfile(fn):
538 return fn
539
540 # didn't find it; try existing path
541 for p in string.split(os.environ['Path'],';'):
542 fn = os.path.join(os.path.abspath(p),exe)
543 if os.path.isfile(fn):
544 return fn
545
546 return exe
Tim Peters182b5ac2004-07-18 06:16:08 +0000547
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000548 def get_msvc_paths(self, path, platform='x86'):
549 """Get a list of devstudio directories (include, lib or path).
550
551 Return a list of strings. The list will be empty if unable to
552 access the registry or appropriate registry keys not found.
553 """
554
555 if not _can_read_reg:
556 return []
557
558 path = path + " dirs"
Jeremy Hyltone9a92aa2003-07-17 14:41:07 +0000559 if self.__version >= 7:
560 key = (r"%s\%0.1f\VC\VC_OBJECTS_PLATFORM_INFO\Win32\Directories"
561 % (self.__root, self.__version))
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000562 else:
563 key = (r"%s\6.0\Build System\Components\Platforms"
Jeremy Hylton93724db2003-05-09 16:55:28 +0000564 r"\Win32 (%s)\Directories" % (self.__root, platform))
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000565
566 for base in HKEYS:
567 d = read_values(base, key)
568 if d:
Jeremy Hyltone9a92aa2003-07-17 14:41:07 +0000569 if self.__version >= 7:
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000570 return string.split(self.__macros.sub(d[path]), ";")
571 else:
572 return string.split(d[path], ";")
Thomas Hellerb3105912003-11-28 19:42:56 +0000573 # MSVC 6 seems to create the registry entries we need only when
574 # the GUI is run.
575 if self.__version == 6:
576 for base in HKEYS:
577 if read_values(base, r"%s\6.0" % self.__root) is not None:
578 self.warn("It seems you have Visual Studio 6 installed, "
579 "but the expected registry settings are not present.\n"
580 "You must at least run the Visual Studio GUI once "
581 "so that these entries are created.")
582 break
Jeremy Hylton9ddf6c32003-05-09 16:06:42 +0000583 return []
584
585 def set_path_env_var(self, name):
586 """Set environment variable 'name' to an MSVC path type value.
587
588 This is equivalent to a SET command prior to execution of spawned
589 commands.
590 """
591
592 if name == "lib":
593 p = self.get_msvc_paths("library")
594 else:
595 p = self.get_msvc_paths(name)
596 if p:
597 os.environ[name] = string.join(p, ';')