blob: c7a69c359455cd0fd3414019dbf0778738a62746 [file] [log] [blame]
Greg Warddbd12761999-08-29 18:15:07 +00001"""distutils.ccompiler
2
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 Warddbd12761999-08-29 18:15:07 +000015from distutils.errors import *
Greg Ward3d50b901999-09-08 02:36:01 +000016from distutils.ccompiler import \
17 CCompiler, gen_preprocess_options, gen_lib_options
Greg Warddbd12761999-08-29 18:15:07 +000018
Greg Ward62e33932000-02-10 02:52:42 +000019
Greg Ward7642f5c2000-03-31 16:47:40 +000020_can_read_reg = 0
21try:
22 import winreg
Greg Ward7642f5c2000-03-31 16:47:40 +000023 _can_read_reg = 1
Greg Ward19ce1662000-03-31 19:04:25 +000024 hkey_mod = winreg
25
26 RegOpenKeyEx = winreg.OpenKeyEx
27 RegEnumKey = winreg.EnumKey
28 RegEnumValue = winreg.EnumValue
29 RegError = winreg.error
30
Greg Ward7642f5c2000-03-31 16:47:40 +000031except ImportError:
32 try:
33 import win32api
34 import win32con
Greg Ward7642f5c2000-03-31 16:47:40 +000035 _can_read_reg = 1
Greg Ward1027e3f2000-03-31 16:53:42 +000036 hkey_mod = win32con
Greg Ward19ce1662000-03-31 19:04:25 +000037
38 RegOpenKeyEx = win32api.RegOpenKeyEx
39 RegEnumKey = win32api.RegEnumKey
40 RegEnumValue = win32api.RegEnumValue
41 RegError = win32api.error
42
Greg Ward7642f5c2000-03-31 16:47:40 +000043 except ImportError:
44 pass
Greg Ward1027e3f2000-03-31 16:53:42 +000045
46if _can_read_reg:
47 HKEY_CLASSES_ROOT = hkey_mod.HKEY_CLASSES_ROOT
48 HKEY_LOCAL_MACHINE = hkey_mod.HKEY_LOCAL_MACHINE
49 HKEY_CURRENT_USER = hkey_mod.HKEY_CURRENT_USER
50 HKEY_USERS = hkey_mod.HKEY_USERS
Greg Ward1027e3f2000-03-31 16:53:42 +000051
Greg Ward7642f5c2000-03-31 16:47:40 +000052
53
Greg Ward62e33932000-02-10 02:52:42 +000054def get_devstudio_versions ():
Greg Ward62e33932000-02-10 02:52:42 +000055 """Get list of devstudio versions from the Windows registry. Return a
Greg Ward69988092000-02-11 02:47:15 +000056 list of strings containing version numbers; the list will be
Greg Ward62e33932000-02-10 02:52:42 +000057 empty if we were unable to access the registry (eg. couldn't import
58 a registry-access module) or the appropriate registry keys weren't
Greg Ward69988092000-02-11 02:47:15 +000059 found."""
60
Greg Ward7642f5c2000-03-31 16:47:40 +000061 if not _can_read_reg:
Greg Ward62e33932000-02-10 02:52:42 +000062 return []
Greg Ward1b9c6f72000-02-08 02:39:44 +000063
64 K = 'Software\\Microsoft\\Devstudio'
65 L = []
Greg Ward1027e3f2000-03-31 16:53:42 +000066 for base in (HKEY_CLASSES_ROOT,
67 HKEY_LOCAL_MACHINE,
68 HKEY_CURRENT_USER,
69 HKEY_USERS):
Greg Ward1b9c6f72000-02-08 02:39:44 +000070 try:
Greg Ward1027e3f2000-03-31 16:53:42 +000071 k = RegOpenKeyEx(base,K)
Greg Ward1b9c6f72000-02-08 02:39:44 +000072 i = 0
73 while 1:
74 try:
Greg Ward1027e3f2000-03-31 16:53:42 +000075 p = RegEnumKey(k,i)
Greg Ward1b9c6f72000-02-08 02:39:44 +000076 if p[0] in '123456789' and p not in L:
77 L.append(p)
Greg Ward1027e3f2000-03-31 16:53:42 +000078 except RegError:
Greg Ward1b9c6f72000-02-08 02:39:44 +000079 break
80 i = i + 1
Greg Ward1027e3f2000-03-31 16:53:42 +000081 except RegError:
Greg Ward1b9c6f72000-02-08 02:39:44 +000082 pass
83 L.sort()
84 L.reverse()
85 return L
86
Greg Ward62e33932000-02-10 02:52:42 +000087# get_devstudio_versions ()
88
89
90def get_msvc_paths (path, version='6.0', platform='x86'):
Greg Ward69988092000-02-11 02:47:15 +000091 """Get a list of devstudio directories (include, lib or path). Return
92 a list of strings; will be empty list if unable to access the
93 registry or appropriate registry keys not found."""
94
Greg Ward7642f5c2000-03-31 16:47:40 +000095 if not _can_read_reg:
Greg Ward69988092000-02-11 02:47:15 +000096 return []
Greg Ward1b9c6f72000-02-08 02:39:44 +000097
98 L = []
Greg Ward62e33932000-02-10 02:52:42 +000099 if path=='lib':
100 path= 'Library'
Greg Ward1b9c6f72000-02-08 02:39:44 +0000101 path = string.upper(path + ' Dirs')
Greg Ward62e33932000-02-10 02:52:42 +0000102 K = ('Software\\Microsoft\\Devstudio\\%s\\' +
103 'Build System\\Components\\Platforms\\Win32 (%s)\\Directories') % \
104 (version,platform)
Greg Ward1027e3f2000-03-31 16:53:42 +0000105 for base in (HKEY_CLASSES_ROOT,
106 HKEY_LOCAL_MACHINE,
107 HKEY_CURRENT_USER,
108 HKEY_USERS):
Greg Ward1b9c6f72000-02-08 02:39:44 +0000109 try:
Greg Ward1027e3f2000-03-31 16:53:42 +0000110 k = RegOpenKeyEx(base,K)
Greg Ward1b9c6f72000-02-08 02:39:44 +0000111 i = 0
112 while 1:
113 try:
Greg Ward1027e3f2000-03-31 16:53:42 +0000114 (p,v,t) = RegEnumValue(k,i)
Greg Ward62e33932000-02-10 02:52:42 +0000115 if string.upper(p) == path:
Greg Ward1b9c6f72000-02-08 02:39:44 +0000116 V = string.split(v,';')
117 for v in V:
Greg Ward62e33932000-02-10 02:52:42 +0000118 if v == '' or v in L: continue
Greg Ward1b9c6f72000-02-08 02:39:44 +0000119 L.append(v)
120 break
121 i = i + 1
Greg Ward1027e3f2000-03-31 16:53:42 +0000122 except RegError:
Greg Ward1b9c6f72000-02-08 02:39:44 +0000123 break
Greg Ward1027e3f2000-03-31 16:53:42 +0000124 except RegError:
Greg Ward1b9c6f72000-02-08 02:39:44 +0000125 pass
126 return L
127
Greg Ward62e33932000-02-10 02:52:42 +0000128# get_msvc_paths()
129
130
Greg Ward69988092000-02-11 02:47:15 +0000131def find_exe (exe, version_number):
132 """Try to find an MSVC executable program 'exe' (from version
133 'version_number' of MSVC) in several places: first, one of the MSVC
134 program search paths from the registry; next, the directories in the
135 PATH environment variable. If any of those work, return an absolute
136 path that is known to exist. If none of them work, just return the
137 original program name, 'exe'."""
Greg Ward1b9c6f72000-02-08 02:39:44 +0000138
Greg Ward69988092000-02-11 02:47:15 +0000139 for p in get_msvc_paths ('path', version_number):
140 fn = os.path.join (os.path.abspath(p), exe)
141 if os.path.isfile(fn):
142 return fn
143
144 # didn't find it; try existing path
145 for p in string.split (os.environ['Path'],';'):
146 fn = os.path.join(os.path.abspath(p),exe)
147 if os.path.isfile(fn):
148 return fn
149
150 return exe # last desperate hope
Greg Ward1b9c6f72000-02-08 02:39:44 +0000151
Greg Ward62e33932000-02-10 02:52:42 +0000152
Greg Ward5de8cee2000-02-11 02:52:39 +0000153def set_path_env_var (name, version_number):
154 """Set environment variable 'name' to an MSVC path type value obtained
155 from 'get_msvc_paths()'. This is equivalent to a SET command prior
156 to execution of spawned commands."""
Greg Ward69988092000-02-11 02:47:15 +0000157
Greg Ward5de8cee2000-02-11 02:52:39 +0000158 p = get_msvc_paths (name, version_number)
Greg Ward62e33932000-02-10 02:52:42 +0000159 if p:
Greg Ward5de8cee2000-02-11 02:52:39 +0000160 os.environ[name] = string.join (p,';')
Greg Ward62e33932000-02-10 02:52:42 +0000161
Greg Warddbd12761999-08-29 18:15:07 +0000162
Greg Ward3d50b901999-09-08 02:36:01 +0000163class MSVCCompiler (CCompiler) :
164 """Concrete class that implements an interface to Microsoft Visual C++,
165 as defined by the CCompiler abstract class."""
Greg Warddbd12761999-08-29 18:15:07 +0000166
Greg Warddf178f91999-09-29 12:29:10 +0000167 compiler_type = 'msvc'
168
Greg Ward32c4a8a2000-03-06 03:40:29 +0000169 # Private class data (need to distinguish C from C++ source for compiler)
170 _c_extensions = ['.c']
171 _cpp_extensions = ['.cc','.cpp']
172
173 # Needed for the filename generation methods provided by the
174 # base class, CCompiler.
175 src_extensions = _c_extensions + _cpp_extensions
176 obj_extension = '.obj'
177 static_lib_extension = '.lib'
178 shared_lib_extension = '.dll'
179 static_lib_format = shared_lib_format = '%s%s'
180 exe_extension = '.exe'
181
182
Greg Warddbd12761999-08-29 18:15:07 +0000183 def __init__ (self,
184 verbose=0,
Greg Wardc74138d1999-10-03 20:47:52 +0000185 dry_run=0,
186 force=0):
Greg Warddbd12761999-08-29 18:15:07 +0000187
Greg Wardc74138d1999-10-03 20:47:52 +0000188 CCompiler.__init__ (self, verbose, dry_run, force)
Greg Ward5de8cee2000-02-11 02:52:39 +0000189 versions = get_devstudio_versions ()
Greg Ward69988092000-02-11 02:47:15 +0000190
Greg Ward5de8cee2000-02-11 02:52:39 +0000191 if versions:
192 version = versions[0] # highest version
Greg Ward69988092000-02-11 02:47:15 +0000193
Greg Ward41b4dd62000-03-29 04:13:00 +0000194 self.cc = find_exe("cl.exe", version)
195 self.link = find_exe("link.exe", version)
196 self.lib = find_exe("lib.exe", version)
Greg Ward5de8cee2000-02-11 02:52:39 +0000197 set_path_env_var ('lib', version)
198 set_path_env_var ('include', version)
199 path=get_msvc_paths('path', version)
Greg Ward69988092000-02-11 02:47:15 +0000200 try:
201 for p in string.split(os.environ['path'],';'):
202 path.append(p)
203 except KeyError:
204 pass
205 os.environ['path'] = string.join(path,';')
206 else:
207 # devstudio not found in the registry
208 self.cc = "cl.exe"
209 self.link = "link.exe"
Greg Ward09fc5422000-03-10 01:49:26 +0000210 self.lib = "lib.exe"
Greg Ward69988092000-02-11 02:47:15 +0000211
Greg Warddbd12761999-08-29 18:15:07 +0000212 self.preprocess_options = None
Greg Ward69988092000-02-11 02:47:15 +0000213 self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3' ]
Greg Ward32c4a8a2000-03-06 03:40:29 +0000214 self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/Z7', '/D_DEBUG']
Greg Warddbd12761999-08-29 18:15:07 +0000215
Greg Ward1b9c6f72000-02-08 02:39:44 +0000216 self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO']
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000217 self.ldflags_shared_debug = [
218 '/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG'
219 ]
Greg Warddbd12761999-08-29 18:15:07 +0000220 self.ldflags_static = [ '/nologo']
221
Greg Warddbd12761999-08-29 18:15:07 +0000222
223 # -- Worker methods ------------------------------------------------
Greg Warddbd12761999-08-29 18:15:07 +0000224
Greg Warddbd12761999-08-29 18:15:07 +0000225 def compile (self,
226 sources,
Greg Warddf178f91999-09-29 12:29:10 +0000227 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000228 macros=None,
Greg Ward0bdd90a1999-12-12 17:19:58 +0000229 include_dirs=None,
Greg Ward386b8442000-02-09 02:18:39 +0000230 debug=0,
Greg Warddf178f91999-09-29 12:29:10 +0000231 extra_preargs=None,
232 extra_postargs=None):
Greg Warddbd12761999-08-29 18:15:07 +0000233
Greg Ward32c4a8a2000-03-06 03:40:29 +0000234 (output_dir, macros, include_dirs) = \
235 self._fix_compile_args (output_dir, macros, include_dirs)
236 (objects, skip_sources) = self._prep_compile (sources, output_dir)
Greg Warddbd12761999-08-29 18:15:07 +0000237
Greg Ward32c4a8a2000-03-06 03:40:29 +0000238 if extra_postargs is None:
239 extra_postargs = []
Greg Warddbd12761999-08-29 18:15:07 +0000240
Greg Ward32c4a8a2000-03-06 03:40:29 +0000241 pp_opts = gen_preprocess_options (macros, include_dirs)
242 compile_opts = extra_preargs or []
243 compile_opts.append ('/c')
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000244 if debug:
Greg Ward32c4a8a2000-03-06 03:40:29 +0000245 compile_opts.extend (self.compile_options_debug)
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000246 else:
Greg Ward32c4a8a2000-03-06 03:40:29 +0000247 compile_opts.extend (self.compile_options)
Greg Warddbd12761999-08-29 18:15:07 +0000248
Greg Ward32c4a8a2000-03-06 03:40:29 +0000249 for i in range (len (sources)):
250 src = sources[i] ; obj = objects[i]
251 ext = (os.path.splitext (src))[1]
Greg Warddbd12761999-08-29 18:15:07 +0000252
Greg Ward32c4a8a2000-03-06 03:40:29 +0000253 if skip_sources[src]:
254 self.announce ("skipping %s (%s up-to-date)" % (src, obj))
255 else:
256 if ext in self._c_extensions:
257 input_opt = "/Tc" + src
258 elif ext in self._cpp_extensions:
259 input_opt = "/Tp" + src
Greg Warddbd12761999-08-29 18:15:07 +0000260
Greg Ward32c4a8a2000-03-06 03:40:29 +0000261 output_opt = "/Fo" + obj
Greg Warddbd12761999-08-29 18:15:07 +0000262
Greg Ward32c4a8a2000-03-06 03:40:29 +0000263 self.mkpath (os.path.dirname (obj))
264 self.spawn ([self.cc] + compile_opts + pp_opts +
265 [input_opt, output_opt] +
266 extra_postargs)
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000267
Greg Ward32c4a8a2000-03-06 03:40:29 +0000268 return objects
Greg Warddbd12761999-08-29 18:15:07 +0000269
Greg Ward32c4a8a2000-03-06 03:40:29 +0000270 # compile ()
Greg Ward3d50b901999-09-08 02:36:01 +0000271
272
Greg Ward09fc5422000-03-10 01:49:26 +0000273 def create_static_lib (self,
274 objects,
275 output_libname,
276 output_dir=None,
277 debug=0,
278 extra_preargs=None,
279 extra_postargs=None):
Greg Warddbd12761999-08-29 18:15:07 +0000280
Greg Ward2f557a22000-03-26 21:42:28 +0000281 (objects, output_dir) = self._fix_object_args (objects, output_dir)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000282 output_filename = \
283 self.library_filename (output_libname, output_dir=output_dir)
Greg Warddbd12761999-08-29 18:15:07 +0000284
Greg Ward32c4a8a2000-03-06 03:40:29 +0000285 if self._need_link (objects, output_filename):
Greg Ward09fc5422000-03-10 01:49:26 +0000286 lib_args = objects + ['/OUT:' + output_filename]
Greg Ward32c4a8a2000-03-06 03:40:29 +0000287 if debug:
288 pass # XXX what goes here?
289 if extra_preargs:
Greg Ward09fc5422000-03-10 01:49:26 +0000290 lib_args[:0] = extra_preargs
Greg Ward32c4a8a2000-03-06 03:40:29 +0000291 if extra_postargs:
Greg Ward09fc5422000-03-10 01:49:26 +0000292 lib_args.extend (extra_postargs)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000293 self.spawn ([self.link] + ld_args)
294 else:
295 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Warddbd12761999-08-29 18:15:07 +0000296
Greg Ward09fc5422000-03-10 01:49:26 +0000297 # create_static_lib ()
Greg Warddbd12761999-08-29 18:15:07 +0000298
299
300 def link_shared_lib (self,
301 objects,
302 output_libname,
Greg Warddf178f91999-09-29 12:29:10 +0000303 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000304 libraries=None,
305 library_dirs=None,
Greg Ward2f557a22000-03-26 21:42:28 +0000306 runtime_library_dirs=None,
Greg Ward386b8442000-02-09 02:18:39 +0000307 debug=0,
Greg Warddf178f91999-09-29 12:29:10 +0000308 extra_preargs=None,
309 extra_postargs=None):
310
Greg Warddf178f91999-09-29 12:29:10 +0000311 self.link_shared_object (objects,
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000312 self.shared_library_name(output_libname),
313 output_dir=output_dir,
314 libraries=libraries,
315 library_dirs=library_dirs,
316 debug=debug,
317 extra_preargs=extra_preargs,
318 extra_postargs=extra_postargs)
319
Greg Warddbd12761999-08-29 18:15:07 +0000320
321 def link_shared_object (self,
322 objects,
323 output_filename,
Greg Warddf178f91999-09-29 12:29:10 +0000324 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000325 libraries=None,
326 library_dirs=None,
Greg Ward2f557a22000-03-26 21:42:28 +0000327 runtime_library_dirs=None,
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000328 debug=0,
Greg Warddf178f91999-09-29 12:29:10 +0000329 extra_preargs=None,
330 extra_postargs=None):
Greg Ward32c4a8a2000-03-06 03:40:29 +0000331
Greg Ward2f557a22000-03-26 21:42:28 +0000332 (objects, output_dir) = self._fix_object_args (objects, output_dir)
333 (libraries, library_dirs, runtime_library_dirs) = \
334 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
335
336 if self.runtime_library_dirs:
337 self.warn ("I don't know what to do with 'runtime_library_dirs': "
338 + str (runtime_library_dirs))
Greg Warddbd12761999-08-29 18:15:07 +0000339
Greg Wardd03f88a2000-03-18 15:19:51 +0000340 lib_opts = gen_lib_options (self,
Greg Ward2f557a22000-03-26 21:42:28 +0000341 library_dirs, runtime_library_dirs,
Greg Wardd03f88a2000-03-18 15:19:51 +0000342 libraries)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000343 if type (output_dir) not in (StringType, NoneType):
344 raise TypeError, "'output_dir' must be a string or None"
345 if output_dir is not None:
346 output_filename = os.path.join (output_dir, output_filename)
Greg Warddbd12761999-08-29 18:15:07 +0000347
Greg Ward32c4a8a2000-03-06 03:40:29 +0000348 if self._need_link (objects, output_filename):
349
350 if debug:
351 ldflags = self.ldflags_shared_debug
Greg Ward32c4a8a2000-03-06 03:40:29 +0000352 else:
353 ldflags = self.ldflags_shared
354
355 ld_args = ldflags + lib_opts + \
356 objects + ['/OUT:' + output_filename]
357
358 if extra_preargs:
359 ld_args[:0] = extra_preargs
360 if extra_postargs:
361 ld_args.extend (extra_postargs)
362
Greg Ward02a1a2b2000-04-15 22:15:07 +0000363 print "link_shared_object():"
364 print " output_filename =", output_filename
365 print " mkpath'ing:", os.path.dirname (output_filename)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000366 self.mkpath (os.path.dirname (output_filename))
367 self.spawn ([self.link] + ld_args)
368
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000369 else:
Greg Ward32c4a8a2000-03-06 03:40:29 +0000370 self.announce ("skipping %s (up-to-date)" % output_filename)
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000371
Greg Ward32c4a8a2000-03-06 03:40:29 +0000372 # link_shared_object ()
373
Greg Ward4ba9b2e2000-02-10 02:15:52 +0000374
Greg Ward32c4a8a2000-03-06 03:40:29 +0000375 # -- Miscellaneous methods -----------------------------------------
376 # These are all used by the 'gen_lib_options() function, in
377 # ccompiler.py.
Greg Wardc74138d1999-10-03 20:47:52 +0000378
379 def library_dir_option (self, dir):
380 return "/LIBPATH:" + dir
381
Greg Wardd03f88a2000-03-18 15:19:51 +0000382 def runtime_library_dir_option (self, dir):
383 raise DistutilsPlatformError, \
384 "don't know how to set runtime library search path for MSVC++"
385
Greg Wardc74138d1999-10-03 20:47:52 +0000386 def library_option (self, lib):
387 return self.library_filename (lib)
388
389
390 def find_library_file (self, dirs, lib):
391
392 for dir in dirs:
393 libfile = os.path.join (dir, self.library_filename (lib))
394 if os.path.exists (libfile):
395 return libfile
396
397 else:
398 # Oops, didn't find it in *any* of 'dirs'
399 return None
400
401 # find_library_file ()
402
Greg Warddbd12761999-08-29 18:15:07 +0000403# class MSVCCompiler