1
2
3
4
5
6
7
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import re
import sys
def ExtractImportantEnvironment():
"""Extracts environment variables required for the toolchain from the
current environment."""
envvars_to_save = (
'goma_.*', # TODO(scottmg): This is ugly, but needed for goma.
'Path',
'PATHEXT',
'SystemRoot',
'TEMP',
'TMP',
)
result = {}
for envvar in envvars_to_save:
if envvar in os.environ:
if envvar == 'Path':
# Our own rules (for running gyp-win-tool) and other actions in
# Chromium rely on python being in the path. Add the path to this
# python here so that if it's not in the path when ninja is run
# later, python will still be found.
result[envvar.upper()] = os.path.dirname(sys.executable) + \
os.pathsep + os.environ[envvar]
else:
result[envvar.upper()] = os.environ[envvar]
for required in ('SYSTEMROOT', 'TEMP', 'TMP'):
if required not in result:
raise Exception('Environment variable "%s" '
'required to be set to valid path' % required)
return result
def FormatAsEnvironmentBlock(envvar_dict):
"""Format as an 'environment block' directly suitable for CreateProcess.
Briefly this is a list of key=value\0, terminated by an additional \0. See
CreateProcess documentation for more details."""
block = ''
nul = '\0'
for key, value in envvar_dict.iteritems():
block += key + '=' + value + nul
block += nul
return block
def CopyTool(source_path):
"""Copies the given tool to the current directory, including a warning not
to edit it."""
with open(source_path) as source_file:
tool_source = source_file.readlines()
# Add header and write it out to the current directory (which should be the
# root build dir).
with open("gyp-win-tool", 'w') as tool_file:
tool_file.write(''.join([tool_source[0],
'# Generated by setup_toolchain.py do not edit.\n']
+ tool_source[1:]))
# Find the tool source, it's the first argument, and copy it.
if len(sys.argv) != 2:
print "Need one argument (win_tool source path)."
sys.exit(1)
CopyTool(sys.argv[1])
# Write the environment file to the current directory.
environ = FormatAsEnvironmentBlock(ExtractImportantEnvironment())
with open('environment.x86', 'wb') as env_file:
env_file.write(environ)
|