| # 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) |
| |