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 | 3add77f | 2000-05-30 02:02:49 +0000 | [diff] [blame^] | 15 | from distutils.errors import \ |
| 16 | DistutilsExecError, DistutilsPlatformError, \ |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 17 | CompileError, LibError, LinkError |
Greg Ward | 3add77f | 2000-05-30 02:02:49 +0000 | [diff] [blame^] | 18 | from distutils.ccompiler import \ |
| 19 | CCompiler, gen_preprocess_options, gen_lib_options |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 20 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 21 | _can_read_reg = 0 |
| 22 | try: |
| 23 | import winreg |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 24 | _can_read_reg = 1 |
Greg Ward | 19ce166 | 2000-03-31 19:04:25 +0000 | [diff] [blame] | 25 | hkey_mod = winreg |
| 26 | |
| 27 | RegOpenKeyEx = winreg.OpenKeyEx |
| 28 | RegEnumKey = winreg.EnumKey |
| 29 | RegEnumValue = winreg.EnumValue |
| 30 | RegError = winreg.error |
| 31 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 32 | except ImportError: |
| 33 | try: |
| 34 | import win32api |
| 35 | import win32con |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 36 | _can_read_reg = 1 |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 37 | hkey_mod = win32con |
Greg Ward | 19ce166 | 2000-03-31 19:04:25 +0000 | [diff] [blame] | 38 | |
| 39 | RegOpenKeyEx = win32api.RegOpenKeyEx |
| 40 | RegEnumKey = win32api.RegEnumKey |
| 41 | RegEnumValue = win32api.RegEnumValue |
| 42 | RegError = win32api.error |
| 43 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 44 | except ImportError: |
| 45 | pass |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 46 | |
| 47 | if _can_read_reg: |
| 48 | HKEY_CLASSES_ROOT = hkey_mod.HKEY_CLASSES_ROOT |
| 49 | HKEY_LOCAL_MACHINE = hkey_mod.HKEY_LOCAL_MACHINE |
| 50 | HKEY_CURRENT_USER = hkey_mod.HKEY_CURRENT_USER |
| 51 | HKEY_USERS = hkey_mod.HKEY_USERS |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 52 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 53 | |
| 54 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 55 | def get_devstudio_versions (): |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 56 | """Get list of devstudio versions from the Windows registry. Return a |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 57 | list of strings containing version numbers; the list will be |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 58 | empty if we were unable to access the registry (eg. couldn't import |
| 59 | a registry-access module) or the appropriate registry keys weren't |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 60 | found.""" |
| 61 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 62 | if not _can_read_reg: |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 63 | return [] |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 64 | |
| 65 | K = 'Software\\Microsoft\\Devstudio' |
| 66 | L = [] |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 67 | for base in (HKEY_CLASSES_ROOT, |
| 68 | HKEY_LOCAL_MACHINE, |
| 69 | HKEY_CURRENT_USER, |
| 70 | HKEY_USERS): |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 71 | try: |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 72 | k = RegOpenKeyEx(base,K) |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 73 | i = 0 |
| 74 | while 1: |
| 75 | try: |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 76 | p = RegEnumKey(k,i) |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 77 | if p[0] in '123456789' and p not in L: |
| 78 | L.append(p) |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 79 | except RegError: |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 80 | break |
| 81 | i = i + 1 |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 82 | except RegError: |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 83 | pass |
| 84 | L.sort() |
| 85 | L.reverse() |
| 86 | return L |
| 87 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 88 | # get_devstudio_versions () |
| 89 | |
| 90 | |
| 91 | def get_msvc_paths (path, version='6.0', platform='x86'): |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 92 | """Get a list of devstudio directories (include, lib or path). Return |
| 93 | a list of strings; will be empty list if unable to access the |
| 94 | registry or appropriate registry keys not found.""" |
| 95 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 96 | if not _can_read_reg: |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 97 | return [] |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 98 | |
| 99 | L = [] |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 100 | if path=='lib': |
| 101 | path= 'Library' |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 102 | path = string.upper(path + ' Dirs') |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 103 | K = ('Software\\Microsoft\\Devstudio\\%s\\' + |
| 104 | 'Build System\\Components\\Platforms\\Win32 (%s)\\Directories') % \ |
| 105 | (version,platform) |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 106 | for base in (HKEY_CLASSES_ROOT, |
| 107 | HKEY_LOCAL_MACHINE, |
| 108 | HKEY_CURRENT_USER, |
| 109 | HKEY_USERS): |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 110 | try: |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 111 | k = RegOpenKeyEx(base,K) |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 112 | i = 0 |
| 113 | while 1: |
| 114 | try: |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 115 | (p,v,t) = RegEnumValue(k,i) |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 116 | if string.upper(p) == path: |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 117 | V = string.split(v,';') |
| 118 | for v in V: |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 119 | if v == '' or v in L: continue |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 120 | L.append(v) |
| 121 | break |
| 122 | i = i + 1 |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 123 | except RegError: |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 124 | break |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 125 | except RegError: |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 126 | pass |
| 127 | return L |
| 128 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 129 | # get_msvc_paths() |
| 130 | |
| 131 | |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 132 | def find_exe (exe, version_number): |
| 133 | """Try to find an MSVC executable program 'exe' (from version |
| 134 | 'version_number' of MSVC) in several places: first, one of the MSVC |
| 135 | program search paths from the registry; next, the directories in the |
| 136 | PATH environment variable. If any of those work, return an absolute |
| 137 | path that is known to exist. If none of them work, just return the |
| 138 | original program name, 'exe'.""" |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 139 | |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 140 | for p in get_msvc_paths ('path', version_number): |
| 141 | fn = os.path.join (os.path.abspath(p), exe) |
| 142 | if os.path.isfile(fn): |
| 143 | return fn |
| 144 | |
| 145 | # didn't find it; try existing path |
| 146 | for p in string.split (os.environ['Path'],';'): |
| 147 | fn = os.path.join(os.path.abspath(p),exe) |
| 148 | if os.path.isfile(fn): |
| 149 | return fn |
| 150 | |
| 151 | return exe # last desperate hope |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 152 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 153 | |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 154 | def set_path_env_var (name, version_number): |
| 155 | """Set environment variable 'name' to an MSVC path type value obtained |
| 156 | from 'get_msvc_paths()'. This is equivalent to a SET command prior |
| 157 | to execution of spawned commands.""" |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 158 | |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 159 | p = get_msvc_paths (name, version_number) |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 160 | if p: |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 161 | os.environ[name] = string.join (p,';') |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 162 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 163 | |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 164 | class MSVCCompiler (CCompiler) : |
| 165 | """Concrete class that implements an interface to Microsoft Visual C++, |
| 166 | as defined by the CCompiler abstract class.""" |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 167 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 168 | compiler_type = 'msvc' |
| 169 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 170 | # Private class data (need to distinguish C from C++ source for compiler) |
| 171 | _c_extensions = ['.c'] |
| 172 | _cpp_extensions = ['.cc','.cpp'] |
| 173 | |
| 174 | # Needed for the filename generation methods provided by the |
| 175 | # base class, CCompiler. |
| 176 | src_extensions = _c_extensions + _cpp_extensions |
| 177 | obj_extension = '.obj' |
| 178 | static_lib_extension = '.lib' |
| 179 | shared_lib_extension = '.dll' |
| 180 | static_lib_format = shared_lib_format = '%s%s' |
| 181 | exe_extension = '.exe' |
| 182 | |
| 183 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 184 | def __init__ (self, |
| 185 | verbose=0, |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 186 | dry_run=0, |
| 187 | force=0): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 188 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 189 | CCompiler.__init__ (self, verbose, dry_run, force) |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 190 | versions = get_devstudio_versions () |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 191 | |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 192 | if versions: |
| 193 | version = versions[0] # highest version |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 194 | |
Greg Ward | 41b4dd6 | 2000-03-29 04:13:00 +0000 | [diff] [blame] | 195 | self.cc = find_exe("cl.exe", version) |
| 196 | self.link = find_exe("link.exe", version) |
| 197 | self.lib = find_exe("lib.exe", version) |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 198 | set_path_env_var ('lib', version) |
| 199 | set_path_env_var ('include', version) |
| 200 | path=get_msvc_paths('path', version) |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 201 | try: |
| 202 | for p in string.split(os.environ['path'],';'): |
| 203 | path.append(p) |
| 204 | except KeyError: |
| 205 | pass |
| 206 | os.environ['path'] = string.join(path,';') |
| 207 | else: |
| 208 | # devstudio not found in the registry |
| 209 | self.cc = "cl.exe" |
| 210 | self.link = "link.exe" |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame] | 211 | self.lib = "lib.exe" |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 212 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 213 | self.preprocess_options = None |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 214 | self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3' ] |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 215 | self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/Z7', '/D_DEBUG'] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 216 | |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 217 | self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO'] |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 218 | self.ldflags_shared_debug = [ |
| 219 | '/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG' |
| 220 | ] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 221 | self.ldflags_static = [ '/nologo'] |
| 222 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 223 | |
| 224 | # -- Worker methods ------------------------------------------------ |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 225 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 226 | def compile (self, |
| 227 | sources, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 228 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 229 | macros=None, |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 230 | include_dirs=None, |
Greg Ward | 386b844 | 2000-02-09 02:18:39 +0000 | [diff] [blame] | 231 | debug=0, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 232 | extra_preargs=None, |
| 233 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 234 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 235 | (output_dir, macros, include_dirs) = \ |
| 236 | self._fix_compile_args (output_dir, macros, include_dirs) |
| 237 | (objects, skip_sources) = self._prep_compile (sources, output_dir) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 238 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 239 | if extra_postargs is None: |
| 240 | extra_postargs = [] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 241 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 242 | pp_opts = gen_preprocess_options (macros, include_dirs) |
| 243 | compile_opts = extra_preargs or [] |
| 244 | compile_opts.append ('/c') |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 245 | if debug: |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 246 | compile_opts.extend (self.compile_options_debug) |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 247 | else: |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 248 | compile_opts.extend (self.compile_options) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 249 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 250 | for i in range (len (sources)): |
| 251 | src = sources[i] ; obj = objects[i] |
| 252 | ext = (os.path.splitext (src))[1] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 253 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 254 | if skip_sources[src]: |
| 255 | self.announce ("skipping %s (%s up-to-date)" % (src, obj)) |
| 256 | else: |
| 257 | if ext in self._c_extensions: |
| 258 | input_opt = "/Tc" + src |
| 259 | elif ext in self._cpp_extensions: |
| 260 | input_opt = "/Tp" + src |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 261 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 262 | output_opt = "/Fo" + obj |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 263 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 264 | self.mkpath (os.path.dirname (obj)) |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 265 | try: |
| 266 | self.spawn ([self.cc] + compile_opts + pp_opts + |
| 267 | [input_opt, output_opt] + |
| 268 | extra_postargs) |
| 269 | except DistutilsExecError, msg: |
| 270 | raise CompileError, msg |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 271 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 272 | return objects |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 273 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 274 | # compile () |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 275 | |
| 276 | |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame] | 277 | def create_static_lib (self, |
| 278 | objects, |
| 279 | output_libname, |
| 280 | output_dir=None, |
| 281 | debug=0, |
| 282 | extra_preargs=None, |
| 283 | extra_postargs=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 284 | |
Greg Ward | 2f557a2 | 2000-03-26 21:42:28 +0000 | [diff] [blame] | 285 | (objects, output_dir) = self._fix_object_args (objects, output_dir) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 286 | output_filename = \ |
| 287 | self.library_filename (output_libname, output_dir=output_dir) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 288 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 289 | if self._need_link (objects, output_filename): |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame] | 290 | lib_args = objects + ['/OUT:' + output_filename] |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 291 | if debug: |
| 292 | pass # XXX what goes here? |
| 293 | if extra_preargs: |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame] | 294 | lib_args[:0] = extra_preargs |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 295 | if extra_postargs: |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame] | 296 | lib_args.extend (extra_postargs) |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 297 | try: |
| 298 | self.spawn ([self.link] + ld_args) |
| 299 | except DistutilsExecError, msg: |
| 300 | raise LibError, msg |
| 301 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 302 | else: |
| 303 | self.announce ("skipping %s (up-to-date)" % output_filename) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 304 | |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame] | 305 | # create_static_lib () |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 306 | |
| 307 | |
| 308 | def link_shared_lib (self, |
| 309 | objects, |
| 310 | output_libname, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 311 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 312 | libraries=None, |
| 313 | library_dirs=None, |
Greg Ward | 2f557a2 | 2000-03-26 21:42:28 +0000 | [diff] [blame] | 314 | runtime_library_dirs=None, |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 315 | export_symbols=None, |
Greg Ward | 386b844 | 2000-02-09 02:18:39 +0000 | [diff] [blame] | 316 | debug=0, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 317 | extra_preargs=None, |
| 318 | extra_postargs=None): |
| 319 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 320 | self.link_shared_object (objects, |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 321 | self.shared_library_name(output_libname), |
| 322 | output_dir=output_dir, |
| 323 | libraries=libraries, |
| 324 | library_dirs=library_dirs, |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 325 | runtime_library_dirs=runtime_library_dirs, |
| 326 | export_symbols=export_symbols, |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 327 | debug=debug, |
| 328 | extra_preargs=extra_preargs, |
| 329 | extra_postargs=extra_postargs) |
| 330 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 331 | |
| 332 | def link_shared_object (self, |
| 333 | objects, |
| 334 | output_filename, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 335 | output_dir=None, |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 336 | libraries=None, |
| 337 | library_dirs=None, |
Greg Ward | 2f557a2 | 2000-03-26 21:42:28 +0000 | [diff] [blame] | 338 | runtime_library_dirs=None, |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 339 | export_symbols=None, |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 340 | debug=0, |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 341 | extra_preargs=None, |
| 342 | extra_postargs=None): |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 343 | |
Greg Ward | 2f557a2 | 2000-03-26 21:42:28 +0000 | [diff] [blame] | 344 | (objects, output_dir) = self._fix_object_args (objects, output_dir) |
| 345 | (libraries, library_dirs, runtime_library_dirs) = \ |
| 346 | self._fix_lib_args (libraries, library_dirs, runtime_library_dirs) |
| 347 | |
Greg Ward | f70c603 | 2000-04-19 02:16:49 +0000 | [diff] [blame] | 348 | if runtime_library_dirs: |
Greg Ward | 2f557a2 | 2000-03-26 21:42:28 +0000 | [diff] [blame] | 349 | self.warn ("I don't know what to do with 'runtime_library_dirs': " |
| 350 | + str (runtime_library_dirs)) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 351 | |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 352 | lib_opts = gen_lib_options (self, |
Greg Ward | 2f557a2 | 2000-03-26 21:42:28 +0000 | [diff] [blame] | 353 | library_dirs, runtime_library_dirs, |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 354 | libraries) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 355 | if output_dir is not None: |
| 356 | output_filename = os.path.join (output_dir, output_filename) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 357 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 358 | if self._need_link (objects, output_filename): |
| 359 | |
| 360 | if debug: |
| 361 | ldflags = self.ldflags_shared_debug |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 362 | else: |
| 363 | ldflags = self.ldflags_shared |
| 364 | |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 365 | export_opts = [] |
| 366 | for sym in (export_symbols or []): |
| 367 | export_opts.append("/EXPORT:" + sym) |
| 368 | |
| 369 | ld_args = (ldflags + lib_opts + export_opts + |
| 370 | objects + ['/OUT:' + output_filename]) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 371 | |
| 372 | if extra_preargs: |
| 373 | ld_args[:0] = extra_preargs |
| 374 | if extra_postargs: |
| 375 | ld_args.extend (extra_postargs) |
| 376 | |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 377 | print "link_shared_object():" |
| 378 | print " output_filename =", output_filename |
| 379 | print " mkpath'ing:", os.path.dirname (output_filename) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 380 | self.mkpath (os.path.dirname (output_filename)) |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 381 | try: |
| 382 | self.spawn ([self.link] + ld_args) |
| 383 | except DistutilsExecError, msg: |
| 384 | raise LinkError, msg |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 385 | |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 386 | else: |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 387 | self.announce ("skipping %s (up-to-date)" % output_filename) |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 388 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 389 | # link_shared_object () |
Greg Ward | f70c603 | 2000-04-19 02:16:49 +0000 | [diff] [blame] | 390 | |
| 391 | |
| 392 | def link_executable (self, |
| 393 | objects, |
| 394 | output_progname, |
| 395 | output_dir=None, |
| 396 | libraries=None, |
| 397 | library_dirs=None, |
| 398 | runtime_library_dirs=None, |
| 399 | debug=0, |
| 400 | extra_preargs=None, |
| 401 | extra_postargs=None): |
| 402 | |
| 403 | (objects, output_dir) = self._fix_object_args (objects, output_dir) |
| 404 | (libraries, library_dirs, runtime_library_dirs) = \ |
| 405 | self._fix_lib_args (libraries, library_dirs, runtime_library_dirs) |
| 406 | |
| 407 | if runtime_library_dirs: |
| 408 | self.warn ("I don't know what to do with 'runtime_library_dirs': " |
| 409 | + str (runtime_library_dirs)) |
| 410 | |
| 411 | lib_opts = gen_lib_options (self, |
| 412 | library_dirs, runtime_library_dirs, |
| 413 | libraries) |
| 414 | output_filename = output_progname + self.exe_extension |
| 415 | if output_dir is not None: |
| 416 | output_filename = os.path.join (output_dir, output_filename) |
| 417 | |
| 418 | if self._need_link (objects, output_filename): |
| 419 | |
| 420 | if debug: |
| 421 | ldflags = self.ldflags_shared_debug[1:] |
| 422 | else: |
| 423 | ldflags = self.ldflags_shared[1:] |
| 424 | |
| 425 | ld_args = ldflags + lib_opts + \ |
| 426 | objects + ['/OUT:' + output_filename] |
| 427 | |
| 428 | if extra_preargs: |
| 429 | ld_args[:0] = extra_preargs |
| 430 | if extra_postargs: |
| 431 | ld_args.extend (extra_postargs) |
| 432 | |
| 433 | self.mkpath (os.path.dirname (output_filename)) |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 434 | try: |
| 435 | self.spawn ([self.link] + ld_args) |
| 436 | except DistutilsExecError, msg: |
| 437 | raise LinkError, msg |
Greg Ward | f70c603 | 2000-04-19 02:16:49 +0000 | [diff] [blame] | 438 | else: |
| 439 | self.announce ("skipping %s (up-to-date)" % output_filename) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 440 | |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 441 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 442 | # -- Miscellaneous methods ----------------------------------------- |
| 443 | # These are all used by the 'gen_lib_options() function, in |
| 444 | # ccompiler.py. |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 445 | |
| 446 | def library_dir_option (self, dir): |
| 447 | return "/LIBPATH:" + dir |
| 448 | |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 449 | def runtime_library_dir_option (self, dir): |
| 450 | raise DistutilsPlatformError, \ |
| 451 | "don't know how to set runtime library search path for MSVC++" |
| 452 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 453 | def library_option (self, lib): |
| 454 | return self.library_filename (lib) |
| 455 | |
| 456 | |
| 457 | def find_library_file (self, dirs, lib): |
| 458 | |
| 459 | for dir in dirs: |
| 460 | libfile = os.path.join (dir, self.library_filename (lib)) |
| 461 | if os.path.exists (libfile): |
| 462 | return libfile |
| 463 | |
| 464 | else: |
| 465 | # Oops, didn't find it in *any* of 'dirs' |
| 466 | return None |
| 467 | |
| 468 | # find_library_file () |
| 469 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 470 | # class MSVCCompiler |