1 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
9 def ExtractImportantEnvironment():
10 """Extracts environment variables required for the toolchain from the
11 current environment."""
13 'goma_.*', # TODO(scottmg): This is ugly, but needed for goma.
21 for envvar in envvars_to_save:
22 if envvar in os.environ:
24 # Our own rules (for running gyp-win-tool) and other actions in
25 # Chromium rely on python being in the path. Add the path to this
26 # python here so that if it's not in the path when ninja is run
27 # later, python will still be found.
28 result[envvar.upper()] = os.path.dirname(sys.executable) + \
29 os.pathsep + os.environ[envvar]
31 result[envvar.upper()] = os.environ[envvar]
32 for required in ('SYSTEMROOT', 'TEMP', 'TMP'):
33 if required not in result:
34 raise Exception('Environment variable "%s" '
35 'required to be set to valid path' % required)
38 def FormatAsEnvironmentBlock(envvar_dict):
39 """Format as an 'environment block' directly suitable for CreateProcess.
40 Briefly this is a list of key=value\0, terminated by an additional \0. See
41 CreateProcess documentation for more details."""
44 for key, value in envvar_dict.iteritems():
45 block += key + '=' + value + nul
49 def CopyTool(source_path):
50 """Copies the given tool to the current directory, including a warning not
52 with open(source_path) as source_file:
53 tool_source = source_file.readlines()
55 # Add header and write it out to the current directory (which should be the
57 with open("gyp-win-tool", 'w') as tool_file:
58 tool_file.write(''.join([tool_source[0],
59 '# Generated by setup_toolchain.py do not edit.\n']
62 # Find the tool source, it's the first argument, and copy it.
63 if len(sys.argv) != 2:
64 print "Need one argument (win_tool source path)."
68 # Write the environment file to the current directory.
69 environ = FormatAsEnvironmentBlock(ExtractImportantEnvironment())
70 with open('environment.x86', 'wb') as env_file:
71 env_file.write(environ)