1 | n/a | """distutils.msvccompiler |
---|
2 | n/a | |
---|
3 | n/a | Contains MSVCCompiler, an implementation of the abstract CCompiler class |
---|
4 | n/a | for the Microsoft Visual Studio. |
---|
5 | n/a | """ |
---|
6 | n/a | |
---|
7 | n/a | # Written by Perry Stoll |
---|
8 | n/a | # hacked by Robin Becker and Thomas Heller to do a better job of |
---|
9 | n/a | # finding DevStudio (through the registry) |
---|
10 | n/a | |
---|
11 | n/a | import sys, os |
---|
12 | n/a | from distutils.errors import \ |
---|
13 | n/a | DistutilsExecError, DistutilsPlatformError, \ |
---|
14 | n/a | CompileError, LibError, LinkError |
---|
15 | n/a | from distutils.ccompiler import \ |
---|
16 | n/a | CCompiler, gen_preprocess_options, gen_lib_options |
---|
17 | n/a | from distutils import log |
---|
18 | n/a | |
---|
19 | n/a | _can_read_reg = False |
---|
20 | n/a | try: |
---|
21 | n/a | import winreg |
---|
22 | n/a | |
---|
23 | n/a | _can_read_reg = True |
---|
24 | n/a | hkey_mod = winreg |
---|
25 | n/a | |
---|
26 | n/a | RegOpenKeyEx = winreg.OpenKeyEx |
---|
27 | n/a | RegEnumKey = winreg.EnumKey |
---|
28 | n/a | RegEnumValue = winreg.EnumValue |
---|
29 | n/a | RegError = winreg.error |
---|
30 | n/a | |
---|
31 | n/a | except ImportError: |
---|
32 | n/a | try: |
---|
33 | n/a | import win32api |
---|
34 | n/a | import win32con |
---|
35 | n/a | _can_read_reg = True |
---|
36 | n/a | hkey_mod = win32con |
---|
37 | n/a | |
---|
38 | n/a | RegOpenKeyEx = win32api.RegOpenKeyEx |
---|
39 | n/a | RegEnumKey = win32api.RegEnumKey |
---|
40 | n/a | RegEnumValue = win32api.RegEnumValue |
---|
41 | n/a | RegError = win32api.error |
---|
42 | n/a | except ImportError: |
---|
43 | n/a | log.info("Warning: Can't read registry to find the " |
---|
44 | n/a | "necessary compiler setting\n" |
---|
45 | n/a | "Make sure that Python modules winreg, " |
---|
46 | n/a | "win32api or win32con are installed.") |
---|
47 | n/a | pass |
---|
48 | n/a | |
---|
49 | n/a | if _can_read_reg: |
---|
50 | n/a | HKEYS = (hkey_mod.HKEY_USERS, |
---|
51 | n/a | hkey_mod.HKEY_CURRENT_USER, |
---|
52 | n/a | hkey_mod.HKEY_LOCAL_MACHINE, |
---|
53 | n/a | hkey_mod.HKEY_CLASSES_ROOT) |
---|
54 | n/a | |
---|
55 | n/a | def read_keys(base, key): |
---|
56 | n/a | """Return list of registry keys.""" |
---|
57 | n/a | try: |
---|
58 | n/a | handle = RegOpenKeyEx(base, key) |
---|
59 | n/a | except RegError: |
---|
60 | n/a | return None |
---|
61 | n/a | L = [] |
---|
62 | n/a | i = 0 |
---|
63 | n/a | while True: |
---|
64 | n/a | try: |
---|
65 | n/a | k = RegEnumKey(handle, i) |
---|
66 | n/a | except RegError: |
---|
67 | n/a | break |
---|
68 | n/a | L.append(k) |
---|
69 | n/a | i += 1 |
---|
70 | n/a | return L |
---|
71 | n/a | |
---|
72 | n/a | def read_values(base, key): |
---|
73 | n/a | """Return dict of registry keys and values. |
---|
74 | n/a | |
---|
75 | n/a | All names are converted to lowercase. |
---|
76 | n/a | """ |
---|
77 | n/a | try: |
---|
78 | n/a | handle = RegOpenKeyEx(base, key) |
---|
79 | n/a | except RegError: |
---|
80 | n/a | return None |
---|
81 | n/a | d = {} |
---|
82 | n/a | i = 0 |
---|
83 | n/a | while True: |
---|
84 | n/a | try: |
---|
85 | n/a | name, value, type = RegEnumValue(handle, i) |
---|
86 | n/a | except RegError: |
---|
87 | n/a | break |
---|
88 | n/a | name = name.lower() |
---|
89 | n/a | d[convert_mbcs(name)] = convert_mbcs(value) |
---|
90 | n/a | i += 1 |
---|
91 | n/a | return d |
---|
92 | n/a | |
---|
93 | n/a | def convert_mbcs(s): |
---|
94 | n/a | dec = getattr(s, "decode", None) |
---|
95 | n/a | if dec is not None: |
---|
96 | n/a | try: |
---|
97 | n/a | s = dec("mbcs") |
---|
98 | n/a | except UnicodeError: |
---|
99 | n/a | pass |
---|
100 | n/a | return s |
---|
101 | n/a | |
---|
102 | n/a | class MacroExpander: |
---|
103 | n/a | def __init__(self, version): |
---|
104 | n/a | self.macros = {} |
---|
105 | n/a | self.load_macros(version) |
---|
106 | n/a | |
---|
107 | n/a | def set_macro(self, macro, path, key): |
---|
108 | n/a | for base in HKEYS: |
---|
109 | n/a | d = read_values(base, path) |
---|
110 | n/a | if d: |
---|
111 | n/a | self.macros["$(%s)" % macro] = d[key] |
---|
112 | n/a | break |
---|
113 | n/a | |
---|
114 | n/a | def load_macros(self, version): |
---|
115 | n/a | vsbase = r"Software\Microsoft\VisualStudio\%0.1f" % version |
---|
116 | n/a | self.set_macro("VCInstallDir", vsbase + r"\Setup\VC", "productdir") |
---|
117 | n/a | self.set_macro("VSInstallDir", vsbase + r"\Setup\VS", "productdir") |
---|
118 | n/a | net = r"Software\Microsoft\.NETFramework" |
---|
119 | n/a | self.set_macro("FrameworkDir", net, "installroot") |
---|
120 | n/a | try: |
---|
121 | n/a | if version > 7.0: |
---|
122 | n/a | self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv1.1") |
---|
123 | n/a | else: |
---|
124 | n/a | self.set_macro("FrameworkSDKDir", net, "sdkinstallroot") |
---|
125 | n/a | except KeyError as exc: # |
---|
126 | n/a | raise DistutilsPlatformError( |
---|
127 | n/a | """Python was built with Visual Studio 2003; |
---|
128 | n/a | extensions must be built with a compiler than can generate compatible binaries. |
---|
129 | n/a | Visual Studio 2003 was not found on this system. If you have Cygwin installed, |
---|
130 | n/a | you can try compiling with MingW32, by passing "-c mingw32" to setup.py.""") |
---|
131 | n/a | |
---|
132 | n/a | p = r"Software\Microsoft\NET Framework Setup\Product" |
---|
133 | n/a | for base in HKEYS: |
---|
134 | n/a | try: |
---|
135 | n/a | h = RegOpenKeyEx(base, p) |
---|
136 | n/a | except RegError: |
---|
137 | n/a | continue |
---|
138 | n/a | key = RegEnumKey(h, 0) |
---|
139 | n/a | d = read_values(base, r"%s\%s" % (p, key)) |
---|
140 | n/a | self.macros["$(FrameworkVersion)"] = d["version"] |
---|
141 | n/a | |
---|
142 | n/a | def sub(self, s): |
---|
143 | n/a | for k, v in self.macros.items(): |
---|
144 | n/a | s = s.replace(k, v) |
---|
145 | n/a | return s |
---|
146 | n/a | |
---|
147 | n/a | def get_build_version(): |
---|
148 | n/a | """Return the version of MSVC that was used to build Python. |
---|
149 | n/a | |
---|
150 | n/a | For Python 2.3 and up, the version number is included in |
---|
151 | n/a | sys.version. For earlier versions, assume the compiler is MSVC 6. |
---|
152 | n/a | """ |
---|
153 | n/a | prefix = "MSC v." |
---|
154 | n/a | i = sys.version.find(prefix) |
---|
155 | n/a | if i == -1: |
---|
156 | n/a | return 6 |
---|
157 | n/a | i = i + len(prefix) |
---|
158 | n/a | s, rest = sys.version[i:].split(" ", 1) |
---|
159 | n/a | majorVersion = int(s[:-2]) - 6 |
---|
160 | n/a | if majorVersion >= 13: |
---|
161 | n/a | # v13 was skipped and should be v14 |
---|
162 | n/a | majorVersion += 1 |
---|
163 | n/a | minorVersion = int(s[2:3]) / 10.0 |
---|
164 | n/a | # I don't think paths are affected by minor version in version 6 |
---|
165 | n/a | if majorVersion == 6: |
---|
166 | n/a | minorVersion = 0 |
---|
167 | n/a | if majorVersion >= 6: |
---|
168 | n/a | return majorVersion + minorVersion |
---|
169 | n/a | # else we don't know what version of the compiler this is |
---|
170 | n/a | return None |
---|
171 | n/a | |
---|
172 | n/a | def get_build_architecture(): |
---|
173 | n/a | """Return the processor architecture. |
---|
174 | n/a | |
---|
175 | n/a | Possible results are "Intel", "Itanium", or "AMD64". |
---|
176 | n/a | """ |
---|
177 | n/a | |
---|
178 | n/a | prefix = " bit (" |
---|
179 | n/a | i = sys.version.find(prefix) |
---|
180 | n/a | if i == -1: |
---|
181 | n/a | return "Intel" |
---|
182 | n/a | j = sys.version.find(")", i) |
---|
183 | n/a | return sys.version[i+len(prefix):j] |
---|
184 | n/a | |
---|
185 | n/a | def normalize_and_reduce_paths(paths): |
---|
186 | n/a | """Return a list of normalized paths with duplicates removed. |
---|
187 | n/a | |
---|
188 | n/a | The current order of paths is maintained. |
---|
189 | n/a | """ |
---|
190 | n/a | # Paths are normalized so things like: /a and /a/ aren't both preserved. |
---|
191 | n/a | reduced_paths = [] |
---|
192 | n/a | for p in paths: |
---|
193 | n/a | np = os.path.normpath(p) |
---|
194 | n/a | # XXX(nnorwitz): O(n**2), if reduced_paths gets long perhaps use a set. |
---|
195 | n/a | if np not in reduced_paths: |
---|
196 | n/a | reduced_paths.append(np) |
---|
197 | n/a | return reduced_paths |
---|
198 | n/a | |
---|
199 | n/a | |
---|
200 | n/a | class MSVCCompiler(CCompiler) : |
---|
201 | n/a | """Concrete class that implements an interface to Microsoft Visual C++, |
---|
202 | n/a | as defined by the CCompiler abstract class.""" |
---|
203 | n/a | |
---|
204 | n/a | compiler_type = 'msvc' |
---|
205 | n/a | |
---|
206 | n/a | # Just set this so CCompiler's constructor doesn't barf. We currently |
---|
207 | n/a | # don't use the 'set_executables()' bureaucracy provided by CCompiler, |
---|
208 | n/a | # as it really isn't necessary for this sort of single-compiler class. |
---|
209 | n/a | # Would be nice to have a consistent interface with UnixCCompiler, |
---|
210 | n/a | # though, so it's worth thinking about. |
---|
211 | n/a | executables = {} |
---|
212 | n/a | |
---|
213 | n/a | # Private class data (need to distinguish C from C++ source for compiler) |
---|
214 | n/a | _c_extensions = ['.c'] |
---|
215 | n/a | _cpp_extensions = ['.cc', '.cpp', '.cxx'] |
---|
216 | n/a | _rc_extensions = ['.rc'] |
---|
217 | n/a | _mc_extensions = ['.mc'] |
---|
218 | n/a | |
---|
219 | n/a | # Needed for the filename generation methods provided by the |
---|
220 | n/a | # base class, CCompiler. |
---|
221 | n/a | src_extensions = (_c_extensions + _cpp_extensions + |
---|
222 | n/a | _rc_extensions + _mc_extensions) |
---|
223 | n/a | res_extension = '.res' |
---|
224 | n/a | obj_extension = '.obj' |
---|
225 | n/a | static_lib_extension = '.lib' |
---|
226 | n/a | shared_lib_extension = '.dll' |
---|
227 | n/a | static_lib_format = shared_lib_format = '%s%s' |
---|
228 | n/a | exe_extension = '.exe' |
---|
229 | n/a | |
---|
230 | n/a | def __init__(self, verbose=0, dry_run=0, force=0): |
---|
231 | n/a | CCompiler.__init__ (self, verbose, dry_run, force) |
---|
232 | n/a | self.__version = get_build_version() |
---|
233 | n/a | self.__arch = get_build_architecture() |
---|
234 | n/a | if self.__arch == "Intel": |
---|
235 | n/a | # x86 |
---|
236 | n/a | if self.__version >= 7: |
---|
237 | n/a | self.__root = r"Software\Microsoft\VisualStudio" |
---|
238 | n/a | self.__macros = MacroExpander(self.__version) |
---|
239 | n/a | else: |
---|
240 | n/a | self.__root = r"Software\Microsoft\Devstudio" |
---|
241 | n/a | self.__product = "Visual Studio version %s" % self.__version |
---|
242 | n/a | else: |
---|
243 | n/a | # Win64. Assume this was built with the platform SDK |
---|
244 | n/a | self.__product = "Microsoft SDK compiler %s" % (self.__version + 6) |
---|
245 | n/a | |
---|
246 | n/a | self.initialized = False |
---|
247 | n/a | |
---|
248 | n/a | def initialize(self): |
---|
249 | n/a | self.__paths = [] |
---|
250 | n/a | if "DISTUTILS_USE_SDK" in os.environ and "MSSdk" in os.environ and self.find_exe("cl.exe"): |
---|
251 | n/a | # Assume that the SDK set up everything alright; don't try to be |
---|
252 | n/a | # smarter |
---|
253 | n/a | self.cc = "cl.exe" |
---|
254 | n/a | self.linker = "link.exe" |
---|
255 | n/a | self.lib = "lib.exe" |
---|
256 | n/a | self.rc = "rc.exe" |
---|
257 | n/a | self.mc = "mc.exe" |
---|
258 | n/a | else: |
---|
259 | n/a | self.__paths = self.get_msvc_paths("path") |
---|
260 | n/a | |
---|
261 | n/a | if len(self.__paths) == 0: |
---|
262 | n/a | raise DistutilsPlatformError("Python was built with %s, " |
---|
263 | n/a | "and extensions need to be built with the same " |
---|
264 | n/a | "version of the compiler, but it isn't installed." |
---|
265 | n/a | % self.__product) |
---|
266 | n/a | |
---|
267 | n/a | self.cc = self.find_exe("cl.exe") |
---|
268 | n/a | self.linker = self.find_exe("link.exe") |
---|
269 | n/a | self.lib = self.find_exe("lib.exe") |
---|
270 | n/a | self.rc = self.find_exe("rc.exe") # resource compiler |
---|
271 | n/a | self.mc = self.find_exe("mc.exe") # message compiler |
---|
272 | n/a | self.set_path_env_var('lib') |
---|
273 | n/a | self.set_path_env_var('include') |
---|
274 | n/a | |
---|
275 | n/a | # extend the MSVC path with the current path |
---|
276 | n/a | try: |
---|
277 | n/a | for p in os.environ['path'].split(';'): |
---|
278 | n/a | self.__paths.append(p) |
---|
279 | n/a | except KeyError: |
---|
280 | n/a | pass |
---|
281 | n/a | self.__paths = normalize_and_reduce_paths(self.__paths) |
---|
282 | n/a | os.environ['path'] = ";".join(self.__paths) |
---|
283 | n/a | |
---|
284 | n/a | self.preprocess_options = None |
---|
285 | n/a | if self.__arch == "Intel": |
---|
286 | n/a | self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', '/GX' , |
---|
287 | n/a | '/DNDEBUG'] |
---|
288 | n/a | self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GX', |
---|
289 | n/a | '/Z7', '/D_DEBUG'] |
---|
290 | n/a | else: |
---|
291 | n/a | # Win64 |
---|
292 | n/a | self.compile_options = [ '/nologo', '/Ox', '/MD', '/W3', '/GS-' , |
---|
293 | n/a | '/DNDEBUG'] |
---|
294 | n/a | self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GS-', |
---|
295 | n/a | '/Z7', '/D_DEBUG'] |
---|
296 | n/a | |
---|
297 | n/a | self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO'] |
---|
298 | n/a | if self.__version >= 7: |
---|
299 | n/a | self.ldflags_shared_debug = [ |
---|
300 | n/a | '/DLL', '/nologo', '/INCREMENTAL:no', '/DEBUG' |
---|
301 | n/a | ] |
---|
302 | n/a | else: |
---|
303 | n/a | self.ldflags_shared_debug = [ |
---|
304 | n/a | '/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG' |
---|
305 | n/a | ] |
---|
306 | n/a | self.ldflags_static = [ '/nologo'] |
---|
307 | n/a | |
---|
308 | n/a | self.initialized = True |
---|
309 | n/a | |
---|
310 | n/a | # -- Worker methods ------------------------------------------------ |
---|
311 | n/a | |
---|
312 | n/a | def object_filenames(self, |
---|
313 | n/a | source_filenames, |
---|
314 | n/a | strip_dir=0, |
---|
315 | n/a | output_dir=''): |
---|
316 | n/a | # Copied from ccompiler.py, extended to return .res as 'object'-file |
---|
317 | n/a | # for .rc input file |
---|
318 | n/a | if output_dir is None: output_dir = '' |
---|
319 | n/a | obj_names = [] |
---|
320 | n/a | for src_name in source_filenames: |
---|
321 | n/a | (base, ext) = os.path.splitext (src_name) |
---|
322 | n/a | base = os.path.splitdrive(base)[1] # Chop off the drive |
---|
323 | n/a | base = base[os.path.isabs(base):] # If abs, chop off leading / |
---|
324 | n/a | if ext not in self.src_extensions: |
---|
325 | n/a | # Better to raise an exception instead of silently continuing |
---|
326 | n/a | # and later complain about sources and targets having |
---|
327 | n/a | # different lengths |
---|
328 | n/a | raise CompileError ("Don't know how to compile %s" % src_name) |
---|
329 | n/a | if strip_dir: |
---|
330 | n/a | base = os.path.basename (base) |
---|
331 | n/a | if ext in self._rc_extensions: |
---|
332 | n/a | obj_names.append (os.path.join (output_dir, |
---|
333 | n/a | base + self.res_extension)) |
---|
334 | n/a | elif ext in self._mc_extensions: |
---|
335 | n/a | obj_names.append (os.path.join (output_dir, |
---|
336 | n/a | base + self.res_extension)) |
---|
337 | n/a | else: |
---|
338 | n/a | obj_names.append (os.path.join (output_dir, |
---|
339 | n/a | base + self.obj_extension)) |
---|
340 | n/a | return obj_names |
---|
341 | n/a | |
---|
342 | n/a | |
---|
343 | n/a | def compile(self, sources, |
---|
344 | n/a | output_dir=None, macros=None, include_dirs=None, debug=0, |
---|
345 | n/a | extra_preargs=None, extra_postargs=None, depends=None): |
---|
346 | n/a | |
---|
347 | n/a | if not self.initialized: |
---|
348 | n/a | self.initialize() |
---|
349 | n/a | compile_info = self._setup_compile(output_dir, macros, include_dirs, |
---|
350 | n/a | sources, depends, extra_postargs) |
---|
351 | n/a | macros, objects, extra_postargs, pp_opts, build = compile_info |
---|
352 | n/a | |
---|
353 | n/a | compile_opts = extra_preargs or [] |
---|
354 | n/a | compile_opts.append ('/c') |
---|
355 | n/a | if debug: |
---|
356 | n/a | compile_opts.extend(self.compile_options_debug) |
---|
357 | n/a | else: |
---|
358 | n/a | compile_opts.extend(self.compile_options) |
---|
359 | n/a | |
---|
360 | n/a | for obj in objects: |
---|
361 | n/a | try: |
---|
362 | n/a | src, ext = build[obj] |
---|
363 | n/a | except KeyError: |
---|
364 | n/a | continue |
---|
365 | n/a | if debug: |
---|
366 | n/a | # pass the full pathname to MSVC in debug mode, |
---|
367 | n/a | # this allows the debugger to find the source file |
---|
368 | n/a | # without asking the user to browse for it |
---|
369 | n/a | src = os.path.abspath(src) |
---|
370 | n/a | |
---|
371 | n/a | if ext in self._c_extensions: |
---|
372 | n/a | input_opt = "/Tc" + src |
---|
373 | n/a | elif ext in self._cpp_extensions: |
---|
374 | n/a | input_opt = "/Tp" + src |
---|
375 | n/a | elif ext in self._rc_extensions: |
---|
376 | n/a | # compile .RC to .RES file |
---|
377 | n/a | input_opt = src |
---|
378 | n/a | output_opt = "/fo" + obj |
---|
379 | n/a | try: |
---|
380 | n/a | self.spawn([self.rc] + pp_opts + |
---|
381 | n/a | [output_opt] + [input_opt]) |
---|
382 | n/a | except DistutilsExecError as msg: |
---|
383 | n/a | raise CompileError(msg) |
---|
384 | n/a | continue |
---|
385 | n/a | elif ext in self._mc_extensions: |
---|
386 | n/a | # Compile .MC to .RC file to .RES file. |
---|
387 | n/a | # * '-h dir' specifies the directory for the |
---|
388 | n/a | # generated include file |
---|
389 | n/a | # * '-r dir' specifies the target directory of the |
---|
390 | n/a | # generated RC file and the binary message resource |
---|
391 | n/a | # it includes |
---|
392 | n/a | # |
---|
393 | n/a | # For now (since there are no options to change this), |
---|
394 | n/a | # we use the source-directory for the include file and |
---|
395 | n/a | # the build directory for the RC file and message |
---|
396 | n/a | # resources. This works at least for win32all. |
---|
397 | n/a | h_dir = os.path.dirname(src) |
---|
398 | n/a | rc_dir = os.path.dirname(obj) |
---|
399 | n/a | try: |
---|
400 | n/a | # first compile .MC to .RC and .H file |
---|
401 | n/a | self.spawn([self.mc] + |
---|
402 | n/a | ['-h', h_dir, '-r', rc_dir] + [src]) |
---|
403 | n/a | base, _ = os.path.splitext (os.path.basename (src)) |
---|
404 | n/a | rc_file = os.path.join (rc_dir, base + '.rc') |
---|
405 | n/a | # then compile .RC to .RES file |
---|
406 | n/a | self.spawn([self.rc] + |
---|
407 | n/a | ["/fo" + obj] + [rc_file]) |
---|
408 | n/a | |
---|
409 | n/a | except DistutilsExecError as msg: |
---|
410 | n/a | raise CompileError(msg) |
---|
411 | n/a | continue |
---|
412 | n/a | else: |
---|
413 | n/a | # how to handle this file? |
---|
414 | n/a | raise CompileError("Don't know how to compile %s to %s" |
---|
415 | n/a | % (src, obj)) |
---|
416 | n/a | |
---|
417 | n/a | output_opt = "/Fo" + obj |
---|
418 | n/a | try: |
---|
419 | n/a | self.spawn([self.cc] + compile_opts + pp_opts + |
---|
420 | n/a | [input_opt, output_opt] + |
---|
421 | n/a | extra_postargs) |
---|
422 | n/a | except DistutilsExecError as msg: |
---|
423 | n/a | raise CompileError(msg) |
---|
424 | n/a | |
---|
425 | n/a | return objects |
---|
426 | n/a | |
---|
427 | n/a | |
---|
428 | n/a | def create_static_lib(self, |
---|
429 | n/a | objects, |
---|
430 | n/a | output_libname, |
---|
431 | n/a | output_dir=None, |
---|
432 | n/a | debug=0, |
---|
433 | n/a | target_lang=None): |
---|
434 | n/a | |
---|
435 | n/a | if not self.initialized: |
---|
436 | n/a | self.initialize() |
---|
437 | n/a | (objects, output_dir) = self._fix_object_args(objects, output_dir) |
---|
438 | n/a | output_filename = self.library_filename(output_libname, |
---|
439 | n/a | output_dir=output_dir) |
---|
440 | n/a | |
---|
441 | n/a | if self._need_link(objects, output_filename): |
---|
442 | n/a | lib_args = objects + ['/OUT:' + output_filename] |
---|
443 | n/a | if debug: |
---|
444 | n/a | pass # XXX what goes here? |
---|
445 | n/a | try: |
---|
446 | n/a | self.spawn([self.lib] + lib_args) |
---|
447 | n/a | except DistutilsExecError as msg: |
---|
448 | n/a | raise LibError(msg) |
---|
449 | n/a | else: |
---|
450 | n/a | log.debug("skipping %s (up-to-date)", output_filename) |
---|
451 | n/a | |
---|
452 | n/a | |
---|
453 | n/a | def link(self, |
---|
454 | n/a | target_desc, |
---|
455 | n/a | objects, |
---|
456 | n/a | output_filename, |
---|
457 | n/a | output_dir=None, |
---|
458 | n/a | libraries=None, |
---|
459 | n/a | library_dirs=None, |
---|
460 | n/a | runtime_library_dirs=None, |
---|
461 | n/a | export_symbols=None, |
---|
462 | n/a | debug=0, |
---|
463 | n/a | extra_preargs=None, |
---|
464 | n/a | extra_postargs=None, |
---|
465 | n/a | build_temp=None, |
---|
466 | n/a | target_lang=None): |
---|
467 | n/a | |
---|
468 | n/a | if not self.initialized: |
---|
469 | n/a | self.initialize() |
---|
470 | n/a | (objects, output_dir) = self._fix_object_args(objects, output_dir) |
---|
471 | n/a | fixed_args = self._fix_lib_args(libraries, library_dirs, |
---|
472 | n/a | runtime_library_dirs) |
---|
473 | n/a | (libraries, library_dirs, runtime_library_dirs) = fixed_args |
---|
474 | n/a | |
---|
475 | n/a | if runtime_library_dirs: |
---|
476 | n/a | self.warn ("I don't know what to do with 'runtime_library_dirs': " |
---|
477 | n/a | + str (runtime_library_dirs)) |
---|
478 | n/a | |
---|
479 | n/a | lib_opts = gen_lib_options(self, |
---|
480 | n/a | library_dirs, runtime_library_dirs, |
---|
481 | n/a | libraries) |
---|
482 | n/a | if output_dir is not None: |
---|
483 | n/a | output_filename = os.path.join(output_dir, output_filename) |
---|
484 | n/a | |
---|
485 | n/a | if self._need_link(objects, output_filename): |
---|
486 | n/a | if target_desc == CCompiler.EXECUTABLE: |
---|
487 | n/a | if debug: |
---|
488 | n/a | ldflags = self.ldflags_shared_debug[1:] |
---|
489 | n/a | else: |
---|
490 | n/a | ldflags = self.ldflags_shared[1:] |
---|
491 | n/a | else: |
---|
492 | n/a | if debug: |
---|
493 | n/a | ldflags = self.ldflags_shared_debug |
---|
494 | n/a | else: |
---|
495 | n/a | ldflags = self.ldflags_shared |
---|
496 | n/a | |
---|
497 | n/a | export_opts = [] |
---|
498 | n/a | for sym in (export_symbols or []): |
---|
499 | n/a | export_opts.append("/EXPORT:" + sym) |
---|
500 | n/a | |
---|
501 | n/a | ld_args = (ldflags + lib_opts + export_opts + |
---|
502 | n/a | objects + ['/OUT:' + output_filename]) |
---|
503 | n/a | |
---|
504 | n/a | # The MSVC linker generates .lib and .exp files, which cannot be |
---|
505 | n/a | # suppressed by any linker switches. The .lib files may even be |
---|
506 | n/a | # needed! Make sure they are generated in the temporary build |
---|
507 | n/a | # directory. Since they have different names for debug and release |
---|
508 | n/a | # builds, they can go into the same directory. |
---|
509 | n/a | if export_symbols is not None: |
---|
510 | n/a | (dll_name, dll_ext) = os.path.splitext( |
---|
511 | n/a | os.path.basename(output_filename)) |
---|
512 | n/a | implib_file = os.path.join( |
---|
513 | n/a | os.path.dirname(objects[0]), |
---|
514 | n/a | self.library_filename(dll_name)) |
---|
515 | n/a | ld_args.append ('/IMPLIB:' + implib_file) |
---|
516 | n/a | |
---|
517 | n/a | if extra_preargs: |
---|
518 | n/a | ld_args[:0] = extra_preargs |
---|
519 | n/a | if extra_postargs: |
---|
520 | n/a | ld_args.extend(extra_postargs) |
---|
521 | n/a | |
---|
522 | n/a | self.mkpath(os.path.dirname(output_filename)) |
---|
523 | n/a | try: |
---|
524 | n/a | self.spawn([self.linker] + ld_args) |
---|
525 | n/a | except DistutilsExecError as msg: |
---|
526 | n/a | raise LinkError(msg) |
---|
527 | n/a | |
---|
528 | n/a | else: |
---|
529 | n/a | log.debug("skipping %s (up-to-date)", output_filename) |
---|
530 | n/a | |
---|
531 | n/a | |
---|
532 | n/a | # -- Miscellaneous methods ----------------------------------------- |
---|
533 | n/a | # These are all used by the 'gen_lib_options() function, in |
---|
534 | n/a | # ccompiler.py. |
---|
535 | n/a | |
---|
536 | n/a | def library_dir_option(self, dir): |
---|
537 | n/a | return "/LIBPATH:" + dir |
---|
538 | n/a | |
---|
539 | n/a | def runtime_library_dir_option(self, dir): |
---|
540 | n/a | raise DistutilsPlatformError( |
---|
541 | n/a | "don't know how to set runtime library search path for MSVC++") |
---|
542 | n/a | |
---|
543 | n/a | def library_option(self, lib): |
---|
544 | n/a | return self.library_filename(lib) |
---|
545 | n/a | |
---|
546 | n/a | |
---|
547 | n/a | def find_library_file(self, dirs, lib, debug=0): |
---|
548 | n/a | # Prefer a debugging library if found (and requested), but deal |
---|
549 | n/a | # with it if we don't have one. |
---|
550 | n/a | if debug: |
---|
551 | n/a | try_names = [lib + "_d", lib] |
---|
552 | n/a | else: |
---|
553 | n/a | try_names = [lib] |
---|
554 | n/a | for dir in dirs: |
---|
555 | n/a | for name in try_names: |
---|
556 | n/a | libfile = os.path.join(dir, self.library_filename (name)) |
---|
557 | n/a | if os.path.exists(libfile): |
---|
558 | n/a | return libfile |
---|
559 | n/a | else: |
---|
560 | n/a | # Oops, didn't find it in *any* of 'dirs' |
---|
561 | n/a | return None |
---|
562 | n/a | |
---|
563 | n/a | # Helper methods for using the MSVC registry settings |
---|
564 | n/a | |
---|
565 | n/a | def find_exe(self, exe): |
---|
566 | n/a | """Return path to an MSVC executable program. |
---|
567 | n/a | |
---|
568 | n/a | Tries to find the program in several places: first, one of the |
---|
569 | n/a | MSVC program search paths from the registry; next, the directories |
---|
570 | n/a | in the PATH environment variable. If any of those work, return an |
---|
571 | n/a | absolute path that is known to exist. If none of them work, just |
---|
572 | n/a | return the original program name, 'exe'. |
---|
573 | n/a | """ |
---|
574 | n/a | for p in self.__paths: |
---|
575 | n/a | fn = os.path.join(os.path.abspath(p), exe) |
---|
576 | n/a | if os.path.isfile(fn): |
---|
577 | n/a | return fn |
---|
578 | n/a | |
---|
579 | n/a | # didn't find it; try existing path |
---|
580 | n/a | for p in os.environ['Path'].split(';'): |
---|
581 | n/a | fn = os.path.join(os.path.abspath(p),exe) |
---|
582 | n/a | if os.path.isfile(fn): |
---|
583 | n/a | return fn |
---|
584 | n/a | |
---|
585 | n/a | return exe |
---|
586 | n/a | |
---|
587 | n/a | def get_msvc_paths(self, path, platform='x86'): |
---|
588 | n/a | """Get a list of devstudio directories (include, lib or path). |
---|
589 | n/a | |
---|
590 | n/a | Return a list of strings. The list will be empty if unable to |
---|
591 | n/a | access the registry or appropriate registry keys not found. |
---|
592 | n/a | """ |
---|
593 | n/a | if not _can_read_reg: |
---|
594 | n/a | return [] |
---|
595 | n/a | |
---|
596 | n/a | path = path + " dirs" |
---|
597 | n/a | if self.__version >= 7: |
---|
598 | n/a | key = (r"%s\%0.1f\VC\VC_OBJECTS_PLATFORM_INFO\Win32\Directories" |
---|
599 | n/a | % (self.__root, self.__version)) |
---|
600 | n/a | else: |
---|
601 | n/a | key = (r"%s\6.0\Build System\Components\Platforms" |
---|
602 | n/a | r"\Win32 (%s)\Directories" % (self.__root, platform)) |
---|
603 | n/a | |
---|
604 | n/a | for base in HKEYS: |
---|
605 | n/a | d = read_values(base, key) |
---|
606 | n/a | if d: |
---|
607 | n/a | if self.__version >= 7: |
---|
608 | n/a | return self.__macros.sub(d[path]).split(";") |
---|
609 | n/a | else: |
---|
610 | n/a | return d[path].split(";") |
---|
611 | n/a | # MSVC 6 seems to create the registry entries we need only when |
---|
612 | n/a | # the GUI is run. |
---|
613 | n/a | if self.__version == 6: |
---|
614 | n/a | for base in HKEYS: |
---|
615 | n/a | if read_values(base, r"%s\6.0" % self.__root) is not None: |
---|
616 | n/a | self.warn("It seems you have Visual Studio 6 installed, " |
---|
617 | n/a | "but the expected registry settings are not present.\n" |
---|
618 | n/a | "You must at least run the Visual Studio GUI once " |
---|
619 | n/a | "so that these entries are created.") |
---|
620 | n/a | break |
---|
621 | n/a | return [] |
---|
622 | n/a | |
---|
623 | n/a | def set_path_env_var(self, name): |
---|
624 | n/a | """Set environment variable 'name' to an MSVC path type value. |
---|
625 | n/a | |
---|
626 | n/a | This is equivalent to a SET command prior to execution of spawned |
---|
627 | n/a | commands. |
---|
628 | n/a | """ |
---|
629 | n/a | |
---|
630 | n/a | if name == "lib": |
---|
631 | n/a | p = self.get_msvc_paths("library") |
---|
632 | n/a | else: |
---|
633 | n/a | p = self.get_msvc_paths(name) |
---|
634 | n/a | if p: |
---|
635 | n/a | os.environ[name] = ';'.join(p) |
---|
636 | n/a | |
---|
637 | n/a | |
---|
638 | n/a | if get_build_version() >= 8.0: |
---|
639 | n/a | log.debug("Importing new compiler from distutils.msvc9compiler") |
---|
640 | n/a | OldMSVCCompiler = MSVCCompiler |
---|
641 | n/a | from distutils.msvc9compiler import MSVCCompiler |
---|
642 | n/a | # get_build_architecture not really relevant now we support cross-compile |
---|
643 | n/a | from distutils.msvc9compiler import MacroExpander |
---|