xref: /aosp_15_r20/external/boringssl/src/util/bot/vs_toolchain.py (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
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__))
15toolchain_dir = os.path.join(script_dir, 'win_toolchain')
16json_data_file = os.path.join(script_dir, 'win_toolchain.json')
17
18
19def 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
55def 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
63def _GetDesiredVsToolchainHashes(version):
64  """Load a list of SHA1s corresponding to the toolchains that we want installed
65  to build with."""
66  if version == '2017':
67    # VS 2017 Update 9 (15.9.12) with 10.0.18362 SDK, 10.0.17763 version of
68    # Debuggers, and 10.0.17134 version of d3dcompiler_47.dll, with ARM64
69    # libraries.
70    return ['418b3076791776573a815eb298c8aa590307af63']
71  if version == '2019':
72    # VS 2019 16.61 with 10.0.20348.0 SDK, 10.0.22621.755 version of Debuggers,
73    # with ARM64 libraries and UWP support.
74    return ['0b5ee4d2b1']
75  if version == '2022':
76    # VS 2022 17.9.2 with 10.0.22621.2428 SDK with ARM64 libraries and UWP
77    # support.
78    return ['7393122652']
79  raise Exception('Unsupported VS version %s' % version)
80
81
82def Update(version):
83  """Requests an update of the toolchain to the specific hashes we have at
84  this revision. The update outputs a .json of the various configuration
85  information required to pass to vs_env.py which we use in
86  |SetEnvironmentForCPU()|.
87  """
88  depot_tools_path = FindDepotTools()
89  get_toolchain_args = [
90      sys.executable,
91      os.path.join(depot_tools_path,
92                  'win_toolchain',
93                  'get_toolchain_if_necessary.py'),
94      '--output-json', json_data_file,
95      '--toolchain-dir', toolchain_dir,
96    ] + _GetDesiredVsToolchainHashes(version)
97  subprocess.check_call(get_toolchain_args)
98  return 0
99
100
101def main():
102  if not sys.platform.startswith(('win32', 'cygwin')):
103    return 0
104  commands = {
105      'update': Update,
106  }
107  if len(sys.argv) < 2 or sys.argv[1] not in commands:
108    print('Expected one of: %s' % ', '.join(commands), file=sys.stderr)
109    return 1
110  return commands[sys.argv[1]](*sys.argv[2:])
111
112
113if __name__ == '__main__':
114  sys.exit(main())
115