1 | n/a | """distutils._msvccompiler |
---|
2 | n/a | |
---|
3 | n/a | Contains MSVCCompiler, an implementation of the abstract CCompiler class |
---|
4 | n/a | for Microsoft Visual Studio 2015. |
---|
5 | n/a | |
---|
6 | n/a | The module is compatible with VS 2015 and later. You can find legacy support |
---|
7 | n/a | for older versions in distutils.msvc9compiler and 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 VS 2005 and VS 2008 by Christian Heimes |
---|
14 | n/a | # ported to VS 2015 by Steve Dower |
---|
15 | n/a | |
---|
16 | n/a | import os |
---|
17 | n/a | import shutil |
---|
18 | n/a | import stat |
---|
19 | n/a | import subprocess |
---|
20 | n/a | |
---|
21 | n/a | from distutils.errors import DistutilsExecError, DistutilsPlatformError, \ |
---|
22 | n/a | CompileError, LibError, LinkError |
---|
23 | n/a | from distutils.ccompiler import CCompiler, 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 | from itertools import count |
---|
29 | n/a | |
---|
30 | n/a | def _find_vcvarsall(plat_spec): |
---|
31 | n/a | try: |
---|
32 | n/a | key = winreg.OpenKeyEx( |
---|
33 | n/a | winreg.HKEY_LOCAL_MACHINE, |
---|
34 | n/a | r"Software\Microsoft\VisualStudio\SxS\VC7", |
---|
35 | n/a | access=winreg.KEY_READ | winreg.KEY_WOW64_32KEY |
---|
36 | n/a | ) |
---|
37 | n/a | except OSError: |
---|
38 | n/a | log.debug("Visual C++ is not registered") |
---|
39 | n/a | return None, None |
---|
40 | n/a | |
---|
41 | n/a | with key: |
---|
42 | n/a | best_version = 0 |
---|
43 | n/a | best_dir = None |
---|
44 | n/a | for i in count(): |
---|
45 | n/a | try: |
---|
46 | n/a | v, vc_dir, vt = winreg.EnumValue(key, i) |
---|
47 | n/a | except OSError: |
---|
48 | n/a | break |
---|
49 | n/a | if v and vt == winreg.REG_SZ and os.path.isdir(vc_dir): |
---|
50 | n/a | try: |
---|
51 | n/a | version = int(float(v)) |
---|
52 | n/a | except (ValueError, TypeError): |
---|
53 | n/a | continue |
---|
54 | n/a | if version >= 14 and version > best_version: |
---|
55 | n/a | best_version, best_dir = version, vc_dir |
---|
56 | n/a | if not best_version: |
---|
57 | n/a | log.debug("No suitable Visual C++ version found") |
---|
58 | n/a | return None, None |
---|
59 | n/a | |
---|
60 | n/a | vcvarsall = os.path.join(best_dir, "vcvarsall.bat") |
---|
61 | n/a | if not os.path.isfile(vcvarsall): |
---|
62 | n/a | log.debug("%s cannot be found", vcvarsall) |
---|
63 | n/a | return None, None |
---|
64 | n/a | |
---|
65 | n/a | vcruntime = None |
---|
66 | n/a | vcruntime_spec = _VCVARS_PLAT_TO_VCRUNTIME_REDIST.get(plat_spec) |
---|
67 | n/a | if vcruntime_spec: |
---|
68 | n/a | vcruntime = os.path.join(best_dir, |
---|
69 | n/a | vcruntime_spec.format(best_version)) |
---|
70 | n/a | if not os.path.isfile(vcruntime): |
---|
71 | n/a | log.debug("%s cannot be found", vcruntime) |
---|
72 | n/a | vcruntime = None |
---|
73 | n/a | |
---|
74 | n/a | return vcvarsall, vcruntime |
---|
75 | n/a | |
---|
76 | n/a | def _get_vc_env(plat_spec): |
---|
77 | n/a | if os.getenv("DISTUTILS_USE_SDK"): |
---|
78 | n/a | return { |
---|
79 | n/a | key.lower(): value |
---|
80 | n/a | for key, value in os.environ.items() |
---|
81 | n/a | } |
---|
82 | n/a | |
---|
83 | n/a | vcvarsall, vcruntime = _find_vcvarsall(plat_spec) |
---|
84 | n/a | if not vcvarsall: |
---|
85 | n/a | raise DistutilsPlatformError("Unable to find vcvarsall.bat") |
---|
86 | n/a | |
---|
87 | n/a | try: |
---|
88 | n/a | out = subprocess.check_output( |
---|
89 | n/a | 'cmd /u /c "{}" {} && set'.format(vcvarsall, plat_spec), |
---|
90 | n/a | stderr=subprocess.STDOUT, |
---|
91 | n/a | ).decode('utf-16le', errors='replace') |
---|
92 | n/a | except subprocess.CalledProcessError as exc: |
---|
93 | n/a | log.error(exc.output) |
---|
94 | n/a | raise DistutilsPlatformError("Error executing {}" |
---|
95 | n/a | .format(exc.cmd)) |
---|
96 | n/a | |
---|
97 | n/a | env = { |
---|
98 | n/a | key.lower(): value |
---|
99 | n/a | for key, _, value in |
---|
100 | n/a | (line.partition('=') for line in out.splitlines()) |
---|
101 | n/a | if key and value |
---|
102 | n/a | } |
---|
103 | n/a | |
---|
104 | n/a | if vcruntime: |
---|
105 | n/a | env['py_vcruntime_redist'] = vcruntime |
---|
106 | n/a | return env |
---|
107 | n/a | |
---|
108 | n/a | def _find_exe(exe, paths=None): |
---|
109 | n/a | """Return path to an MSVC executable program. |
---|
110 | n/a | |
---|
111 | n/a | Tries to find the program in several places: first, one of the |
---|
112 | n/a | MSVC program search paths from the registry; next, the directories |
---|
113 | n/a | in the PATH environment variable. If any of those work, return an |
---|
114 | n/a | absolute path that is known to exist. If none of them work, just |
---|
115 | n/a | return the original program name, 'exe'. |
---|
116 | n/a | """ |
---|
117 | n/a | if not paths: |
---|
118 | n/a | paths = os.getenv('path').split(os.pathsep) |
---|
119 | n/a | for p in paths: |
---|
120 | n/a | fn = os.path.join(os.path.abspath(p), exe) |
---|
121 | n/a | if os.path.isfile(fn): |
---|
122 | n/a | return fn |
---|
123 | n/a | return exe |
---|
124 | n/a | |
---|
125 | n/a | # A map keyed by get_platform() return values to values accepted by |
---|
126 | n/a | # 'vcvarsall.bat'. Always cross-compile from x86 to work with the |
---|
127 | n/a | # lighter-weight MSVC installs that do not include native 64-bit tools. |
---|
128 | n/a | PLAT_TO_VCVARS = { |
---|
129 | n/a | 'win32' : 'x86', |
---|
130 | n/a | 'win-amd64' : 'x86_amd64', |
---|
131 | n/a | } |
---|
132 | n/a | |
---|
133 | n/a | # A map keyed by get_platform() return values to the file under |
---|
134 | n/a | # the VC install directory containing the vcruntime redistributable. |
---|
135 | n/a | _VCVARS_PLAT_TO_VCRUNTIME_REDIST = { |
---|
136 | n/a | 'x86' : 'redist\\x86\\Microsoft.VC{0}0.CRT\\vcruntime{0}0.dll', |
---|
137 | n/a | 'amd64' : 'redist\\x64\\Microsoft.VC{0}0.CRT\\vcruntime{0}0.dll', |
---|
138 | n/a | 'x86_amd64' : 'redist\\x64\\Microsoft.VC{0}0.CRT\\vcruntime{0}0.dll', |
---|
139 | n/a | } |
---|
140 | n/a | |
---|
141 | n/a | # A set containing the DLLs that are guaranteed to be available for |
---|
142 | n/a | # all micro versions of this Python version. Known extension |
---|
143 | n/a | # dependencies that are not in this set will be copied to the output |
---|
144 | n/a | # path. |
---|
145 | n/a | _BUNDLED_DLLS = frozenset(['vcruntime140.dll']) |
---|
146 | n/a | |
---|
147 | n/a | class MSVCCompiler(CCompiler) : |
---|
148 | n/a | """Concrete class that implements an interface to Microsoft Visual C++, |
---|
149 | n/a | as defined by the CCompiler abstract class.""" |
---|
150 | n/a | |
---|
151 | n/a | compiler_type = 'msvc' |
---|
152 | n/a | |
---|
153 | n/a | # Just set this so CCompiler's constructor doesn't barf. We currently |
---|
154 | n/a | # don't use the 'set_executables()' bureaucracy provided by CCompiler, |
---|
155 | n/a | # as it really isn't necessary for this sort of single-compiler class. |
---|
156 | n/a | # Would be nice to have a consistent interface with UnixCCompiler, |
---|
157 | n/a | # though, so it's worth thinking about. |
---|
158 | n/a | executables = {} |
---|
159 | n/a | |
---|
160 | n/a | # Private class data (need to distinguish C from C++ source for compiler) |
---|
161 | n/a | _c_extensions = ['.c'] |
---|
162 | n/a | _cpp_extensions = ['.cc', '.cpp', '.cxx'] |
---|
163 | n/a | _rc_extensions = ['.rc'] |
---|
164 | n/a | _mc_extensions = ['.mc'] |
---|
165 | n/a | |
---|
166 | n/a | # Needed for the filename generation methods provided by the |
---|
167 | n/a | # base class, CCompiler. |
---|
168 | n/a | src_extensions = (_c_extensions + _cpp_extensions + |
---|
169 | n/a | _rc_extensions + _mc_extensions) |
---|
170 | n/a | res_extension = '.res' |
---|
171 | n/a | obj_extension = '.obj' |
---|
172 | n/a | static_lib_extension = '.lib' |
---|
173 | n/a | shared_lib_extension = '.dll' |
---|
174 | n/a | static_lib_format = shared_lib_format = '%s%s' |
---|
175 | n/a | exe_extension = '.exe' |
---|
176 | n/a | |
---|
177 | n/a | |
---|
178 | n/a | def __init__(self, verbose=0, dry_run=0, force=0): |
---|
179 | n/a | CCompiler.__init__ (self, verbose, dry_run, force) |
---|
180 | n/a | # target platform (.plat_name is consistent with 'bdist') |
---|
181 | n/a | self.plat_name = None |
---|
182 | n/a | self.initialized = False |
---|
183 | n/a | |
---|
184 | n/a | def initialize(self, plat_name=None): |
---|
185 | n/a | # multi-init means we would need to check platform same each time... |
---|
186 | n/a | assert not self.initialized, "don't init multiple times" |
---|
187 | n/a | if plat_name is None: |
---|
188 | n/a | plat_name = get_platform() |
---|
189 | n/a | # sanity check for platforms to prevent obscure errors later. |
---|
190 | n/a | if plat_name not in PLAT_TO_VCVARS: |
---|
191 | n/a | raise DistutilsPlatformError("--plat-name must be one of {}" |
---|
192 | n/a | .format(tuple(PLAT_TO_VCVARS))) |
---|
193 | n/a | |
---|
194 | n/a | # Get the vcvarsall.bat spec for the requested platform. |
---|
195 | n/a | plat_spec = PLAT_TO_VCVARS[plat_name] |
---|
196 | n/a | |
---|
197 | n/a | vc_env = _get_vc_env(plat_spec) |
---|
198 | n/a | if not vc_env: |
---|
199 | n/a | raise DistutilsPlatformError("Unable to find a compatible " |
---|
200 | n/a | "Visual Studio installation.") |
---|
201 | n/a | |
---|
202 | n/a | self._paths = vc_env.get('path', '') |
---|
203 | n/a | paths = self._paths.split(os.pathsep) |
---|
204 | n/a | self.cc = _find_exe("cl.exe", paths) |
---|
205 | n/a | self.linker = _find_exe("link.exe", paths) |
---|
206 | n/a | self.lib = _find_exe("lib.exe", paths) |
---|
207 | n/a | self.rc = _find_exe("rc.exe", paths) # resource compiler |
---|
208 | n/a | self.mc = _find_exe("mc.exe", paths) # message compiler |
---|
209 | n/a | self.mt = _find_exe("mt.exe", paths) # message compiler |
---|
210 | n/a | self._vcruntime_redist = vc_env.get('py_vcruntime_redist', '') |
---|
211 | n/a | |
---|
212 | n/a | for dir in vc_env.get('include', '').split(os.pathsep): |
---|
213 | n/a | if dir: |
---|
214 | n/a | self.add_include_dir(dir) |
---|
215 | n/a | |
---|
216 | n/a | for dir in vc_env.get('lib', '').split(os.pathsep): |
---|
217 | n/a | if dir: |
---|
218 | n/a | self.add_library_dir(dir) |
---|
219 | n/a | |
---|
220 | n/a | self.preprocess_options = None |
---|
221 | n/a | # If vcruntime_redist is available, link against it dynamically. Otherwise, |
---|
222 | n/a | # use /MT[d] to build statically, then switch from libucrt[d].lib to ucrt[d].lib |
---|
223 | n/a | # later to dynamically link to ucrtbase but not vcruntime. |
---|
224 | n/a | self.compile_options = [ |
---|
225 | n/a | '/nologo', '/Ox', '/W3', '/GL', '/DNDEBUG' |
---|
226 | n/a | ] |
---|
227 | n/a | self.compile_options.append('/MD' if self._vcruntime_redist else '/MT') |
---|
228 | n/a | |
---|
229 | n/a | self.compile_options_debug = [ |
---|
230 | n/a | '/nologo', '/Od', '/MDd', '/Zi', '/W3', '/D_DEBUG' |
---|
231 | n/a | ] |
---|
232 | n/a | |
---|
233 | n/a | ldflags = [ |
---|
234 | n/a | '/nologo', '/INCREMENTAL:NO', '/LTCG' |
---|
235 | n/a | ] |
---|
236 | n/a | if not self._vcruntime_redist: |
---|
237 | n/a | ldflags.extend(('/nodefaultlib:libucrt.lib', 'ucrt.lib')) |
---|
238 | n/a | |
---|
239 | n/a | ldflags_debug = [ |
---|
240 | n/a | '/nologo', '/INCREMENTAL:NO', '/LTCG', '/DEBUG:FULL' |
---|
241 | n/a | ] |
---|
242 | n/a | |
---|
243 | n/a | self.ldflags_exe = [*ldflags, '/MANIFEST:EMBED,ID=1'] |
---|
244 | n/a | self.ldflags_exe_debug = [*ldflags_debug, '/MANIFEST:EMBED,ID=1'] |
---|
245 | n/a | self.ldflags_shared = [*ldflags, '/DLL', '/MANIFEST:EMBED,ID=2', '/MANIFESTUAC:NO'] |
---|
246 | n/a | self.ldflags_shared_debug = [*ldflags_debug, '/DLL', '/MANIFEST:EMBED,ID=2', '/MANIFESTUAC:NO'] |
---|
247 | n/a | self.ldflags_static = [*ldflags] |
---|
248 | n/a | self.ldflags_static_debug = [*ldflags_debug] |
---|
249 | n/a | |
---|
250 | n/a | self._ldflags = { |
---|
251 | n/a | (CCompiler.EXECUTABLE, None): self.ldflags_exe, |
---|
252 | n/a | (CCompiler.EXECUTABLE, False): self.ldflags_exe, |
---|
253 | n/a | (CCompiler.EXECUTABLE, True): self.ldflags_exe_debug, |
---|
254 | n/a | (CCompiler.SHARED_OBJECT, None): self.ldflags_shared, |
---|
255 | n/a | (CCompiler.SHARED_OBJECT, False): self.ldflags_shared, |
---|
256 | n/a | (CCompiler.SHARED_OBJECT, True): self.ldflags_shared_debug, |
---|
257 | n/a | (CCompiler.SHARED_LIBRARY, None): self.ldflags_static, |
---|
258 | n/a | (CCompiler.SHARED_LIBRARY, False): self.ldflags_static, |
---|
259 | n/a | (CCompiler.SHARED_LIBRARY, True): self.ldflags_static_debug, |
---|
260 | n/a | } |
---|
261 | n/a | |
---|
262 | n/a | self.initialized = True |
---|
263 | n/a | |
---|
264 | n/a | # -- Worker methods ------------------------------------------------ |
---|
265 | n/a | |
---|
266 | n/a | def object_filenames(self, |
---|
267 | n/a | source_filenames, |
---|
268 | n/a | strip_dir=0, |
---|
269 | n/a | output_dir=''): |
---|
270 | n/a | ext_map = { |
---|
271 | n/a | **{ext: self.obj_extension for ext in self.src_extensions}, |
---|
272 | n/a | **{ext: self.res_extension for ext in self._rc_extensions + self._mc_extensions}, |
---|
273 | n/a | } |
---|
274 | n/a | |
---|
275 | n/a | output_dir = output_dir or '' |
---|
276 | n/a | |
---|
277 | n/a | def make_out_path(p): |
---|
278 | n/a | base, ext = os.path.splitext(p) |
---|
279 | n/a | if strip_dir: |
---|
280 | n/a | base = os.path.basename(base) |
---|
281 | n/a | else: |
---|
282 | n/a | _, base = os.path.splitdrive(base) |
---|
283 | n/a | if base.startswith((os.path.sep, os.path.altsep)): |
---|
284 | n/a | base = base[1:] |
---|
285 | n/a | try: |
---|
286 | n/a | # XXX: This may produce absurdly long paths. We should check |
---|
287 | n/a | # the length of the result and trim base until we fit within |
---|
288 | n/a | # 260 characters. |
---|
289 | n/a | return os.path.join(output_dir, base + ext_map[ext]) |
---|
290 | n/a | except LookupError: |
---|
291 | n/a | # Better to raise an exception instead of silently continuing |
---|
292 | n/a | # and later complain about sources and targets having |
---|
293 | n/a | # different lengths |
---|
294 | n/a | raise CompileError("Don't know how to compile {}".format(p)) |
---|
295 | n/a | |
---|
296 | n/a | return list(map(make_out_path, source_filenames)) |
---|
297 | n/a | |
---|
298 | n/a | |
---|
299 | n/a | def compile(self, sources, |
---|
300 | n/a | output_dir=None, macros=None, include_dirs=None, debug=0, |
---|
301 | n/a | extra_preargs=None, extra_postargs=None, depends=None): |
---|
302 | n/a | |
---|
303 | n/a | if not self.initialized: |
---|
304 | n/a | self.initialize() |
---|
305 | n/a | compile_info = self._setup_compile(output_dir, macros, include_dirs, |
---|
306 | n/a | sources, depends, extra_postargs) |
---|
307 | n/a | macros, objects, extra_postargs, pp_opts, build = compile_info |
---|
308 | n/a | |
---|
309 | n/a | compile_opts = extra_preargs or [] |
---|
310 | n/a | compile_opts.append('/c') |
---|
311 | n/a | if debug: |
---|
312 | n/a | compile_opts.extend(self.compile_options_debug) |
---|
313 | n/a | else: |
---|
314 | n/a | compile_opts.extend(self.compile_options) |
---|
315 | n/a | |
---|
316 | n/a | |
---|
317 | n/a | add_cpp_opts = False |
---|
318 | n/a | |
---|
319 | n/a | for obj in objects: |
---|
320 | n/a | try: |
---|
321 | n/a | src, ext = build[obj] |
---|
322 | n/a | except KeyError: |
---|
323 | n/a | continue |
---|
324 | n/a | if debug: |
---|
325 | n/a | # pass the full pathname to MSVC in debug mode, |
---|
326 | n/a | # this allows the debugger to find the source file |
---|
327 | n/a | # without asking the user to browse for it |
---|
328 | n/a | src = os.path.abspath(src) |
---|
329 | n/a | |
---|
330 | n/a | if ext in self._c_extensions: |
---|
331 | n/a | input_opt = "/Tc" + src |
---|
332 | n/a | elif ext in self._cpp_extensions: |
---|
333 | n/a | input_opt = "/Tp" + src |
---|
334 | n/a | add_cpp_opts = True |
---|
335 | n/a | elif ext in self._rc_extensions: |
---|
336 | n/a | # compile .RC to .RES file |
---|
337 | n/a | input_opt = src |
---|
338 | n/a | output_opt = "/fo" + obj |
---|
339 | n/a | try: |
---|
340 | n/a | self.spawn([self.rc] + pp_opts + [output_opt, input_opt]) |
---|
341 | n/a | except DistutilsExecError as msg: |
---|
342 | n/a | raise CompileError(msg) |
---|
343 | n/a | continue |
---|
344 | n/a | elif ext in self._mc_extensions: |
---|
345 | n/a | # Compile .MC to .RC file to .RES file. |
---|
346 | n/a | # * '-h dir' specifies the directory for the |
---|
347 | n/a | # generated include file |
---|
348 | n/a | # * '-r dir' specifies the target directory of the |
---|
349 | n/a | # generated RC file and the binary message resource |
---|
350 | n/a | # it includes |
---|
351 | n/a | # |
---|
352 | n/a | # For now (since there are no options to change this), |
---|
353 | n/a | # we use the source-directory for the include file and |
---|
354 | n/a | # the build directory for the RC file and message |
---|
355 | n/a | # resources. This works at least for win32all. |
---|
356 | n/a | h_dir = os.path.dirname(src) |
---|
357 | n/a | rc_dir = os.path.dirname(obj) |
---|
358 | n/a | try: |
---|
359 | n/a | # first compile .MC to .RC and .H file |
---|
360 | n/a | self.spawn([self.mc, '-h', h_dir, '-r', rc_dir, src]) |
---|
361 | n/a | base, _ = os.path.splitext(os.path.basename (src)) |
---|
362 | n/a | rc_file = os.path.join(rc_dir, base + '.rc') |
---|
363 | n/a | # then compile .RC to .RES file |
---|
364 | n/a | self.spawn([self.rc, "/fo" + obj, rc_file]) |
---|
365 | n/a | |
---|
366 | n/a | except DistutilsExecError as msg: |
---|
367 | n/a | raise CompileError(msg) |
---|
368 | n/a | continue |
---|
369 | n/a | else: |
---|
370 | n/a | # how to handle this file? |
---|
371 | n/a | raise CompileError("Don't know how to compile {} to {}" |
---|
372 | n/a | .format(src, obj)) |
---|
373 | n/a | |
---|
374 | n/a | args = [self.cc] + compile_opts + pp_opts |
---|
375 | n/a | if add_cpp_opts: |
---|
376 | n/a | args.append('/EHsc') |
---|
377 | n/a | args.append(input_opt) |
---|
378 | n/a | args.append("/Fo" + obj) |
---|
379 | n/a | args.extend(extra_postargs) |
---|
380 | n/a | |
---|
381 | n/a | try: |
---|
382 | n/a | self.spawn(args) |
---|
383 | n/a | except DistutilsExecError as msg: |
---|
384 | n/a | raise CompileError(msg) |
---|
385 | n/a | |
---|
386 | n/a | return objects |
---|
387 | n/a | |
---|
388 | n/a | |
---|
389 | n/a | def create_static_lib(self, |
---|
390 | n/a | objects, |
---|
391 | n/a | output_libname, |
---|
392 | n/a | output_dir=None, |
---|
393 | n/a | debug=0, |
---|
394 | n/a | target_lang=None): |
---|
395 | n/a | |
---|
396 | n/a | if not self.initialized: |
---|
397 | n/a | self.initialize() |
---|
398 | n/a | objects, output_dir = self._fix_object_args(objects, output_dir) |
---|
399 | n/a | output_filename = self.library_filename(output_libname, |
---|
400 | n/a | output_dir=output_dir) |
---|
401 | n/a | |
---|
402 | n/a | if self._need_link(objects, output_filename): |
---|
403 | n/a | lib_args = objects + ['/OUT:' + output_filename] |
---|
404 | n/a | if debug: |
---|
405 | n/a | pass # XXX what goes here? |
---|
406 | n/a | try: |
---|
407 | n/a | log.debug('Executing "%s" %s', self.lib, ' '.join(lib_args)) |
---|
408 | n/a | self.spawn([self.lib] + lib_args) |
---|
409 | n/a | except DistutilsExecError as msg: |
---|
410 | n/a | raise LibError(msg) |
---|
411 | n/a | else: |
---|
412 | n/a | log.debug("skipping %s (up-to-date)", output_filename) |
---|
413 | n/a | |
---|
414 | n/a | |
---|
415 | n/a | def link(self, |
---|
416 | n/a | target_desc, |
---|
417 | n/a | objects, |
---|
418 | n/a | output_filename, |
---|
419 | n/a | output_dir=None, |
---|
420 | n/a | libraries=None, |
---|
421 | n/a | library_dirs=None, |
---|
422 | n/a | runtime_library_dirs=None, |
---|
423 | n/a | export_symbols=None, |
---|
424 | n/a | debug=0, |
---|
425 | n/a | extra_preargs=None, |
---|
426 | n/a | extra_postargs=None, |
---|
427 | n/a | build_temp=None, |
---|
428 | n/a | target_lang=None): |
---|
429 | n/a | |
---|
430 | n/a | if not self.initialized: |
---|
431 | n/a | self.initialize() |
---|
432 | n/a | objects, output_dir = self._fix_object_args(objects, output_dir) |
---|
433 | n/a | fixed_args = self._fix_lib_args(libraries, library_dirs, |
---|
434 | n/a | runtime_library_dirs) |
---|
435 | n/a | libraries, library_dirs, runtime_library_dirs = fixed_args |
---|
436 | n/a | |
---|
437 | n/a | if runtime_library_dirs: |
---|
438 | n/a | self.warn("I don't know what to do with 'runtime_library_dirs': " |
---|
439 | n/a | + str(runtime_library_dirs)) |
---|
440 | n/a | |
---|
441 | n/a | lib_opts = gen_lib_options(self, |
---|
442 | n/a | library_dirs, runtime_library_dirs, |
---|
443 | n/a | libraries) |
---|
444 | n/a | if output_dir is not None: |
---|
445 | n/a | output_filename = os.path.join(output_dir, output_filename) |
---|
446 | n/a | |
---|
447 | n/a | if self._need_link(objects, output_filename): |
---|
448 | n/a | ldflags = self._ldflags[target_desc, debug] |
---|
449 | n/a | |
---|
450 | n/a | export_opts = ["/EXPORT:" + sym for sym in (export_symbols or [])] |
---|
451 | n/a | |
---|
452 | n/a | ld_args = (ldflags + lib_opts + export_opts + |
---|
453 | n/a | objects + ['/OUT:' + output_filename]) |
---|
454 | n/a | |
---|
455 | n/a | # The MSVC linker generates .lib and .exp files, which cannot be |
---|
456 | n/a | # suppressed by any linker switches. The .lib files may even be |
---|
457 | n/a | # needed! Make sure they are generated in the temporary build |
---|
458 | n/a | # directory. Since they have different names for debug and release |
---|
459 | n/a | # builds, they can go into the same directory. |
---|
460 | n/a | build_temp = os.path.dirname(objects[0]) |
---|
461 | n/a | if export_symbols is not None: |
---|
462 | n/a | (dll_name, dll_ext) = os.path.splitext( |
---|
463 | n/a | os.path.basename(output_filename)) |
---|
464 | n/a | implib_file = os.path.join( |
---|
465 | n/a | build_temp, |
---|
466 | n/a | self.library_filename(dll_name)) |
---|
467 | n/a | ld_args.append ('/IMPLIB:' + implib_file) |
---|
468 | n/a | |
---|
469 | n/a | if extra_preargs: |
---|
470 | n/a | ld_args[:0] = extra_preargs |
---|
471 | n/a | if extra_postargs: |
---|
472 | n/a | ld_args.extend(extra_postargs) |
---|
473 | n/a | |
---|
474 | n/a | output_dir = os.path.dirname(os.path.abspath(output_filename)) |
---|
475 | n/a | self.mkpath(output_dir) |
---|
476 | n/a | try: |
---|
477 | n/a | log.debug('Executing "%s" %s', self.linker, ' '.join(ld_args)) |
---|
478 | n/a | self.spawn([self.linker] + ld_args) |
---|
479 | n/a | self._copy_vcruntime(output_dir) |
---|
480 | n/a | except DistutilsExecError as msg: |
---|
481 | n/a | raise LinkError(msg) |
---|
482 | n/a | else: |
---|
483 | n/a | log.debug("skipping %s (up-to-date)", output_filename) |
---|
484 | n/a | |
---|
485 | n/a | def _copy_vcruntime(self, output_dir): |
---|
486 | n/a | vcruntime = self._vcruntime_redist |
---|
487 | n/a | if not vcruntime or not os.path.isfile(vcruntime): |
---|
488 | n/a | return |
---|
489 | n/a | |
---|
490 | n/a | if os.path.basename(vcruntime).lower() in _BUNDLED_DLLS: |
---|
491 | n/a | return |
---|
492 | n/a | |
---|
493 | n/a | log.debug('Copying "%s"', vcruntime) |
---|
494 | n/a | vcruntime = shutil.copy(vcruntime, output_dir) |
---|
495 | n/a | os.chmod(vcruntime, stat.S_IWRITE) |
---|
496 | n/a | |
---|
497 | n/a | def spawn(self, cmd): |
---|
498 | n/a | old_path = os.getenv('path') |
---|
499 | n/a | try: |
---|
500 | n/a | os.environ['path'] = self._paths |
---|
501 | n/a | return super().spawn(cmd) |
---|
502 | n/a | finally: |
---|
503 | n/a | os.environ['path'] = old_path |
---|
504 | n/a | |
---|
505 | n/a | # -- Miscellaneous methods ----------------------------------------- |
---|
506 | n/a | # These are all used by the 'gen_lib_options() function, in |
---|
507 | n/a | # ccompiler.py. |
---|
508 | n/a | |
---|
509 | n/a | def library_dir_option(self, dir): |
---|
510 | n/a | return "/LIBPATH:" + dir |
---|
511 | n/a | |
---|
512 | n/a | def runtime_library_dir_option(self, dir): |
---|
513 | n/a | raise DistutilsPlatformError( |
---|
514 | n/a | "don't know how to set runtime library search path for MSVC") |
---|
515 | n/a | |
---|
516 | n/a | def library_option(self, lib): |
---|
517 | n/a | return self.library_filename(lib) |
---|
518 | n/a | |
---|
519 | n/a | def find_library_file(self, dirs, lib, debug=0): |
---|
520 | n/a | # Prefer a debugging library if found (and requested), but deal |
---|
521 | n/a | # with it if we don't have one. |
---|
522 | n/a | if debug: |
---|
523 | n/a | try_names = [lib + "_d", lib] |
---|
524 | n/a | else: |
---|
525 | n/a | try_names = [lib] |
---|
526 | n/a | for dir in dirs: |
---|
527 | n/a | for name in try_names: |
---|
528 | n/a | libfile = os.path.join(dir, self.library_filename(name)) |
---|
529 | n/a | if os.path.isfile(libfile): |
---|
530 | n/a | return libfile |
---|
531 | n/a | else: |
---|
532 | n/a | # Oops, didn't find it in *any* of 'dirs' |
---|
533 | n/a | return None |
---|