OLD | NEW |
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import errno | 5 import errno |
6 import os | 6 import os |
7 import re | 7 import re |
8 import subprocess | 8 import subprocess |
9 import sys | 9 import sys |
10 | 10 |
11 """ | 11 """ |
12 Copies the given "win tool" (which the toolchain uses to wrap compiler | 12 Copies the given "win tool" (which the toolchain uses to wrap compiler |
13 invocations) and the environment blocks for the 32-bit and 64-bit builds on | 13 invocations) and the environment blocks for the 32-bit and 64-bit builds on |
14 Windows to the build directory. | 14 Windows to the build directory. |
15 | 15 |
16 The arguments are the visual studio install location and the location of the | 16 The arguments are the visual studio install location and the location of the |
17 win tool. The script assumes that the root build directory is the current dir | 17 win tool. The script assumes that the root build directory is the current dir |
18 and the files will be written to the current directory. | 18 and the files will be written to the current directory. |
19 """ | 19 """ |
20 | 20 |
21 | 21 |
22 def ExtractImportantEnvironment(): | 22 def _ExtractImportantEnvironment(output_of_set): |
23 """Extracts environment variables required for the toolchain from the | 23 """Extracts environment variables required for the toolchain to run from |
24 current environment.""" | 24 a textual dump output by the cmd.exe 'set' command.""" |
25 envvars_to_save = ( | 25 envvars_to_save = ( |
26 'goma_.*', # TODO(scottmg): This is ugly, but needed for goma. | 26 'goma_.*', # TODO(scottmg): This is ugly, but needed for goma. |
27 'include', # Needed by midl compiler. | 27 'include', |
| 28 'lib', |
| 29 'libpath', |
28 'path', | 30 'path', |
29 'pathext', | 31 'pathext', |
30 'systemroot', | 32 'systemroot', |
31 'temp', | 33 'temp', |
32 'tmp', | 34 'tmp', |
33 ) | 35 ) |
34 result = {} | 36 env = {} |
35 for envvar in envvars_to_save: | 37 for line in output_of_set.splitlines(): |
36 if envvar in os.environ: | 38 for envvar in envvars_to_save: |
37 envvar = envvar.lower() | 39 if re.match(envvar + '=', line.lower()): |
38 if envvar == 'path': | 40 var, setting = line.split('=', 1) |
39 # Our own rules (for running gyp-win-tool) and other actions in | 41 if envvar == 'path': |
40 # Chromium rely on python being in the path. Add the path to this | 42 # Our own rules (for running gyp-win-tool) and other actions in |
41 # python here so that if it's not in the path when ninja is run | 43 # Chromium rely on python being in the path. Add the path to this |
42 # later, python will still be found. | 44 # python here so that if it's not in the path when ninja is run |
43 result[envvar.upper()] = os.path.dirname(sys.executable) + \ | 45 # later, python will still be found. |
44 os.pathsep + os.environ[envvar] | 46 setting = os.path.dirname(sys.executable) + os.pathsep + setting |
45 else: | 47 env[var.upper()] = setting |
46 result[envvar.upper()] = os.environ[envvar] | 48 break |
47 for required in ('SYSTEMROOT', 'TEMP', 'TMP'): | 49 for required in ('SYSTEMROOT', 'TEMP', 'TMP'): |
48 if required not in result: | 50 if required not in env: |
49 raise Exception('Environment variable "%s" ' | 51 raise Exception('Environment variable "%s" ' |
50 'required to be set to valid path' % required) | 52 'required to be set to valid path' % required) |
51 return result | 53 return env |
52 | 54 |
53 | 55 |
54 def FormatAsEnvironmentBlock(envvar_dict): | 56 def _SetupScript(target_arch, sdk_dir): |
| 57 """Returns a command (with arguments) to be used to set up the |
| 58 environment.""" |
| 59 # Check if we are running in the SDK command line environment and use |
| 60 # the setup script from the SDK if so. |target_arch| should be either |
| 61 # 'x86' or 'x64'. |
| 62 assert target_arch in ('x86', 'x64') |
| 63 if bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', 1))) and sdk_dir: |
| 64 return [os.path.normpath(os.path.join(sdk_dir, 'Bin/SetEnv.Cmd')), |
| 65 '/' + target_arch] |
| 66 else: |
| 67 # We only support x64-hosted tools. |
| 68 # TODO(scottmg|dpranke): Non-depot_tools toolchain: need to get Visual |
| 69 # Studio install location from registry. |
| 70 return [os.path.normpath(os.path.join(FIND_VS_IN_REG, 'VC/vcvarsall.bat')), |
| 71 'amd64_x86' if target_arch == 'x86' else 'amd64'] |
| 72 |
| 73 |
| 74 def _FormatAsEnvironmentBlock(envvar_dict): |
55 """Format as an 'environment block' directly suitable for CreateProcess. | 75 """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 | 76 Briefly this is a list of key=value\0, terminated by an additional \0. See |
57 CreateProcess documentation for more details.""" | 77 CreateProcess documentation for more details.""" |
58 block = '' | 78 block = '' |
59 nul = '\0' | 79 nul = '\0' |
60 for key, value in envvar_dict.iteritems(): | 80 for key, value in envvar_dict.iteritems(): |
61 block += key + '=' + value + nul | 81 block += key + '=' + value + nul |
62 block += nul | 82 block += nul |
63 return block | 83 return block |
64 | 84 |
65 | 85 |
66 def CopyTool(source_path): | 86 def _CopyTool(source_path): |
67 """Copies the given tool to the current directory, including a warning not | 87 """Copies the given tool to the current directory, including a warning not |
68 to edit it.""" | 88 to edit it.""" |
69 with open(source_path) as source_file: | 89 with open(source_path) as source_file: |
70 tool_source = source_file.readlines() | 90 tool_source = source_file.readlines() |
71 | 91 |
72 # Add header and write it out to the current directory (which should be the | 92 # Add header and write it out to the current directory (which should be the |
73 # root build dir). | 93 # root build dir). |
74 with open("gyp-win-tool", 'w') as tool_file: | 94 with open("gyp-win-tool", 'w') as tool_file: |
75 tool_file.write(''.join([tool_source[0], | 95 tool_file.write(''.join([tool_source[0], |
76 '# Generated by setup_toolchain.py do not edit.\n'] | 96 '# Generated by setup_toolchain.py do not edit.\n'] |
77 + tool_source[1:])) | 97 + tool_source[1:])) |
78 | 98 |
79 if len(sys.argv) != 4: | |
80 print('Usage setup_toolchain.py ' | |
81 '<visual studio path> <win tool path> <win sdk path>') | |
82 sys.exit(2) | |
83 vs_path = sys.argv[1] | |
84 tool_source = sys.argv[2] | |
85 win_sdk_path = sys.argv[3] | |
86 | 99 |
87 CopyTool(tool_source) | 100 def main(): |
| 101 if len(sys.argv) != 5: |
| 102 print('Usage setup_toolchain.py ' |
| 103 '<visual studio path> <win tool path> <win sdk path> <runtime dirs>') |
| 104 sys.exit(2) |
| 105 vs_path = sys.argv[1] |
| 106 tool_source = sys.argv[2] |
| 107 win_sdk_path = sys.argv[3] |
| 108 runtime_dirs = sys.argv[4] |
88 | 109 |
89 important_env_vars = ExtractImportantEnvironment() | 110 _CopyTool(tool_source) |
90 path = important_env_vars["PATH"].split(";") | |
91 | 111 |
92 # Add 32-bit compiler path to the beginning and write the block. | 112 archs = ('x86', 'x64') |
93 path32 = [os.path.join(vs_path, "VC\\BIN")] + \ | 113 # TODO(scottmg|goma): Do we need an equivalent of |
94 [os.path.join(win_sdk_path, "bin\\x86")] + \ | 114 # ninja_use_custom_environment_files? |
95 path | 115 for arch in archs: |
96 important_env_vars["PATH"] = ";".join(path32) | 116 # Extract environment variables for subprocesses. |
97 environ = FormatAsEnvironmentBlock(important_env_vars) | 117 args = _SetupScript(arch, win_sdk_path) |
98 with open('environment.x86', 'wb') as env_file: | 118 args.extend(('&&', 'set')) |
99 env_file.write(environ) | 119 popen = subprocess.Popen( |
| 120 args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 121 variables, _ = popen.communicate() |
| 122 env = _ExtractImportantEnvironment(variables) |
| 123 env['PATH'] = runtime_dirs + ';' + env['PATH'] |
100 | 124 |
101 # Add 64-bit compiler path to the beginning and write the block. | 125 # TODO(scottmg|thakis|dpranke): Is there an equivalent to |
102 path64 = [os.path.join(vs_path, "VC\\BIN\\amd64")] + \ | 126 # msvs_system_include_dirs that we need to inject into INCLUDE here? |
103 [os.path.join(win_sdk_path, "bin\\x64")] + \ | 127 |
104 path | 128 env_block = _FormatAsEnvironmentBlock(env) |
105 important_env_vars["PATH"] = ";".join(path64) | 129 with open('environment.' + arch, 'wb') as f: |
106 environ = FormatAsEnvironmentBlock(important_env_vars) | 130 f.write(env_block) |
107 with open('environment.x64', 'wb') as env_file: | 131 |
108 env_file.write(environ) | 132 |
| 133 if __name__ == '__main__': |
| 134 main() |
OLD | NEW |