blob: 10cd6081d317eb5280d7ff3c51e4f1d53cf76189 [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
8#
9__rcsid__ = "$Id$"
10
11import os
12import sys
Greg Ward1b9c6f72000-02-08 02:39:44 +000013import string
Greg Warddbd12761999-08-29 18:15:07 +000014from distutils.errors import *
Greg Ward3d50b901999-09-08 02:36:01 +000015from distutils.ccompiler import \
16 CCompiler, gen_preprocess_options, gen_lib_options
Greg Warddbd12761999-08-29 18:15:07 +000017
Greg Ward1b9c6f72000-02-08 02:39:44 +000018def _devstudio_versions():
19 "Get a list of devstudio versions"
20 try:
21 import win32api
22 import win32con
23 except ImportError:
24 return None
25
26 K = 'Software\\Microsoft\\Devstudio'
27 L = []
28 for base in (win32con.HKEY_CLASSES_ROOT,win32con.HKEY_LOCAL_MACHINE,win32con.HKEY_CURRENT_USER,win32con.HKEY_USERS):
29 try:
30 k = win32api.RegOpenKeyEx(base,K)
31 i = 0
32 while 1:
33 try:
34 p = win32api.RegEnumKey(k,i)
35 if p[0] in '123456789' and p not in L:
36 L.append(p)
37 except win32api.error:
38 break
39 i = i + 1
40 except win32api.error:
41 pass
42 L.sort()
43 L.reverse()
44 return L
45
46def _msvc_get_paths(path, vNum='6.0', platform='x86'):
47 "Get a devstudio path (include, lib or path)"
48 try:
49 import win32api
50 import win32con
51 except ImportError:
52 return None
53
54 L = []
55 if path=='lib': path= 'Library'
56 path = string.upper(path + ' Dirs')
57 K = 'Software\\Microsoft\\Devstudio\\%s\\Build System\\Components\\Platforms\\Win32 (%s)\\Directories' \
58 % (vNum,platform)
59 for base in (win32con.HKEY_CLASSES_ROOT,win32con.HKEY_LOCAL_MACHINE,win32con.HKEY_CURRENT_USER,win32con.HKEY_USERS):
60 try:
61 k = win32api.RegOpenKeyEx(base,K)
62 i = 0
63 while 1:
64 try:
65 (p,v,t) = win32api.RegEnumValue(k,i)
66 if string.upper(p)==path:
67 V = string.split(v,';')
68 for v in V:
69 if v=='' or v in L: continue
70 L.append(v)
71 break
72 i = i + 1
73 except win32api.error:
74 break
75 except win32api.error:
76 pass
77 return L
78
79def _find_exe(exe):
80 for v in _devstudio_versions():
81 for p in _msvc_get_paths('path',v):
82 fn=os.path.join(os.path.abspath(p),exe)
83 if os.path.isfile(fn): return fn
84
85 #didn't find it; try existing path
86 try:
87 for p in string.split(os.environ['Path'],';'):
88 fn=os.path.join(os.path.abspath(p),exe)
89 if os.path.isfile(fn): return fn
90 except:
91 pass
92 return exe #last desperate hope
93
94def _find_SET(n):
95 for v in _devstudio_versions():
96 p=_msvc_get_paths(n,v)
97 if p!=[]: return p
98 return []
99
100def _do_SET(n):
101 p=_find_SET(n)
102 if p!=[]: os.environ[n]=string.join(p,';')
Greg Warddbd12761999-08-29 18:15:07 +0000103
Greg Ward3d50b901999-09-08 02:36:01 +0000104class MSVCCompiler (CCompiler) :
105 """Concrete class that implements an interface to Microsoft Visual C++,
106 as defined by the CCompiler abstract class."""
Greg Warddbd12761999-08-29 18:15:07 +0000107
Greg Warddf178f91999-09-29 12:29:10 +0000108 compiler_type = 'msvc'
109
Greg Warddbd12761999-08-29 18:15:07 +0000110 def __init__ (self,
111 verbose=0,
Greg Wardc74138d1999-10-03 20:47:52 +0000112 dry_run=0,
113 force=0):
Greg Warddbd12761999-08-29 18:15:07 +0000114
Greg Wardc74138d1999-10-03 20:47:52 +0000115 CCompiler.__init__ (self, verbose, dry_run, force)
Greg Warddbd12761999-08-29 18:15:07 +0000116
Greg Warddbd12761999-08-29 18:15:07 +0000117 # XXX This is a nasty dependency to add on something otherwise
Greg Ward3d50b901999-09-08 02:36:01 +0000118 # pretty clean. move it to build_ext under an nt specific part.
119 # shared libraries need to link against python15.lib
Greg Warddbd12761999-08-29 18:15:07 +0000120 self.add_library ( "python" + sys.version[0] + sys.version[2] )
121 self.add_library_dir( os.path.join( sys.exec_prefix, 'libs' ) )
122
Greg Ward1b9c6f72000-02-08 02:39:44 +0000123 self.cc = _find_exe("cl.exe")
124 self.link = _find_exe("link.exe")
125 _do_SET('lib')
126 _do_SET('include')
127 path=_find_SET('path')
128 try:
129 for p in string.split(os.environ['path'],';'):
130 path.append(p)
131 except KeyError:
132 pass
133 os.environ['path'] = string.join(path,';')
Greg Warddbd12761999-08-29 18:15:07 +0000134 self.preprocess_options = None
Greg Ward01f52152000-01-17 21:57:17 +0000135 self.compile_options = [ '/nologo', '/Ox', '/MD' ]
Greg Warddbd12761999-08-29 18:15:07 +0000136
Greg Ward1b9c6f72000-02-08 02:39:44 +0000137 self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO']
Greg Warddbd12761999-08-29 18:15:07 +0000138 self.ldflags_static = [ '/nologo']
139
Greg Warddbd12761999-08-29 18:15:07 +0000140
141 # -- Worker methods ------------------------------------------------
142 # (must be implemented by subclasses)
143
144 _c_extensions = [ '.c' ]
Greg Ward3d50b901999-09-08 02:36:01 +0000145 _cpp_extensions = [ '.cc', '.cpp' ]
Greg Warddbd12761999-08-29 18:15:07 +0000146
147 _obj_ext = '.obj'
Greg Ward3d50b901999-09-08 02:36:01 +0000148 _exe_ext = '.exe'
Greg Warddbd12761999-08-29 18:15:07 +0000149 _shared_lib_ext = '.dll'
150 _static_lib_ext = '.lib'
Greg Warddf178f91999-09-29 12:29:10 +0000151
152 # XXX the 'output_dir' parameter is ignored by the methods in this
153 # class! I just put it in to be consistent with CCompiler and
154 # UnixCCompiler, but someone who actually knows Visual C++ will
155 # have to make it work...
Greg Warddbd12761999-08-29 18:15:07 +0000156
157 def compile (self,
158 sources,
Greg Warddf178f91999-09-29 12:29:10 +0000159 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000160 macros=None,
Greg Ward0bdd90a1999-12-12 17:19:58 +0000161 include_dirs=None,
Greg Warddf178f91999-09-29 12:29:10 +0000162 extra_preargs=None,
163 extra_postargs=None):
Greg Warddbd12761999-08-29 18:15:07 +0000164
165 if macros is None:
166 macros = []
Greg Ward0bdd90a1999-12-12 17:19:58 +0000167 if include_dirs is None:
168 include_dirs = []
Greg Warddbd12761999-08-29 18:15:07 +0000169
170 objectFiles = []
171
Greg Ward0bdd90a1999-12-12 17:19:58 +0000172 base_pp_opts = \
173 gen_preprocess_options (self.macros + macros,
174 self.include_dirs + include_dirs)
Greg Warddbd12761999-08-29 18:15:07 +0000175
176 base_pp_opts.append('/c')
177
178 for srcFile in sources:
179 base,ext = os.path.splitext(srcFile)
180 objFile = base + ".obj"
181
182 if ext in self._c_extensions:
183 fileOpt = "/Tc"
184 elif ext in self._cpp_extensions:
185 fileOpt = "/Tp"
186
187 inputOpt = fileOpt + srcFile
188 outputOpt = "/Fo" + objFile
189
Greg Warddf178f91999-09-29 12:29:10 +0000190 cc_args = self.compile_options + \
191 base_pp_opts + \
192 [outputOpt, inputOpt]
193 if extra_preargs:
194 cc_args[:0] = extra_preargs
195 if extra_postargs:
196 cc_args.extend (extra_postargs)
Greg Warddbd12761999-08-29 18:15:07 +0000197
Greg Warddf178f91999-09-29 12:29:10 +0000198 self.spawn ([self.cc] + cc_args)
Greg Warddbd12761999-08-29 18:15:07 +0000199 objectFiles.append( objFile )
Greg Warddbd12761999-08-29 18:15:07 +0000200 return objectFiles
Greg Ward3d50b901999-09-08 02:36:01 +0000201
202
Greg Warddbd12761999-08-29 18:15:07 +0000203 # XXX this is kind of useless without 'link_binary()' or
204 # 'link_executable()' or something -- or maybe 'link_static_lib()'
205 # should not exist at all, and we just have 'link_binary()'?
206 def link_static_lib (self,
207 objects,
208 output_libname,
Greg Warddf178f91999-09-29 12:29:10 +0000209 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000210 libraries=None,
Greg Warddf178f91999-09-29 12:29:10 +0000211 library_dirs=None,
212 extra_preargs=None,
213 extra_postargs=None):
Greg Warddbd12761999-08-29 18:15:07 +0000214
Greg Warddbd12761999-08-29 18:15:07 +0000215 if libraries is None:
216 libraries = []
217 if library_dirs is None:
218 library_dirs = []
Greg Warddbd12761999-08-29 18:15:07 +0000219
Greg Ward3d50b901999-09-08 02:36:01 +0000220 lib_opts = gen_lib_options (self.libraries + libraries,
221 self.library_dirs + library_dirs,
222 "%s.lib", "/LIBPATH:%s")
Greg Warddbd12761999-08-29 18:15:07 +0000223
Greg Warddbd12761999-08-29 18:15:07 +0000224 ld_args = self.ldflags_static + lib_opts + \
225 objects + ['/OUT:' + output_filename]
Greg Warddf178f91999-09-29 12:29:10 +0000226 if extra_preargs:
227 ld_args[:0] = extra_preargs
228 if extra_postargs:
229 ld_args.extend (extra_postargs)
Greg Warddbd12761999-08-29 18:15:07 +0000230
231 self.spawn ( [ self.link ] + ld_args )
232
233
234 def link_shared_lib (self,
235 objects,
236 output_libname,
Greg Warddf178f91999-09-29 12:29:10 +0000237 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000238 libraries=None,
239 library_dirs=None,
Greg Warddf178f91999-09-29 12:29:10 +0000240 extra_preargs=None,
241 extra_postargs=None):
242
Greg Warddbd12761999-08-29 18:15:07 +0000243 # XXX should we sanity check the library name? (eg. no
244 # slashes)
Greg Warddf178f91999-09-29 12:29:10 +0000245 self.link_shared_object (objects,
246 self.shared_library_name(output_libname))
Greg Warddbd12761999-08-29 18:15:07 +0000247
248 def link_shared_object (self,
249 objects,
250 output_filename,
Greg Warddf178f91999-09-29 12:29:10 +0000251 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000252 libraries=None,
253 library_dirs=None,
Greg Warddf178f91999-09-29 12:29:10 +0000254 extra_preargs=None,
255 extra_postargs=None):
Greg Warddbd12761999-08-29 18:15:07 +0000256 """Link a bunch of stuff together to create a shared object
257 file. Much like 'link_shared_lib()', except the output
258 filename is explicitly supplied as 'output_filename'."""
259 if libraries is None:
260 libraries = []
261 if library_dirs is None:
262 library_dirs = []
Greg Warddbd12761999-08-29 18:15:07 +0000263
Greg Wardc74138d1999-10-03 20:47:52 +0000264 lib_opts = gen_lib_options (self,
265 self.library_dirs + library_dirs,
266 self.libraries + libraries)
Greg Warddbd12761999-08-29 18:15:07 +0000267
Greg Warddbd12761999-08-29 18:15:07 +0000268 ld_args = self.ldflags_shared + lib_opts + \
269 objects + ['/OUT:' + output_filename]
Greg Warddf178f91999-09-29 12:29:10 +0000270 if extra_preargs:
271 ld_args[:0] = extra_preargs
272 if extra_postargs:
273 ld_args.extend (extra_postargs)
Greg Warddbd12761999-08-29 18:15:07 +0000274
275 self.spawn ( [ self.link ] + ld_args )
276
277
278 # -- Filename mangling methods -------------------------------------
279
280 def _change_extensions( self, filenames, newExtension ):
281 object_filenames = []
282
283 for srcFile in filenames:
284 base,ext = os.path.splitext( srcFile )
285 # XXX should we strip off any existing path?
286 object_filenames.append( base + newExtension )
287
288 return object_filenames
289
290 def object_filenames (self, source_filenames):
291 """Return the list of object filenames corresponding to each
292 specified source filename."""
293 return self._change_extensions( source_filenames, self._obj_ext )
294
295 def shared_object_filename (self, source_filename):
296 """Return the shared object filename corresponding to a
297 specified source filename."""
298 return self._change_extensions( source_filenames, self._shared_lib_ext )
299
300 def library_filename (self, libname):
301 """Return the static library filename corresponding to the
302 specified library name."""
Greg Wardc8a95c82000-01-17 18:00:04 +0000303 return "%s%s" %( libname, self._static_lib_ext )
Greg Warddbd12761999-08-29 18:15:07 +0000304
305 def shared_library_filename (self, libname):
306 """Return the shared library filename corresponding to the
307 specified library name."""
Greg Wardc8a95c82000-01-17 18:00:04 +0000308 return "%s%s" %( libname, self._shared_lib_ext )
Greg Warddbd12761999-08-29 18:15:07 +0000309
Greg Wardc74138d1999-10-03 20:47:52 +0000310
311 def library_dir_option (self, dir):
312 return "/LIBPATH:" + dir
313
314 def library_option (self, lib):
315 return self.library_filename (lib)
316
317
318 def find_library_file (self, dirs, lib):
319
320 for dir in dirs:
321 libfile = os.path.join (dir, self.library_filename (lib))
322 if os.path.exists (libfile):
323 return libfile
324
325 else:
326 # Oops, didn't find it in *any* of 'dirs'
327 return None
328
329 # find_library_file ()
330
Greg Warddbd12761999-08-29 18:15:07 +0000331# class MSVCCompiler