Greg Ward | bfc79d6 | 2000-06-28 01:29:09 +0000 | [diff] [blame] | 1 | """distutils.msvccompiler |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 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 | |
Andrew M. Kuchling | a6483d2 | 2002-11-14 02:25:42 +0000 | [diff] [blame] | 6 | # Written by Perry Stoll |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 7 | # hacked by Robin Becker and Thomas Heller to do a better job of |
| 8 | # finding DevStudio (through the registry) |
| 9 | |
Andrew M. Kuchling | d448f66 | 2002-11-19 13:12:28 +0000 | [diff] [blame] | 10 | # This module should be kept compatible with Python 1.5.2. |
| 11 | |
Greg Ward | 3ce77fd | 2000-03-02 01:49:45 +0000 | [diff] [blame] | 12 | __revision__ = "$Id$" |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 13 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 14 | import sys, os, string |
| 15 | from types import * |
Greg Ward | 3add77f | 2000-05-30 02:02:49 +0000 | [diff] [blame] | 16 | from distutils.errors import \ |
| 17 | DistutilsExecError, DistutilsPlatformError, \ |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 18 | CompileError, LibError, LinkError |
Greg Ward | 3add77f | 2000-05-30 02:02:49 +0000 | [diff] [blame] | 19 | from distutils.ccompiler import \ |
| 20 | CCompiler, gen_preprocess_options, gen_lib_options |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 21 | from distutils import log |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 22 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 23 | _can_read_reg = 0 |
| 24 | try: |
Greg Ward | 1b5ec76 | 2000-06-30 19:37:59 +0000 | [diff] [blame] | 25 | import _winreg |
Greg Ward | 83c3870 | 2000-06-29 23:04:59 +0000 | [diff] [blame] | 26 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 27 | _can_read_reg = 1 |
Greg Ward | cd079c4 | 2000-06-29 22:59:10 +0000 | [diff] [blame] | 28 | hkey_mod = _winreg |
Greg Ward | 19ce166 | 2000-03-31 19:04:25 +0000 | [diff] [blame] | 29 | |
Greg Ward | cd079c4 | 2000-06-29 22:59:10 +0000 | [diff] [blame] | 30 | RegOpenKeyEx = _winreg.OpenKeyEx |
| 31 | RegEnumKey = _winreg.EnumKey |
| 32 | RegEnumValue = _winreg.EnumValue |
| 33 | RegError = _winreg.error |
Greg Ward | 19ce166 | 2000-03-31 19:04:25 +0000 | [diff] [blame] | 34 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 35 | except ImportError: |
| 36 | try: |
| 37 | import win32api |
| 38 | import win32con |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 39 | _can_read_reg = 1 |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 40 | hkey_mod = win32con |
Greg Ward | 19ce166 | 2000-03-31 19:04:25 +0000 | [diff] [blame] | 41 | |
| 42 | RegOpenKeyEx = win32api.RegOpenKeyEx |
| 43 | RegEnumKey = win32api.RegEnumKey |
| 44 | RegEnumValue = win32api.RegEnumValue |
| 45 | RegError = win32api.error |
| 46 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 47 | except ImportError: |
| 48 | pass |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 49 | |
| 50 | if _can_read_reg: |
| 51 | HKEY_CLASSES_ROOT = hkey_mod.HKEY_CLASSES_ROOT |
| 52 | HKEY_LOCAL_MACHINE = hkey_mod.HKEY_LOCAL_MACHINE |
| 53 | HKEY_CURRENT_USER = hkey_mod.HKEY_CURRENT_USER |
| 54 | HKEY_USERS = hkey_mod.HKEY_USERS |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 55 | |
| 56 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 57 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 58 | def get_devstudio_versions (): |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 59 | """Get list of devstudio versions from the Windows registry. Return a |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 60 | list of strings containing version numbers; the list will be |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 61 | empty if we were unable to access the registry (eg. couldn't import |
| 62 | a registry-access module) or the appropriate registry keys weren't |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 63 | found.""" |
| 64 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 65 | if not _can_read_reg: |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 66 | return [] |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 67 | |
| 68 | K = 'Software\\Microsoft\\Devstudio' |
| 69 | L = [] |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 70 | for base in (HKEY_CLASSES_ROOT, |
| 71 | HKEY_LOCAL_MACHINE, |
| 72 | HKEY_CURRENT_USER, |
| 73 | HKEY_USERS): |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 74 | try: |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 75 | k = RegOpenKeyEx(base,K) |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 76 | i = 0 |
| 77 | while 1: |
| 78 | try: |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 79 | p = RegEnumKey(k,i) |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 80 | if p[0] in '123456789' and p not in L: |
| 81 | L.append(p) |
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 | break |
| 84 | i = i + 1 |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 85 | except RegError: |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 86 | pass |
| 87 | L.sort() |
| 88 | L.reverse() |
| 89 | return L |
| 90 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 91 | # get_devstudio_versions () |
| 92 | |
| 93 | |
| 94 | def get_msvc_paths (path, version='6.0', platform='x86'): |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 95 | """Get a list of devstudio directories (include, lib or path). Return |
| 96 | a list of strings; will be empty list if unable to access the |
| 97 | registry or appropriate registry keys not found.""" |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 98 | |
Greg Ward | 7642f5c | 2000-03-31 16:47:40 +0000 | [diff] [blame] | 99 | if not _can_read_reg: |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 100 | return [] |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 101 | |
| 102 | L = [] |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 103 | if path=='lib': |
| 104 | path= 'Library' |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 105 | path = string.upper(path + ' Dirs') |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 106 | K = ('Software\\Microsoft\\Devstudio\\%s\\' + |
| 107 | 'Build System\\Components\\Platforms\\Win32 (%s)\\Directories') % \ |
| 108 | (version,platform) |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 109 | for base in (HKEY_CLASSES_ROOT, |
| 110 | HKEY_LOCAL_MACHINE, |
| 111 | HKEY_CURRENT_USER, |
| 112 | HKEY_USERS): |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 113 | try: |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 114 | k = RegOpenKeyEx(base,K) |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 115 | i = 0 |
| 116 | while 1: |
| 117 | try: |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 118 | (p,v,t) = RegEnumValue(k,i) |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 119 | if string.upper(p) == path: |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 120 | V = string.split(v,';') |
| 121 | for v in V: |
Thomas Heller | 745b460 | 2002-02-08 14:41:31 +0000 | [diff] [blame] | 122 | if hasattr(v, "encode"): |
| 123 | try: |
| 124 | v = v.encode("mbcs") |
| 125 | except UnicodeError: |
| 126 | pass |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 127 | if v == '' or v in L: continue |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 128 | L.append(v) |
| 129 | break |
| 130 | i = i + 1 |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 131 | except RegError: |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 132 | break |
Greg Ward | 1027e3f | 2000-03-31 16:53:42 +0000 | [diff] [blame] | 133 | except RegError: |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 134 | pass |
| 135 | return L |
| 136 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 137 | # get_msvc_paths() |
| 138 | |
| 139 | |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 140 | def find_exe (exe, version_number): |
| 141 | """Try to find an MSVC executable program 'exe' (from version |
| 142 | 'version_number' of MSVC) in several places: first, one of the MSVC |
| 143 | program search paths from the registry; next, the directories in the |
| 144 | PATH environment variable. If any of those work, return an absolute |
| 145 | path that is known to exist. If none of them work, just return the |
| 146 | original program name, 'exe'.""" |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 147 | |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 148 | for p in get_msvc_paths ('path', version_number): |
| 149 | fn = os.path.join (os.path.abspath(p), exe) |
| 150 | if os.path.isfile(fn): |
| 151 | return fn |
| 152 | |
| 153 | # didn't find it; try existing path |
| 154 | for p in string.split (os.environ['Path'],';'): |
| 155 | fn = os.path.join(os.path.abspath(p),exe) |
| 156 | if os.path.isfile(fn): |
| 157 | return fn |
| 158 | |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 159 | return exe # last desperate hope |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 160 | |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 161 | |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 162 | def set_path_env_var (name, version_number): |
| 163 | """Set environment variable 'name' to an MSVC path type value obtained |
| 164 | from 'get_msvc_paths()'. This is equivalent to a SET command prior |
| 165 | to execution of spawned commands.""" |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 166 | |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 167 | p = get_msvc_paths (name, version_number) |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 168 | if p: |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 169 | os.environ[name] = string.join (p,';') |
Greg Ward | 62e3393 | 2000-02-10 02:52:42 +0000 | [diff] [blame] | 170 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 171 | |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 172 | class MSVCCompiler (CCompiler) : |
| 173 | """Concrete class that implements an interface to Microsoft Visual C++, |
| 174 | as defined by the CCompiler abstract class.""" |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 175 | |
Greg Ward | df178f9 | 1999-09-29 12:29:10 +0000 | [diff] [blame] | 176 | compiler_type = 'msvc' |
| 177 | |
Greg Ward | 992c8f9 | 2000-06-25 02:31:16 +0000 | [diff] [blame] | 178 | # Just set this so CCompiler's constructor doesn't barf. We currently |
| 179 | # don't use the 'set_executables()' bureaucracy provided by CCompiler, |
| 180 | # as it really isn't necessary for this sort of single-compiler class. |
| 181 | # Would be nice to have a consistent interface with UnixCCompiler, |
| 182 | # though, so it's worth thinking about. |
| 183 | executables = {} |
| 184 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 185 | # Private class data (need to distinguish C from C++ source for compiler) |
| 186 | _c_extensions = ['.c'] |
Greg Ward | 408e9ae | 2000-08-30 17:32:24 +0000 | [diff] [blame] | 187 | _cpp_extensions = ['.cc', '.cpp', '.cxx'] |
Greg Ward | 9c0ea13 | 2000-09-19 23:56:43 +0000 | [diff] [blame] | 188 | _rc_extensions = ['.rc'] |
| 189 | _mc_extensions = ['.mc'] |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 190 | |
| 191 | # Needed for the filename generation methods provided by the |
| 192 | # base class, CCompiler. |
Greg Ward | 9c0ea13 | 2000-09-19 23:56:43 +0000 | [diff] [blame] | 193 | src_extensions = (_c_extensions + _cpp_extensions + |
| 194 | _rc_extensions + _mc_extensions) |
| 195 | res_extension = '.res' |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 196 | obj_extension = '.obj' |
| 197 | static_lib_extension = '.lib' |
| 198 | shared_lib_extension = '.dll' |
| 199 | static_lib_format = shared_lib_format = '%s%s' |
| 200 | exe_extension = '.exe' |
| 201 | |
| 202 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 203 | def __init__ (self, |
| 204 | verbose=0, |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 205 | dry_run=0, |
| 206 | force=0): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 207 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 208 | CCompiler.__init__ (self, verbose, dry_run, force) |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 209 | versions = get_devstudio_versions () |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 210 | |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 211 | if versions: |
| 212 | version = versions[0] # highest version |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 213 | |
Greg Ward | 41b4dd6 | 2000-03-29 04:13:00 +0000 | [diff] [blame] | 214 | self.cc = find_exe("cl.exe", version) |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 215 | self.linker = find_exe("link.exe", version) |
Greg Ward | 41b4dd6 | 2000-03-29 04:13:00 +0000 | [diff] [blame] | 216 | self.lib = find_exe("lib.exe", version) |
Greg Ward | 9c0ea13 | 2000-09-19 23:56:43 +0000 | [diff] [blame] | 217 | self.rc = find_exe("rc.exe", version) # resource compiler |
| 218 | self.mc = find_exe("mc.exe", version) # message compiler |
Greg Ward | 5de8cee | 2000-02-11 02:52:39 +0000 | [diff] [blame] | 219 | set_path_env_var ('lib', version) |
| 220 | set_path_env_var ('include', version) |
| 221 | path=get_msvc_paths('path', version) |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 222 | try: |
| 223 | for p in string.split(os.environ['path'],';'): |
| 224 | path.append(p) |
| 225 | except KeyError: |
| 226 | pass |
| 227 | os.environ['path'] = string.join(path,';') |
| 228 | else: |
| 229 | # devstudio not found in the registry |
| 230 | self.cc = "cl.exe" |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 231 | self.linker = "link.exe" |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame] | 232 | self.lib = "lib.exe" |
Greg Ward | 9c0ea13 | 2000-09-19 23:56:43 +0000 | [diff] [blame] | 233 | self.rc = "rc.exe" |
| 234 | self.mc = "mc.exe" |
Greg Ward | 6998809 | 2000-02-11 02:47:15 +0000 | [diff] [blame] | 235 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 236 | self.preprocess_options = None |
Jeremy Hylton | 2683ac7 | 2002-06-18 19:08:40 +0000 | [diff] [blame] | 237 | self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', '/GX' , |
| 238 | '/DNDEBUG'] |
Greg Ward | 8a98cd9 | 2000-08-31 00:31:07 +0000 | [diff] [blame] | 239 | self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GX', |
| 240 | '/Z7', '/D_DEBUG'] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 241 | |
Greg Ward | 1b9c6f7 | 2000-02-08 02:39:44 +0000 | [diff] [blame] | 242 | self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO'] |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 243 | self.ldflags_shared_debug = [ |
| 244 | '/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG' |
| 245 | ] |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 246 | self.ldflags_static = [ '/nologo'] |
| 247 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 248 | |
| 249 | # -- Worker methods ------------------------------------------------ |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 250 | |
Greg Ward | 9c0ea13 | 2000-09-19 23:56:43 +0000 | [diff] [blame] | 251 | def object_filenames (self, |
| 252 | source_filenames, |
| 253 | strip_dir=0, |
| 254 | output_dir=''): |
| 255 | # Copied from ccompiler.py, extended to return .res as 'object'-file |
| 256 | # for .rc input file |
| 257 | if output_dir is None: output_dir = '' |
| 258 | obj_names = [] |
| 259 | for src_name in source_filenames: |
| 260 | (base, ext) = os.path.splitext (src_name) |
| 261 | if ext not in self.src_extensions: |
| 262 | # Better to raise an exception instead of silently continuing |
| 263 | # and later complain about sources and targets having |
| 264 | # different lengths |
| 265 | raise CompileError ("Don't know how to compile %s" % src_name) |
| 266 | if strip_dir: |
| 267 | base = os.path.basename (base) |
| 268 | if ext in self._rc_extensions: |
| 269 | obj_names.append (os.path.join (output_dir, |
| 270 | base + self.res_extension)) |
| 271 | elif ext in self._mc_extensions: |
| 272 | obj_names.append (os.path.join (output_dir, |
| 273 | base + self.res_extension)) |
| 274 | else: |
| 275 | obj_names.append (os.path.join (output_dir, |
| 276 | base + self.obj_extension)) |
| 277 | return obj_names |
| 278 | |
| 279 | # object_filenames () |
| 280 | |
| 281 | |
Jeremy Hylton | 1bba31d | 2002-06-13 17:28:18 +0000 | [diff] [blame] | 282 | def compile(self, sources, |
| 283 | output_dir=None, macros=None, include_dirs=None, debug=0, |
| 284 | extra_preargs=None, extra_postargs=None, depends=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 285 | |
Jeremy Hylton | 1bba31d | 2002-06-13 17:28:18 +0000 | [diff] [blame] | 286 | macros, objects, extra_postargs, pp_opts, build = \ |
| 287 | self._setup_compile(output_dir, macros, include_dirs, sources, |
| 288 | depends, extra_postargs) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 289 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 290 | compile_opts = extra_preargs or [] |
| 291 | compile_opts.append ('/c') |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 292 | if debug: |
Jeremy Hylton | 1bba31d | 2002-06-13 17:28:18 +0000 | [diff] [blame] | 293 | compile_opts.extend(self.compile_options_debug) |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 294 | else: |
Jeremy Hylton | 1bba31d | 2002-06-13 17:28:18 +0000 | [diff] [blame] | 295 | compile_opts.extend(self.compile_options) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 296 | |
Jeremy Hylton | 1bba31d | 2002-06-13 17:28:18 +0000 | [diff] [blame] | 297 | for obj, (src, ext) in build.items(): |
| 298 | if debug: |
| 299 | # pass the full pathname to MSVC in debug mode, |
| 300 | # this allows the debugger to find the source file |
| 301 | # without asking the user to browse for it |
| 302 | src = os.path.abspath(src) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 303 | |
Jeremy Hylton | 1bba31d | 2002-06-13 17:28:18 +0000 | [diff] [blame] | 304 | if ext in self._c_extensions: |
| 305 | input_opt = "/Tc" + src |
| 306 | elif ext in self._cpp_extensions: |
| 307 | input_opt = "/Tp" + src |
| 308 | elif ext in self._rc_extensions: |
| 309 | # compile .RC to .RES file |
| 310 | input_opt = src |
| 311 | output_opt = "/fo" + obj |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 312 | try: |
Jeremy Hylton | 1bba31d | 2002-06-13 17:28:18 +0000 | [diff] [blame] | 313 | self.spawn ([self.rc] + |
| 314 | [output_opt] + [input_opt]) |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 315 | except DistutilsExecError, msg: |
| 316 | raise CompileError, msg |
Jeremy Hylton | 1bba31d | 2002-06-13 17:28:18 +0000 | [diff] [blame] | 317 | continue |
| 318 | elif ext in self._mc_extensions: |
| 319 | |
| 320 | # Compile .MC to .RC file to .RES file. |
| 321 | # * '-h dir' specifies the directory for the |
| 322 | # generated include file |
| 323 | # * '-r dir' specifies the target directory of the |
| 324 | # generated RC file and the binary message resource |
| 325 | # it includes |
| 326 | # |
| 327 | # For now (since there are no options to change this), |
| 328 | # we use the source-directory for the include file and |
| 329 | # the build directory for the RC file and message |
| 330 | # resources. This works at least for win32all. |
| 331 | |
| 332 | h_dir = os.path.dirname (src) |
| 333 | rc_dir = os.path.dirname (obj) |
| 334 | try: |
| 335 | # first compile .MC to .RC and .H file |
| 336 | self.spawn ([self.mc] + |
| 337 | ['-h', h_dir, '-r', rc_dir] + [src]) |
| 338 | base, _ = os.path.splitext (os.path.basename (src)) |
| 339 | rc_file = os.path.join (rc_dir, base + '.rc') |
| 340 | # then compile .RC to .RES file |
| 341 | self.spawn ([self.rc] + |
| 342 | ["/fo" + obj] + [rc_file]) |
| 343 | |
| 344 | except DistutilsExecError, msg: |
| 345 | raise CompileError, msg |
| 346 | continue |
| 347 | else: |
| 348 | # how to handle this file? |
| 349 | raise CompileError ( |
| 350 | "Don't know how to compile %s to %s" % \ |
| 351 | (src, obj)) |
| 352 | |
| 353 | output_opt = "/Fo" + obj |
| 354 | try: |
| 355 | self.spawn ([self.cc] + compile_opts + pp_opts + |
| 356 | [input_opt, output_opt] + |
| 357 | extra_postargs) |
| 358 | except DistutilsExecError, msg: |
| 359 | raise CompileError, msg |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 360 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 361 | return objects |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 362 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 363 | # compile () |
Greg Ward | 3d50b90 | 1999-09-08 02:36:01 +0000 | [diff] [blame] | 364 | |
| 365 | |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame] | 366 | def create_static_lib (self, |
| 367 | objects, |
| 368 | output_libname, |
| 369 | output_dir=None, |
| 370 | debug=0, |
Gustavo Niemeyer | 6b01685 | 2002-11-05 16:12:02 +0000 | [diff] [blame] | 371 | target_lang=None): |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 372 | |
Greg Ward | 2f557a2 | 2000-03-26 21:42:28 +0000 | [diff] [blame] | 373 | (objects, output_dir) = self._fix_object_args (objects, output_dir) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 374 | output_filename = \ |
| 375 | self.library_filename (output_libname, output_dir=output_dir) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 376 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 377 | if self._need_link (objects, output_filename): |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame] | 378 | lib_args = objects + ['/OUT:' + output_filename] |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 379 | if debug: |
| 380 | pass # XXX what goes here? |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 381 | try: |
Greg Ward | 992c8f9 | 2000-06-25 02:31:16 +0000 | [diff] [blame] | 382 | self.spawn ([self.lib] + lib_args) |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 383 | except DistutilsExecError, msg: |
| 384 | raise LibError, msg |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 385 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 386 | else: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 387 | log.debug("skipping %s (up-to-date)", output_filename) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 388 | |
Greg Ward | 09fc542 | 2000-03-10 01:49:26 +0000 | [diff] [blame] | 389 | # create_static_lib () |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 390 | |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 391 | def link (self, |
| 392 | target_desc, |
| 393 | objects, |
| 394 | output_filename, |
| 395 | output_dir=None, |
| 396 | libraries=None, |
| 397 | library_dirs=None, |
| 398 | runtime_library_dirs=None, |
| 399 | export_symbols=None, |
| 400 | debug=0, |
| 401 | extra_preargs=None, |
| 402 | extra_postargs=None, |
Gustavo Niemeyer | 6b01685 | 2002-11-05 16:12:02 +0000 | [diff] [blame] | 403 | build_temp=None, |
| 404 | target_lang=None): |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 405 | |
Greg Ward | 2f557a2 | 2000-03-26 21:42:28 +0000 | [diff] [blame] | 406 | (objects, output_dir) = self._fix_object_args (objects, output_dir) |
| 407 | (libraries, library_dirs, runtime_library_dirs) = \ |
| 408 | self._fix_lib_args (libraries, library_dirs, runtime_library_dirs) |
| 409 | |
Greg Ward | f70c603 | 2000-04-19 02:16:49 +0000 | [diff] [blame] | 410 | if runtime_library_dirs: |
Greg Ward | 2f557a2 | 2000-03-26 21:42:28 +0000 | [diff] [blame] | 411 | self.warn ("I don't know what to do with 'runtime_library_dirs': " |
| 412 | + str (runtime_library_dirs)) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 413 | |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 414 | lib_opts = gen_lib_options (self, |
Greg Ward | 2f557a2 | 2000-03-26 21:42:28 +0000 | [diff] [blame] | 415 | library_dirs, runtime_library_dirs, |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 416 | libraries) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 417 | if output_dir is not None: |
| 418 | output_filename = os.path.join (output_dir, output_filename) |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 419 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 420 | if self._need_link (objects, output_filename): |
| 421 | |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 422 | if target_desc == CCompiler.EXECUTABLE: |
| 423 | if debug: |
| 424 | ldflags = self.ldflags_shared_debug[1:] |
| 425 | else: |
| 426 | ldflags = self.ldflags_shared[1:] |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 427 | else: |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 428 | if debug: |
| 429 | ldflags = self.ldflags_shared_debug |
| 430 | else: |
| 431 | ldflags = self.ldflags_shared |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 432 | |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 433 | export_opts = [] |
| 434 | for sym in (export_symbols or []): |
| 435 | export_opts.append("/EXPORT:" + sym) |
| 436 | |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 437 | ld_args = (ldflags + lib_opts + export_opts + |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 438 | objects + ['/OUT:' + output_filename]) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 439 | |
Greg Ward | 159eb92 | 2000-08-02 00:00:30 +0000 | [diff] [blame] | 440 | # The MSVC linker generates .lib and .exp files, which cannot be |
| 441 | # suppressed by any linker switches. The .lib files may even be |
| 442 | # needed! Make sure they are generated in the temporary build |
| 443 | # directory. Since they have different names for debug and release |
| 444 | # builds, they can go into the same directory. |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 445 | if export_symbols is not None: |
| 446 | (dll_name, dll_ext) = os.path.splitext( |
| 447 | os.path.basename(output_filename)) |
| 448 | implib_file = os.path.join( |
| 449 | os.path.dirname(objects[0]), |
| 450 | self.library_filename(dll_name)) |
| 451 | ld_args.append ('/IMPLIB:' + implib_file) |
Greg Ward | 159eb92 | 2000-08-02 00:00:30 +0000 | [diff] [blame] | 452 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 453 | if extra_preargs: |
| 454 | ld_args[:0] = extra_preargs |
| 455 | if extra_postargs: |
Greg Ward | 159eb92 | 2000-08-02 00:00:30 +0000 | [diff] [blame] | 456 | ld_args.extend(extra_postargs) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 457 | |
| 458 | self.mkpath (os.path.dirname (output_filename)) |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 459 | try: |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 460 | self.spawn ([self.linker] + ld_args) |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 461 | except DistutilsExecError, msg: |
| 462 | raise LinkError, msg |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 463 | |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 464 | else: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 465 | log.debug("skipping %s (up-to-date)", output_filename) |
Greg Ward | 4ba9b2e | 2000-02-10 02:15:52 +0000 | [diff] [blame] | 466 | |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 467 | # link () |
Greg Ward | f70c603 | 2000-04-19 02:16:49 +0000 | [diff] [blame] | 468 | |
| 469 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 470 | # -- Miscellaneous methods ----------------------------------------- |
| 471 | # These are all used by the 'gen_lib_options() function, in |
| 472 | # ccompiler.py. |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 473 | |
| 474 | def library_dir_option (self, dir): |
| 475 | return "/LIBPATH:" + dir |
| 476 | |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 477 | def runtime_library_dir_option (self, dir): |
| 478 | raise DistutilsPlatformError, \ |
| 479 | "don't know how to set runtime library search path for MSVC++" |
| 480 | |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 481 | def library_option (self, lib): |
| 482 | return self.library_filename (lib) |
| 483 | |
| 484 | |
Greg Ward | d142564 | 2000-08-04 01:29:27 +0000 | [diff] [blame] | 485 | def find_library_file (self, dirs, lib, debug=0): |
| 486 | # Prefer a debugging library if found (and requested), but deal |
| 487 | # with it if we don't have one. |
| 488 | if debug: |
| 489 | try_names = [lib + "_d", lib] |
| 490 | else: |
| 491 | try_names = [lib] |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 492 | for dir in dirs: |
Greg Ward | d142564 | 2000-08-04 01:29:27 +0000 | [diff] [blame] | 493 | for name in try_names: |
| 494 | libfile = os.path.join(dir, self.library_filename (name)) |
| 495 | if os.path.exists(libfile): |
| 496 | return libfile |
Greg Ward | c74138d | 1999-10-03 20:47:52 +0000 | [diff] [blame] | 497 | else: |
| 498 | # Oops, didn't find it in *any* of 'dirs' |
| 499 | return None |
| 500 | |
| 501 | # find_library_file () |
| 502 | |
Greg Ward | dbd1276 | 1999-08-29 18:15:07 +0000 | [diff] [blame] | 503 | # class MSVCCompiler |