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 # Copies the given "win tool" (which the toolchain uses to wrap compiler | 5 # Copies the given "win tool" (which the toolchain uses to wrap compiler |
6 # invocations) and the environment blocks for the 32-bit and 64-bit builds on | 6 # invocations) and the environment blocks for the 32-bit and 64-bit builds on |
7 # Windows to the build directory. | 7 # Windows to the build directory. |
8 # | 8 # |
9 # The arguments are the visual studio install location and the location of the | 9 # The arguments are the visual studio install location and the location of the |
10 # win tool. The script assumes that the root build directory is the current dir | 10 # win tool. The script assumes that the root build directory is the current dir |
11 # and the files will be written to the current directory. | 11 # and the files will be written to the current directory. |
12 | 12 |
13 import errno | 13 import errno |
| 14 import json |
14 import os | 15 import os |
15 import re | 16 import re |
16 import subprocess | 17 import subprocess |
17 import sys | 18 import sys |
18 | 19 |
19 SCRIPT_DIR = os.path.dirname(__file__) | 20 SCRIPT_DIR = os.path.dirname(__file__) |
20 | 21 |
21 def _ExtractImportantEnvironment(output_of_set): | 22 def _ExtractImportantEnvironment(output_of_set): |
22 """Extracts environment variables required for the toolchain to run from | 23 """Extracts environment variables required for the toolchain to run from |
23 a textual dump output by the cmd.exe 'set' command.""" | 24 a textual dump output by the cmd.exe 'set' command.""" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 """Return path to the GYP_MSVS_VERSION of Visual Studio. | 61 """Return path to the GYP_MSVS_VERSION of Visual Studio. |
61 """ | 62 """ |
62 | 63 |
63 # Use the code in build/vs_toolchain.py to avoid duplicating code. | 64 # Use the code in build/vs_toolchain.py to avoid duplicating code. |
64 chromium_dir = os.path.abspath(os.path.join(SCRIPT_DIR, '..', '..', '..')) | 65 chromium_dir = os.path.abspath(os.path.join(SCRIPT_DIR, '..', '..', '..')) |
65 sys.path.append(os.path.join(chromium_dir, 'build')) | 66 sys.path.append(os.path.join(chromium_dir, 'build')) |
66 import vs_toolchain | 67 import vs_toolchain |
67 return vs_toolchain.DetectVisualStudioPath() | 68 return vs_toolchain.DetectVisualStudioPath() |
68 | 69 |
69 | 70 |
70 def _SetupScript(target_cpu, sdk_dir): | 71 def _LoadEnvFromBat(args): |
71 """Returns a command (with arguments) to be used to set up the | 72 """Given a bat command, runs it and returns env vars set by it.""" |
72 environment.""" | 73 args = args[:] |
73 # Check if we are running in the SDK command line environment and use | |
74 # the setup script from the SDK if so. |target_cpu| should be either | |
75 # 'x86' or 'x64'. | |
76 assert target_cpu in ('x86', 'x64') | |
77 if bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', 1))) and sdk_dir: | |
78 return [os.path.normpath(os.path.join(sdk_dir, 'Bin/SetEnv.Cmd')), | |
79 '/' + target_cpu] | |
80 else: | |
81 if 'GYP_MSVS_OVERRIDE_PATH' not in os.environ: | |
82 os.environ['GYP_MSVS_OVERRIDE_PATH'] = _DetectVisualStudioPath() | |
83 # We only support x64-hosted tools. | |
84 return [os.path.normpath(os.path.join(os.environ['GYP_MSVS_OVERRIDE_PATH'], | |
85 'VC/vcvarsall.bat')), | |
86 'amd64_x86' if target_cpu == 'x86' else 'amd64'] | |
87 | |
88 | |
89 def _LoadToolchainEnv(cpu, win_sdk_path): | |
90 """Returns a dictionary with environment variables that must be set while | |
91 running binaries from the toolchain (e.g. INCLUDE and PATH for cl.exe).""" | |
92 args = _SetupScript(cpu, win_sdk_path) | |
93 args.extend(('&&', 'set')) | 74 args.extend(('&&', 'set')) |
94 popen = subprocess.Popen( | 75 popen = subprocess.Popen( |
95 args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 76 args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
96 variables, _ = popen.communicate() | 77 variables, _ = popen.communicate() |
97 if popen.returncode != 0: | 78 if popen.returncode != 0: |
98 raise Exception('"%s" failed with error %d' % (args, popen.returncode)) | 79 raise Exception('"%s" failed with error %d' % (args, popen.returncode)) |
| 80 return variables |
| 81 |
| 82 |
| 83 def _LoadToolchainEnv(cpu, sdk_dir): |
| 84 """Returns a dictionary with environment variables that must be set while |
| 85 running binaries from the toolchain (e.g. INCLUDE and PATH for cl.exe).""" |
| 86 # Check if we are running in the SDK command line environment and use |
| 87 # the setup script from the SDK if so. |cpu| should be either |
| 88 # 'x86' or 'x64'. |
| 89 assert cpu in ('x86', 'x64') |
| 90 if bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', 1))) and sdk_dir: |
| 91 # Load environment from json file. |
| 92 env = os.path.normpath(os.path.join(sdk_dir, 'Bin/SetEnv.%s.json' % cpu)) |
| 93 env = json.load(open(env))['env'] |
| 94 for k in env: |
| 95 entries = [os.path.join(*([os.path.join(sdk_dir, 'bin')] + e)) |
| 96 for e in env[k]] |
| 97 env[k] = os.pathsep.join(entries) |
| 98 # PATH is a bit of a special case, it's in addition to the current PATH. |
| 99 env['PATH'] = env['PATH'] + os.pathsep + os.environ['PATH'] |
| 100 # Augment with the current env to pick up TEMP and friends. |
| 101 for k in os.environ: |
| 102 if k not in env: |
| 103 env[k] = os.environ[k] |
| 104 |
| 105 varlines = [] |
| 106 for k in sorted(env.keys()): |
| 107 varlines.append('%s=%s' % (str(k), str(env[k]))) |
| 108 variables = '\n'.join(varlines) |
| 109 |
| 110 # Check that the json file contained the same environment as the .cmd file. |
| 111 script = os.path.normpath(os.path.join(sdk_dir, 'Bin/SetEnv.cmd')) |
| 112 assert _ExtractImportantEnvironment(variables) == \ |
| 113 _ExtractImportantEnvironment(_LoadEnvFromBat([script, '/' + cpu])) |
| 114 else: |
| 115 if 'GYP_MSVS_OVERRIDE_PATH' not in os.environ: |
| 116 os.environ['GYP_MSVS_OVERRIDE_PATH'] = _DetectVisualStudioPath() |
| 117 # We only support x64-hosted tools. |
| 118 args = [os.path.normpath(os.path.join(os.environ['GYP_MSVS_OVERRIDE_PATH'], |
| 119 'VC/vcvarsall.bat')), |
| 120 'amd64_x86' if cpu == 'x86' else 'amd64'] |
| 121 variables = _LoadEnvFromBat(args) |
99 return _ExtractImportantEnvironment(variables) | 122 return _ExtractImportantEnvironment(variables) |
100 | 123 |
101 | 124 |
102 def _FormatAsEnvironmentBlock(envvar_dict): | 125 def _FormatAsEnvironmentBlock(envvar_dict): |
103 """Format as an 'environment block' directly suitable for CreateProcess. | 126 """Format as an 'environment block' directly suitable for CreateProcess. |
104 Briefly this is a list of key=value\0, terminated by an additional \0. See | 127 Briefly this is a list of key=value\0, terminated by an additional \0. See |
105 CreateProcess documentation for more details.""" | 128 CreateProcess documentation for more details.""" |
106 block = '' | 129 block = '' |
107 nul = '\0' | 130 nul = '\0' |
108 for key, value in envvar_dict.iteritems(): | 131 for key, value in envvar_dict.iteritems(): |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 env_block = _FormatAsEnvironmentBlock(env) | 203 env_block = _FormatAsEnvironmentBlock(env) |
181 with open('environment.winrt_' + cpu, 'wb') as f: | 204 with open('environment.winrt_' + cpu, 'wb') as f: |
182 f.write(env_block) | 205 f.write(env_block) |
183 | 206 |
184 assert vc_bin_dir | 207 assert vc_bin_dir |
185 print 'vc_bin_dir = "%s"' % vc_bin_dir | 208 print 'vc_bin_dir = "%s"' % vc_bin_dir |
186 | 209 |
187 | 210 |
188 if __name__ == '__main__': | 211 if __name__ == '__main__': |
189 main() | 212 main() |
OLD | NEW |