blob: 5ac60b2e471457b3f6ae0f6f08202fabbb216ec0 [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
13from distutils.errors import *
Greg Ward3d50b901999-09-08 02:36:01 +000014from distutils.ccompiler import \
15 CCompiler, gen_preprocess_options, gen_lib_options
Greg Warddbd12761999-08-29 18:15:07 +000016
17
Greg Ward3d50b901999-09-08 02:36:01 +000018class MSVCCompiler (CCompiler) :
19 """Concrete class that implements an interface to Microsoft Visual C++,
20 as defined by the CCompiler abstract class."""
Greg Warddbd12761999-08-29 18:15:07 +000021
Greg Warddf178f91999-09-29 12:29:10 +000022 compiler_type = 'msvc'
23
Greg Warddbd12761999-08-29 18:15:07 +000024 def __init__ (self,
25 verbose=0,
Greg Wardc74138d1999-10-03 20:47:52 +000026 dry_run=0,
27 force=0):
Greg Warddbd12761999-08-29 18:15:07 +000028
Greg Wardc74138d1999-10-03 20:47:52 +000029 CCompiler.__init__ (self, verbose, dry_run, force)
Greg Warddbd12761999-08-29 18:15:07 +000030
Greg Warddbd12761999-08-29 18:15:07 +000031 # XXX This is a nasty dependency to add on something otherwise
Greg Ward3d50b901999-09-08 02:36:01 +000032 # pretty clean. move it to build_ext under an nt specific part.
33 # shared libraries need to link against python15.lib
Greg Warddbd12761999-08-29 18:15:07 +000034 self.add_library ( "python" + sys.version[0] + sys.version[2] )
35 self.add_library_dir( os.path.join( sys.exec_prefix, 'libs' ) )
36
37 self.cc = "cl.exe"
38 self.link = "link.exe"
39 self.preprocess_options = None
40 self.compile_options = [ '/nologo' ]
41
42 self.ldflags_shared = ['/DLL', '/nologo']
43 self.ldflags_static = [ '/nologo']
44
Greg Warddbd12761999-08-29 18:15:07 +000045
46 # -- Worker methods ------------------------------------------------
47 # (must be implemented by subclasses)
48
49 _c_extensions = [ '.c' ]
Greg Ward3d50b901999-09-08 02:36:01 +000050 _cpp_extensions = [ '.cc', '.cpp' ]
Greg Warddbd12761999-08-29 18:15:07 +000051
52 _obj_ext = '.obj'
Greg Ward3d50b901999-09-08 02:36:01 +000053 _exe_ext = '.exe'
Greg Warddbd12761999-08-29 18:15:07 +000054 _shared_lib_ext = '.dll'
55 _static_lib_ext = '.lib'
Greg Warddf178f91999-09-29 12:29:10 +000056
57 # XXX the 'output_dir' parameter is ignored by the methods in this
58 # class! I just put it in to be consistent with CCompiler and
59 # UnixCCompiler, but someone who actually knows Visual C++ will
60 # have to make it work...
Greg Warddbd12761999-08-29 18:15:07 +000061
62 def compile (self,
63 sources,
Greg Warddf178f91999-09-29 12:29:10 +000064 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +000065 macros=None,
Greg Warddf178f91999-09-29 12:29:10 +000066 includes=None,
67 extra_preargs=None,
68 extra_postargs=None):
Greg Warddbd12761999-08-29 18:15:07 +000069
70 if macros is None:
71 macros = []
72 if includes is None:
73 includes = []
74
75 objectFiles = []
76
Greg Ward3d50b901999-09-08 02:36:01 +000077 base_pp_opts = gen_preprocess_options (self.macros + macros,
78 self.include_dirs + includes)
Greg Warddbd12761999-08-29 18:15:07 +000079
80 base_pp_opts.append('/c')
81
82 for srcFile in sources:
83 base,ext = os.path.splitext(srcFile)
84 objFile = base + ".obj"
85
86 if ext in self._c_extensions:
87 fileOpt = "/Tc"
88 elif ext in self._cpp_extensions:
89 fileOpt = "/Tp"
90
91 inputOpt = fileOpt + srcFile
92 outputOpt = "/Fo" + objFile
93
Greg Warddf178f91999-09-29 12:29:10 +000094 cc_args = self.compile_options + \
95 base_pp_opts + \
96 [outputOpt, inputOpt]
97 if extra_preargs:
98 cc_args[:0] = extra_preargs
99 if extra_postargs:
100 cc_args.extend (extra_postargs)
Greg Warddbd12761999-08-29 18:15:07 +0000101
Greg Warddf178f91999-09-29 12:29:10 +0000102 self.spawn ([self.cc] + cc_args)
Greg Warddbd12761999-08-29 18:15:07 +0000103 objectFiles.append( objFile )
Greg Warddbd12761999-08-29 18:15:07 +0000104 return objectFiles
Greg Ward3d50b901999-09-08 02:36:01 +0000105
106
Greg Warddbd12761999-08-29 18:15:07 +0000107 # XXX this is kind of useless without 'link_binary()' or
108 # 'link_executable()' or something -- or maybe 'link_static_lib()'
109 # should not exist at all, and we just have 'link_binary()'?
110 def link_static_lib (self,
111 objects,
112 output_libname,
Greg Warddf178f91999-09-29 12:29:10 +0000113 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000114 libraries=None,
Greg Warddf178f91999-09-29 12:29:10 +0000115 library_dirs=None,
116 extra_preargs=None,
117 extra_postargs=None):
Greg Warddbd12761999-08-29 18:15:07 +0000118
Greg Warddbd12761999-08-29 18:15:07 +0000119 if libraries is None:
120 libraries = []
121 if library_dirs is None:
122 library_dirs = []
Greg Warddbd12761999-08-29 18:15:07 +0000123
Greg Ward3d50b901999-09-08 02:36:01 +0000124 lib_opts = gen_lib_options (self.libraries + libraries,
125 self.library_dirs + library_dirs,
126 "%s.lib", "/LIBPATH:%s")
Greg Warddbd12761999-08-29 18:15:07 +0000127
Greg Warddbd12761999-08-29 18:15:07 +0000128 ld_args = self.ldflags_static + lib_opts + \
129 objects + ['/OUT:' + output_filename]
Greg Warddf178f91999-09-29 12:29:10 +0000130 if extra_preargs:
131 ld_args[:0] = extra_preargs
132 if extra_postargs:
133 ld_args.extend (extra_postargs)
Greg Warddbd12761999-08-29 18:15:07 +0000134
135 self.spawn ( [ self.link ] + ld_args )
136
137
138 def link_shared_lib (self,
139 objects,
140 output_libname,
Greg Warddf178f91999-09-29 12:29:10 +0000141 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000142 libraries=None,
143 library_dirs=None,
Greg Warddf178f91999-09-29 12:29:10 +0000144 extra_preargs=None,
145 extra_postargs=None):
146
Greg Warddbd12761999-08-29 18:15:07 +0000147 # XXX should we sanity check the library name? (eg. no
148 # slashes)
Greg Warddf178f91999-09-29 12:29:10 +0000149 self.link_shared_object (objects,
150 self.shared_library_name(output_libname))
Greg Warddbd12761999-08-29 18:15:07 +0000151
152 def link_shared_object (self,
153 objects,
154 output_filename,
Greg Warddf178f91999-09-29 12:29:10 +0000155 output_dir=None,
Greg Warddbd12761999-08-29 18:15:07 +0000156 libraries=None,
157 library_dirs=None,
Greg Warddf178f91999-09-29 12:29:10 +0000158 extra_preargs=None,
159 extra_postargs=None):
Greg Warddbd12761999-08-29 18:15:07 +0000160 """Link a bunch of stuff together to create a shared object
161 file. Much like 'link_shared_lib()', except the output
162 filename is explicitly supplied as 'output_filename'."""
163 if libraries is None:
164 libraries = []
165 if library_dirs is None:
166 library_dirs = []
Greg Warddbd12761999-08-29 18:15:07 +0000167
Greg Wardc74138d1999-10-03 20:47:52 +0000168 lib_opts = gen_lib_options (self,
169 self.library_dirs + library_dirs,
170 self.libraries + libraries)
Greg Warddbd12761999-08-29 18:15:07 +0000171
Greg Warddbd12761999-08-29 18:15:07 +0000172 ld_args = self.ldflags_shared + lib_opts + \
173 objects + ['/OUT:' + output_filename]
Greg Warddf178f91999-09-29 12:29:10 +0000174 if extra_preargs:
175 ld_args[:0] = extra_preargs
176 if extra_postargs:
177 ld_args.extend (extra_postargs)
Greg Warddbd12761999-08-29 18:15:07 +0000178
179 self.spawn ( [ self.link ] + ld_args )
180
181
182 # -- Filename mangling methods -------------------------------------
183
184 def _change_extensions( self, filenames, newExtension ):
185 object_filenames = []
186
187 for srcFile in filenames:
188 base,ext = os.path.splitext( srcFile )
189 # XXX should we strip off any existing path?
190 object_filenames.append( base + newExtension )
191
192 return object_filenames
193
194 def object_filenames (self, source_filenames):
195 """Return the list of object filenames corresponding to each
196 specified source filename."""
197 return self._change_extensions( source_filenames, self._obj_ext )
198
199 def shared_object_filename (self, source_filename):
200 """Return the shared object filename corresponding to a
201 specified source filename."""
202 return self._change_extensions( source_filenames, self._shared_lib_ext )
203
Greg Wardc74138d1999-10-03 20:47:52 +0000204 # XXX ummm... these aren't right, are they? I thought library 'foo' on
205 # DOS/Windows was to be found in "foo.lib", not "libfoo.lib"!
206
Greg Warddbd12761999-08-29 18:15:07 +0000207 def library_filename (self, libname):
208 """Return the static library filename corresponding to the
209 specified library name."""
210 return "lib%s%s" %( libname, self._static_lib_ext )
211
212 def shared_library_filename (self, libname):
213 """Return the shared library filename corresponding to the
214 specified library name."""
215 return "lib%s%s" %( libname, self._shared_lib_ext )
216
Greg Wardc74138d1999-10-03 20:47:52 +0000217
218 def library_dir_option (self, dir):
219 return "/LIBPATH:" + dir
220
221 def library_option (self, lib):
222 return self.library_filename (lib)
223
224
225 def find_library_file (self, dirs, lib):
226
227 for dir in dirs:
228 libfile = os.path.join (dir, self.library_filename (lib))
229 if os.path.exists (libfile):
230 return libfile
231
232 else:
233 # Oops, didn't find it in *any* of 'dirs'
234 return None
235
236 # find_library_file ()
237
Greg Warddbd12761999-08-29 18:15:07 +0000238# class MSVCCompiler