1 """SCons.Platform.win32
2
3 Platform-specific initialization for Win32 systems.
4
5 There normally shouldn't be any need to import this module directly. It
6 will usually be imported through the generic SCons.Platform.Platform()
7 selection method.
8 """
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 __revision__ = "src/engine/SCons/Platform/win32.py 3a41ed6b288cee8d085373ad7fa02894e1903864 2019-01-23 17:30:35 bdeegan"
34
35 import os
36 import os.path
37 import sys
38 import tempfile
39
40 from SCons.Platform.posix import exitvalmap
41 from SCons.Platform import TempFileMunge
42 from SCons.Platform.virtualenv import ImportVirtualenv
43 from SCons.Platform.virtualenv import ignore_virtualenv, enable_virtualenv
44 import SCons.Util
45
46 try:
47 import msvcrt
48 import win32api
49 import win32con
50
51 msvcrt.get_osfhandle
52 win32api.SetHandleInformation
53 win32con.HANDLE_FLAG_INHERIT
54 except ImportError:
55 parallel_msg = \
56 "you do not seem to have the pywin32 extensions installed;\n" + \
57 "\tparallel (-j) builds may not work reliably with open Python files."
58 except AttributeError:
59 parallel_msg = \
60 "your pywin32 extensions do not support file handle operations;\n" + \
61 "\tparallel (-j) builds may not work reliably with open Python files."
62 else:
63 parallel_msg = None
64
65 _builtin_open = open
66
68 fp = _builtin_open(*args, **kw)
69 win32api.SetHandleInformation(msvcrt.get_osfhandle(fp.fileno()),
70 win32con.HANDLE_FLAG_INHERIT,
71 0)
72 return fp
73
74 open = _scons_open
75
76 if sys.version_info.major == 2:
77 _builtin_file = file
80 _builtin_file.__init__(self, *args, **kw)
81 win32api.SetHandleInformation(msvcrt.get_osfhandle(self.fileno()),
82 win32con.HANDLE_FLAG_INHERIT, 0)
83 file = _scons_file
84 else:
85
86 pass
87
88
89
90 if False:
91
92 try:
93 from ctypes import windll
94 import shutil
95
96 CopyFile = windll.kernel32.CopyFileA
97 SetFileTime = windll.kernel32.SetFileTime
98
99 _shutil_copy = shutil.copy
100 _shutil_copy2 = shutil.copy2
101
102 shutil.copy2 = CopyFile
103
107
108 shutil.copy = win_api_copyfile
109
110 except AttributeError:
111 parallel_msg = \
112 "Couldn't override shutil.copy or shutil.copy2 falling back to shutil defaults"
113
114
115
116
117
118
119
120 try:
121 import threading
122 spawn_lock = threading.Lock()
123
124
125
126
127
128
129
130 - def spawnve(mode, file, args, env):
131 spawn_lock.acquire()
132 try:
133 if mode == os.P_WAIT:
134 ret = os.spawnve(os.P_NOWAIT, file, args, env)
135 else:
136 ret = os.spawnve(mode, file, args, env)
137 finally:
138 spawn_lock.release()
139 if mode == os.P_WAIT:
140 pid, status = os.waitpid(ret, 0)
141 ret = status >> 8
142 return ret
143 except ImportError:
144
145
146
147
148
149
150 - def spawnve(mode, file, args, env):
152
153
154
155
156
157
158 -def piped_spawn(sh, escape, cmd, args, env, stdout, stderr):
159
160
161
162
163
164
165 if not sh:
166 sys.stderr.write("scons: Could not find command interpreter, is it in your PATH?\n")
167 return 127
168 else:
169
170 tmpFileStdout = os.path.normpath(tempfile.mktemp())
171 tmpFileStderr = os.path.normpath(tempfile.mktemp())
172
173
174 stdoutRedirected = 0
175 stderrRedirected = 0
176 for arg in args:
177
178 if arg.find( ">", 0, 1 ) != -1 or arg.find( "1>", 0, 2 ) != -1:
179 stdoutRedirected = 1
180
181 if arg.find( "2>", 0, 2 ) != -1:
182 stderrRedirected = 1
183
184
185 if stdoutRedirected == 0:
186 args.append(">" + str(tmpFileStdout))
187 if stderrRedirected == 0:
188 args.append("2>" + str(tmpFileStderr))
189
190
191 try:
192 args = [sh, '/C', escape(' '.join(args)) ]
193 ret = spawnve(os.P_WAIT, sh, args, env)
194 except OSError as e:
195
196 try:
197 ret = exitvalmap[e.errno]
198 except KeyError:
199 sys.stderr.write("scons: unknown OSError exception code %d - %s: %s\n" % (e.errno, cmd, e.strerror))
200 if stderr is not None:
201 stderr.write("scons: %s: %s\n" % (cmd, e.strerror))
202
203
204 if stdout is not None and stdoutRedirected == 0:
205 try:
206 stdout.write(open( tmpFileStdout, "r" ).read())
207 os.remove( tmpFileStdout )
208 except (IOError, OSError):
209 pass
210
211 if stderr is not None and stderrRedirected == 0:
212 try:
213 stderr.write(open( tmpFileStderr, "r" ).read())
214 os.remove( tmpFileStderr )
215 except (IOError, OSError):
216 pass
217 return ret
218
219
221 try:
222 result = spawnve(os.P_WAIT, l[0], l, env)
223 except (OSError, EnvironmentError) as e:
224 try:
225 result = exitvalmap[e.errno]
226 sys.stderr.write("scons: %s: %s\n" % (l[0], e.strerror))
227 except KeyError:
228 result = 127
229 if len(l) > 2:
230 if len(l[2]) < 1000:
231 command = ' '.join(l[0:3])
232 else:
233 command = l[0]
234 else:
235 command = l[0]
236 sys.stderr.write("scons: unknown OSError exception code %d - '%s': %s\n" % (e.errno, command, e.strerror))
237 return result
238
239
240 -def spawn(sh, escape, cmd, args, env):
241 if not sh:
242 sys.stderr.write("scons: Could not find command interpreter, is it in your PATH?\n")
243 return 127
244 return exec_spawn([sh, '/C', escape(' '.join(args))], env)
245
246
247
248
249
250
252 if x[-1] == '\\':
253 x = x + '\\'
254 return '"' + x + '"'
255
256
257 _system_root = None
258
259
290
291
317
318
320 """
321 Determine which windows CPU were running on.
322 A class for defining architecture-specific settings and logic.
323 """
327
328 SupportedArchitectureList = [
329 ArchDefinition(
330 'x86',
331 ['i386', 'i486', 'i586', 'i686'],
332 ),
333
334 ArchDefinition(
335 'x86_64',
336 ['AMD64', 'amd64', 'em64t', 'EM64T', 'x86_64'],
337 ),
338
339 ArchDefinition(
340 'ia64',
341 ['IA64'],
342 ),
343 ]
344
345 SupportedArchitectureMap = {}
346 for a in SupportedArchitectureList:
347 SupportedArchitectureMap[a.arch] = a
348 for s in a.synonyms:
349 SupportedArchitectureMap[s] = a
350
351
353 """Returns the definition for the specified architecture string.
354
355 If no string is specified, the system default is returned (as defined
356 by the PROCESSOR_ARCHITEW6432 or PROCESSOR_ARCHITECTURE environment
357 variables).
358 """
359 if arch is None:
360 arch = os.environ.get('PROCESSOR_ARCHITEW6432')
361 if not arch:
362 arch = os.environ.get('PROCESSOR_ARCHITECTURE')
363 return SupportedArchitectureMap.get(arch, ArchDefinition('', ['']))
364
365
367
368
369 cmd_interp = ''
370
371 if SCons.Util.can_read_reg:
372 try:
373
374 k=SCons.Util.RegOpenKeyEx(SCons.Util.hkey_mod.HKEY_LOCAL_MACHINE,
375 'Software\\Microsoft\\Windows NT\\CurrentVersion')
376 val, tok = SCons.Util.RegQueryValueEx(k, 'SystemRoot')
377 cmd_interp = os.path.join(val, 'System32\\cmd.exe')
378 except SCons.Util.RegError:
379 try:
380
381 k=SCons.Util.RegOpenKeyEx(SCons.Util.hkey_mod.HKEY_LOCAL_MACHINE,
382 'Software\\Microsoft\\Windows\\CurrentVersion')
383 val, tok = SCons.Util.RegQueryValueEx(k, 'SystemRoot')
384 cmd_interp = os.path.join(val, 'command.com')
385 except KeyboardInterrupt:
386 raise
387 except:
388 pass
389
390
391
392
393
394
395 if not cmd_interp:
396 systemroot = get_system_root()
397 tmp_path = systemroot + os.pathsep + \
398 os.path.join(systemroot,'System32')
399 tmp_pathext = '.com;.exe;.bat;.cmd'
400 if 'PATHEXT' in os.environ:
401 tmp_pathext = os.environ['PATHEXT']
402 cmd_interp = SCons.Util.WhereIs('cmd', tmp_path, tmp_pathext)
403 if not cmd_interp:
404 cmd_interp = SCons.Util.WhereIs('command', tmp_path, tmp_pathext)
405
406 if not cmd_interp:
407 cmd_interp = env.Detect('cmd')
408 if not cmd_interp:
409 cmd_interp = env.Detect('command')
410
411 if 'ENV' not in env:
412 env['ENV'] = {}
413
414
415
416
417
418
419
420
421
422 import_env = ['SystemDrive', 'SystemRoot', 'TEMP', 'TMP' ]
423 for var in import_env:
424 v = os.environ.get(var)
425 if v:
426 env['ENV'][var] = v
427
428 if 'COMSPEC' not in env['ENV']:
429 v = os.environ.get("COMSPEC")
430 if v:
431 env['ENV']['COMSPEC'] = v
432
433 env.AppendENVPath('PATH', get_system_root() + '\\System32')
434
435 env['ENV']['PATHEXT'] = '.COM;.EXE;.BAT;.CMD'
436 env['OBJPREFIX'] = ''
437 env['OBJSUFFIX'] = '.obj'
438 env['SHOBJPREFIX'] = '$OBJPREFIX'
439 env['SHOBJSUFFIX'] = '$OBJSUFFIX'
440 env['PROGPREFIX'] = ''
441 env['PROGSUFFIX'] = '.exe'
442 env['LIBPREFIX'] = ''
443 env['LIBSUFFIX'] = '.lib'
444 env['SHLIBPREFIX'] = ''
445 env['SHLIBSUFFIX'] = '.dll'
446 env['LIBPREFIXES'] = [ '$LIBPREFIX' ]
447 env['LIBSUFFIXES'] = [ '$LIBSUFFIX' ]
448 env['PSPAWN'] = piped_spawn
449 env['SPAWN'] = spawn
450 env['SHELL'] = cmd_interp
451 env['TEMPFILE'] = TempFileMunge
452 env['TEMPFILEPREFIX'] = '@'
453 env['MAXLINELENGTH'] = 2048
454 env['ESCAPE'] = escape
455
456 env['HOST_OS'] = 'win32'
457 env['HOST_ARCH'] = get_architecture().arch
458
459 if enable_virtualenv and not ignore_virtualenv:
460 ImportVirtualenv(env)
461
462
463
464
465
466
467
468