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