blob: e58e6c10c81f9ea50f984222ed5a232d702f7ff9 [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
Greg Warddf178f91999-09-29 12:29:10 +00004for the Microsoft Visual Studio."""
Greg Warddbd12761999-08-29 18:15:07 +00005
6
7# created 1999/08/19, 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
Greg Ward3ce77fd2000-03-02 01:49:45 +000011__revision__ = "$Id$"
Greg Warddbd12761999-08-29 18:15:07 +000012
Greg Ward32c4a8a2000-03-06 03:40:29 +000013import sys, os, string
14from types import *
Greg Ward3add77f2000-05-30 02:02:49 +000015from distutils.errors import \
16 DistutilsExecError, DistutilsPlatformError, \
Greg Wardd1517112000-05-30 01:56:44 +000017 CompileError, LibError, LinkError
Greg Ward3add77f2000-05-30 02:02:49 +000018from distutils.ccompiler import \
19 CCompiler, gen_preprocess_options, gen_lib_options
Greg Ward62e33932000-02-10 02:52:42 +000020
Greg Ward7642f5c2000-03-31 16:47:40 +000021_can_read_reg = 0
22try:
Greg Ward1b5ec762000-06-30 19:37:59 +000023 import _winreg
Greg Ward83c38702000-06-29 23:04:59 +000024
Greg Ward7642f5c2000-03-31 16:47:40 +000025 _can_read_reg = 1
Greg Wardcd079c42000-06-29 22:59:10 +000026 hkey_mod = _winreg
Greg Ward19ce1662000-03-31 19:04:25 +000027
Greg Wardcd079c42000-06-29 22:59:10 +000028 RegOpenKeyEx = _winreg.OpenKeyEx
29 RegEnumKey = _winreg.EnumKey
30 RegEnumValue = _winreg.EnumValue
31 RegError = _winreg.error
Greg Ward19ce1662000-03-31 19:04:25 +000032
Greg Ward7642f5c2000-03-31 16:47:40 +000033except ImportError:
34 try:
35 import win32api
36 import win32con
Greg Ward7642f5c2000-03-31 16:47:40 +000037 _can_read_reg = 1
Greg Ward1027e3f2000-03-31 16:53:42 +000038 hkey_mod = win32con
Greg Ward19ce1662000-03-31 19:04:25 +000039
40 RegOpenKeyEx = win32api.RegOpenKeyEx
41 RegEnumKey = win32api.RegEnumKey
42 RegEnumValue = win32api.RegEnumValue
43 RegError = win32api.error
44
Greg Ward7642f5c2000-03-31 16:47:40 +000045 except ImportError:
46 pass
Greg Ward1027e3f2000-03-31 16:53:42 +000047
48if _can_read_reg:
49 HKEY_CLASSES_ROOT = hkey_mod.HKEY_CLASSES_ROOT
50 HKEY_LOCAL_MACHINE = hkey_mod.HKEY_LOCAL_MACHINE
51 HKEY_CURRENT_USER = hkey_mod.HKEY_CURRENT_USER
52 HKEY_USERS = hkey_mod.HKEY_USERS
Greg Ward1027e3f2000-03-31 16:53:42 +000053
Greg Ward7642f5c2000-03-31 16:47:40 +000054
55
Greg Ward62e33932000-02-10 02:52:42 +000056def get_devstudio_versions ():
Greg Ward62e33932000-02-10 02:52:42 +000057 """Get list of devstudio versions from the Windows registry. Return a
Greg Ward69988092000-02-11 02:47:15 +000058 list of strings containing version numbers; the list will be
Greg Ward62e33932000-02-10 02:52:42 +000059 empty if we were unable to access the registry (eg. couldn't import
60 a registry-access module) or the appropriate registry keys weren't
Greg Ward69988092000-02-11 02:47:15 +000061 found."""
62
Greg Ward7642f5c2000-03-31 16:47:40 +000063 if not _can_read_reg:
Greg Ward62e33932000-02-10 02:52:42 +000064 return []
Greg Ward1b9c6f72000-02-08 02:39:44 +000065
66 K = 'Software\\Microsoft\\Devstudio'
67 L = []
Greg Ward1027e3f2000-03-31 16:53:42 +000068 for base in (HKEY_CLASSES_ROOT,
69 HKEY_LOCAL_MACHINE,
70 HKEY_CURRENT_USER,
71 HKEY_USERS):
Greg Ward1b9c6f72000-02-08 02:39:44 +000072 try:
Greg Ward1027e3f2000-03-31 16:53:42 +000073 k = RegOpenKeyEx(base,K)
Greg Ward1b9c6f72000-02-08 02:39:44 +000074 i = 0
75 while 1:
76 try:
Greg Ward1027e3f2000-03-31 16:53:42 +000077 p = RegEnumKey(k,i)
Greg Ward1b9c6f72000-02-08 02:39:44 +000078 if p[0] in '123456789' and p not in L:
79 L.append(p)
Greg Ward1027e3f2000-03-31 16:53:42 +000080 except RegError:
Greg Ward1b9c6f72000-02-08 02:39:44 +000081 break
82 i = i + 1
Greg Ward1027e3f2000-03-31 16:53:42 +000083 except RegError:
Greg Ward1b9c6f72000-02-08 02:39:44 +000084 pass
85 L.sort()
86 L.reverse()
87 return L
88
Greg Ward62e33932000-02-10 02:52:42 +000089# get_devstudio_versions ()
90
91
92def get_msvc_paths (path, version='6.0', platform='x86'):
Greg Ward69988092000-02-11 02:47:15 +000093 """Get a list of devstudio directories (include, lib or path). Return
94 a list of strings; will be empty list if unable to access the
95 registry or appropriate registry keys not found."""
96
Greg Ward7642f5c2000-03-31 16:47:40 +000097 if not _can_read_reg:
Greg Ward69988092000-02-11 02:47:15 +000098 return []
Greg Ward1b9c6f72000-02-08 02:39:44 +000099
100 L = []
Greg Ward62e33932000-02-10 02:52:42 +0000101 if path=='lib':
102 path= 'Library'
Greg Ward1b9c6f72000-02-08 02:39:44 +0000103 path = string.upper(path + ' Dirs')
Greg Ward62e33932000-02-10 02:52:42 +0000104 K = ('Software\\Microsoft\\Devstudio\\%s\\' +
105 'Build System\\Components\\Platforms\\Win32 (%s)\\Directories') % \
106 (version,platform)
Greg Ward1027e3f2000-03-31 16:53:42 +0000107 for base in (HKEY_CLASSES_ROOT,
108 HKEY_LOCAL_MACHINE,
109 HKEY_CURRENT_USER,
110 HKEY_USERS):
Greg Ward1b9c6f72000-02-08 02:39:44 +0000111 try:
Greg Ward1027e3f2000-03-31 16:53:42 +0000112 k = RegOpenKeyEx(base,K)
Greg Ward1b9c6f72000-02-08 02:39:44 +0000113 i = 0
114 while 1:
115 try:
Greg Ward1027e3f2000-03-31 16:53:42 +0000116 (p,v,t) = RegEnumValue(k,i)
Greg Ward62e33932000-02-10 02:52:42 +0000117 if string.upper(p) == path:
Greg Ward1b9c6f72000-02-08 02:39:44 +0000118 V = string.split(v,';')
119 for v in V:
Greg Ward62e33932000-02-10 02:52:42 +0000120 if v == '' or v in L: continue
Greg Ward1b9c6f72000-02-08 02:39:44 +0000121 L.append(v)
122 break
123 i = i + 1
Greg Ward1027e3f2000-03-31 16:53:42 +0000124 except RegError:
Greg Ward1b9c6f72000-02-08 02:39:44 +0000125 break
Greg Ward1027e3f2000-03-31 16:53:42 +0000126 except RegError:
Greg Ward1b9c6f72000-02-08 02:39:44 +0000127 pass
128 return L
129
Greg Ward62e33932000-02-10 02:52:42 +0000130# get_msvc_paths()
131
132
Greg Ward69988092000-02-11 02:47:15 +0000133def find_exe (exe, version_number):
134 """Try to find an MSVC executable program 'exe' (from version
135 'version_number' of MSVC) in several places: first, one of the MSVC
136 program search paths from the registry; next, the directories in the
137 PATH environment variable. If any of those work, return an absolute
138 path that is known to exist. If none of them work, just return the
139 original program name, 'exe'."""
Greg Ward1b9c6f72000-02-08 02:39:44 +0000140
Greg Ward69988092000-02-11 02:47:15 +0000141 for p in get_msvc_paths ('path', version_number):
142 fn = os.path.join (os.path.abspath(p), exe)
143 if os.path.isfile(fn):
144 return fn
145
146 # didn't find it; try existing path
147 for p in string.split (os.environ['Path'],';'):
148 fn = os.path.join(os.path.abspath(p),exe)
149 if os.path.isfile(fn):
150 return fn
151
152 return exe # last desperate hope
Greg Ward1b9c6f72000-02-08 02:39:44 +0000153
Greg Ward62e33932000-02-10 02:52:42 +0000154
Greg Ward5de8cee2000-02-11 02:52:39 +0000155def set_path_env_var (name, version_number):
156 """Set environment variable 'name' to an MSVC path type value obtained
157 from 'get_msvc_paths()'. This is equivalent to a SET command prior
158 to execution of spawned commands."""
Greg Ward69988092000-02-11 02:47:15 +0000159
Greg Ward5de8cee2000-02-11 02:52:39 +0000160 p = get_msvc_paths (name, version_number)
Greg Ward62e33932000-02-10 02:52:42 +0000161 if p:
Greg Ward5de8cee2000-02-11 02:52:39 +0000162 os.environ[name] = string.join (p,';')
Greg Ward62e33932000-02-10 02:52:42 +0000163
Greg Warddbd12761999-08-29 18:15:07 +0000164
Greg Ward3d50b901999-09-08 02:36:01 +0000165class MSVCCompiler (CCompiler) :
166 """Concrete class that implements an interface to Microsoft Visual C++,
167 as defined by the CCompiler abstract class."""
Greg Warddbd12761999-08-29 18:15:07 +0000168
Greg Warddf178f91999-09-29 12:29:10 +0000169 compiler_type = 'msvc'
170
Greg Ward992c8f92000-06-25 02:31:16 +0000171 # Just set this so CCompiler's constructor doesn't barf. We currently
172 # don't use the 'set_executables()' bureaucracy provided by CCompiler,
173 # as it really isn't necessary for this sort of single-compiler class.
174 # Would be nice to have a consistent interface with UnixCCompiler,
175 # though, so it's worth thinking about.
176 executables = {}
177
Greg Ward32c4a8a2000-03-06 03:40:29 +0000178 # Private class data (need to distinguish C from C++ source for compiler)
179 _c_extensions = ['.c']
180 _cpp_extensions = ['.cc','.cpp']
181
182 # Needed for the filename generation methods provided by the
183 # base class, CCompiler.
184 src_extensions = _c_extensions + _cpp_extensions
185 obj_extension = '.obj'
186 static_lib_extension = '.lib'
187 shared_lib_extension = '.dll'
188 static_lib_format = shared_lib_format = '%s%s'
189 exe_extension = '.exe'
190
191
Greg Warddbd12761999-08-29 18:15:07 +0000192 def __init__ (self,
193 verbose=0,
Greg Wardc74138d1999-10-03 20:47:52 +0000194 dry_run=0,
195 force=0):
Greg Warddbd12761999-08-29 18:15:07 +0000196
Greg Wardc74138d1999-10-03 20:47:52 +0000197 CCompiler.__init__ (self, verbose, dry_run, force)
Greg Ward5de8cee2000-02-11 02:52:39 +0000198 versions = get_devstudio_versions ()
Greg Ward69988092000-02-11 02:47:15 +0000199
Greg Ward5de8cee2000-02-11 02:52:39 +0000200 if versions:
201 version = versions[0] # highest version
Greg Ward69988092000-02-11 02:47:15 +0000202
Greg Ward41b4dd62000-03-29 04:13:00 +0000203 self.cc = find_exe("cl.exe", version)
204 self.link = find_exe("link.exe", version)
205 self.lib = find_exe("lib.exe", version)
Greg Ward5de8cee2000-02-11 02:52:39 +0000206 set_path_env_var ('lib', version)
207 set_path_env_var ('include', version)
208 path=get_msvc_paths('path', version)
Greg Ward69988092000-02-11 02:47:15 +0000209 try:
210 for p in string.split(os.environ['path'],';'):
211 path.append(p)
212 except KeyError:
213 pass
214 os.environ['path'] = string.join(path,';')
215 else:
216 # devstudio not found in the registry
217 self.cc = "cl.exe"
218 self.link = "link.exe"
Greg Ward09fc5422000-03-10 01:49:26 +0000219 self.lib = "lib.exe"
Greg Ward69988092000-02-11 02:47:15 +0000220
Greg Warddbd12761999-08-29 18:15:07 +0000221 self.preprocess_options = None
Greg Ward69988092000-02-11 02:47:15 +0000222 self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3' ]
Greg Ward32c4a8a2000-03-06 03:40:29 +0000223 self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/Z7', '/D_DEBUG']
Greg Warddbd12761999-08-29 18:15:07 +0000224
Greg Ward1b9c6f72000-02-08 02:39:44 +0000225 self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO']
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000226 self.ldflags_shared_debug = [
227 '/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG'
228 ]
Greg Warddbd12761999-08-29 18:15:07 +0000229 self.ldflags_static = [ '/nologo']
230
Greg Warddbd12761999-08-29 18:15:07 +0000231
232 # -- Worker methods ------------------------------------------------
Greg Warddbd12761999-08-29 18:15:07 +0000233
Greg Warddbd12761999-08-29 18:15:07 +0000234 def compile (self,
235 sources,
Greg Warddf178f91999-09-29 12:29:10 +0000236 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000237 macros=None,
Greg Ward0bdd90a1999-12-12 17:19:58 +0000238 include_dirs=None,
Greg Ward386b8442000-02-09 02:18:39 +0000239 debug=0,
Greg Warddf178f91999-09-29 12:29:10 +0000240 extra_preargs=None,
241 extra_postargs=None):
Greg Warddbd12761999-08-29 18:15:07 +0000242
Greg Ward32c4a8a2000-03-06 03:40:29 +0000243 (output_dir, macros, include_dirs) = \
244 self._fix_compile_args (output_dir, macros, include_dirs)
245 (objects, skip_sources) = self._prep_compile (sources, output_dir)
Greg Warddbd12761999-08-29 18:15:07 +0000246
Greg Ward32c4a8a2000-03-06 03:40:29 +0000247 if extra_postargs is None:
248 extra_postargs = []
Greg Warddbd12761999-08-29 18:15:07 +0000249
Greg Ward32c4a8a2000-03-06 03:40:29 +0000250 pp_opts = gen_preprocess_options (macros, include_dirs)
251 compile_opts = extra_preargs or []
252 compile_opts.append ('/c')
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000253 if debug:
Greg Ward32c4a8a2000-03-06 03:40:29 +0000254 compile_opts.extend (self.compile_options_debug)
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000255 else:
Greg Ward32c4a8a2000-03-06 03:40:29 +0000256 compile_opts.extend (self.compile_options)
Greg Warddbd12761999-08-29 18:15:07 +0000257
Greg Ward32c4a8a2000-03-06 03:40:29 +0000258 for i in range (len (sources)):
259 src = sources[i] ; obj = objects[i]
260 ext = (os.path.splitext (src))[1]
Greg Warddbd12761999-08-29 18:15:07 +0000261
Greg Ward32c4a8a2000-03-06 03:40:29 +0000262 if skip_sources[src]:
263 self.announce ("skipping %s (%s up-to-date)" % (src, obj))
264 else:
265 if ext in self._c_extensions:
266 input_opt = "/Tc" + src
267 elif ext in self._cpp_extensions:
268 input_opt = "/Tp" + src
Greg Warddbd12761999-08-29 18:15:07 +0000269
Greg Ward32c4a8a2000-03-06 03:40:29 +0000270 output_opt = "/Fo" + obj
Greg Warddbd12761999-08-29 18:15:07 +0000271
Greg Ward32c4a8a2000-03-06 03:40:29 +0000272 self.mkpath (os.path.dirname (obj))
Greg Wardd1517112000-05-30 01:56:44 +0000273 try:
274 self.spawn ([self.cc] + compile_opts + pp_opts +
275 [input_opt, output_opt] +
276 extra_postargs)
277 except DistutilsExecError, msg:
278 raise CompileError, msg
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000279
Greg Ward32c4a8a2000-03-06 03:40:29 +0000280 return objects
Greg Warddbd12761999-08-29 18:15:07 +0000281
Greg Ward32c4a8a2000-03-06 03:40:29 +0000282 # compile ()
Greg Ward3d50b901999-09-08 02:36:01 +0000283
284
Greg Ward09fc5422000-03-10 01:49:26 +0000285 def create_static_lib (self,
286 objects,
287 output_libname,
288 output_dir=None,
289 debug=0,
290 extra_preargs=None,
291 extra_postargs=None):
Greg Warddbd12761999-08-29 18:15:07 +0000292
Greg Ward2f557a22000-03-26 21:42:28 +0000293 (objects, output_dir) = self._fix_object_args (objects, output_dir)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000294 output_filename = \
295 self.library_filename (output_libname, output_dir=output_dir)
Greg Warddbd12761999-08-29 18:15:07 +0000296
Greg Ward32c4a8a2000-03-06 03:40:29 +0000297 if self._need_link (objects, output_filename):
Greg Ward09fc5422000-03-10 01:49:26 +0000298 lib_args = objects + ['/OUT:' + output_filename]
Greg Ward32c4a8a2000-03-06 03:40:29 +0000299 if debug:
300 pass # XXX what goes here?
301 if extra_preargs:
Greg Ward09fc5422000-03-10 01:49:26 +0000302 lib_args[:0] = extra_preargs
Greg Ward32c4a8a2000-03-06 03:40:29 +0000303 if extra_postargs:
Greg Ward09fc5422000-03-10 01:49:26 +0000304 lib_args.extend (extra_postargs)
Greg Wardd1517112000-05-30 01:56:44 +0000305 try:
Greg Ward992c8f92000-06-25 02:31:16 +0000306 self.spawn ([self.lib] + lib_args)
Greg Wardd1517112000-05-30 01:56:44 +0000307 except DistutilsExecError, msg:
308 raise LibError, msg
309
Greg Ward32c4a8a2000-03-06 03:40:29 +0000310 else:
311 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Warddbd12761999-08-29 18:15:07 +0000312
Greg Ward09fc5422000-03-10 01:49:26 +0000313 # create_static_lib ()
Greg Warddbd12761999-08-29 18:15:07 +0000314
315
316 def link_shared_lib (self,
317 objects,
318 output_libname,
Greg Warddf178f91999-09-29 12:29:10 +0000319 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000320 libraries=None,
321 library_dirs=None,
Greg Ward2f557a22000-03-26 21:42:28 +0000322 runtime_library_dirs=None,
Greg Ward5299b6a2000-05-20 13:23:21 +0000323 export_symbols=None,
Greg Ward386b8442000-02-09 02:18:39 +0000324 debug=0,
Greg Warddf178f91999-09-29 12:29:10 +0000325 extra_preargs=None,
Greg Wardbfc79d62000-06-28 01:29:09 +0000326 extra_postargs=None,
327 build_temp=None):
Greg Warddf178f91999-09-29 12:29:10 +0000328
Greg Warddf178f91999-09-29 12:29:10 +0000329 self.link_shared_object (objects,
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000330 self.shared_library_name(output_libname),
331 output_dir=output_dir,
332 libraries=libraries,
333 library_dirs=library_dirs,
Greg Ward5299b6a2000-05-20 13:23:21 +0000334 runtime_library_dirs=runtime_library_dirs,
335 export_symbols=export_symbols,
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000336 debug=debug,
337 extra_preargs=extra_preargs,
Greg Wardbfc79d62000-06-28 01:29:09 +0000338 extra_postargs=extra_postargs,
339 build_temp=build_temp)
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000340
Greg Warddbd12761999-08-29 18:15:07 +0000341
342 def link_shared_object (self,
343 objects,
344 output_filename,
Greg Warddf178f91999-09-29 12:29:10 +0000345 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000346 libraries=None,
347 library_dirs=None,
Greg Ward2f557a22000-03-26 21:42:28 +0000348 runtime_library_dirs=None,
Greg Ward5299b6a2000-05-20 13:23:21 +0000349 export_symbols=None,
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000350 debug=0,
Greg Warddf178f91999-09-29 12:29:10 +0000351 extra_preargs=None,
Greg Wardbfc79d62000-06-28 01:29:09 +0000352 extra_postargs=None,
353 build_temp=None):
Greg Ward32c4a8a2000-03-06 03:40:29 +0000354
Greg Ward2f557a22000-03-26 21:42:28 +0000355 (objects, output_dir) = self._fix_object_args (objects, output_dir)
356 (libraries, library_dirs, runtime_library_dirs) = \
357 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
358
Greg Wardf70c6032000-04-19 02:16:49 +0000359 if runtime_library_dirs:
Greg Ward2f557a22000-03-26 21:42:28 +0000360 self.warn ("I don't know what to do with 'runtime_library_dirs': "
361 + str (runtime_library_dirs))
Greg Warddbd12761999-08-29 18:15:07 +0000362
Greg Wardd03f88a2000-03-18 15:19:51 +0000363 lib_opts = gen_lib_options (self,
Greg Ward2f557a22000-03-26 21:42:28 +0000364 library_dirs, runtime_library_dirs,
Greg Wardd03f88a2000-03-18 15:19:51 +0000365 libraries)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000366 if output_dir is not None:
367 output_filename = os.path.join (output_dir, output_filename)
Greg Warddbd12761999-08-29 18:15:07 +0000368
Greg Ward32c4a8a2000-03-06 03:40:29 +0000369 if self._need_link (objects, output_filename):
370
371 if debug:
372 ldflags = self.ldflags_shared_debug
Greg Ward32c4a8a2000-03-06 03:40:29 +0000373 else:
374 ldflags = self.ldflags_shared
375
Greg Ward5299b6a2000-05-20 13:23:21 +0000376 export_opts = []
377 for sym in (export_symbols or []):
378 export_opts.append("/EXPORT:" + sym)
379
380 ld_args = (ldflags + lib_opts + export_opts +
381 objects + ['/OUT:' + output_filename])
Greg Ward32c4a8a2000-03-06 03:40:29 +0000382
383 if extra_preargs:
384 ld_args[:0] = extra_preargs
385 if extra_postargs:
386 ld_args.extend (extra_postargs)
387
Greg Ward02a1a2b2000-04-15 22:15:07 +0000388 print "link_shared_object():"
389 print " output_filename =", output_filename
390 print " mkpath'ing:", os.path.dirname (output_filename)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000391 self.mkpath (os.path.dirname (output_filename))
Greg Wardd1517112000-05-30 01:56:44 +0000392 try:
393 self.spawn ([self.link] + ld_args)
394 except DistutilsExecError, msg:
395 raise LinkError, msg
Greg Ward32c4a8a2000-03-06 03:40:29 +0000396
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000397 else:
Greg Ward32c4a8a2000-03-06 03:40:29 +0000398 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000399
Greg Ward32c4a8a2000-03-06 03:40:29 +0000400 # link_shared_object ()
Greg Wardf70c6032000-04-19 02:16:49 +0000401
402
403 def link_executable (self,
404 objects,
405 output_progname,
406 output_dir=None,
407 libraries=None,
408 library_dirs=None,
409 runtime_library_dirs=None,
410 debug=0,
411 extra_preargs=None,
412 extra_postargs=None):
413
414 (objects, output_dir) = self._fix_object_args (objects, output_dir)
415 (libraries, library_dirs, runtime_library_dirs) = \
416 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
417
418 if runtime_library_dirs:
419 self.warn ("I don't know what to do with 'runtime_library_dirs': "
420 + str (runtime_library_dirs))
421
422 lib_opts = gen_lib_options (self,
423 library_dirs, runtime_library_dirs,
424 libraries)
425 output_filename = output_progname + self.exe_extension
426 if output_dir is not None:
427 output_filename = os.path.join (output_dir, output_filename)
428
429 if self._need_link (objects, output_filename):
430
431 if debug:
432 ldflags = self.ldflags_shared_debug[1:]
433 else:
434 ldflags = self.ldflags_shared[1:]
435
436 ld_args = ldflags + lib_opts + \
437 objects + ['/OUT:' + output_filename]
438
439 if extra_preargs:
440 ld_args[:0] = extra_preargs
441 if extra_postargs:
442 ld_args.extend (extra_postargs)
443
444 self.mkpath (os.path.dirname (output_filename))
Greg Wardd1517112000-05-30 01:56:44 +0000445 try:
446 self.spawn ([self.link] + ld_args)
447 except DistutilsExecError, msg:
448 raise LinkError, msg
Greg Wardf70c6032000-04-19 02:16:49 +0000449 else:
450 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000451
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000452
Greg Ward32c4a8a2000-03-06 03:40:29 +0000453 # -- Miscellaneous methods -----------------------------------------
454 # These are all used by the 'gen_lib_options() function, in
455 # ccompiler.py.
Greg Wardc74138d1999-10-03 20:47:52 +0000456
457 def library_dir_option (self, dir):
458 return "/LIBPATH:" + dir
459
Greg Wardd03f88a2000-03-18 15:19:51 +0000460 def runtime_library_dir_option (self, dir):
461 raise DistutilsPlatformError, \
462 "don't know how to set runtime library search path for MSVC++"
463
Greg Wardc74138d1999-10-03 20:47:52 +0000464 def library_option (self, lib):
465 return self.library_filename (lib)
466
467
468 def find_library_file (self, dirs, lib):
469
470 for dir in dirs:
471 libfile = os.path.join (dir, self.library_filename (lib))
472 if os.path.exists (libfile):
473 return libfile
474
475 else:
476 # Oops, didn't find it in *any* of 'dirs'
477 return None
478
479 # find_library_file ()
480
Greg Warddbd12761999-08-29 18:15:07 +0000481# class MSVCCompiler