1# Copyright 2014 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 5from __future__ import print_function 6 7import json 8import os 9import os.path 10import subprocess 11import sys 12 13 14script_dir = os.path.dirname(os.path.realpath(__file__)) 15json_data_file = os.path.join(script_dir, 'win_toolchain.json') 16 17 18def SetEnvironmentForCPU(cpu): 19 """Sets the environment to build with the selected toolchain for |cpu|.""" 20 with open(json_data_file, 'r') as tempf: 21 toolchain_data = json.load(tempf) 22 sdk_dir = toolchain_data['win_sdk'] 23 os.environ['WINDOWSSDKDIR'] = sdk_dir 24 os.environ['WDK_DIR'] = toolchain_data['wdk'] 25 # Include the VS runtime in the PATH in case it's not machine-installed. 26 vs_runtime_dll_dirs = toolchain_data['runtime_dirs'] 27 runtime_path = os.pathsep.join(vs_runtime_dll_dirs) 28 os.environ['PATH'] = runtime_path + os.pathsep + os.environ['PATH'] 29 30 # Set up the architecture-specific environment from the SetEnv files. See 31 # _LoadToolchainEnv() from setup_toolchain.py in Chromium. 32 assert cpu in ('x86', 'x64', 'arm', 'arm64') 33 with open(os.path.join(sdk_dir, 'bin', 'SetEnv.%s.json' % cpu)) as f: 34 env = json.load(f)['env'] 35 if env['VSINSTALLDIR'] == [["..", "..\\"]]: 36 # Old-style paths were relative to the win_sdk\bin directory. 37 json_relative_dir = os.path.join(sdk_dir, 'bin') 38 else: 39 # New-style paths are relative to the toolchain directory. 40 json_relative_dir = toolchain_data['path'] 41 for k in env: 42 entries = [os.path.join(*([json_relative_dir] + e)) for e in env[k]] 43 # clang-cl wants INCLUDE to be ;-separated even on non-Windows, 44 # lld-link wants LIB to be ;-separated even on non-Windows. Path gets :. 45 sep = os.pathsep if k == 'PATH' else ';' 46 env[k] = sep.join(entries) 47 # PATH is a bit of a special case, it's in addition to the current PATH. 48 env['PATH'] = env['PATH'] + os.pathsep + os.environ['PATH'] 49 50 for k, v in env.items(): 51 os.environ[k] = v 52 53 54def FindDepotTools(): 55 """Returns the path to depot_tools in $PATH.""" 56 for path in os.environ['PATH'].split(os.pathsep): 57 if os.path.isfile(os.path.join(path, 'gclient.py')): 58 return path 59 raise Exception("depot_tools not found!") 60 61 62def _GetDesiredVsToolchainHashes(version): 63 """Load a list of SHA1s corresponding to the toolchains that we want installed 64 to build with.""" 65 if version == '2017': 66 # VS 2017 Update 9 (15.9.12) with 10.0.18362 SDK, 10.0.17763 version of 67 # Debuggers, and 10.0.17134 version of d3dcompiler_47.dll, with ARM64 68 # libraries. 69 return ['418b3076791776573a815eb298c8aa590307af63'] 70 if version == '2019': 71 # VS 2019 16.61 with 10.0.20348.0 SDK, 10.0.19041 version of Debuggers 72 # with ARM64 libraries and UWP support. 73 return ['1023ce2e82'] 74 raise Exception('Unsupported VS version %s' % version) 75 76 77def Update(version): 78 """Requests an update of the toolchain to the specific hashes we have at 79 this revision. The update outputs a .json of the various configuration 80 information required to pass to vs_env.py which we use in 81 |SetEnvironmentForCPU()|. 82 """ 83 depot_tools_path = FindDepotTools() 84 get_toolchain_args = [ 85 sys.executable, 86 os.path.join(depot_tools_path, 87 'win_toolchain', 88 'get_toolchain_if_necessary.py'), 89 '--output-json', json_data_file, 90 ] + _GetDesiredVsToolchainHashes(version) 91 subprocess.check_call(get_toolchain_args) 92 return 0 93 94 95def main(): 96 if not sys.platform.startswith(('win32', 'cygwin')): 97 return 0 98 commands = { 99 'update': Update, 100 } 101 if len(sys.argv) < 2 or sys.argv[1] not in commands: 102 print('Expected one of: %s' % ', '.join(commands), file=sys.stderr) 103 return 1 104 return commands[sys.argv[1]](*sys.argv[2:]) 105 106 107if __name__ == '__main__': 108 sys.exit(main()) 109