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 | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 18 | |
| 19 | def get_devstudio_versions (): |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 20 | """Get list of devstudio versions from the Windows registry. Return a |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame^] | 21 | list of strings containing version numbers; the list will be |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 22 | empty if we were unable to access the registry (eg. couldn't import |
| 23 | a registry-access module) or the appropriate registry keys weren't |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame^] | 24 | found.""" |
| 25 | |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 26 | try: |
| 27 | import win32api |
| 28 | import win32con |
| 29 | except ImportError: |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 30 | return [] |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 31 | |
| 32 | K = 'Software\\Microsoft\\Devstudio' |
| 33 | L = [] |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 34 | for base in (win32con.HKEY_CLASSES_ROOT, |
| 35 | win32con.HKEY_LOCAL_MACHINE, |
| 36 | win32con.HKEY_CURRENT_USER, |
| 37 | win32con.HKEY_USERS): |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 38 | try: |
| 39 | k = win32api.RegOpenKeyEx(base,K) |
| 40 | i = 0 |
| 41 | while 1: |
| 42 | try: |
| 43 | p = win32api.RegEnumKey(k,i) |
| 44 | if p[0] in '123456789' and p not in L: |
| 45 | L.append(p) |
| 46 | except win32api.error: |
| 47 | break |
| 48 | i = i + 1 |
| 49 | except win32api.error: |
| 50 | pass |
| 51 | L.sort() |
| 52 | L.reverse() |
| 53 | return L |
| 54 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 55 | # get_devstudio_versions () |
| 56 | |
| 57 | |
| 58 | def get_msvc_paths (path, version='6.0', platform='x86'): |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame^] | 59 | """Get a list of devstudio directories (include, lib or path). Return |
| 60 | a list of strings; will be empty list if unable to access the |
| 61 | registry or appropriate registry keys not found.""" |
| 62 | |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 63 | try: |
| 64 | import win32api |
| 65 | import win32con |
| 66 | except ImportError: |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame^] | 67 | return [] |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 68 | |
| 69 | L = [] |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 70 | if path=='lib': |
| 71 | path= 'Library' |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 72 | path = string.upper(path + ' Dirs') |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 73 | K = ('Software\\Microsoft\\Devstudio\\%s\\' + |
| 74 | 'Build System\\Components\\Platforms\\Win32 (%s)\\Directories') % \ |
| 75 | (version,platform) |
| 76 | for base in (win32con.HKEY_CLASSES_ROOT, |
| 77 | win32con.HKEY_LOCAL_MACHINE, |
| 78 | win32con.HKEY_CURRENT_USER, |
| 79 | win32con.HKEY_USERS): |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 80 | try: |
| 81 | k = win32api.RegOpenKeyEx(base,K) |
| 82 | i = 0 |
| 83 | while 1: |
| 84 | try: |
| 85 | (p,v,t) = win32api.RegEnumValue(k,i) |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 86 | if string.upper(p) == path: |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 87 | V = string.split(v,';') |
| 88 | for v in V: |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 89 | if v == '' or v in L: continue |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 90 | L.append(v) |
| 91 | break |
| 92 | i = i + 1 |
| 93 | except win32api.error: |
| 94 | break |
| 95 | except win32api.error: |
| 96 | pass |
| 97 | return L |
| 98 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 99 | # get_msvc_paths() |
| 100 | |
| 101 | |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame^] | 102 | def find_exe (exe, version_number): |
| 103 | """Try to find an MSVC executable program 'exe' (from version |
| 104 | 'version_number' of MSVC) in several places: first, one of the MSVC |
| 105 | program search paths from the registry; next, the directories in the |
| 106 | PATH environment variable. If any of those work, return an absolute |
| 107 | path that is known to exist. If none of them work, just return the |
| 108 | original program name, 'exe'.""" |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 109 | |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame^] | 110 | for p in get_msvc_paths ('path', version_number): |
| 111 | fn = os.path.join (os.path.abspath(p), exe) |
| 112 | if os.path.isfile(fn): |
| 113 | return fn |
| 114 | |
| 115 | # didn't find it; try existing path |
| 116 | for p in string.split (os.environ['Path'],';'): |
| 117 | fn = os.path.join(os.path.abspath(p),exe) |
| 118 | if os.path.isfile(fn): |
| 119 | return fn |
| 120 | |
| 121 | return exe # last desperate hope |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 122 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 123 | |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame^] | 124 | def _find_SET(name,version_number): |
| 125 | """looks up in the registry and returns a list of values suitable for |
| 126 | use in a SET command eg SET name=value. Normally the value will be a |
| 127 | ';' separated list similar to a path list. |
| 128 | |
| 129 | name is the name of an MSVC path and version_number is a version_number |
| 130 | of an MSVC installation.""" |
| 131 | return get_msvc_paths(name, version_number) |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 132 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 133 | |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame^] | 134 | def _do_SET(name, version_number): |
| 135 | """sets os.environ[name] to an MSVC path type value obtained from |
| 136 | _find_SET. This is equivalent to a SET command prior to execution of |
| 137 | spawned commands.""" |
| 138 | p=_find_SET(name, version_number) |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 139 | if p: |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame^] | 140 | os.environ[name]=string.join(p,';') |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 141 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 142 | |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 143 | class MSVCCompiler (CCompiler) : |
| 144 | """Concrete class that implements an interface to Microsoft Visual C++, |
| 145 | as defined by the CCompiler abstract class.""" |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 146 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 147 | compiler_type = 'msvc' |
| 148 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 149 | def __init__ (self, |
| 150 | verbose=0, |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 151 | dry_run=0, |
| 152 | force=0): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 153 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 154 | CCompiler.__init__ (self, verbose, dry_run, force) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 155 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 156 | self.add_library_dir( os.path.join( sys.exec_prefix, 'libs' ) ) |
| 157 | |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame^] | 158 | vNum = get_devstudio_versions () |
| 159 | |
| 160 | if vNum: |
| 161 | vNum = vNum[0] # highest version |
| 162 | |
| 163 | self.cc = _find_exe("cl.exe", vNum) |
| 164 | self.link = _find_exe("link.exe", vNum) |
| 165 | _do_SET('lib', vNum) |
| 166 | _do_SET('include', vNum) |
| 167 | path=_find_SET('path', vNum) |
| 168 | try: |
| 169 | for p in string.split(os.environ['path'],';'): |
| 170 | path.append(p) |
| 171 | except KeyError: |
| 172 | pass |
| 173 | os.environ['path'] = string.join(path,';') |
| 174 | else: |
| 175 | # devstudio not found in the registry |
| 176 | self.cc = "cl.exe" |
| 177 | self.link = "link.exe" |
| 178 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 179 | self.preprocess_options = None |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame^] | 180 | self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3' ] |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 181 | self.compile_options_debug = [ |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame^] | 182 | '/nologo', '/Od', '/MDd', '/W3', '/Z7', '/D_DEBUG' |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 183 | ] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 184 | |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 185 | self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO'] |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 186 | self.ldflags_shared_debug = [ |
| 187 | '/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG' |
| 188 | ] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 189 | self.ldflags_static = [ '/nologo'] |
| 190 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 191 | |
| 192 | # -- Worker methods ------------------------------------------------ |
| 193 | # (must be implemented by subclasses) |
| 194 | |
| 195 | _c_extensions = [ '.c' ] |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 196 | _cpp_extensions = [ '.cc', '.cpp' ] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 197 | |
| 198 | _obj_ext = '.obj' |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 199 | _exe_ext = '.exe' |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 200 | _shared_lib_ext = '.dll' |
| 201 | _static_lib_ext = '.lib' |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 202 | |
| 203 | # XXX the 'output_dir' parameter is ignored by the methods in this |
| 204 | # class! I just put it in to be consistent with CCompiler and |
| 205 | # UnixCCompiler, but someone who actually knows Visual C++ will |
| 206 | # have to make it work... |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 207 | |
| 208 | def compile (self, |
| 209 | sources, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 210 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 211 | macros=None, |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 212 | include_dirs=None, |
Greg Ward | 386b844 | 2000-02-09 02:18:39 +0000 | [diff] [blame] | 213 | debug=0, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 214 | extra_preargs=None, |
| 215 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 216 | |
| 217 | if macros is None: |
| 218 | macros = [] |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 219 | if include_dirs is None: |
| 220 | include_dirs = [] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 221 | |
| 222 | objectFiles = [] |
| 223 | |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 224 | base_pp_opts = \ |
| 225 | gen_preprocess_options (self.macros + macros, |
| 226 | self.include_dirs + include_dirs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 227 | |
| 228 | base_pp_opts.append('/c') |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 229 | |
| 230 | if debug: |
| 231 | compile_options = self.compile_options_debug |
| 232 | else: |
| 233 | compile_options = self.compile_options |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 234 | |
| 235 | for srcFile in sources: |
| 236 | base,ext = os.path.splitext(srcFile) |
| 237 | objFile = base + ".obj" |
| 238 | |
| 239 | if ext in self._c_extensions: |
| 240 | fileOpt = "/Tc" |
| 241 | elif ext in self._cpp_extensions: |
| 242 | fileOpt = "/Tp" |
| 243 | |
| 244 | inputOpt = fileOpt + srcFile |
| 245 | outputOpt = "/Fo" + objFile |
| 246 | |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 247 | cc_args = compile_options + \ |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 248 | base_pp_opts + \ |
| 249 | [outputOpt, inputOpt] |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 250 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 251 | if extra_preargs: |
| 252 | cc_args[:0] = extra_preargs |
| 253 | if extra_postargs: |
| 254 | cc_args.extend (extra_postargs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 255 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 256 | self.spawn ([self.cc] + cc_args) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 257 | objectFiles.append( objFile ) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 258 | return objectFiles |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 259 | |
| 260 | |
Greg Ward | 386b844 | 2000-02-09 02:18:39 +0000 | [diff] [blame] | 261 | # XXX the signature of this method is different from CCompiler and |
| 262 | # UnixCCompiler -- but those extra parameters (libraries, library_dirs) |
| 263 | # are actually used. So: are they really *needed*, or can they be |
| 264 | # ditched? If needed, the CCompiler API will have to change... |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 265 | def link_static_lib (self, |
| 266 | objects, |
| 267 | output_libname, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 268 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 269 | libraries=None, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 270 | library_dirs=None, |
Greg Ward | 386b844 | 2000-02-09 02:18:39 +0000 | [diff] [blame] | 271 | debug=0, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 272 | extra_preargs=None, |
| 273 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 274 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 275 | if libraries is None: |
| 276 | libraries = [] |
| 277 | if library_dirs is None: |
| 278 | library_dirs = [] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 279 | |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 280 | lib_opts = gen_lib_options (self.libraries + libraries, |
| 281 | self.library_dirs + library_dirs, |
| 282 | "%s.lib", "/LIBPATH:%s") |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 283 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 284 | ld_args = self.ldflags_static + lib_opts + \ |
| 285 | objects + ['/OUT:' + output_filename] |
Greg Ward | 386b844 | 2000-02-09 02:18:39 +0000 | [diff] [blame] | 286 | if debug: |
| 287 | pass # XXX what goes here? |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 288 | if extra_preargs: |
| 289 | ld_args[:0] = extra_preargs |
| 290 | if extra_postargs: |
| 291 | ld_args.extend (extra_postargs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 292 | |
| 293 | self.spawn ( [ self.link ] + ld_args ) |
| 294 | |
| 295 | |
| 296 | def link_shared_lib (self, |
| 297 | objects, |
| 298 | output_libname, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 299 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 300 | libraries=None, |
| 301 | library_dirs=None, |
Greg Ward | 386b844 | 2000-02-09 02:18:39 +0000 | [diff] [blame] | 302 | debug=0, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 303 | extra_preargs=None, |
| 304 | extra_postargs=None): |
| 305 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 306 | # XXX should we sanity check the library name? (eg. no |
| 307 | # slashes) |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 308 | self.link_shared_object (objects, |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 309 | self.shared_library_name(output_libname), |
| 310 | output_dir=output_dir, |
| 311 | libraries=libraries, |
| 312 | library_dirs=library_dirs, |
| 313 | debug=debug, |
| 314 | extra_preargs=extra_preargs, |
| 315 | extra_postargs=extra_postargs) |
| 316 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 317 | |
| 318 | def link_shared_object (self, |
| 319 | objects, |
| 320 | output_filename, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 321 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 322 | libraries=None, |
| 323 | library_dirs=None, |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 324 | debug=0, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 325 | extra_preargs=None, |
| 326 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 327 | """Link a bunch of stuff together to create a shared object |
| 328 | file. Much like 'link_shared_lib()', except the output |
| 329 | filename is explicitly supplied as 'output_filename'.""" |
| 330 | if libraries is None: |
| 331 | libraries = [] |
| 332 | if library_dirs is None: |
| 333 | library_dirs = [] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 334 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 335 | lib_opts = gen_lib_options (self, |
| 336 | self.library_dirs + library_dirs, |
| 337 | self.libraries + libraries) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 338 | |
Greg Ward | 386b844 | 2000-02-09 02:18:39 +0000 | [diff] [blame] | 339 | if debug: |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 340 | ldflags = self.ldflags_shared_debug |
| 341 | basename, ext = os.path.splitext (output_filename) |
| 342 | #XXX not sure this belongs here |
| 343 | # extensions in debug_mode are named 'module_d.pyd' |
| 344 | output_filename = basename + '_d' + ext |
| 345 | else: |
| 346 | ldflags = self.ldflags_shared |
| 347 | |
| 348 | ld_args = ldflags + lib_opts + \ |
| 349 | objects + ['/OUT:' + output_filename] |
| 350 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 351 | if extra_preargs: |
| 352 | ld_args[:0] = extra_preargs |
| 353 | if extra_postargs: |
| 354 | ld_args.extend (extra_postargs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 355 | |
| 356 | self.spawn ( [ self.link ] + ld_args ) |
| 357 | |
| 358 | |
| 359 | # -- Filename mangling methods ------------------------------------- |
| 360 | |
| 361 | def _change_extensions( self, filenames, newExtension ): |
| 362 | object_filenames = [] |
| 363 | |
| 364 | for srcFile in filenames: |
| 365 | base,ext = os.path.splitext( srcFile ) |
| 366 | # XXX should we strip off any existing path? |
| 367 | object_filenames.append( base + newExtension ) |
| 368 | |
| 369 | return object_filenames |
| 370 | |
| 371 | def object_filenames (self, source_filenames): |
| 372 | """Return the list of object filenames corresponding to each |
| 373 | specified source filename.""" |
| 374 | return self._change_extensions( source_filenames, self._obj_ext ) |
| 375 | |
| 376 | def shared_object_filename (self, source_filename): |
| 377 | """Return the shared object filename corresponding to a |
| 378 | specified source filename.""" |
| 379 | return self._change_extensions( source_filenames, self._shared_lib_ext ) |
| 380 | |
| 381 | def library_filename (self, libname): |
| 382 | """Return the static library filename corresponding to the |
| 383 | specified library name.""" |
Greg Ward | c8a95c8 | 2000-01-17 18:00:04 +0000 | [diff] [blame] | 384 | return "%s%s" %( libname, self._static_lib_ext ) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 385 | |
| 386 | def shared_library_filename (self, libname): |
| 387 | """Return the shared library filename corresponding to the |
| 388 | specified library name.""" |
Greg Ward | c8a95c8 | 2000-01-17 18:00:04 +0000 | [diff] [blame] | 389 | return "%s%s" %( libname, self._shared_lib_ext ) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 390 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 391 | |
| 392 | def library_dir_option (self, dir): |
| 393 | return "/LIBPATH:" + dir |
| 394 | |
| 395 | def library_option (self, lib): |
| 396 | return self.library_filename (lib) |
| 397 | |
| 398 | |
| 399 | def find_library_file (self, dirs, lib): |
| 400 | |
| 401 | for dir in dirs: |
| 402 | libfile = os.path.join (dir, self.library_filename (lib)) |
| 403 | if os.path.exists (libfile): |
| 404 | return libfile |
| 405 | |
| 406 | else: |
| 407 | # Oops, didn't find it in *any* of 'dirs' |
| 408 | return None |
| 409 | |
| 410 | # find_library_file () |
| 411 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 412 | # class MSVCCompiler |