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 |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 13 | import string |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 14 | from distutils.errors import * |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 15 | from distutils.ccompiler import \ |
| 16 | CCompiler, gen_preprocess_options, gen_lib_options |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 17 | |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 18 | def _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 | |
| 46 | def _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 | |
| 79 | def _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 | |
| 94 | def _find_SET(n): |
| 95 | for v in _devstudio_versions(): |
| 96 | p=_msvc_get_paths(n,v) |
| 97 | if p!=[]: return p |
| 98 | return [] |
| 99 | |
| 100 | def _do_SET(n): |
| 101 | p=_find_SET(n) |
| 102 | if p!=[]: os.environ[n]=string.join(p,';') |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 103 | |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 104 | class MSVCCompiler (CCompiler) : |
| 105 | """Concrete class that implements an interface to Microsoft Visual C++, |
| 106 | as defined by the CCompiler abstract class.""" |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 107 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 108 | compiler_type = 'msvc' |
| 109 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 110 | def __init__ (self, |
| 111 | verbose=0, |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 112 | dry_run=0, |
| 113 | force=0): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 114 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 115 | CCompiler.__init__ (self, verbose, dry_run, force) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 116 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 117 | # XXX This is a nasty dependency to add on something otherwise |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 118 | # pretty clean. move it to build_ext under an nt specific part. |
| 119 | # shared libraries need to link against python15.lib |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 120 | self.add_library ( "python" + sys.version[0] + sys.version[2] ) |
| 121 | self.add_library_dir( os.path.join( sys.exec_prefix, 'libs' ) ) |
| 122 | |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 123 | 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 Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 134 | self.preprocess_options = None |
Greg Ward | 01f5215 | 2000-01-17 21:57:17 +0000 | [diff] [blame] | 135 | self.compile_options = [ '/nologo', '/Ox', '/MD' ] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 136 | |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 137 | self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO'] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 138 | self.ldflags_static = [ '/nologo'] |
| 139 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 140 | |
| 141 | # -- Worker methods ------------------------------------------------ |
| 142 | # (must be implemented by subclasses) |
| 143 | |
| 144 | _c_extensions = [ '.c' ] |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 145 | _cpp_extensions = [ '.cc', '.cpp' ] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 146 | |
| 147 | _obj_ext = '.obj' |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 148 | _exe_ext = '.exe' |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 149 | _shared_lib_ext = '.dll' |
| 150 | _static_lib_ext = '.lib' |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 151 | |
| 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 Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 156 | |
| 157 | def compile (self, |
| 158 | sources, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 159 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 160 | macros=None, |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 161 | include_dirs=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 162 | extra_preargs=None, |
| 163 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 164 | |
| 165 | if macros is None: |
| 166 | macros = [] |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 167 | if include_dirs is None: |
| 168 | include_dirs = [] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 169 | |
| 170 | objectFiles = [] |
| 171 | |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 172 | base_pp_opts = \ |
| 173 | gen_preprocess_options (self.macros + macros, |
| 174 | self.include_dirs + include_dirs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 175 | |
| 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 Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 190 | 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 Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 197 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 198 | self.spawn ([self.cc] + cc_args) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 199 | objectFiles.append( objFile ) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 200 | return objectFiles |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 201 | |
| 202 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 203 | # 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 Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 209 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 210 | libraries=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 211 | library_dirs=None, |
| 212 | extra_preargs=None, |
| 213 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 214 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 215 | if libraries is None: |
| 216 | libraries = [] |
| 217 | if library_dirs is None: |
| 218 | library_dirs = [] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 219 | |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 220 | lib_opts = gen_lib_options (self.libraries + libraries, |
| 221 | self.library_dirs + library_dirs, |
| 222 | "%s.lib", "/LIBPATH:%s") |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 223 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 224 | ld_args = self.ldflags_static + lib_opts + \ |
| 225 | objects + ['/OUT:' + output_filename] |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 226 | if extra_preargs: |
| 227 | ld_args[:0] = extra_preargs |
| 228 | if extra_postargs: |
| 229 | ld_args.extend (extra_postargs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 230 | |
| 231 | self.spawn ( [ self.link ] + ld_args ) |
| 232 | |
| 233 | |
| 234 | def link_shared_lib (self, |
| 235 | objects, |
| 236 | output_libname, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 237 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 238 | libraries=None, |
| 239 | library_dirs=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 240 | extra_preargs=None, |
| 241 | extra_postargs=None): |
| 242 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 243 | # XXX should we sanity check the library name? (eg. no |
| 244 | # slashes) |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 245 | self.link_shared_object (objects, |
| 246 | self.shared_library_name(output_libname)) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 247 | |
| 248 | def link_shared_object (self, |
| 249 | objects, |
| 250 | output_filename, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 251 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 252 | libraries=None, |
| 253 | library_dirs=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 254 | extra_preargs=None, |
| 255 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 256 | """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 Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 263 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 264 | lib_opts = gen_lib_options (self, |
| 265 | self.library_dirs + library_dirs, |
| 266 | self.libraries + libraries) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 267 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 268 | ld_args = self.ldflags_shared + lib_opts + \ |
| 269 | objects + ['/OUT:' + output_filename] |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 270 | if extra_preargs: |
| 271 | ld_args[:0] = extra_preargs |
| 272 | if extra_postargs: |
| 273 | ld_args.extend (extra_postargs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 274 | |
| 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 Ward | c8a95c8 | 2000-01-17 18:00:04 +0000 | [diff] [blame] | 303 | return "%s%s" %( libname, self._static_lib_ext ) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 304 | |
| 305 | def shared_library_filename (self, libname): |
| 306 | """Return the shared library filename corresponding to the |
| 307 | specified library name.""" |
Greg Ward | c8a95c8 | 2000-01-17 18:00:04 +0000 | [diff] [blame] | 308 | return "%s%s" %( libname, self._shared_lib_ext ) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 309 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 310 | |
| 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 Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 331 | # class MSVCCompiler |