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