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 |
Greg Ward | 8dbf681 | 2000-01-17 20:40:48 +0000 | [diff] [blame^] | 40 | self.compile_options = [ '/nologo', '/Ox', '/MD', '/GD' ] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 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 | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 66 | include_dirs=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 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 = [] |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 72 | if include_dirs is None: |
| 73 | include_dirs = [] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 74 | |
| 75 | objectFiles = [] |
| 76 | |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 77 | base_pp_opts = \ |
| 78 | gen_preprocess_options (self.macros + macros, |
| 79 | self.include_dirs + include_dirs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 80 | |
| 81 | base_pp_opts.append('/c') |
| 82 | |
| 83 | for srcFile in sources: |
| 84 | base,ext = os.path.splitext(srcFile) |
| 85 | objFile = base + ".obj" |
| 86 | |
| 87 | if ext in self._c_extensions: |
| 88 | fileOpt = "/Tc" |
| 89 | elif ext in self._cpp_extensions: |
| 90 | fileOpt = "/Tp" |
| 91 | |
| 92 | inputOpt = fileOpt + srcFile |
| 93 | outputOpt = "/Fo" + objFile |
| 94 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 95 | cc_args = self.compile_options + \ |
| 96 | base_pp_opts + \ |
| 97 | [outputOpt, inputOpt] |
| 98 | if extra_preargs: |
| 99 | cc_args[:0] = extra_preargs |
| 100 | if extra_postargs: |
| 101 | cc_args.extend (extra_postargs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 102 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 103 | self.spawn ([self.cc] + cc_args) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 104 | objectFiles.append( objFile ) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 105 | return objectFiles |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 106 | |
| 107 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 108 | # XXX this is kind of useless without 'link_binary()' or |
| 109 | # 'link_executable()' or something -- or maybe 'link_static_lib()' |
| 110 | # should not exist at all, and we just have 'link_binary()'? |
| 111 | def link_static_lib (self, |
| 112 | objects, |
| 113 | output_libname, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 114 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 115 | libraries=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 116 | library_dirs=None, |
| 117 | extra_preargs=None, |
| 118 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 119 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 120 | if libraries is None: |
| 121 | libraries = [] |
| 122 | if library_dirs is None: |
| 123 | library_dirs = [] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 124 | |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 125 | lib_opts = gen_lib_options (self.libraries + libraries, |
| 126 | self.library_dirs + library_dirs, |
| 127 | "%s.lib", "/LIBPATH:%s") |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 128 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 129 | ld_args = self.ldflags_static + lib_opts + \ |
| 130 | objects + ['/OUT:' + output_filename] |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 131 | if extra_preargs: |
| 132 | ld_args[:0] = extra_preargs |
| 133 | if extra_postargs: |
| 134 | ld_args.extend (extra_postargs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 135 | |
| 136 | self.spawn ( [ self.link ] + ld_args ) |
| 137 | |
| 138 | |
| 139 | def link_shared_lib (self, |
| 140 | objects, |
| 141 | output_libname, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 142 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 143 | libraries=None, |
| 144 | library_dirs=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 145 | extra_preargs=None, |
| 146 | extra_postargs=None): |
| 147 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 148 | # XXX should we sanity check the library name? (eg. no |
| 149 | # slashes) |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 150 | self.link_shared_object (objects, |
| 151 | self.shared_library_name(output_libname)) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 152 | |
| 153 | def link_shared_object (self, |
| 154 | objects, |
| 155 | output_filename, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 156 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 157 | libraries=None, |
| 158 | library_dirs=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 159 | extra_preargs=None, |
| 160 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 161 | """Link a bunch of stuff together to create a shared object |
| 162 | file. Much like 'link_shared_lib()', except the output |
| 163 | filename is explicitly supplied as 'output_filename'.""" |
| 164 | if libraries is None: |
| 165 | libraries = [] |
| 166 | if library_dirs is None: |
| 167 | library_dirs = [] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 168 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 169 | lib_opts = gen_lib_options (self, |
| 170 | self.library_dirs + library_dirs, |
| 171 | self.libraries + libraries) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 172 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 173 | ld_args = self.ldflags_shared + lib_opts + \ |
| 174 | objects + ['/OUT:' + output_filename] |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 175 | if extra_preargs: |
| 176 | ld_args[:0] = extra_preargs |
| 177 | if extra_postargs: |
| 178 | ld_args.extend (extra_postargs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 179 | |
| 180 | self.spawn ( [ self.link ] + ld_args ) |
| 181 | |
| 182 | |
| 183 | # -- Filename mangling methods ------------------------------------- |
| 184 | |
| 185 | def _change_extensions( self, filenames, newExtension ): |
| 186 | object_filenames = [] |
| 187 | |
| 188 | for srcFile in filenames: |
| 189 | base,ext = os.path.splitext( srcFile ) |
| 190 | # XXX should we strip off any existing path? |
| 191 | object_filenames.append( base + newExtension ) |
| 192 | |
| 193 | return object_filenames |
| 194 | |
| 195 | def object_filenames (self, source_filenames): |
| 196 | """Return the list of object filenames corresponding to each |
| 197 | specified source filename.""" |
| 198 | return self._change_extensions( source_filenames, self._obj_ext ) |
| 199 | |
| 200 | def shared_object_filename (self, source_filename): |
| 201 | """Return the shared object filename corresponding to a |
| 202 | specified source filename.""" |
| 203 | return self._change_extensions( source_filenames, self._shared_lib_ext ) |
| 204 | |
| 205 | def library_filename (self, libname): |
| 206 | """Return the static library filename corresponding to the |
| 207 | specified library name.""" |
Greg Ward | c8a95c8 | 2000-01-17 18:00:04 +0000 | [diff] [blame] | 208 | return "%s%s" %( libname, self._static_lib_ext ) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 209 | |
| 210 | def shared_library_filename (self, libname): |
| 211 | """Return the shared library filename corresponding to the |
| 212 | specified library name.""" |
Greg Ward | c8a95c8 | 2000-01-17 18:00:04 +0000 | [diff] [blame] | 213 | return "%s%s" %( libname, self._shared_lib_ext ) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 214 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 215 | |
| 216 | def library_dir_option (self, dir): |
| 217 | return "/LIBPATH:" + dir |
| 218 | |
| 219 | def library_option (self, lib): |
| 220 | return self.library_filename (lib) |
| 221 | |
| 222 | |
| 223 | def find_library_file (self, dirs, lib): |
| 224 | |
| 225 | for dir in dirs: |
| 226 | libfile = os.path.join (dir, self.library_filename (lib)) |
| 227 | if os.path.exists (libfile): |
| 228 | return libfile |
| 229 | |
| 230 | else: |
| 231 | # Oops, didn't find it in *any* of 'dirs' |
| 232 | return None |
| 233 | |
| 234 | # find_library_file () |
| 235 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 236 | # class MSVCCompiler |