- add sources.
[platform/framework/web/crosswalk.git] / src / tools / gn / secondary / build / toolchain / win / setup_toolchain.py
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.
4
5 import os
6 import re
7 import sys
8
9 def ExtractImportantEnvironment():
10   """Extracts environment variables required for the toolchain from the
11   current environment."""
12   envvars_to_save = (
13       'goma_.*', # TODO(scottmg): This is ugly, but needed for goma.
14       'Path',
15       'PATHEXT',
16       'SystemRoot',
17       'TEMP',
18       'TMP',
19       )
20   result = {}
21   for envvar in envvars_to_save:
22     if envvar in os.environ:
23       if envvar == 'Path':
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]
30       else:
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)
36   return result
37
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."""
42   block = ''
43   nul = '\0'
44   for key, value in envvar_dict.iteritems():
45     block += key + '=' + value + nul
46   block += nul
47   return block
48
49 def CopyTool(source_path):
50   """Copies the given tool to the current directory, including a warning not
51   to edit it."""
52   with open(source_path) as source_file:
53     tool_source = source_file.readlines()
54
55   # Add header and write it out to the current directory (which should be the
56   # root build dir).
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']
60                             + tool_source[1:]))
61
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)."
65   sys.exit(1)
66 CopyTool(sys.argv[1])
67
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)
72