Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 1 | """distutils.ccompiler |
| 2 | |
| 3 | Contains MSVCCompiler, an implementation of the abstract CCompiler class |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 4 | for the Microsoft Visual Studio.""" |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 5 | |
| 6 | |
| 7 | # created 1999/08/19, Perry Stoll |
| 8 | # |
| 9 | __rcsid__ = "$Id$" |
| 10 | |
| 11 | import os |
| 12 | import sys |
| 13 | from distutils.errors import * |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 14 | from distutils.ccompiler import \ |
| 15 | CCompiler, gen_preprocess_options, gen_lib_options |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 16 | |
| 17 | |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 18 | class MSVCCompiler (CCompiler) : |
| 19 | """Concrete class that implements an interface to Microsoft Visual C++, |
| 20 | as defined by the CCompiler abstract class.""" |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 21 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 22 | compiler_type = 'msvc' |
| 23 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 24 | def __init__ (self, |
| 25 | verbose=0, |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 26 | dry_run=0, |
| 27 | force=0): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 28 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 29 | CCompiler.__init__ (self, verbose, dry_run, force) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 30 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 31 | # XXX This is a nasty dependency to add on something otherwise |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 32 | # pretty clean. move it to build_ext under an nt specific part. |
| 33 | # shared libraries need to link against python15.lib |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 34 | 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 Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 45 | |
| 46 | # -- Worker methods ------------------------------------------------ |
| 47 | # (must be implemented by subclasses) |
| 48 | |
| 49 | _c_extensions = [ '.c' ] |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 50 | _cpp_extensions = [ '.cc', '.cpp' ] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 51 | |
| 52 | _obj_ext = '.obj' |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 53 | _exe_ext = '.exe' |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 54 | _shared_lib_ext = '.dll' |
| 55 | _static_lib_ext = '.lib' |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 56 | |
| 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 Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 61 | |
| 62 | def compile (self, |
| 63 | sources, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 64 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 65 | macros=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 66 | includes=None, |
| 67 | extra_preargs=None, |
| 68 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 69 | |
| 70 | if macros is None: |
| 71 | macros = [] |
| 72 | if includes is None: |
| 73 | includes = [] |
| 74 | |
| 75 | objectFiles = [] |
| 76 | |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 77 | base_pp_opts = gen_preprocess_options (self.macros + macros, |
| 78 | self.include_dirs + includes) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 79 | |
| 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 Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 94 | 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 Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 101 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 102 | self.spawn ([self.cc] + cc_args) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 103 | objectFiles.append( objFile ) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 104 | return objectFiles |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 105 | |
| 106 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 107 | # 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 Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 113 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 114 | libraries=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 115 | library_dirs=None, |
| 116 | extra_preargs=None, |
| 117 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 118 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 119 | if libraries is None: |
| 120 | libraries = [] |
| 121 | if library_dirs is None: |
| 122 | library_dirs = [] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 123 | |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 124 | lib_opts = gen_lib_options (self.libraries + libraries, |
| 125 | self.library_dirs + library_dirs, |
| 126 | "%s.lib", "/LIBPATH:%s") |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 127 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 128 | ld_args = self.ldflags_static + lib_opts + \ |
| 129 | objects + ['/OUT:' + output_filename] |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 130 | if extra_preargs: |
| 131 | ld_args[:0] = extra_preargs |
| 132 | if extra_postargs: |
| 133 | ld_args.extend (extra_postargs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 134 | |
| 135 | self.spawn ( [ self.link ] + ld_args ) |
| 136 | |
| 137 | |
| 138 | def link_shared_lib (self, |
| 139 | objects, |
| 140 | output_libname, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 141 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 142 | libraries=None, |
| 143 | library_dirs=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 144 | extra_preargs=None, |
| 145 | extra_postargs=None): |
| 146 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 147 | # XXX should we sanity check the library name? (eg. no |
| 148 | # slashes) |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 149 | self.link_shared_object (objects, |
| 150 | self.shared_library_name(output_libname)) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 151 | |
| 152 | def link_shared_object (self, |
| 153 | objects, |
| 154 | output_filename, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 155 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 156 | libraries=None, |
| 157 | library_dirs=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 158 | extra_preargs=None, |
| 159 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 160 | """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 Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 167 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 168 | lib_opts = gen_lib_options (self, |
| 169 | self.library_dirs + library_dirs, |
| 170 | self.libraries + libraries) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 171 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 172 | ld_args = self.ldflags_shared + lib_opts + \ |
| 173 | objects + ['/OUT:' + output_filename] |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 174 | if extra_preargs: |
| 175 | ld_args[:0] = extra_preargs |
| 176 | if extra_postargs: |
| 177 | ld_args.extend (extra_postargs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 178 | |
| 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 Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 204 | # 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 Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 207 | 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 Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 217 | |
| 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 Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 238 | # class MSVCCompiler |