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 |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 8 | # hacked by Robin Becker and Thomas Heller to do a better job of |
| 9 | # finding DevStudio (through the registry) |
| 10 | |
Greg Ward | 3ce77fd | 2000-03-02 01:49:45 +0000 | [diff] [blame] | 11 | __revision__ = "$Id$" |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 12 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 13 | import sys, os, string |
| 14 | from types import * |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 15 | from distutils.errors import * |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 16 | from distutils.ccompiler import \ |
| 17 | CCompiler, gen_preprocess_options, gen_lib_options |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 18 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 19 | |
| 20 | def get_devstudio_versions (): |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 21 | """Get list of devstudio versions from the Windows registry. Return a |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 22 | list of strings containing version numbers; the list will be |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 23 | empty if we were unable to access the registry (eg. couldn't import |
| 24 | a registry-access module) or the appropriate registry keys weren't |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 25 | found.""" |
| 26 | |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 27 | try: |
| 28 | import win32api |
| 29 | import win32con |
| 30 | except ImportError: |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 31 | return [] |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 32 | |
| 33 | K = 'Software\\Microsoft\\Devstudio' |
| 34 | L = [] |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 35 | for base in (win32con.HKEY_CLASSES_ROOT, |
| 36 | win32con.HKEY_LOCAL_MACHINE, |
| 37 | win32con.HKEY_CURRENT_USER, |
| 38 | win32con.HKEY_USERS): |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 39 | try: |
| 40 | k = win32api.RegOpenKeyEx(base,K) |
| 41 | i = 0 |
| 42 | while 1: |
| 43 | try: |
| 44 | p = win32api.RegEnumKey(k,i) |
| 45 | if p[0] in '123456789' and p not in L: |
| 46 | L.append(p) |
| 47 | except win32api.error: |
| 48 | break |
| 49 | i = i + 1 |
| 50 | except win32api.error: |
| 51 | pass |
| 52 | L.sort() |
| 53 | L.reverse() |
| 54 | return L |
| 55 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 56 | # get_devstudio_versions () |
| 57 | |
| 58 | |
| 59 | def get_msvc_paths (path, version='6.0', platform='x86'): |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 60 | """Get a list of devstudio directories (include, lib or path). Return |
| 61 | a list of strings; will be empty list if unable to access the |
| 62 | registry or appropriate registry keys not found.""" |
| 63 | |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 64 | try: |
| 65 | import win32api |
| 66 | import win32con |
| 67 | except ImportError: |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 68 | return [] |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 69 | |
| 70 | L = [] |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 71 | if path=='lib': |
| 72 | path= 'Library' |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 73 | path = string.upper(path + ' Dirs') |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 74 | K = ('Software\\Microsoft\\Devstudio\\%s\\' + |
| 75 | 'Build System\\Components\\Platforms\\Win32 (%s)\\Directories') % \ |
| 76 | (version,platform) |
| 77 | for base in (win32con.HKEY_CLASSES_ROOT, |
| 78 | win32con.HKEY_LOCAL_MACHINE, |
| 79 | win32con.HKEY_CURRENT_USER, |
| 80 | win32con.HKEY_USERS): |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 81 | try: |
| 82 | k = win32api.RegOpenKeyEx(base,K) |
| 83 | i = 0 |
| 84 | while 1: |
| 85 | try: |
| 86 | (p,v,t) = win32api.RegEnumValue(k,i) |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 87 | if string.upper(p) == path: |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 88 | V = string.split(v,';') |
| 89 | for v in V: |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 90 | if v == '' or v in L: continue |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 91 | L.append(v) |
| 92 | break |
| 93 | i = i + 1 |
| 94 | except win32api.error: |
| 95 | break |
| 96 | except win32api.error: |
| 97 | pass |
| 98 | return L |
| 99 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 100 | # get_msvc_paths() |
| 101 | |
| 102 | |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 103 | def find_exe (exe, version_number): |
| 104 | """Try to find an MSVC executable program 'exe' (from version |
| 105 | 'version_number' of MSVC) in several places: first, one of the MSVC |
| 106 | program search paths from the registry; next, the directories in the |
| 107 | PATH environment variable. If any of those work, return an absolute |
| 108 | path that is known to exist. If none of them work, just return the |
| 109 | original program name, 'exe'.""" |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 110 | |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 111 | for p in get_msvc_paths ('path', version_number): |
| 112 | fn = os.path.join (os.path.abspath(p), exe) |
| 113 | if os.path.isfile(fn): |
| 114 | return fn |
| 115 | |
| 116 | # didn't find it; try existing path |
| 117 | for p in string.split (os.environ['Path'],';'): |
| 118 | fn = os.path.join(os.path.abspath(p),exe) |
| 119 | if os.path.isfile(fn): |
| 120 | return fn |
| 121 | |
| 122 | return exe # last desperate hope |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 123 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 124 | |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 125 | def set_path_env_var (name, version_number): |
| 126 | """Set environment variable 'name' to an MSVC path type value obtained |
| 127 | from 'get_msvc_paths()'. This is equivalent to a SET command prior |
| 128 | to execution of spawned commands.""" |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 129 | |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 130 | p = get_msvc_paths (name, version_number) |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 131 | if p: |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 132 | os.environ[name] = string.join (p,';') |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 133 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 134 | |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 135 | class MSVCCompiler (CCompiler) : |
| 136 | """Concrete class that implements an interface to Microsoft Visual C++, |
| 137 | as defined by the CCompiler abstract class.""" |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 138 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 139 | compiler_type = 'msvc' |
| 140 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 141 | # Private class data (need to distinguish C from C++ source for compiler) |
| 142 | _c_extensions = ['.c'] |
| 143 | _cpp_extensions = ['.cc','.cpp'] |
| 144 | |
| 145 | # Needed for the filename generation methods provided by the |
| 146 | # base class, CCompiler. |
| 147 | src_extensions = _c_extensions + _cpp_extensions |
| 148 | obj_extension = '.obj' |
| 149 | static_lib_extension = '.lib' |
| 150 | shared_lib_extension = '.dll' |
| 151 | static_lib_format = shared_lib_format = '%s%s' |
| 152 | exe_extension = '.exe' |
| 153 | |
| 154 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 155 | def __init__ (self, |
| 156 | verbose=0, |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 157 | dry_run=0, |
| 158 | force=0): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 159 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 160 | CCompiler.__init__ (self, verbose, dry_run, force) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 161 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 162 | self.add_library_dir( os.path.join( sys.exec_prefix, 'libs' ) ) |
| 163 | |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 164 | versions = get_devstudio_versions () |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 165 | |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 166 | if versions: |
| 167 | version = versions[0] # highest version |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 168 | |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 169 | self.cc = _find_exe("cl.exe", version) |
| 170 | self.link = _find_exe("link.exe", version) |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame^] | 171 | self.lib = _find_exe("lib.exe", version) |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 172 | set_path_env_var ('lib', version) |
| 173 | set_path_env_var ('include', version) |
| 174 | path=get_msvc_paths('path', version) |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 175 | try: |
| 176 | for p in string.split(os.environ['path'],';'): |
| 177 | path.append(p) |
| 178 | except KeyError: |
| 179 | pass |
| 180 | os.environ['path'] = string.join(path,';') |
| 181 | else: |
| 182 | # devstudio not found in the registry |
| 183 | self.cc = "cl.exe" |
| 184 | self.link = "link.exe" |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame^] | 185 | self.lib = "lib.exe" |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 186 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 187 | self.preprocess_options = None |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 188 | self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3' ] |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 189 | self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/Z7', '/D_DEBUG'] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 190 | |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 191 | self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO'] |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 192 | self.ldflags_shared_debug = [ |
| 193 | '/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG' |
| 194 | ] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 195 | self.ldflags_static = [ '/nologo'] |
| 196 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 197 | |
| 198 | # -- Worker methods ------------------------------------------------ |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 199 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 200 | def compile (self, |
| 201 | sources, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 202 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 203 | macros=None, |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 204 | include_dirs=None, |
Greg Ward | 386b844 | 2000-02-09 02:18:39 +0000 | [diff] [blame] | 205 | debug=0, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 206 | extra_preargs=None, |
| 207 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 208 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 209 | (output_dir, macros, include_dirs) = \ |
| 210 | self._fix_compile_args (output_dir, macros, include_dirs) |
| 211 | (objects, skip_sources) = self._prep_compile (sources, output_dir) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 212 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 213 | if extra_postargs is None: |
| 214 | extra_postargs = [] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 215 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 216 | pp_opts = gen_preprocess_options (macros, include_dirs) |
| 217 | compile_opts = extra_preargs or [] |
| 218 | compile_opts.append ('/c') |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 219 | if debug: |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 220 | compile_opts.extend (self.compile_options_debug) |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 221 | else: |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 222 | compile_opts.extend (self.compile_options) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 223 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 224 | for i in range (len (sources)): |
| 225 | src = sources[i] ; obj = objects[i] |
| 226 | ext = (os.path.splitext (src))[1] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 227 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 228 | if skip_sources[src]: |
| 229 | self.announce ("skipping %s (%s up-to-date)" % (src, obj)) |
| 230 | else: |
| 231 | if ext in self._c_extensions: |
| 232 | input_opt = "/Tc" + src |
| 233 | elif ext in self._cpp_extensions: |
| 234 | input_opt = "/Tp" + src |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 235 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 236 | output_opt = "/Fo" + obj |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 237 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 238 | self.mkpath (os.path.dirname (obj)) |
| 239 | self.spawn ([self.cc] + compile_opts + pp_opts + |
| 240 | [input_opt, output_opt] + |
| 241 | extra_postargs) |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 242 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 243 | return objects |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 244 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 245 | # compile () |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 246 | |
| 247 | |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame^] | 248 | def create_static_lib (self, |
| 249 | objects, |
| 250 | output_libname, |
| 251 | output_dir=None, |
| 252 | debug=0, |
| 253 | extra_preargs=None, |
| 254 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 255 | |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame^] | 256 | (objects, output_dir) = \ |
| 257 | self._fix_link_args (objects, output_dir, takes_libs=0) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 258 | output_filename = \ |
| 259 | self.library_filename (output_libname, output_dir=output_dir) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 260 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 261 | if self._need_link (objects, output_filename): |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame^] | 262 | lib_args = objects + ['/OUT:' + output_filename] |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 263 | if debug: |
| 264 | pass # XXX what goes here? |
| 265 | if extra_preargs: |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame^] | 266 | lib_args[:0] = extra_preargs |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 267 | if extra_postargs: |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame^] | 268 | lib_args.extend (extra_postargs) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 269 | self.spawn ([self.link] + ld_args) |
| 270 | else: |
| 271 | self.announce ("skipping %s (up-to-date)" % output_filename) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 272 | |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame^] | 273 | # create_static_lib () |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 274 | |
| 275 | |
| 276 | def link_shared_lib (self, |
| 277 | objects, |
| 278 | output_libname, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 279 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 280 | libraries=None, |
| 281 | library_dirs=None, |
Greg Ward | 386b844 | 2000-02-09 02:18:39 +0000 | [diff] [blame] | 282 | debug=0, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 283 | extra_preargs=None, |
| 284 | extra_postargs=None): |
| 285 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 286 | self.link_shared_object (objects, |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 287 | self.shared_library_name(output_libname), |
| 288 | output_dir=output_dir, |
| 289 | libraries=libraries, |
| 290 | library_dirs=library_dirs, |
| 291 | debug=debug, |
| 292 | extra_preargs=extra_preargs, |
| 293 | extra_postargs=extra_postargs) |
| 294 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 295 | |
| 296 | def link_shared_object (self, |
| 297 | objects, |
| 298 | output_filename, |
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 | 4ba9b2e | 2000-02-10 02:15:52 +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): |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 305 | |
| 306 | (objects, output_dir, libraries, library_dirs) = \ |
| 307 | self._fix_link_args (objects, output_dir, takes_libs=1, |
| 308 | libraries=libraries, library_dirs=library_dirs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 309 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 310 | lib_opts = gen_lib_options (self, library_dirs, libraries) |
| 311 | if type (output_dir) not in (StringType, NoneType): |
| 312 | raise TypeError, "'output_dir' must be a string or None" |
| 313 | if output_dir is not None: |
| 314 | output_filename = os.path.join (output_dir, output_filename) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 315 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 316 | if self._need_link (objects, output_filename): |
| 317 | |
| 318 | if debug: |
| 319 | ldflags = self.ldflags_shared_debug |
| 320 | # XXX not sure this belongs here |
| 321 | # extensions in debug_mode are named 'module_d.pyd' |
| 322 | basename, ext = os.path.splitext (output_filename) |
| 323 | output_filename = basename + '_d' + ext |
| 324 | else: |
| 325 | ldflags = self.ldflags_shared |
| 326 | |
| 327 | ld_args = ldflags + lib_opts + \ |
| 328 | objects + ['/OUT:' + output_filename] |
| 329 | |
| 330 | if extra_preargs: |
| 331 | ld_args[:0] = extra_preargs |
| 332 | if extra_postargs: |
| 333 | ld_args.extend (extra_postargs) |
| 334 | |
| 335 | self.mkpath (os.path.dirname (output_filename)) |
| 336 | self.spawn ([self.link] + ld_args) |
| 337 | |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 338 | else: |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 339 | self.announce ("skipping %s (up-to-date)" % output_filename) |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 340 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 341 | # link_shared_object () |
| 342 | |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 343 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 344 | # -- Miscellaneous methods ----------------------------------------- |
| 345 | # These are all used by the 'gen_lib_options() function, in |
| 346 | # ccompiler.py. |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 347 | |
| 348 | def library_dir_option (self, dir): |
| 349 | return "/LIBPATH:" + dir |
| 350 | |
| 351 | def library_option (self, lib): |
| 352 | return self.library_filename (lib) |
| 353 | |
| 354 | |
| 355 | def find_library_file (self, dirs, lib): |
| 356 | |
| 357 | for dir in dirs: |
| 358 | libfile = os.path.join (dir, self.library_filename (lib)) |
| 359 | if os.path.exists (libfile): |
| 360 | return libfile |
| 361 | |
| 362 | else: |
| 363 | # Oops, didn't find it in *any* of 'dirs' |
| 364 | return None |
| 365 | |
| 366 | # find_library_file () |
| 367 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 368 | # class MSVCCompiler |