1 | n/a | """distutils.msvc9compiler |
---|
2 | n/a | |
---|
3 | n/a | Contains MSVCCompiler, an implementation of the abstract CCompiler class |
---|
4 | n/a | for the Microsoft Visual Studio 2008. |
---|
5 | n/a | |
---|
6 | n/a | The module is compatible with VS 2005 and VS 2008. You can find legacy support |
---|
7 | n/a | for older versions of VS in distutils.msvccompiler. |
---|
8 | n/a | """ |
---|
9 | n/a | |
---|
10 | n/a | # Written by Perry Stoll |
---|
11 | n/a | # hacked by Robin Becker and Thomas Heller to do a better job of |
---|
12 | n/a | # finding DevStudio (through the registry) |
---|
13 | n/a | # ported to VS2005 and VS 2008 by Christian Heimes |
---|
14 | n/a | |
---|
15 | n/a | import os |
---|
16 | n/a | import subprocess |
---|
17 | n/a | import sys |
---|
18 | n/a | import re |
---|
19 | n/a | |
---|
20 | n/a | from distutils.errors import DistutilsExecError, DistutilsPlatformError, \ |
---|
21 | n/a | CompileError, LibError, LinkError |
---|
22 | n/a | from distutils.ccompiler import CCompiler, gen_preprocess_options, \ |
---|
23 | n/a | gen_lib_options |
---|
24 | n/a | from distutils import log |
---|
25 | n/a | from distutils.util import get_platform |
---|
26 | n/a | |
---|
27 | n/a | import winreg |
---|
28 | n/a | |
---|
29 | n/a | RegOpenKeyEx = winreg.OpenKeyEx |
---|
30 | n/a | RegEnumKey = winreg.EnumKey |
---|
31 | n/a | RegEnumValue = winreg.EnumValue |
---|
32 | n/a | RegError = winreg.error |
---|
33 | n/a | |
---|
34 | n/a | HKEYS = (winreg.HKEY_USERS, |
---|
35 | n/a | winreg.HKEY_CURRENT_USER, |
---|
36 | n/a | winreg.HKEY_LOCAL_MACHINE, |
---|
37 | n/a | winreg.HKEY_CLASSES_ROOT) |
---|
38 | n/a | |
---|
39 | n/a | NATIVE_WIN64 = (sys.platform == 'win32' and sys.maxsize > 2**32) |
---|
40 | n/a | if NATIVE_WIN64: |
---|
41 | n/a | # Visual C++ is a 32-bit application, so we need to look in |
---|
42 | n/a | # the corresponding registry branch, if we're running a |
---|
43 | n/a | # 64-bit Python on Win64 |
---|
44 | n/a | VS_BASE = r"Software\Wow6432Node\Microsoft\VisualStudio\%0.1f" |
---|
45 | n/a | WINSDK_BASE = r"Software\Wow6432Node\Microsoft\Microsoft SDKs\Windows" |
---|
46 | n/a | NET_BASE = r"Software\Wow6432Node\Microsoft\.NETFramework" |
---|
47 | n/a | else: |
---|
48 | n/a | VS_BASE = r"Software\Microsoft\VisualStudio\%0.1f" |
---|
49 | n/a | WINSDK_BASE = r"Software\Microsoft\Microsoft SDKs\Windows" |
---|
50 | n/a | NET_BASE = r"Software\Microsoft\.NETFramework" |
---|
51 | n/a | |
---|
52 | n/a | # A map keyed by get_platform() return values to values accepted by |
---|
53 | n/a | # 'vcvarsall.bat'. Note a cross-compile may combine these (eg, 'x86_amd64' is |
---|
54 | n/a | # the param to cross-compile on x86 targeting amd64.) |
---|
55 | n/a | PLAT_TO_VCVARS = { |
---|
56 | n/a | 'win32' : 'x86', |
---|
57 | n/a | 'win-amd64' : 'amd64', |
---|
58 | n/a | 'win-ia64' : 'ia64', |
---|
59 | n/a | } |
---|
60 | n/a | |
---|
61 | n/a | class Reg: |
---|
62 | n/a | """Helper class to read values from the registry |
---|
63 | n/a | """ |
---|
64 | n/a | |
---|
65 | n/a | def get_value(cls, path, key): |
---|
66 | n/a | for base in HKEYS: |
---|
67 | n/a | d = cls.read_values(base, path) |
---|
68 | n/a | if d and key in d: |
---|
69 | n/a | return d[key] |
---|
70 | n/a | raise KeyError(key) |
---|
71 | n/a | get_value = classmethod(get_value) |
---|
72 | n/a | |
---|
73 | n/a | def read_keys(cls, base, key): |
---|
74 | n/a | """Return list of registry keys.""" |
---|
75 | n/a | try: |
---|
76 | n/a | handle = RegOpenKeyEx(base, key) |
---|
77 | n/a | except RegError: |
---|
78 | n/a | return None |
---|
79 | n/a | L = [] |
---|
80 | n/a | i = 0 |
---|
81 | n/a | while True: |
---|
82 | n/a | try: |
---|
83 | n/a | k = RegEnumKey(handle, i) |
---|
84 | n/a | except RegError: |
---|
85 | n/a | break |
---|
86 | n/a | L.append(k) |
---|
87 | n/a | i += 1 |
---|
88 | n/a | return L |
---|
89 | n/a | read_keys = classmethod(read_keys) |
---|
90 | n/a | |
---|
91 | n/a | def read_values(cls, base, key): |
---|
92 | n/a | """Return dict of registry keys and values. |
---|
93 | n/a | |
---|
94 | n/a | All names are converted to lowercase. |
---|
95 | n/a | """ |
---|
96 | n/a | try: |
---|
97 | n/a | handle = RegOpenKeyEx(base, key) |
---|
98 | n/a | except RegError: |
---|
99 | n/a | return None |
---|
100 | n/a | d = {} |
---|
101 | n/a | i = 0 |
---|
102 | n/a | while True: |
---|
103 | n/a | try: |
---|
104 | n/a | name, value, type = RegEnumValue(handle, i) |
---|
105 | n/a | except RegError: |
---|
106 | n/a | break |
---|
107 | n/a | name = name.lower() |
---|
108 | n/a | d[cls.convert_mbcs(name)] = cls.convert_mbcs(value) |
---|
109 | n/a | i += 1 |
---|
110 | n/a | return d |
---|
111 | n/a | read_values = classmethod(read_values) |
---|
112 | n/a | |
---|
113 | n/a | def convert_mbcs(s): |
---|
114 | n/a | dec = getattr(s, "decode", None) |
---|
115 | n/a | if dec is not None: |
---|
116 | n/a | try: |
---|
117 | n/a | s = dec("mbcs") |
---|
118 | n/a | except UnicodeError: |
---|
119 | n/a | pass |
---|
120 | n/a | return s |
---|
121 | n/a | convert_mbcs = staticmethod(convert_mbcs) |
---|
122 | n/a | |
---|
123 | n/a | class MacroExpander: |
---|
124 | n/a | |
---|
125 | n/a | def __init__(self, version): |
---|
126 | n/a | self.macros = {} |
---|
127 | n/a | self.vsbase = VS_BASE % version |
---|
128 | n/a | self.load_macros(version) |
---|
129 | n/a | |
---|
130 | n/a | def set_macro(self, macro, path, key): |
---|
131 | n/a | self.macros["$(%s)" % macro] = Reg.get_value(path, key) |
---|
132 | n/a | |
---|
133 | n/a | def load_macros(self, version): |
---|
134 | n/a | self.set_macro("VCInstallDir", self.vsbase + r"\Setup\VC", "productdir") |
---|
135 | n/a | self.set_macro("VSInstallDir", self.vsbase + r"\Setup\VS", "productdir") |
---|
136 | n/a | self.set_macro("FrameworkDir", NET_BASE, "installroot") |
---|
137 | n/a | try: |
---|
138 | n/a | if version >= 8.0: |
---|
139 | n/a | self.set_macro("FrameworkSDKDir", NET_BASE, |
---|
140 | n/a | "sdkinstallrootv2.0") |
---|
141 | n/a | else: |
---|
142 | n/a | raise KeyError("sdkinstallrootv2.0") |
---|
143 | n/a | except KeyError: |
---|
144 | n/a | raise DistutilsPlatformError( |
---|
145 | n/a | """Python was built with Visual Studio 2008; |
---|
146 | n/a | extensions must be built with a compiler than can generate compatible binaries. |
---|
147 | n/a | Visual Studio 2008 was not found on this system. If you have Cygwin installed, |
---|
148 | n/a | you can try compiling with MingW32, by passing "-c mingw32" to setup.py.""") |
---|
149 | n/a | |
---|
150 | n/a | if version >= 9.0: |
---|
151 | n/a | self.set_macro("FrameworkVersion", self.vsbase, "clr version") |
---|
152 | n/a | self.set_macro("WindowsSdkDir", WINSDK_BASE, "currentinstallfolder") |
---|
153 | n/a | else: |
---|
154 | n/a | p = r"Software\Microsoft\NET Framework Setup\Product" |
---|
155 | n/a | for base in HKEYS: |
---|
156 | n/a | try: |
---|
157 | n/a | h = RegOpenKeyEx(base, p) |
---|
158 | n/a | except RegError: |
---|
159 | n/a | continue |
---|
160 | n/a | key = RegEnumKey(h, 0) |
---|
161 | n/a | d = Reg.get_value(base, r"%s\%s" % (p, key)) |
---|
162 | n/a | self.macros["$(FrameworkVersion)"] = d["version"] |
---|
163 | n/a | |
---|
164 | n/a | def sub(self, s): |
---|
165 | n/a | for k, v in self.macros.items(): |
---|
166 | n/a | s = s.replace(k, v) |
---|
167 | n/a | return s |
---|
168 | n/a | |
---|
169 | n/a | def get_build_version(): |
---|
170 | n/a | """Return the version of MSVC that was used to build Python. |
---|
171 | n/a | |
---|
172 | n/a | For Python 2.3 and up, the version number is included in |
---|
173 | n/a | sys.version. For earlier versions, assume the compiler is MSVC 6. |
---|
174 | n/a | """ |
---|
175 | n/a | prefix = "MSC v." |
---|
176 | n/a | i = sys.version.find(prefix) |
---|
177 | n/a | if i == -1: |
---|
178 | n/a | return 6 |
---|
179 | n/a | i = i + len(prefix) |
---|
180 | n/a | s, rest = sys.version[i:].split(" ", 1) |
---|
181 | n/a | majorVersion = int(s[:-2]) - 6 |
---|
182 | n/a | if majorVersion >= 13: |
---|
183 | n/a | # v13 was skipped and should be v14 |
---|
184 | n/a | majorVersion += 1 |
---|
185 | n/a | minorVersion = int(s[2:3]) / 10.0 |
---|
186 | n/a | # I don't think paths are affected by minor version in version 6 |
---|
187 | n/a | if majorVersion == 6: |
---|
188 | n/a | minorVersion = 0 |
---|
189 | n/a | if majorVersion >= 6: |
---|
190 | n/a | return majorVersion + minorVersion |
---|
191 | n/a | # else we don't know what version of the compiler this is |
---|
192 | n/a | return None |
---|
193 | n/a | |
---|
194 | n/a | def normalize_and_reduce_paths(paths): |
---|
195 | n/a | """Return a list of normalized paths with duplicates removed. |
---|
196 | n/a | |
---|
197 | n/a | The current order of paths is maintained. |
---|
198 | n/a | """ |
---|
199 | n/a | # Paths are normalized so things like: /a and /a/ aren't both preserved. |
---|
200 | n/a | reduced_paths = [] |
---|
201 | n/a | for p in paths: |
---|
202 | n/a | np = os.path.normpath(p) |
---|
203 | n/a | # XXX(nnorwitz): O(n**2), if reduced_paths gets long perhaps use a set. |
---|
204 | n/a | if np not in reduced_paths: |
---|
205 | n/a | reduced_paths.append(np) |
---|
206 | n/a | return reduced_paths |
---|
207 | n/a | |
---|
208 | n/a | def removeDuplicates(variable): |
---|
209 | n/a | """Remove duplicate values of an environment variable. |
---|
210 | n/a | """ |
---|
211 | n/a | oldList = variable.split(os.pathsep) |
---|
212 | n/a | newList = [] |
---|
213 | n/a | for i in oldList: |
---|
214 | n/a | if i not in newList: |
---|
215 | n/a | newList.append(i) |
---|
216 | n/a | newVariable = os.pathsep.join(newList) |
---|
217 | n/a | return newVariable |
---|
218 | n/a | |
---|
219 | n/a | def find_vcvarsall(version): |
---|
220 | n/a | """Find the vcvarsall.bat file |
---|
221 | n/a | |
---|
222 | n/a | At first it tries to find the productdir of VS 2008 in the registry. If |
---|
223 | n/a | that fails it falls back to the VS90COMNTOOLS env var. |
---|
224 | n/a | """ |
---|
225 | n/a | vsbase = VS_BASE % version |
---|
226 | n/a | try: |
---|
227 | n/a | productdir = Reg.get_value(r"%s\Setup\VC" % vsbase, |
---|
228 | n/a | "productdir") |
---|
229 | n/a | except KeyError: |
---|
230 | n/a | log.debug("Unable to find productdir in registry") |
---|
231 | n/a | productdir = None |
---|
232 | n/a | |
---|
233 | n/a | if not productdir or not os.path.isdir(productdir): |
---|
234 | n/a | toolskey = "VS%0.f0COMNTOOLS" % version |
---|
235 | n/a | toolsdir = os.environ.get(toolskey, None) |
---|
236 | n/a | |
---|
237 | n/a | if toolsdir and os.path.isdir(toolsdir): |
---|
238 | n/a | productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC") |
---|
239 | n/a | productdir = os.path.abspath(productdir) |
---|
240 | n/a | if not os.path.isdir(productdir): |
---|
241 | n/a | log.debug("%s is not a valid directory" % productdir) |
---|
242 | n/a | return None |
---|
243 | n/a | else: |
---|
244 | n/a | log.debug("Env var %s is not set or invalid" % toolskey) |
---|
245 | n/a | if not productdir: |
---|
246 | n/a | log.debug("No productdir found") |
---|
247 | n/a | return None |
---|
248 | n/a | vcvarsall = os.path.join(productdir, "vcvarsall.bat") |
---|
249 | n/a | if os.path.isfile(vcvarsall): |
---|
250 | n/a | return vcvarsall |
---|
251 | n/a | log.debug("Unable to find vcvarsall.bat") |
---|
252 | n/a | return None |
---|
253 | n/a | |
---|
254 | n/a | def query_vcvarsall(version, arch="x86"): |
---|
255 | n/a | """Launch vcvarsall.bat and read the settings from its environment |
---|
256 | n/a | """ |
---|
257 | n/a | vcvarsall = find_vcvarsall(version) |
---|
258 | n/a | interesting = set(("include", "lib", "libpath", "path")) |
---|
259 | n/a | result = {} |
---|
260 | n/a | |
---|
261 | n/a | if vcvarsall is None: |
---|
262 | n/a | raise DistutilsPlatformError("Unable to find vcvarsall.bat") |
---|
263 | n/a | log.debug("Calling 'vcvarsall.bat %s' (version=%s)", arch, version) |
---|
264 | n/a | popen = subprocess.Popen('"%s" %s & set' % (vcvarsall, arch), |
---|
265 | n/a | stdout=subprocess.PIPE, |
---|
266 | n/a | stderr=subprocess.PIPE) |
---|
267 | n/a | try: |
---|
268 | n/a | stdout, stderr = popen.communicate() |
---|
269 | n/a | if popen.wait() != 0: |
---|
270 | n/a | raise DistutilsPlatformError(stderr.decode("mbcs")) |
---|
271 | n/a | |
---|
272 | n/a | stdout = stdout.decode("mbcs") |
---|
273 | n/a | for line in stdout.split("\n"): |
---|
274 | n/a | line = Reg.convert_mbcs(line) |
---|
275 | n/a | if '=' not in line: |
---|
276 | n/a | continue |
---|
277 | n/a | line = line.strip() |
---|
278 | n/a | key, value = line.split('=', 1) |
---|
279 | n/a | key = key.lower() |
---|
280 | n/a | if key in interesting: |
---|
281 | n/a | if value.endswith(os.pathsep): |
---|
282 | n/a | value = value[:-1] |
---|
283 | n/a | result[key] = removeDuplicates(value) |
---|
284 | n/a | |
---|
285 | n/a | finally: |
---|
286 | n/a | popen.stdout.close() |
---|
287 | n/a | popen.stderr.close() |
---|
288 | n/a | |
---|
289 | n/a | if len(result) != len(interesting): |
---|
290 | n/a | raise ValueError(str(list(result.keys()))) |
---|
291 | n/a | |
---|
292 | n/a | return result |
---|
293 | n/a | |
---|
294 | n/a | # More globals |
---|
295 | n/a | VERSION = get_build_version() |
---|
296 | n/a | if VERSION < 8.0: |
---|
297 | n/a | raise DistutilsPlatformError("VC %0.1f is not supported by this module" % VERSION) |
---|
298 | n/a | # MACROS = MacroExpander(VERSION) |
---|
299 | n/a | |
---|
300 | n/a | class MSVCCompiler(CCompiler) : |
---|
301 | n/a | """Concrete class that implements an interface to Microsoft Visual C++, |
---|
302 | n/a | as defined by the CCompiler abstract class.""" |
---|
303 | n/a | |
---|
304 | n/a | compiler_type = 'msvc' |
---|
305 | n/a | |
---|
306 | n/a | # Just set this so CCompiler's constructor doesn't barf. We currently |
---|
307 | n/a | # don't use the 'set_executables()' bureaucracy provided by CCompiler, |
---|
308 | n/a | # as it really isn't necessary for this sort of single-compiler class. |
---|
309 | n/a | # Would be nice to have a consistent interface with UnixCCompiler, |
---|
310 | n/a | # though, so it's worth thinking about. |
---|
311 | n/a | executables = {} |
---|
312 | n/a | |
---|
313 | n/a | # Private class data (need to distinguish C from C++ source for compiler) |
---|
314 | n/a | _c_extensions = ['.c'] |
---|
315 | n/a | _cpp_extensions = ['.cc', '.cpp', '.cxx'] |
---|
316 | n/a | _rc_extensions = ['.rc'] |
---|
317 | n/a | _mc_extensions = ['.mc'] |
---|
318 | n/a | |
---|
319 | n/a | # Needed for the filename generation methods provided by the |
---|
320 | n/a | # base class, CCompiler. |
---|
321 | n/a | src_extensions = (_c_extensions + _cpp_extensions + |
---|
322 | n/a | _rc_extensions + _mc_extensions) |
---|
323 | n/a | res_extension = '.res' |
---|
324 | n/a | obj_extension = '.obj' |
---|
325 | n/a | static_lib_extension = '.lib' |
---|
326 | n/a | shared_lib_extension = '.dll' |
---|
327 | n/a | static_lib_format = shared_lib_format = '%s%s' |
---|
328 | n/a | exe_extension = '.exe' |
---|
329 | n/a | |
---|
330 | n/a | def __init__(self, verbose=0, dry_run=0, force=0): |
---|
331 | n/a | CCompiler.__init__ (self, verbose, dry_run, force) |
---|
332 | n/a | self.__version = VERSION |
---|
333 | n/a | self.__root = r"Software\Microsoft\VisualStudio" |
---|
334 | n/a | # self.__macros = MACROS |
---|
335 | n/a | self.__paths = [] |
---|
336 | n/a | # target platform (.plat_name is consistent with 'bdist') |
---|
337 | n/a | self.plat_name = None |
---|
338 | n/a | self.__arch = None # deprecated name |
---|
339 | n/a | self.initialized = False |
---|
340 | n/a | |
---|
341 | n/a | def initialize(self, plat_name=None): |
---|
342 | n/a | # multi-init means we would need to check platform same each time... |
---|
343 | n/a | assert not self.initialized, "don't init multiple times" |
---|
344 | n/a | if plat_name is None: |
---|
345 | n/a | plat_name = get_platform() |
---|
346 | n/a | # sanity check for platforms to prevent obscure errors later. |
---|
347 | n/a | ok_plats = 'win32', 'win-amd64', 'win-ia64' |
---|
348 | n/a | if plat_name not in ok_plats: |
---|
349 | n/a | raise DistutilsPlatformError("--plat-name must be one of %s" % |
---|
350 | n/a | (ok_plats,)) |
---|
351 | n/a | |
---|
352 | n/a | if "DISTUTILS_USE_SDK" in os.environ and "MSSdk" in os.environ and self.find_exe("cl.exe"): |
---|
353 | n/a | # Assume that the SDK set up everything alright; don't try to be |
---|
354 | n/a | # smarter |
---|
355 | n/a | self.cc = "cl.exe" |
---|
356 | n/a | self.linker = "link.exe" |
---|
357 | n/a | self.lib = "lib.exe" |
---|
358 | n/a | self.rc = "rc.exe" |
---|
359 | n/a | self.mc = "mc.exe" |
---|
360 | n/a | else: |
---|
361 | n/a | # On x86, 'vcvars32.bat amd64' creates an env that doesn't work; |
---|
362 | n/a | # to cross compile, you use 'x86_amd64'. |
---|
363 | n/a | # On AMD64, 'vcvars32.bat amd64' is a native build env; to cross |
---|
364 | n/a | # compile use 'x86' (ie, it runs the x86 compiler directly) |
---|
365 | n/a | # No idea how itanium handles this, if at all. |
---|
366 | n/a | if plat_name == get_platform() or plat_name == 'win32': |
---|
367 | n/a | # native build or cross-compile to win32 |
---|
368 | n/a | plat_spec = PLAT_TO_VCVARS[plat_name] |
---|
369 | n/a | else: |
---|
370 | n/a | # cross compile from win32 -> some 64bit |
---|
371 | n/a | plat_spec = PLAT_TO_VCVARS[get_platform()] + '_' + \ |
---|
372 | n/a | PLAT_TO_VCVARS[plat_name] |
---|
373 | n/a | |
---|
374 | n/a | vc_env = query_vcvarsall(VERSION, plat_spec) |
---|
375 | n/a | |
---|
376 | n/a | self.__paths = vc_env['path'].split(os.pathsep) |
---|
377 | n/a | os.environ['lib'] = vc_env['lib'] |
---|
378 | n/a | os.environ['include'] = vc_env['include'] |
---|
379 | n/a | |
---|
380 | n/a | if len(self.__paths) == 0: |
---|
381 | n/a | raise DistutilsPlatformError("Python was built with %s, " |
---|
382 | n/a | "and extensions need to be built with the same " |
---|
383 | n/a | "version of the compiler, but it isn't installed." |
---|
384 | n/a | % self.__product) |
---|
385 | n/a | |
---|
386 | n/a | self.cc = self.find_exe("cl.exe") |
---|
387 | n/a | self.linker = self.find_exe("link.exe") |
---|
388 | n/a | self.lib = self.find_exe("lib.exe") |
---|
389 | n/a | self.rc = self.find_exe("rc.exe") # resource compiler |
---|
390 | n/a | self.mc = self.find_exe("mc.exe") # message compiler |
---|
391 | n/a | #self.set_path_env_var('lib') |
---|
392 | n/a | #self.set_path_env_var('include') |
---|
393 | n/a | |
---|
394 | n/a | # extend the MSVC path with the current path |
---|
395 | n/a | try: |
---|
396 | n/a | for p in os.environ['path'].split(';'): |
---|
397 | n/a | self.__paths.append(p) |
---|
398 | n/a | except KeyError: |
---|
399 | n/a | pass |
---|
400 | n/a | self.__paths = normalize_and_reduce_paths(self.__paths) |
---|
401 | n/a | os.environ['path'] = ";".join(self.__paths) |
---|
402 | n/a | |
---|
403 | n/a | self.preprocess_options = None |
---|
404 | n/a | if self.__arch == "x86": |
---|
405 | n/a | self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', |
---|
406 | n/a | '/DNDEBUG'] |
---|
407 | n/a | self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', |
---|
408 | n/a | '/Z7', '/D_DEBUG'] |
---|
409 | n/a | else: |
---|
410 | n/a | # Win64 |
---|
411 | n/a | self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', '/GS-' , |
---|
412 | n/a | '/DNDEBUG'] |
---|
413 | n/a | self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GS-', |
---|
414 | n/a | '/Z7', '/D_DEBUG'] |
---|
415 | n/a | |
---|
416 | n/a | self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO'] |
---|
417 | n/a | if self.__version >= 7: |
---|
418 | n/a | self.ldflags_shared_debug = [ |
---|
419 | n/a | '/DLL', '/nologo', '/INCREMENTAL:no', '/DEBUG' |
---|
420 | n/a | ] |
---|
421 | n/a | self.ldflags_static = [ '/nologo'] |
---|
422 | n/a | |
---|
423 | n/a | self.initialized = True |
---|
424 | n/a | |
---|
425 | n/a | # -- Worker methods ------------------------------------------------ |
---|
426 | n/a | |
---|
427 | n/a | def object_filenames(self, |
---|
428 | n/a | source_filenames, |
---|
429 | n/a | strip_dir=0, |
---|
430 | n/a | output_dir=''): |
---|
431 | n/a | # Copied from ccompiler.py, extended to return .res as 'object'-file |
---|
432 | n/a | # for .rc input file |
---|
433 | n/a | if output_dir is None: output_dir = '' |
---|
434 | n/a | obj_names = [] |
---|
435 | n/a | for src_name in source_filenames: |
---|
436 | n/a | (base, ext) = os.path.splitext (src_name) |
---|
437 | n/a | base = os.path.splitdrive(base)[1] # Chop off the drive |
---|
438 | n/a | base = base[os.path.isabs(base):] # If abs, chop off leading / |
---|
439 | n/a | if ext not in self.src_extensions: |
---|
440 | n/a | # Better to raise an exception instead of silently continuing |
---|
441 | n/a | # and later complain about sources and targets having |
---|
442 | n/a | # different lengths |
---|
443 | n/a | raise CompileError ("Don't know how to compile %s" % src_name) |
---|
444 | n/a | if strip_dir: |
---|
445 | n/a | base = os.path.basename (base) |
---|
446 | n/a | if ext in self._rc_extensions: |
---|
447 | n/a | obj_names.append (os.path.join (output_dir, |
---|
448 | n/a | base + self.res_extension)) |
---|
449 | n/a | elif ext in self._mc_extensions: |
---|
450 | n/a | obj_names.append (os.path.join (output_dir, |
---|
451 | n/a | base + self.res_extension)) |
---|
452 | n/a | else: |
---|
453 | n/a | obj_names.append (os.path.join (output_dir, |
---|
454 | n/a | base + self.obj_extension)) |
---|
455 | n/a | return obj_names |
---|
456 | n/a | |
---|
457 | n/a | |
---|
458 | n/a | def compile(self, sources, |
---|
459 | n/a | output_dir=None, macros=None, include_dirs=None, debug=0, |
---|
460 | n/a | extra_preargs=None, extra_postargs=None, depends=None): |
---|
461 | n/a | |
---|
462 | n/a | if not self.initialized: |
---|
463 | n/a | self.initialize() |
---|
464 | n/a | compile_info = self._setup_compile(output_dir, macros, include_dirs, |
---|
465 | n/a | sources, depends, extra_postargs) |
---|
466 | n/a | macros, objects, extra_postargs, pp_opts, build = compile_info |
---|
467 | n/a | |
---|
468 | n/a | compile_opts = extra_preargs or [] |
---|
469 | n/a | compile_opts.append ('/c') |
---|
470 | n/a | if debug: |
---|
471 | n/a | compile_opts.extend(self.compile_options_debug) |
---|
472 | n/a | else: |
---|
473 | n/a | compile_opts.extend(self.compile_options) |
---|
474 | n/a | |
---|
475 | n/a | for obj in objects: |
---|
476 | n/a | try: |
---|
477 | n/a | src, ext = build[obj] |
---|
478 | n/a | except KeyError: |
---|
479 | n/a | continue |
---|
480 | n/a | if debug: |
---|
481 | n/a | # pass the full pathname to MSVC in debug mode, |
---|
482 | n/a | # this allows the debugger to find the source file |
---|
483 | n/a | # without asking the user to browse for it |
---|
484 | n/a | src = os.path.abspath(src) |
---|
485 | n/a | |
---|
486 | n/a | if ext in self._c_extensions: |
---|
487 | n/a | input_opt = "/Tc" + src |
---|
488 | n/a | elif ext in self._cpp_extensions: |
---|
489 | n/a | input_opt = "/Tp" + src |
---|
490 | n/a | elif ext in self._rc_extensions: |
---|
491 | n/a | # compile .RC to .RES file |
---|
492 | n/a | input_opt = src |
---|
493 | n/a | output_opt = "/fo" + obj |
---|
494 | n/a | try: |
---|
495 | n/a | self.spawn([self.rc] + pp_opts + |
---|
496 | n/a | [output_opt] + [input_opt]) |
---|
497 | n/a | except DistutilsExecError as msg: |
---|
498 | n/a | raise CompileError(msg) |
---|
499 | n/a | continue |
---|
500 | n/a | elif ext in self._mc_extensions: |
---|
501 | n/a | # Compile .MC to .RC file to .RES file. |
---|
502 | n/a | # * '-h dir' specifies the directory for the |
---|
503 | n/a | # generated include file |
---|
504 | n/a | # * '-r dir' specifies the target directory of the |
---|
505 | n/a | # generated RC file and the binary message resource |
---|
506 | n/a | # it includes |
---|
507 | n/a | # |
---|
508 | n/a | # For now (since there are no options to change this), |
---|
509 | n/a | # we use the source-directory for the include file and |
---|
510 | n/a | # the build directory for the RC file and message |
---|
511 | n/a | # resources. This works at least for win32all. |
---|
512 | n/a | h_dir = os.path.dirname(src) |
---|
513 | n/a | rc_dir = os.path.dirname(obj) |
---|
514 | n/a | try: |
---|
515 | n/a | # first compile .MC to .RC and .H file |
---|
516 | n/a | self.spawn([self.mc] + |
---|
517 | n/a | ['-h', h_dir, '-r', rc_dir] + [src]) |
---|
518 | n/a | base, _ = os.path.splitext (os.path.basename (src)) |
---|
519 | n/a | rc_file = os.path.join (rc_dir, base + '.rc') |
---|
520 | n/a | # then compile .RC to .RES file |
---|
521 | n/a | self.spawn([self.rc] + |
---|
522 | n/a | ["/fo" + obj] + [rc_file]) |
---|
523 | n/a | |
---|
524 | n/a | except DistutilsExecError as msg: |
---|
525 | n/a | raise CompileError(msg) |
---|
526 | n/a | continue |
---|
527 | n/a | else: |
---|
528 | n/a | # how to handle this file? |
---|
529 | n/a | raise CompileError("Don't know how to compile %s to %s" |
---|
530 | n/a | % (src, obj)) |
---|
531 | n/a | |
---|
532 | n/a | output_opt = "/Fo" + obj |
---|
533 | n/a | try: |
---|
534 | n/a | self.spawn([self.cc] + compile_opts + pp_opts + |
---|
535 | n/a | [input_opt, output_opt] + |
---|
536 | n/a | extra_postargs) |
---|
537 | n/a | except DistutilsExecError as msg: |
---|
538 | n/a | raise CompileError(msg) |
---|
539 | n/a | |
---|
540 | n/a | return objects |
---|
541 | n/a | |
---|
542 | n/a | |
---|
543 | n/a | def create_static_lib(self, |
---|
544 | n/a | objects, |
---|
545 | n/a | output_libname, |
---|
546 | n/a | output_dir=None, |
---|
547 | n/a | debug=0, |
---|
548 | n/a | target_lang=None): |
---|
549 | n/a | |
---|
550 | n/a | if not self.initialized: |
---|
551 | n/a | self.initialize() |
---|
552 | n/a | (objects, output_dir) = self._fix_object_args(objects, output_dir) |
---|
553 | n/a | output_filename = self.library_filename(output_libname, |
---|
554 | n/a | output_dir=output_dir) |
---|
555 | n/a | |
---|
556 | n/a | if self._need_link(objects, output_filename): |
---|
557 | n/a | lib_args = objects + ['/OUT:' + output_filename] |
---|
558 | n/a | if debug: |
---|
559 | n/a | pass # XXX what goes here? |
---|
560 | n/a | try: |
---|
561 | n/a | self.spawn([self.lib] + lib_args) |
---|
562 | n/a | except DistutilsExecError as msg: |
---|
563 | n/a | raise LibError(msg) |
---|
564 | n/a | else: |
---|
565 | n/a | log.debug("skipping %s (up-to-date)", output_filename) |
---|
566 | n/a | |
---|
567 | n/a | |
---|
568 | n/a | def link(self, |
---|
569 | n/a | target_desc, |
---|
570 | n/a | objects, |
---|
571 | n/a | output_filename, |
---|
572 | n/a | output_dir=None, |
---|
573 | n/a | libraries=None, |
---|
574 | n/a | library_dirs=None, |
---|
575 | n/a | runtime_library_dirs=None, |
---|
576 | n/a | export_symbols=None, |
---|
577 | n/a | debug=0, |
---|
578 | n/a | extra_preargs=None, |
---|
579 | n/a | extra_postargs=None, |
---|
580 | n/a | build_temp=None, |
---|
581 | n/a | target_lang=None): |
---|
582 | n/a | |
---|
583 | n/a | if not self.initialized: |
---|
584 | n/a | self.initialize() |
---|
585 | n/a | (objects, output_dir) = self._fix_object_args(objects, output_dir) |
---|
586 | n/a | fixed_args = self._fix_lib_args(libraries, library_dirs, |
---|
587 | n/a | runtime_library_dirs) |
---|
588 | n/a | (libraries, library_dirs, runtime_library_dirs) = fixed_args |
---|
589 | n/a | |
---|
590 | n/a | if runtime_library_dirs: |
---|
591 | n/a | self.warn ("I don't know what to do with 'runtime_library_dirs': " |
---|
592 | n/a | + str (runtime_library_dirs)) |
---|
593 | n/a | |
---|
594 | n/a | lib_opts = gen_lib_options(self, |
---|
595 | n/a | library_dirs, runtime_library_dirs, |
---|
596 | n/a | libraries) |
---|
597 | n/a | if output_dir is not None: |
---|
598 | n/a | output_filename = os.path.join(output_dir, output_filename) |
---|
599 | n/a | |
---|
600 | n/a | if self._need_link(objects, output_filename): |
---|
601 | n/a | if target_desc == CCompiler.EXECUTABLE: |
---|
602 | n/a | if debug: |
---|
603 | n/a | ldflags = self.ldflags_shared_debug[1:] |
---|
604 | n/a | else: |
---|
605 | n/a | ldflags = self.ldflags_shared[1:] |
---|
606 | n/a | else: |
---|
607 | n/a | if debug: |
---|
608 | n/a | ldflags = self.ldflags_shared_debug |
---|
609 | n/a | else: |
---|
610 | n/a | ldflags = self.ldflags_shared |
---|
611 | n/a | |
---|
612 | n/a | export_opts = [] |
---|
613 | n/a | for sym in (export_symbols or []): |
---|
614 | n/a | export_opts.append("/EXPORT:" + sym) |
---|
615 | n/a | |
---|
616 | n/a | ld_args = (ldflags + lib_opts + export_opts + |
---|
617 | n/a | objects + ['/OUT:' + output_filename]) |
---|
618 | n/a | |
---|
619 | n/a | # The MSVC linker generates .lib and .exp files, which cannot be |
---|
620 | n/a | # suppressed by any linker switches. The .lib files may even be |
---|
621 | n/a | # needed! Make sure they are generated in the temporary build |
---|
622 | n/a | # directory. Since they have different names for debug and release |
---|
623 | n/a | # builds, they can go into the same directory. |
---|
624 | n/a | build_temp = os.path.dirname(objects[0]) |
---|
625 | n/a | if export_symbols is not None: |
---|
626 | n/a | (dll_name, dll_ext) = os.path.splitext( |
---|
627 | n/a | os.path.basename(output_filename)) |
---|
628 | n/a | implib_file = os.path.join( |
---|
629 | n/a | build_temp, |
---|
630 | n/a | self.library_filename(dll_name)) |
---|
631 | n/a | ld_args.append ('/IMPLIB:' + implib_file) |
---|
632 | n/a | |
---|
633 | n/a | self.manifest_setup_ldargs(output_filename, build_temp, ld_args) |
---|
634 | n/a | |
---|
635 | n/a | if extra_preargs: |
---|
636 | n/a | ld_args[:0] = extra_preargs |
---|
637 | n/a | if extra_postargs: |
---|
638 | n/a | ld_args.extend(extra_postargs) |
---|
639 | n/a | |
---|
640 | n/a | self.mkpath(os.path.dirname(output_filename)) |
---|
641 | n/a | try: |
---|
642 | n/a | self.spawn([self.linker] + ld_args) |
---|
643 | n/a | except DistutilsExecError as msg: |
---|
644 | n/a | raise LinkError(msg) |
---|
645 | n/a | |
---|
646 | n/a | # embed the manifest |
---|
647 | n/a | # XXX - this is somewhat fragile - if mt.exe fails, distutils |
---|
648 | n/a | # will still consider the DLL up-to-date, but it will not have a |
---|
649 | n/a | # manifest. Maybe we should link to a temp file? OTOH, that |
---|
650 | n/a | # implies a build environment error that shouldn't go undetected. |
---|
651 | n/a | mfinfo = self.manifest_get_embed_info(target_desc, ld_args) |
---|
652 | n/a | if mfinfo is not None: |
---|
653 | n/a | mffilename, mfid = mfinfo |
---|
654 | n/a | out_arg = '-outputresource:%s;%s' % (output_filename, mfid) |
---|
655 | n/a | try: |
---|
656 | n/a | self.spawn(['mt.exe', '-nologo', '-manifest', |
---|
657 | n/a | mffilename, out_arg]) |
---|
658 | n/a | except DistutilsExecError as msg: |
---|
659 | n/a | raise LinkError(msg) |
---|
660 | n/a | else: |
---|
661 | n/a | log.debug("skipping %s (up-to-date)", output_filename) |
---|
662 | n/a | |
---|
663 | n/a | def manifest_setup_ldargs(self, output_filename, build_temp, ld_args): |
---|
664 | n/a | # If we need a manifest at all, an embedded manifest is recommended. |
---|
665 | n/a | # See MSDN article titled |
---|
666 | n/a | # "How to: Embed a Manifest Inside a C/C++ Application" |
---|
667 | n/a | # (currently at http://msdn2.microsoft.com/en-us/library/ms235591(VS.80).aspx) |
---|
668 | n/a | # Ask the linker to generate the manifest in the temp dir, so |
---|
669 | n/a | # we can check it, and possibly embed it, later. |
---|
670 | n/a | temp_manifest = os.path.join( |
---|
671 | n/a | build_temp, |
---|
672 | n/a | os.path.basename(output_filename) + ".manifest") |
---|
673 | n/a | ld_args.append('/MANIFESTFILE:' + temp_manifest) |
---|
674 | n/a | |
---|
675 | n/a | def manifest_get_embed_info(self, target_desc, ld_args): |
---|
676 | n/a | # If a manifest should be embedded, return a tuple of |
---|
677 | n/a | # (manifest_filename, resource_id). Returns None if no manifest |
---|
678 | n/a | # should be embedded. See http://bugs.python.org/issue7833 for why |
---|
679 | n/a | # we want to avoid any manifest for extension modules if we can) |
---|
680 | n/a | for arg in ld_args: |
---|
681 | n/a | if arg.startswith("/MANIFESTFILE:"): |
---|
682 | n/a | temp_manifest = arg.split(":", 1)[1] |
---|
683 | n/a | break |
---|
684 | n/a | else: |
---|
685 | n/a | # no /MANIFESTFILE so nothing to do. |
---|
686 | n/a | return None |
---|
687 | n/a | if target_desc == CCompiler.EXECUTABLE: |
---|
688 | n/a | # by default, executables always get the manifest with the |
---|
689 | n/a | # CRT referenced. |
---|
690 | n/a | mfid = 1 |
---|
691 | n/a | else: |
---|
692 | n/a | # Extension modules try and avoid any manifest if possible. |
---|
693 | n/a | mfid = 2 |
---|
694 | n/a | temp_manifest = self._remove_visual_c_ref(temp_manifest) |
---|
695 | n/a | if temp_manifest is None: |
---|
696 | n/a | return None |
---|
697 | n/a | return temp_manifest, mfid |
---|
698 | n/a | |
---|
699 | n/a | def _remove_visual_c_ref(self, manifest_file): |
---|
700 | n/a | try: |
---|
701 | n/a | # Remove references to the Visual C runtime, so they will |
---|
702 | n/a | # fall through to the Visual C dependency of Python.exe. |
---|
703 | n/a | # This way, when installed for a restricted user (e.g. |
---|
704 | n/a | # runtimes are not in WinSxS folder, but in Python's own |
---|
705 | n/a | # folder), the runtimes do not need to be in every folder |
---|
706 | n/a | # with .pyd's. |
---|
707 | n/a | # Returns either the filename of the modified manifest or |
---|
708 | n/a | # None if no manifest should be embedded. |
---|
709 | n/a | manifest_f = open(manifest_file) |
---|
710 | n/a | try: |
---|
711 | n/a | manifest_buf = manifest_f.read() |
---|
712 | n/a | finally: |
---|
713 | n/a | manifest_f.close() |
---|
714 | n/a | pattern = re.compile( |
---|
715 | n/a | r"""<assemblyIdentity.*?name=("|')Microsoft\."""\ |
---|
716 | n/a | r"""VC\d{2}\.CRT("|').*?(/>|</assemblyIdentity>)""", |
---|
717 | n/a | re.DOTALL) |
---|
718 | n/a | manifest_buf = re.sub(pattern, "", manifest_buf) |
---|
719 | n/a | pattern = r"<dependentAssembly>\s*</dependentAssembly>" |
---|
720 | n/a | manifest_buf = re.sub(pattern, "", manifest_buf) |
---|
721 | n/a | # Now see if any other assemblies are referenced - if not, we |
---|
722 | n/a | # don't want a manifest embedded. |
---|
723 | n/a | pattern = re.compile( |
---|
724 | n/a | r"""<assemblyIdentity.*?name=(?:"|')(.+?)(?:"|')""" |
---|
725 | n/a | r""".*?(?:/>|</assemblyIdentity>)""", re.DOTALL) |
---|
726 | n/a | if re.search(pattern, manifest_buf) is None: |
---|
727 | n/a | return None |
---|
728 | n/a | |
---|
729 | n/a | manifest_f = open(manifest_file, 'w') |
---|
730 | n/a | try: |
---|
731 | n/a | manifest_f.write(manifest_buf) |
---|
732 | n/a | return manifest_file |
---|
733 | n/a | finally: |
---|
734 | n/a | manifest_f.close() |
---|
735 | n/a | except OSError: |
---|
736 | n/a | pass |
---|
737 | n/a | |
---|
738 | n/a | # -- Miscellaneous methods ----------------------------------------- |
---|
739 | n/a | # These are all used by the 'gen_lib_options() function, in |
---|
740 | n/a | # ccompiler.py. |
---|
741 | n/a | |
---|
742 | n/a | def library_dir_option(self, dir): |
---|
743 | n/a | return "/LIBPATH:" + dir |
---|
744 | n/a | |
---|
745 | n/a | def runtime_library_dir_option(self, dir): |
---|
746 | n/a | raise DistutilsPlatformError( |
---|
747 | n/a | "don't know how to set runtime library search path for MSVC++") |
---|
748 | n/a | |
---|
749 | n/a | def library_option(self, lib): |
---|
750 | n/a | return self.library_filename(lib) |
---|
751 | n/a | |
---|
752 | n/a | |
---|
753 | n/a | def find_library_file(self, dirs, lib, debug=0): |
---|
754 | n/a | # Prefer a debugging library if found (and requested), but deal |
---|
755 | n/a | # with it if we don't have one. |
---|
756 | n/a | if debug: |
---|
757 | n/a | try_names = [lib + "_d", lib] |
---|
758 | n/a | else: |
---|
759 | n/a | try_names = [lib] |
---|
760 | n/a | for dir in dirs: |
---|
761 | n/a | for name in try_names: |
---|
762 | n/a | libfile = os.path.join(dir, self.library_filename (name)) |
---|
763 | n/a | if os.path.exists(libfile): |
---|
764 | n/a | return libfile |
---|
765 | n/a | else: |
---|
766 | n/a | # Oops, didn't find it in *any* of 'dirs' |
---|
767 | n/a | return None |
---|
768 | n/a | |
---|
769 | n/a | # Helper methods for using the MSVC registry settings |
---|
770 | n/a | |
---|
771 | n/a | def find_exe(self, exe): |
---|
772 | n/a | """Return path to an MSVC executable program. |
---|
773 | n/a | |
---|
774 | n/a | Tries to find the program in several places: first, one of the |
---|
775 | n/a | MSVC program search paths from the registry; next, the directories |
---|
776 | n/a | in the PATH environment variable. If any of those work, return an |
---|
777 | n/a | absolute path that is known to exist. If none of them work, just |
---|
778 | n/a | return the original program name, 'exe'. |
---|
779 | n/a | """ |
---|
780 | n/a | for p in self.__paths: |
---|
781 | n/a | fn = os.path.join(os.path.abspath(p), exe) |
---|
782 | n/a | if os.path.isfile(fn): |
---|
783 | n/a | return fn |
---|
784 | n/a | |
---|
785 | n/a | # didn't find it; try existing path |
---|
786 | n/a | for p in os.environ['Path'].split(';'): |
---|
787 | n/a | fn = os.path.join(os.path.abspath(p),exe) |
---|
788 | n/a | if os.path.isfile(fn): |
---|
789 | n/a | return fn |
---|
790 | n/a | |
---|
791 | n/a | return exe |
---|