setup_toolchain.py revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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
5import errno
6import os
7import re
8import subprocess
9import sys
10
11"""
12Copies the given "win tool" (which the toolchain uses to wrap compiler
13invocations) and the environment blocks for the 32-bit and 64-bit builds on
14Windows to the build directory.
15
16The arguments are the visual studio install location and the location of the
17win tool. The script assumes that the root build directory is the current dir
18and the files will be written to the current directory.
19"""
20
21
22def ExtractImportantEnvironment():
23  """Extracts environment variables required for the toolchain from the
24  current environment."""
25  envvars_to_save = (
26      'goma_.*',  # TODO(scottmg): This is ugly, but needed for goma.
27      'include',  # Needed by midl compiler.
28      'path',
29      'pathext',
30      'systemroot',
31      'temp',
32      'tmp',
33      )
34  result = {}
35  for envvar in envvars_to_save:
36    if envvar in os.environ:
37      envvar = envvar.lower()
38      if envvar == 'path':
39        # Our own rules (for running gyp-win-tool) and other actions in
40        # Chromium rely on python being in the path. Add the path to this
41        # python here so that if it's not in the path when ninja is run
42        # later, python will still be found.
43        result[envvar.upper()] = os.path.dirname(sys.executable) + \
44            os.pathsep + os.environ[envvar]
45      else:
46        result[envvar.upper()] = os.environ[envvar]
47  for required in ('SYSTEMROOT', 'TEMP', 'TMP'):
48    if required not in result:
49      raise Exception('Environment variable "%s" '
50                      'required to be set to valid path' % required)
51  return result
52
53
54def FormatAsEnvironmentBlock(envvar_dict):
55  """Format as an 'environment block' directly suitable for CreateProcess.
56  Briefly this is a list of key=value\0, terminated by an additional \0. See
57  CreateProcess documentation for more details."""
58  block = ''
59  nul = '\0'
60  for key, value in envvar_dict.iteritems():
61    block += key + '=' + value + nul
62  block += nul
63  return block
64
65
66def CopyTool(source_path):
67  """Copies the given tool to the current directory, including a warning not
68  to edit it."""
69  with open(source_path) as source_file:
70    tool_source = source_file.readlines()
71
72  # Add header and write it out to the current directory (which should be the
73  # root build dir).
74  with open("gyp-win-tool", 'w') as tool_file:
75    tool_file.write(''.join([tool_source[0],
76                             '# Generated by setup_toolchain.py do not edit.\n']
77                            + tool_source[1:]))
78
79if len(sys.argv) != 4:
80  print('Usage setup_toolchain.py '
81        '<visual studio path> <win tool path> <win sdk path>')
82  sys.exit(2)
83vs_path = sys.argv[1]
84tool_source = sys.argv[2]
85win_sdk_path = sys.argv[3]
86
87CopyTool(tool_source)
88
89important_env_vars = ExtractImportantEnvironment()
90path = important_env_vars["PATH"].split(";")
91
92# Add 32-bit compiler path to the beginning and write the block.
93path32 = [os.path.join(vs_path, "VC\\BIN")] + \
94         [os.path.join(win_sdk_path, "bin\\x86")] + \
95         path
96important_env_vars["PATH"] = ";".join(path32)
97environ = FormatAsEnvironmentBlock(important_env_vars)
98with open('environment.x86', 'wb') as env_file:
99  env_file.write(environ)
100
101# Add 64-bit compiler path to the beginning and write the block.
102path64 = [os.path.join(vs_path, "VC\\BIN\\amd64")] + \
103         [os.path.join(win_sdk_path, "bin\\x64")] + \
104         path
105important_env_vars["PATH"] = ";".join(path64)
106environ = FormatAsEnvironmentBlock(important_env_vars)
107with open('environment.x64', 'wb') as env_file:
108  env_file.write(environ)
109