xref: /aosp_15_r20/external/angle/build/vs_toolchain.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*8975f5c5SAndroid Build Coastguard Worker# Copyright 2014 The Chromium Authors
3*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker
6*8975f5c5SAndroid Build Coastguard Worker
7*8975f5c5SAndroid Build Coastguard Workerimport collections
8*8975f5c5SAndroid Build Coastguard Workerimport glob
9*8975f5c5SAndroid Build Coastguard Workerimport json
10*8975f5c5SAndroid Build Coastguard Workerimport os
11*8975f5c5SAndroid Build Coastguard Workerimport platform
12*8975f5c5SAndroid Build Coastguard Workerimport re
13*8975f5c5SAndroid Build Coastguard Workerimport shutil
14*8975f5c5SAndroid Build Coastguard Workerimport stat
15*8975f5c5SAndroid Build Coastguard Workerimport subprocess
16*8975f5c5SAndroid Build Coastguard Workerimport sys
17*8975f5c5SAndroid Build Coastguard Worker
18*8975f5c5SAndroid Build Coastguard Workerfrom gn_helpers import ToGNString
19*8975f5c5SAndroid Build Coastguard Worker
20*8975f5c5SAndroid Build Coastguard Worker# VS 2022 17.9.2 with 10.0.22621.2428 SDK with ARM64 libraries and UWP support.
21*8975f5c5SAndroid Build Coastguard Worker# See go/win-toolchain-reference for instructions about how to update the
22*8975f5c5SAndroid Build Coastguard Worker# toolchain.
23*8975f5c5SAndroid Build Coastguard Worker#
24*8975f5c5SAndroid Build Coastguard Worker# When updating the toolchain, consider the following areas impacted by the
25*8975f5c5SAndroid Build Coastguard Worker# toolchain version:
26*8975f5c5SAndroid Build Coastguard Worker#
27*8975f5c5SAndroid Build Coastguard Worker# * This file -- SDK_VERSION and TOOLCHAIN_HASH
28*8975f5c5SAndroid Build Coastguard Worker#   Determines which version of the toolchain is used by gclient. The hash
29*8975f5c5SAndroid Build Coastguard Worker#   is the name of the toolchain package (minus the zip) in gcloud, and
30*8975f5c5SAndroid Build Coastguard Worker#   SDK_VERSION should match the SDK version in that package.
31*8975f5c5SAndroid Build Coastguard Worker#
32*8975f5c5SAndroid Build Coastguard Worker# * This file -- MSVS_VERSIONS
33*8975f5c5SAndroid Build Coastguard Worker#   Records the supported versions of Visual Studio, in priority order.
34*8975f5c5SAndroid Build Coastguard Worker#
35*8975f5c5SAndroid Build Coastguard Worker# * This file -- MSVC_TOOLSET_VERSION
36*8975f5c5SAndroid Build Coastguard Worker#   Determines the expected MSVC toolset for each version of Visual Studio.
37*8975f5c5SAndroid Build Coastguard Worker#   The packaged toolset version can be seen at <package>/VC/redist/MSVC;
38*8975f5c5SAndroid Build Coastguard Worker#   there will be a folder named `v143` or similar.
39*8975f5c5SAndroid Build Coastguard Worker#
40*8975f5c5SAndroid Build Coastguard Worker# * build/toolchain/win/setup_toolchain.py -- SDK_VERSION
41*8975f5c5SAndroid Build Coastguard Worker#   Secondary specification of the SDK Version, to make sure we're loading the
42*8975f5c5SAndroid Build Coastguard Worker#   right one. Should always match SDK_VERSION in this file.
43*8975f5c5SAndroid Build Coastguard Worker#
44*8975f5c5SAndroid Build Coastguard Worker# * base/win/windows_version.cc -- NTDDI preprocessor check
45*8975f5c5SAndroid Build Coastguard Worker#   Forces developers to have a specific SDK version (or newer). Triggers a
46*8975f5c5SAndroid Build Coastguard Worker#   compiler error if the available SDK is older than the minimum.
47*8975f5c5SAndroid Build Coastguard Worker#
48*8975f5c5SAndroid Build Coastguard Worker# * build/config/win/BUILD.gn -- NTDDI_VERSION
49*8975f5c5SAndroid Build Coastguard Worker#   Specifies which SDK/WDK version is installed. Some of the toolchain headers
50*8975f5c5SAndroid Build Coastguard Worker#   check this macro to conditionally compile code.
51*8975f5c5SAndroid Build Coastguard Worker#
52*8975f5c5SAndroid Build Coastguard Worker# * build/config/win/BUILD.gn -- WINVER and _WIN32_WINNT
53*8975f5c5SAndroid Build Coastguard Worker#   Specify the minimum supported Windows version. These very rarely need to
54*8975f5c5SAndroid Build Coastguard Worker#   be changed.
55*8975f5c5SAndroid Build Coastguard Worker#
56*8975f5c5SAndroid Build Coastguard Worker# * tools/win/setenv.py -- list of accepted `vs_version`s
57*8975f5c5SAndroid Build Coastguard Worker#   Add/remove VS versions when upgrading to a new VS version.
58*8975f5c5SAndroid Build Coastguard Worker#
59*8975f5c5SAndroid Build Coastguard Worker# * docs/windows_build_instructions.md
60*8975f5c5SAndroid Build Coastguard Worker#   Make sure any version numbers in the documentation match the code.
61*8975f5c5SAndroid Build Coastguard Worker#
62*8975f5c5SAndroid Build Coastguard WorkerTOOLCHAIN_HASH = '7393122652'
63*8975f5c5SAndroid Build Coastguard WorkerSDK_VERSION = '10.0.22621.0'
64*8975f5c5SAndroid Build Coastguard Worker
65*8975f5c5SAndroid Build Coastguard Worker# Visual Studio versions are listed in descending order of priority.
66*8975f5c5SAndroid Build Coastguard Worker# The first version is assumed by this script to be the one that is packaged,
67*8975f5c5SAndroid Build Coastguard Worker# which makes a difference for the arm64 runtime.
68*8975f5c5SAndroid Build Coastguard Worker# The second number is an alternate version number, only used in an error string
69*8975f5c5SAndroid Build Coastguard WorkerMSVS_VERSIONS = collections.OrderedDict([
70*8975f5c5SAndroid Build Coastguard Worker    ('2022', '17.0'),  # The VS version in our packaged toolchain.
71*8975f5c5SAndroid Build Coastguard Worker    ('2019', '16.0'),
72*8975f5c5SAndroid Build Coastguard Worker    ('2017', '15.0'),
73*8975f5c5SAndroid Build Coastguard Worker])
74*8975f5c5SAndroid Build Coastguard Worker
75*8975f5c5SAndroid Build Coastguard Worker# List of preferred VC toolset version based on MSVS
76*8975f5c5SAndroid Build Coastguard Worker# Order is not relevant for this dictionary.
77*8975f5c5SAndroid Build Coastguard WorkerMSVC_TOOLSET_VERSION = {
78*8975f5c5SAndroid Build Coastguard Worker    '2022': 'VC143',
79*8975f5c5SAndroid Build Coastguard Worker    '2019': 'VC142',
80*8975f5c5SAndroid Build Coastguard Worker    '2017': 'VC141',
81*8975f5c5SAndroid Build Coastguard Worker}
82*8975f5c5SAndroid Build Coastguard Worker
83*8975f5c5SAndroid Build Coastguard Workerscript_dir = os.path.dirname(os.path.realpath(__file__))
84*8975f5c5SAndroid Build Coastguard Workerjson_data_file = os.path.join(script_dir, 'win_toolchain.json')
85*8975f5c5SAndroid Build Coastguard Worker
86*8975f5c5SAndroid Build Coastguard Worker
87*8975f5c5SAndroid Build Coastguard Workerdef _HostIsWindows():
88*8975f5c5SAndroid Build Coastguard Worker  """Returns True if running on a Windows host (including under cygwin)."""
89*8975f5c5SAndroid Build Coastguard Worker  return sys.platform in ('win32', 'cygwin')
90*8975f5c5SAndroid Build Coastguard Worker
91*8975f5c5SAndroid Build Coastguard Workerdef SetEnvironmentAndGetRuntimeDllDirs():
92*8975f5c5SAndroid Build Coastguard Worker  """Sets up os.environ to use the depot_tools VS toolchain with gyp, and
93*8975f5c5SAndroid Build Coastguard Worker  returns the location of the VC runtime DLLs so they can be copied into
94*8975f5c5SAndroid Build Coastguard Worker  the output directory after gyp generation.
95*8975f5c5SAndroid Build Coastguard Worker
96*8975f5c5SAndroid Build Coastguard Worker  Return value is [x64path, x86path, 'Arm64Unused'] or None. arm64path is
97*8975f5c5SAndroid Build Coastguard Worker  generated separately because there are multiple folders for the arm64 VC
98*8975f5c5SAndroid Build Coastguard Worker  runtime.
99*8975f5c5SAndroid Build Coastguard Worker  """
100*8975f5c5SAndroid Build Coastguard Worker  vs_runtime_dll_dirs = None
101*8975f5c5SAndroid Build Coastguard Worker  depot_tools_win_toolchain = \
102*8975f5c5SAndroid Build Coastguard Worker      bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', '1')))
103*8975f5c5SAndroid Build Coastguard Worker  # When running on a non-Windows host, only do this if the SDK has explicitly
104*8975f5c5SAndroid Build Coastguard Worker  # been downloaded before (in which case json_data_file will exist).
105*8975f5c5SAndroid Build Coastguard Worker  if ((_HostIsWindows() or os.path.exists(json_data_file))
106*8975f5c5SAndroid Build Coastguard Worker      and depot_tools_win_toolchain):
107*8975f5c5SAndroid Build Coastguard Worker    if ShouldUpdateToolchain():
108*8975f5c5SAndroid Build Coastguard Worker      if len(sys.argv) > 1 and sys.argv[1] == 'update':
109*8975f5c5SAndroid Build Coastguard Worker        update_result = Update()
110*8975f5c5SAndroid Build Coastguard Worker      else:
111*8975f5c5SAndroid Build Coastguard Worker        update_result = Update(no_download=True)
112*8975f5c5SAndroid Build Coastguard Worker      if update_result != 0:
113*8975f5c5SAndroid Build Coastguard Worker        raise Exception('Failed to update, error code %d.' % update_result)
114*8975f5c5SAndroid Build Coastguard Worker    with open(json_data_file, 'r') as tempf:
115*8975f5c5SAndroid Build Coastguard Worker      toolchain_data = json.load(tempf)
116*8975f5c5SAndroid Build Coastguard Worker
117*8975f5c5SAndroid Build Coastguard Worker    toolchain = toolchain_data['path']
118*8975f5c5SAndroid Build Coastguard Worker    version = toolchain_data['version']
119*8975f5c5SAndroid Build Coastguard Worker    win_sdk = toolchain_data.get('win_sdk')
120*8975f5c5SAndroid Build Coastguard Worker    wdk = toolchain_data['wdk']
121*8975f5c5SAndroid Build Coastguard Worker    # TODO(scottmg): The order unfortunately matters in these. They should be
122*8975f5c5SAndroid Build Coastguard Worker    # split into separate keys for x64/x86/arm64. (See CopyDlls call below).
123*8975f5c5SAndroid Build Coastguard Worker    # http://crbug.com/345992
124*8975f5c5SAndroid Build Coastguard Worker    vs_runtime_dll_dirs = toolchain_data['runtime_dirs']
125*8975f5c5SAndroid Build Coastguard Worker    # The number of runtime_dirs in the toolchain_data was two (x64/x86) but
126*8975f5c5SAndroid Build Coastguard Worker    # changed to three (x64/x86/arm64) and this code needs to handle both
127*8975f5c5SAndroid Build Coastguard Worker    # possibilities, which can change independently from this code.
128*8975f5c5SAndroid Build Coastguard Worker    if len(vs_runtime_dll_dirs) == 2:
129*8975f5c5SAndroid Build Coastguard Worker      vs_runtime_dll_dirs.append('Arm64Unused')
130*8975f5c5SAndroid Build Coastguard Worker
131*8975f5c5SAndroid Build Coastguard Worker    os.environ['GYP_MSVS_OVERRIDE_PATH'] = toolchain
132*8975f5c5SAndroid Build Coastguard Worker
133*8975f5c5SAndroid Build Coastguard Worker    os.environ['WINDOWSSDKDIR'] = win_sdk
134*8975f5c5SAndroid Build Coastguard Worker    os.environ['WDK_DIR'] = wdk
135*8975f5c5SAndroid Build Coastguard Worker    # Include the VS runtime in the PATH in case it's not machine-installed.
136*8975f5c5SAndroid Build Coastguard Worker    runtime_path = os.path.pathsep.join(vs_runtime_dll_dirs)
137*8975f5c5SAndroid Build Coastguard Worker    os.environ['PATH'] = runtime_path + os.path.pathsep + os.environ['PATH']
138*8975f5c5SAndroid Build Coastguard Worker  elif sys.platform == 'win32' and not depot_tools_win_toolchain:
139*8975f5c5SAndroid Build Coastguard Worker    if not 'GYP_MSVS_OVERRIDE_PATH' in os.environ:
140*8975f5c5SAndroid Build Coastguard Worker      os.environ['GYP_MSVS_OVERRIDE_PATH'] = DetectVisualStudioPath()
141*8975f5c5SAndroid Build Coastguard Worker
142*8975f5c5SAndroid Build Coastguard Worker    # When using an installed toolchain these files aren't needed in the output
143*8975f5c5SAndroid Build Coastguard Worker    # directory in order to run binaries locally, but they are needed in order
144*8975f5c5SAndroid Build Coastguard Worker    # to create isolates or the mini_installer. Copying them to the output
145*8975f5c5SAndroid Build Coastguard Worker    # directory ensures that they are available when needed.
146*8975f5c5SAndroid Build Coastguard Worker    bitness = platform.architecture()[0]
147*8975f5c5SAndroid Build Coastguard Worker    # When running 64-bit python the x64 DLLs will be in System32
148*8975f5c5SAndroid Build Coastguard Worker    # ARM64 binaries will not be available in the system directories because we
149*8975f5c5SAndroid Build Coastguard Worker    # don't build on ARM64 machines.
150*8975f5c5SAndroid Build Coastguard Worker    x64_path = 'System32' if bitness == '64bit' else 'Sysnative'
151*8975f5c5SAndroid Build Coastguard Worker    x64_path = os.path.join(os.path.expandvars('%windir%'), x64_path)
152*8975f5c5SAndroid Build Coastguard Worker    vs_runtime_dll_dirs = [x64_path,
153*8975f5c5SAndroid Build Coastguard Worker                           os.path.join(os.path.expandvars('%windir%'),
154*8975f5c5SAndroid Build Coastguard Worker                                        'SysWOW64'),
155*8975f5c5SAndroid Build Coastguard Worker                           'Arm64Unused']
156*8975f5c5SAndroid Build Coastguard Worker
157*8975f5c5SAndroid Build Coastguard Worker  return vs_runtime_dll_dirs
158*8975f5c5SAndroid Build Coastguard Worker
159*8975f5c5SAndroid Build Coastguard Worker
160*8975f5c5SAndroid Build Coastguard Workerdef _RegistryGetValueUsingWinReg(key, value):
161*8975f5c5SAndroid Build Coastguard Worker  """Use the _winreg module to obtain the value of a registry key.
162*8975f5c5SAndroid Build Coastguard Worker
163*8975f5c5SAndroid Build Coastguard Worker  Args:
164*8975f5c5SAndroid Build Coastguard Worker    key: The registry key.
165*8975f5c5SAndroid Build Coastguard Worker    value: The particular registry value to read.
166*8975f5c5SAndroid Build Coastguard Worker  Return:
167*8975f5c5SAndroid Build Coastguard Worker    contents of the registry key's value, or None on failure.  Throws
168*8975f5c5SAndroid Build Coastguard Worker    ImportError if _winreg is unavailable.
169*8975f5c5SAndroid Build Coastguard Worker  """
170*8975f5c5SAndroid Build Coastguard Worker  import _winreg
171*8975f5c5SAndroid Build Coastguard Worker  try:
172*8975f5c5SAndroid Build Coastguard Worker    root, subkey = key.split('\\', 1)
173*8975f5c5SAndroid Build Coastguard Worker    assert root == 'HKLM'  # Only need HKLM for now.
174*8975f5c5SAndroid Build Coastguard Worker    with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey) as hkey:
175*8975f5c5SAndroid Build Coastguard Worker      return _winreg.QueryValueEx(hkey, value)[0]
176*8975f5c5SAndroid Build Coastguard Worker  except WindowsError:
177*8975f5c5SAndroid Build Coastguard Worker    return None
178*8975f5c5SAndroid Build Coastguard Worker
179*8975f5c5SAndroid Build Coastguard Worker
180*8975f5c5SAndroid Build Coastguard Workerdef _RegistryGetValue(key, value):
181*8975f5c5SAndroid Build Coastguard Worker  try:
182*8975f5c5SAndroid Build Coastguard Worker    return _RegistryGetValueUsingWinReg(key, value)
183*8975f5c5SAndroid Build Coastguard Worker  except ImportError:
184*8975f5c5SAndroid Build Coastguard Worker    raise Exception('The python library _winreg not found.')
185*8975f5c5SAndroid Build Coastguard Worker
186*8975f5c5SAndroid Build Coastguard Worker
187*8975f5c5SAndroid Build Coastguard Workerdef GetVisualStudioVersion():
188*8975f5c5SAndroid Build Coastguard Worker  """Return best available version of Visual Studio.
189*8975f5c5SAndroid Build Coastguard Worker  """
190*8975f5c5SAndroid Build Coastguard Worker  supported_versions = list(MSVS_VERSIONS.keys())
191*8975f5c5SAndroid Build Coastguard Worker
192*8975f5c5SAndroid Build Coastguard Worker  # VS installed in depot_tools for Googlers
193*8975f5c5SAndroid Build Coastguard Worker  if bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', '1'))):
194*8975f5c5SAndroid Build Coastguard Worker    return supported_versions[0]
195*8975f5c5SAndroid Build Coastguard Worker
196*8975f5c5SAndroid Build Coastguard Worker  # VS installed in system for external developers
197*8975f5c5SAndroid Build Coastguard Worker  supported_versions_str = ', '.join('{} ({})'.format(v,k)
198*8975f5c5SAndroid Build Coastguard Worker      for k,v in MSVS_VERSIONS.items())
199*8975f5c5SAndroid Build Coastguard Worker  available_versions = []
200*8975f5c5SAndroid Build Coastguard Worker  for version in supported_versions:
201*8975f5c5SAndroid Build Coastguard Worker    # Checking vs%s_install environment variables.
202*8975f5c5SAndroid Build Coastguard Worker    # For example, vs2019_install could have the value
203*8975f5c5SAndroid Build Coastguard Worker    # "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community".
204*8975f5c5SAndroid Build Coastguard Worker    # Only vs2017_install, vs2019_install and vs2022_install are supported.
205*8975f5c5SAndroid Build Coastguard Worker    path = os.environ.get('vs%s_install' % version)
206*8975f5c5SAndroid Build Coastguard Worker    if path and os.path.exists(path):
207*8975f5c5SAndroid Build Coastguard Worker      available_versions.append(version)
208*8975f5c5SAndroid Build Coastguard Worker      break
209*8975f5c5SAndroid Build Coastguard Worker    # Detecting VS under possible paths.
210*8975f5c5SAndroid Build Coastguard Worker    if version >= '2022':
211*8975f5c5SAndroid Build Coastguard Worker      program_files_path_variable = '%ProgramFiles%'
212*8975f5c5SAndroid Build Coastguard Worker    else:
213*8975f5c5SAndroid Build Coastguard Worker      program_files_path_variable = '%ProgramFiles(x86)%'
214*8975f5c5SAndroid Build Coastguard Worker    path = os.path.expandvars(program_files_path_variable +
215*8975f5c5SAndroid Build Coastguard Worker                              '/Microsoft Visual Studio/%s' % version)
216*8975f5c5SAndroid Build Coastguard Worker    if path and any(
217*8975f5c5SAndroid Build Coastguard Worker        os.path.exists(os.path.join(path, edition))
218*8975f5c5SAndroid Build Coastguard Worker        for edition in ('Enterprise', 'Professional', 'Community', 'Preview',
219*8975f5c5SAndroid Build Coastguard Worker                        'BuildTools')):
220*8975f5c5SAndroid Build Coastguard Worker      available_versions.append(version)
221*8975f5c5SAndroid Build Coastguard Worker      break
222*8975f5c5SAndroid Build Coastguard Worker
223*8975f5c5SAndroid Build Coastguard Worker  if not available_versions:
224*8975f5c5SAndroid Build Coastguard Worker    raise Exception('No supported Visual Studio can be found.'
225*8975f5c5SAndroid Build Coastguard Worker                    ' Supported versions are: %s.' % supported_versions_str)
226*8975f5c5SAndroid Build Coastguard Worker  return available_versions[0]
227*8975f5c5SAndroid Build Coastguard Worker
228*8975f5c5SAndroid Build Coastguard Worker
229*8975f5c5SAndroid Build Coastguard Workerdef DetectVisualStudioPath():
230*8975f5c5SAndroid Build Coastguard Worker  """Return path to the installed Visual Studio.
231*8975f5c5SAndroid Build Coastguard Worker  """
232*8975f5c5SAndroid Build Coastguard Worker
233*8975f5c5SAndroid Build Coastguard Worker  # Note that this code is used from
234*8975f5c5SAndroid Build Coastguard Worker  # build/toolchain/win/setup_toolchain.py as well.
235*8975f5c5SAndroid Build Coastguard Worker  version_as_year = GetVisualStudioVersion()
236*8975f5c5SAndroid Build Coastguard Worker
237*8975f5c5SAndroid Build Coastguard Worker  # The VC++ >=2017 install location needs to be located using COM instead of
238*8975f5c5SAndroid Build Coastguard Worker  # the registry. For details see:
239*8975f5c5SAndroid Build Coastguard Worker  # https://blogs.msdn.microsoft.com/heaths/2016/09/15/changes-to-visual-studio-15-setup/
240*8975f5c5SAndroid Build Coastguard Worker  # For now we use a hardcoded default with an environment variable override.
241*8975f5c5SAndroid Build Coastguard Worker  if version_as_year >= '2022':
242*8975f5c5SAndroid Build Coastguard Worker    program_files_path_variable = '%ProgramFiles%'
243*8975f5c5SAndroid Build Coastguard Worker  else:
244*8975f5c5SAndroid Build Coastguard Worker    program_files_path_variable = '%ProgramFiles(x86)%'
245*8975f5c5SAndroid Build Coastguard Worker  for path in (os.environ.get('vs%s_install' % version_as_year),
246*8975f5c5SAndroid Build Coastguard Worker               os.path.expandvars(program_files_path_variable +
247*8975f5c5SAndroid Build Coastguard Worker                                  '/Microsoft Visual Studio/%s/Enterprise' %
248*8975f5c5SAndroid Build Coastguard Worker                                  version_as_year),
249*8975f5c5SAndroid Build Coastguard Worker               os.path.expandvars(program_files_path_variable +
250*8975f5c5SAndroid Build Coastguard Worker                                  '/Microsoft Visual Studio/%s/Professional' %
251*8975f5c5SAndroid Build Coastguard Worker                                  version_as_year),
252*8975f5c5SAndroid Build Coastguard Worker               os.path.expandvars(program_files_path_variable +
253*8975f5c5SAndroid Build Coastguard Worker                                  '/Microsoft Visual Studio/%s/Community' %
254*8975f5c5SAndroid Build Coastguard Worker                                  version_as_year),
255*8975f5c5SAndroid Build Coastguard Worker               os.path.expandvars(program_files_path_variable +
256*8975f5c5SAndroid Build Coastguard Worker                                  '/Microsoft Visual Studio/%s/Preview' %
257*8975f5c5SAndroid Build Coastguard Worker                                  version_as_year),
258*8975f5c5SAndroid Build Coastguard Worker               os.path.expandvars(program_files_path_variable +
259*8975f5c5SAndroid Build Coastguard Worker                                  '/Microsoft Visual Studio/%s/BuildTools' %
260*8975f5c5SAndroid Build Coastguard Worker                                  version_as_year)):
261*8975f5c5SAndroid Build Coastguard Worker    if path and os.path.exists(path):
262*8975f5c5SAndroid Build Coastguard Worker      return path
263*8975f5c5SAndroid Build Coastguard Worker
264*8975f5c5SAndroid Build Coastguard Worker  raise Exception('Visual Studio Version %s not found.' % version_as_year)
265*8975f5c5SAndroid Build Coastguard Worker
266*8975f5c5SAndroid Build Coastguard Worker
267*8975f5c5SAndroid Build Coastguard Workerdef _CopyRuntimeImpl(target, source, verbose=True):
268*8975f5c5SAndroid Build Coastguard Worker  """Copy |source| to |target| if it doesn't already exist or if it needs to be
269*8975f5c5SAndroid Build Coastguard Worker  updated (comparing last modified time as an approximate float match as for
270*8975f5c5SAndroid Build Coastguard Worker  some reason the values tend to differ by ~1e-07 despite being copies of the
271*8975f5c5SAndroid Build Coastguard Worker  same file... https://crbug.com/603603).
272*8975f5c5SAndroid Build Coastguard Worker  """
273*8975f5c5SAndroid Build Coastguard Worker  if (os.path.isdir(os.path.dirname(target)) and
274*8975f5c5SAndroid Build Coastguard Worker      (not os.path.isfile(target) or
275*8975f5c5SAndroid Build Coastguard Worker       abs(os.stat(target).st_mtime - os.stat(source).st_mtime) >= 0.01)):
276*8975f5c5SAndroid Build Coastguard Worker    if verbose:
277*8975f5c5SAndroid Build Coastguard Worker      print('Copying %s to %s...' % (source, target))
278*8975f5c5SAndroid Build Coastguard Worker    if os.path.exists(target):
279*8975f5c5SAndroid Build Coastguard Worker      # Make the file writable so that we can delete it now, and keep it
280*8975f5c5SAndroid Build Coastguard Worker      # readable.
281*8975f5c5SAndroid Build Coastguard Worker      os.chmod(target, stat.S_IWRITE | stat.S_IREAD)
282*8975f5c5SAndroid Build Coastguard Worker      os.unlink(target)
283*8975f5c5SAndroid Build Coastguard Worker    shutil.copy2(source, target)
284*8975f5c5SAndroid Build Coastguard Worker    # Make the file writable so that we can overwrite or delete it later,
285*8975f5c5SAndroid Build Coastguard Worker    # keep it readable.
286*8975f5c5SAndroid Build Coastguard Worker    os.chmod(target, stat.S_IWRITE | stat.S_IREAD)
287*8975f5c5SAndroid Build Coastguard Worker
288*8975f5c5SAndroid Build Coastguard Workerdef _SortByHighestVersionNumberFirst(list_of_str_versions):
289*8975f5c5SAndroid Build Coastguard Worker  """This sorts |list_of_str_versions| according to version number rules
290*8975f5c5SAndroid Build Coastguard Worker  so that version "1.12" is higher than version "1.9". Does not work
291*8975f5c5SAndroid Build Coastguard Worker  with non-numeric versions like 1.4.a8 which will be higher than
292*8975f5c5SAndroid Build Coastguard Worker  1.4.a12. It does handle the versions being embedded in file paths.
293*8975f5c5SAndroid Build Coastguard Worker  """
294*8975f5c5SAndroid Build Coastguard Worker  def to_int_if_int(x):
295*8975f5c5SAndroid Build Coastguard Worker    try:
296*8975f5c5SAndroid Build Coastguard Worker      return int(x)
297*8975f5c5SAndroid Build Coastguard Worker    except ValueError:
298*8975f5c5SAndroid Build Coastguard Worker      return x
299*8975f5c5SAndroid Build Coastguard Worker
300*8975f5c5SAndroid Build Coastguard Worker  def to_number_sequence(x):
301*8975f5c5SAndroid Build Coastguard Worker    part_sequence = re.split(r'[\\/\.]', x)
302*8975f5c5SAndroid Build Coastguard Worker    return [to_int_if_int(x) for x in part_sequence]
303*8975f5c5SAndroid Build Coastguard Worker
304*8975f5c5SAndroid Build Coastguard Worker  list_of_str_versions.sort(key=to_number_sequence, reverse=True)
305*8975f5c5SAndroid Build Coastguard Worker
306*8975f5c5SAndroid Build Coastguard Worker
307*8975f5c5SAndroid Build Coastguard Workerdef _CopyUCRTRuntime(target_dir, source_dir, target_cpu, suffix):
308*8975f5c5SAndroid Build Coastguard Worker  """Copy both the msvcp and vccorlib runtime DLLs, only if the target doesn't
309*8975f5c5SAndroid Build Coastguard Worker  exist, but the target directory does exist."""
310*8975f5c5SAndroid Build Coastguard Worker  if target_cpu == 'arm64':
311*8975f5c5SAndroid Build Coastguard Worker    # Windows ARM64 VCRuntime is located at {toolchain_root}/VC/Redist/MSVC/
312*8975f5c5SAndroid Build Coastguard Worker    # {x.y.z}/[debug_nonredist/]arm64/Microsoft.VC14x.CRT/.
313*8975f5c5SAndroid Build Coastguard Worker    # Select VC toolset directory based on Visual Studio version
314*8975f5c5SAndroid Build Coastguard Worker    vc_redist_root = FindVCRedistRoot()
315*8975f5c5SAndroid Build Coastguard Worker    if suffix.startswith('.'):
316*8975f5c5SAndroid Build Coastguard Worker      vc_toolset_dir = 'Microsoft.{}.CRT' \
317*8975f5c5SAndroid Build Coastguard Worker         .format(MSVC_TOOLSET_VERSION[GetVisualStudioVersion()])
318*8975f5c5SAndroid Build Coastguard Worker      source_dir = os.path.join(vc_redist_root,
319*8975f5c5SAndroid Build Coastguard Worker                                'arm64', vc_toolset_dir)
320*8975f5c5SAndroid Build Coastguard Worker    else:
321*8975f5c5SAndroid Build Coastguard Worker      vc_toolset_dir = 'Microsoft.{}.DebugCRT' \
322*8975f5c5SAndroid Build Coastguard Worker         .format(MSVC_TOOLSET_VERSION[GetVisualStudioVersion()])
323*8975f5c5SAndroid Build Coastguard Worker      source_dir = os.path.join(vc_redist_root, 'debug_nonredist',
324*8975f5c5SAndroid Build Coastguard Worker                                'arm64', vc_toolset_dir)
325*8975f5c5SAndroid Build Coastguard Worker  file_parts = ('msvcp140', 'vccorlib140', 'vcruntime140')
326*8975f5c5SAndroid Build Coastguard Worker  if target_cpu == 'x64' and GetVisualStudioVersion() != '2017':
327*8975f5c5SAndroid Build Coastguard Worker    file_parts = file_parts + ('vcruntime140_1', )
328*8975f5c5SAndroid Build Coastguard Worker  for file_part in file_parts:
329*8975f5c5SAndroid Build Coastguard Worker    dll = file_part + suffix
330*8975f5c5SAndroid Build Coastguard Worker    target = os.path.join(target_dir, dll)
331*8975f5c5SAndroid Build Coastguard Worker    source = os.path.join(source_dir, dll)
332*8975f5c5SAndroid Build Coastguard Worker    _CopyRuntimeImpl(target, source)
333*8975f5c5SAndroid Build Coastguard Worker  # We must copy ucrtbased.dll for all CPU types. The rest of the Universal CRT
334*8975f5c5SAndroid Build Coastguard Worker  # is installed as part of the OS in Windows 10 and beyond.
335*8975f5c5SAndroid Build Coastguard Worker  if not suffix.startswith('.'):
336*8975f5c5SAndroid Build Coastguard Worker    win_sdk_dir = os.path.normpath(
337*8975f5c5SAndroid Build Coastguard Worker        os.environ.get(
338*8975f5c5SAndroid Build Coastguard Worker            'WINDOWSSDKDIR',
339*8975f5c5SAndroid Build Coastguard Worker            os.path.expandvars('%ProgramFiles(x86)%'
340*8975f5c5SAndroid Build Coastguard Worker                               '\\Windows Kits\\10')))
341*8975f5c5SAndroid Build Coastguard Worker    # ucrtbased.dll is located at {win_sdk_dir}/bin/{a.b.c.d}/{target_cpu}/
342*8975f5c5SAndroid Build Coastguard Worker    # ucrt/.
343*8975f5c5SAndroid Build Coastguard Worker    sdk_bin_root = os.path.join(win_sdk_dir, 'bin')
344*8975f5c5SAndroid Build Coastguard Worker    sdk_bin_sub_dirs = glob.glob(os.path.join(sdk_bin_root, '10.*'))
345*8975f5c5SAndroid Build Coastguard Worker    # Select the most recent SDK if there are multiple versions installed.
346*8975f5c5SAndroid Build Coastguard Worker    _SortByHighestVersionNumberFirst(sdk_bin_sub_dirs)
347*8975f5c5SAndroid Build Coastguard Worker    for directory in sdk_bin_sub_dirs:
348*8975f5c5SAndroid Build Coastguard Worker      sdk_redist_root_version = os.path.join(sdk_bin_root, directory)
349*8975f5c5SAndroid Build Coastguard Worker      if not os.path.isdir(sdk_redist_root_version):
350*8975f5c5SAndroid Build Coastguard Worker        continue
351*8975f5c5SAndroid Build Coastguard Worker      source_dir = os.path.join(sdk_redist_root_version, target_cpu, 'ucrt')
352*8975f5c5SAndroid Build Coastguard Worker      if not os.path.isdir(source_dir):
353*8975f5c5SAndroid Build Coastguard Worker        continue
354*8975f5c5SAndroid Build Coastguard Worker      break
355*8975f5c5SAndroid Build Coastguard Worker    _CopyRuntimeImpl(os.path.join(target_dir, 'ucrtbase' + suffix),
356*8975f5c5SAndroid Build Coastguard Worker                     os.path.join(source_dir, 'ucrtbase' + suffix))
357*8975f5c5SAndroid Build Coastguard Worker
358*8975f5c5SAndroid Build Coastguard Worker
359*8975f5c5SAndroid Build Coastguard Workerdef FindVCComponentRoot(component):
360*8975f5c5SAndroid Build Coastguard Worker  """Find the most recent Tools or Redist or other directory in an MSVC install.
361*8975f5c5SAndroid Build Coastguard Worker  Typical results are {toolchain_root}/VC/{component}/MSVC/{x.y.z}. The {x.y.z}
362*8975f5c5SAndroid Build Coastguard Worker  version number part changes frequently so the highest version number found is
363*8975f5c5SAndroid Build Coastguard Worker  used.
364*8975f5c5SAndroid Build Coastguard Worker  """
365*8975f5c5SAndroid Build Coastguard Worker
366*8975f5c5SAndroid Build Coastguard Worker  SetEnvironmentAndGetRuntimeDllDirs()
367*8975f5c5SAndroid Build Coastguard Worker  assert ('GYP_MSVS_OVERRIDE_PATH' in os.environ)
368*8975f5c5SAndroid Build Coastguard Worker  vc_component_msvc_root = os.path.join(os.environ['GYP_MSVS_OVERRIDE_PATH'],
369*8975f5c5SAndroid Build Coastguard Worker      'VC', component, 'MSVC')
370*8975f5c5SAndroid Build Coastguard Worker  vc_component_msvc_contents = glob.glob(
371*8975f5c5SAndroid Build Coastguard Worker      os.path.join(vc_component_msvc_root, '14.*'))
372*8975f5c5SAndroid Build Coastguard Worker  # Select the most recent toolchain if there are several.
373*8975f5c5SAndroid Build Coastguard Worker  _SortByHighestVersionNumberFirst(vc_component_msvc_contents)
374*8975f5c5SAndroid Build Coastguard Worker  for directory in vc_component_msvc_contents:
375*8975f5c5SAndroid Build Coastguard Worker    if os.path.isdir(directory):
376*8975f5c5SAndroid Build Coastguard Worker      return directory
377*8975f5c5SAndroid Build Coastguard Worker  raise Exception('Unable to find the VC %s directory.' % component)
378*8975f5c5SAndroid Build Coastguard Worker
379*8975f5c5SAndroid Build Coastguard Worker
380*8975f5c5SAndroid Build Coastguard Workerdef FindVCRedistRoot():
381*8975f5c5SAndroid Build Coastguard Worker  """In >=VS2017, Redist binaries are located in
382*8975f5c5SAndroid Build Coastguard Worker  {toolchain_root}/VC/Redist/MSVC/{x.y.z}/{target_cpu}/.
383*8975f5c5SAndroid Build Coastguard Worker
384*8975f5c5SAndroid Build Coastguard Worker  This returns the '{toolchain_root}/VC/Redist/MSVC/{x.y.z}/' path.
385*8975f5c5SAndroid Build Coastguard Worker  """
386*8975f5c5SAndroid Build Coastguard Worker  return FindVCComponentRoot('Redist')
387*8975f5c5SAndroid Build Coastguard Worker
388*8975f5c5SAndroid Build Coastguard Worker
389*8975f5c5SAndroid Build Coastguard Workerdef _CopyRuntime(target_dir, source_dir, target_cpu, debug):
390*8975f5c5SAndroid Build Coastguard Worker  """Copy the VS runtime DLLs, only if the target doesn't exist, but the target
391*8975f5c5SAndroid Build Coastguard Worker  directory does exist. Handles VS 2015, 2017 and 2019."""
392*8975f5c5SAndroid Build Coastguard Worker  suffix = 'd.dll' if debug else '.dll'
393*8975f5c5SAndroid Build Coastguard Worker  # VS 2015, 2017 and 2019 use the same CRT DLLs.
394*8975f5c5SAndroid Build Coastguard Worker  _CopyUCRTRuntime(target_dir, source_dir, target_cpu, suffix)
395*8975f5c5SAndroid Build Coastguard Worker
396*8975f5c5SAndroid Build Coastguard Worker
397*8975f5c5SAndroid Build Coastguard Workerdef CopyDlls(target_dir, configuration, target_cpu):
398*8975f5c5SAndroid Build Coastguard Worker  """Copy the VS runtime DLLs into the requested directory as needed.
399*8975f5c5SAndroid Build Coastguard Worker
400*8975f5c5SAndroid Build Coastguard Worker  configuration is one of 'Debug' or 'Release'.
401*8975f5c5SAndroid Build Coastguard Worker  target_cpu is one of 'x86', 'x64' or 'arm64'.
402*8975f5c5SAndroid Build Coastguard Worker
403*8975f5c5SAndroid Build Coastguard Worker  The debug configuration gets both the debug and release DLLs; the
404*8975f5c5SAndroid Build Coastguard Worker  release config only the latter.
405*8975f5c5SAndroid Build Coastguard Worker  """
406*8975f5c5SAndroid Build Coastguard Worker  vs_runtime_dll_dirs = SetEnvironmentAndGetRuntimeDllDirs()
407*8975f5c5SAndroid Build Coastguard Worker  if not vs_runtime_dll_dirs:
408*8975f5c5SAndroid Build Coastguard Worker    return
409*8975f5c5SAndroid Build Coastguard Worker
410*8975f5c5SAndroid Build Coastguard Worker  x64_runtime, x86_runtime, arm64_runtime = vs_runtime_dll_dirs
411*8975f5c5SAndroid Build Coastguard Worker  if target_cpu == 'x64':
412*8975f5c5SAndroid Build Coastguard Worker    runtime_dir = x64_runtime
413*8975f5c5SAndroid Build Coastguard Worker  elif target_cpu == 'x86':
414*8975f5c5SAndroid Build Coastguard Worker    runtime_dir = x86_runtime
415*8975f5c5SAndroid Build Coastguard Worker  elif target_cpu == 'arm64':
416*8975f5c5SAndroid Build Coastguard Worker    runtime_dir = arm64_runtime
417*8975f5c5SAndroid Build Coastguard Worker  else:
418*8975f5c5SAndroid Build Coastguard Worker    raise Exception('Unknown target_cpu: ' + target_cpu)
419*8975f5c5SAndroid Build Coastguard Worker  _CopyRuntime(target_dir, runtime_dir, target_cpu, debug=False)
420*8975f5c5SAndroid Build Coastguard Worker  if configuration == 'Debug':
421*8975f5c5SAndroid Build Coastguard Worker    _CopyRuntime(target_dir, runtime_dir, target_cpu, debug=True)
422*8975f5c5SAndroid Build Coastguard Worker  _CopyDebugger(target_dir, target_cpu)
423*8975f5c5SAndroid Build Coastguard Worker  if target_cpu == 'arm64':
424*8975f5c5SAndroid Build Coastguard Worker    target_dir = os.path.join(target_dir, 'win_clang_x64')
425*8975f5c5SAndroid Build Coastguard Worker    target_cpu = 'x64'
426*8975f5c5SAndroid Build Coastguard Worker    runtime_dir = x64_runtime
427*8975f5c5SAndroid Build Coastguard Worker    os.makedirs(target_dir, exist_ok=True)
428*8975f5c5SAndroid Build Coastguard Worker    _CopyRuntime(target_dir, runtime_dir, target_cpu, debug=False)
429*8975f5c5SAndroid Build Coastguard Worker    if configuration == 'Debug':
430*8975f5c5SAndroid Build Coastguard Worker      _CopyRuntime(target_dir, runtime_dir, target_cpu, debug=True)
431*8975f5c5SAndroid Build Coastguard Worker    _CopyDebugger(target_dir, target_cpu)
432*8975f5c5SAndroid Build Coastguard Worker
433*8975f5c5SAndroid Build Coastguard Worker
434*8975f5c5SAndroid Build Coastguard Workerdef _CopyDebugger(target_dir, target_cpu):
435*8975f5c5SAndroid Build Coastguard Worker  """Copy dbghelp.dll, dbgcore.dll, and msdia140.dll into the requested
436*8975f5c5SAndroid Build Coastguard Worker  directory.
437*8975f5c5SAndroid Build Coastguard Worker
438*8975f5c5SAndroid Build Coastguard Worker  target_cpu is one of 'x86', 'x64' or 'arm64'.
439*8975f5c5SAndroid Build Coastguard Worker
440*8975f5c5SAndroid Build Coastguard Worker  dbghelp.dll is used when Chrome needs to symbolize stacks. Copying this file
441*8975f5c5SAndroid Build Coastguard Worker  from the SDK directory avoids using the system copy of dbghelp.dll which then
442*8975f5c5SAndroid Build Coastguard Worker  ensures compatibility with recent debug information formats, such as
443*8975f5c5SAndroid Build Coastguard Worker  large-page PDBs. Note that for these DLLs to be deployed to swarming bots they
444*8975f5c5SAndroid Build Coastguard Worker  also need to be listed in group("runtime_libs").
445*8975f5c5SAndroid Build Coastguard Worker
446*8975f5c5SAndroid Build Coastguard Worker  dbgcore.dll is needed when using some functions from dbghelp.dll (like
447*8975f5c5SAndroid Build Coastguard Worker  MinidumpWriteDump).
448*8975f5c5SAndroid Build Coastguard Worker
449*8975f5c5SAndroid Build Coastguard Worker  msdia140.dll is needed for tools like symupload.exe and dump_syms.exe.
450*8975f5c5SAndroid Build Coastguard Worker  """
451*8975f5c5SAndroid Build Coastguard Worker  win_sdk_dir = SetEnvironmentAndGetSDKDir()
452*8975f5c5SAndroid Build Coastguard Worker  if not win_sdk_dir:
453*8975f5c5SAndroid Build Coastguard Worker    return
454*8975f5c5SAndroid Build Coastguard Worker
455*8975f5c5SAndroid Build Coastguard Worker  # List of debug files that should be copied, the first element of the tuple is
456*8975f5c5SAndroid Build Coastguard Worker  # the name of the file and the second indicates if it's optional.
457*8975f5c5SAndroid Build Coastguard Worker  debug_files = [('dbghelp.dll', False), ('dbgcore.dll', True)]
458*8975f5c5SAndroid Build Coastguard Worker  for debug_file, is_optional in debug_files:
459*8975f5c5SAndroid Build Coastguard Worker    full_path = os.path.join(win_sdk_dir, 'Debuggers', target_cpu, debug_file)
460*8975f5c5SAndroid Build Coastguard Worker    if not os.path.exists(full_path):
461*8975f5c5SAndroid Build Coastguard Worker      if is_optional:
462*8975f5c5SAndroid Build Coastguard Worker        continue
463*8975f5c5SAndroid Build Coastguard Worker      else:
464*8975f5c5SAndroid Build Coastguard Worker        raise Exception('%s not found in "%s"\r\nYou must install '
465*8975f5c5SAndroid Build Coastguard Worker                        'Windows 10 SDK version %s including the '
466*8975f5c5SAndroid Build Coastguard Worker                        '"Debugging Tools for Windows" feature.' %
467*8975f5c5SAndroid Build Coastguard Worker                        (debug_file, full_path, SDK_VERSION))
468*8975f5c5SAndroid Build Coastguard Worker    target_path = os.path.join(target_dir, debug_file)
469*8975f5c5SAndroid Build Coastguard Worker    _CopyRuntimeImpl(target_path, full_path)
470*8975f5c5SAndroid Build Coastguard Worker
471*8975f5c5SAndroid Build Coastguard Worker  # The x64 version of msdia140.dll is always used because symupload and
472*8975f5c5SAndroid Build Coastguard Worker  # dump_syms are always built as x64 binaries.
473*8975f5c5SAndroid Build Coastguard Worker  dia_path = os.path.join(NormalizePath(os.environ['GYP_MSVS_OVERRIDE_PATH']),
474*8975f5c5SAndroid Build Coastguard Worker                          'DIA SDK', 'bin', 'amd64', 'msdia140.dll')
475*8975f5c5SAndroid Build Coastguard Worker  _CopyRuntimeImpl(os.path.join(target_dir, 'msdia140.dll'), dia_path)
476*8975f5c5SAndroid Build Coastguard Worker
477*8975f5c5SAndroid Build Coastguard Worker
478*8975f5c5SAndroid Build Coastguard Workerdef _GetDesiredVsToolchainHashes():
479*8975f5c5SAndroid Build Coastguard Worker  """Load a list of SHA1s corresponding to the toolchains that we want installed
480*8975f5c5SAndroid Build Coastguard Worker  to build with."""
481*8975f5c5SAndroid Build Coastguard Worker  # Third parties that do not have access to the canonical toolchain can map
482*8975f5c5SAndroid Build Coastguard Worker  # canonical toolchain version to their own toolchain versions.
483*8975f5c5SAndroid Build Coastguard Worker  toolchain_hash_mapping_key = 'GYP_MSVS_HASH_%s' % TOOLCHAIN_HASH
484*8975f5c5SAndroid Build Coastguard Worker  return [os.environ.get(toolchain_hash_mapping_key, TOOLCHAIN_HASH)]
485*8975f5c5SAndroid Build Coastguard Worker
486*8975f5c5SAndroid Build Coastguard Worker
487*8975f5c5SAndroid Build Coastguard Workerdef ShouldUpdateToolchain():
488*8975f5c5SAndroid Build Coastguard Worker  """Check if the toolchain should be upgraded."""
489*8975f5c5SAndroid Build Coastguard Worker  if not os.path.exists(json_data_file):
490*8975f5c5SAndroid Build Coastguard Worker    return True
491*8975f5c5SAndroid Build Coastguard Worker  with open(json_data_file, 'r') as tempf:
492*8975f5c5SAndroid Build Coastguard Worker    toolchain_data = json.load(tempf)
493*8975f5c5SAndroid Build Coastguard Worker  version = toolchain_data['version']
494*8975f5c5SAndroid Build Coastguard Worker  env_version = GetVisualStudioVersion()
495*8975f5c5SAndroid Build Coastguard Worker  # If there's a mismatch between the version set in the environment and the one
496*8975f5c5SAndroid Build Coastguard Worker  # in the json file then the toolchain should be updated.
497*8975f5c5SAndroid Build Coastguard Worker  return version != env_version
498*8975f5c5SAndroid Build Coastguard Worker
499*8975f5c5SAndroid Build Coastguard Worker
500*8975f5c5SAndroid Build Coastguard Workerdef Update(force=False, no_download=False):
501*8975f5c5SAndroid Build Coastguard Worker  """Requests an update of the toolchain to the specific hashes we have at
502*8975f5c5SAndroid Build Coastguard Worker  this revision. The update outputs a .json of the various configuration
503*8975f5c5SAndroid Build Coastguard Worker  information required to pass to gyp which we use in |GetToolchainDir()|.
504*8975f5c5SAndroid Build Coastguard Worker  If no_download is true then the toolchain will be configured if present but
505*8975f5c5SAndroid Build Coastguard Worker  will not be downloaded.
506*8975f5c5SAndroid Build Coastguard Worker  """
507*8975f5c5SAndroid Build Coastguard Worker  if force != False and force != '--force':
508*8975f5c5SAndroid Build Coastguard Worker    print('Unknown parameter "%s"' % force, file=sys.stderr)
509*8975f5c5SAndroid Build Coastguard Worker    return 1
510*8975f5c5SAndroid Build Coastguard Worker  if force == '--force' or os.path.exists(json_data_file):
511*8975f5c5SAndroid Build Coastguard Worker    force = True
512*8975f5c5SAndroid Build Coastguard Worker
513*8975f5c5SAndroid Build Coastguard Worker  depot_tools_win_toolchain = \
514*8975f5c5SAndroid Build Coastguard Worker      bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', '1')))
515*8975f5c5SAndroid Build Coastguard Worker  if (_HostIsWindows() or force) and depot_tools_win_toolchain:
516*8975f5c5SAndroid Build Coastguard Worker    import find_depot_tools
517*8975f5c5SAndroid Build Coastguard Worker    depot_tools_path = find_depot_tools.add_depot_tools_to_path()
518*8975f5c5SAndroid Build Coastguard Worker
519*8975f5c5SAndroid Build Coastguard Worker    # On Linux, the file system is usually case-sensitive while the Windows
520*8975f5c5SAndroid Build Coastguard Worker    # SDK only works on case-insensitive file systems.  If it doesn't already
521*8975f5c5SAndroid Build Coastguard Worker    # exist, set up a ciopfs fuse mount to put the SDK in a case-insensitive
522*8975f5c5SAndroid Build Coastguard Worker    # part of the file system.
523*8975f5c5SAndroid Build Coastguard Worker    toolchain_dir = os.path.join(depot_tools_path, 'win_toolchain', 'vs_files')
524*8975f5c5SAndroid Build Coastguard Worker    # For testing this block, unmount existing mounts with
525*8975f5c5SAndroid Build Coastguard Worker    # fusermount -u third_party/depot_tools/win_toolchain/vs_files
526*8975f5c5SAndroid Build Coastguard Worker    if sys.platform.startswith('linux') and not os.path.ismount(toolchain_dir):
527*8975f5c5SAndroid Build Coastguard Worker      ciopfs = shutil.which('ciopfs')
528*8975f5c5SAndroid Build Coastguard Worker      if not ciopfs:
529*8975f5c5SAndroid Build Coastguard Worker        # ciopfs not found in PATH; try the one downloaded from the DEPS hook.
530*8975f5c5SAndroid Build Coastguard Worker        ciopfs = os.path.join(script_dir, 'ciopfs')
531*8975f5c5SAndroid Build Coastguard Worker      if not os.path.isdir(toolchain_dir):
532*8975f5c5SAndroid Build Coastguard Worker        os.mkdir(toolchain_dir)
533*8975f5c5SAndroid Build Coastguard Worker      if not os.path.isdir(toolchain_dir + '.ciopfs'):
534*8975f5c5SAndroid Build Coastguard Worker        os.mkdir(toolchain_dir + '.ciopfs')
535*8975f5c5SAndroid Build Coastguard Worker      # Without use_ino, clang's #pragma once and Wnonportable-include-path
536*8975f5c5SAndroid Build Coastguard Worker      # both don't work right, see https://llvm.org/PR34931
537*8975f5c5SAndroid Build Coastguard Worker      # use_ino doesn't slow down builds, so it seems there's no drawback to
538*8975f5c5SAndroid Build Coastguard Worker      # just using it always.
539*8975f5c5SAndroid Build Coastguard Worker      subprocess.check_call([
540*8975f5c5SAndroid Build Coastguard Worker          ciopfs, '-o', 'use_ino', toolchain_dir + '.ciopfs', toolchain_dir])
541*8975f5c5SAndroid Build Coastguard Worker
542*8975f5c5SAndroid Build Coastguard Worker    get_toolchain_args = [
543*8975f5c5SAndroid Build Coastguard Worker        sys.executable,
544*8975f5c5SAndroid Build Coastguard Worker        os.path.join(depot_tools_path,
545*8975f5c5SAndroid Build Coastguard Worker                    'win_toolchain',
546*8975f5c5SAndroid Build Coastguard Worker                    'get_toolchain_if_necessary.py'),
547*8975f5c5SAndroid Build Coastguard Worker        '--output-json', json_data_file,
548*8975f5c5SAndroid Build Coastguard Worker      ] + _GetDesiredVsToolchainHashes()
549*8975f5c5SAndroid Build Coastguard Worker    if force:
550*8975f5c5SAndroid Build Coastguard Worker      get_toolchain_args.append('--force')
551*8975f5c5SAndroid Build Coastguard Worker    if no_download:
552*8975f5c5SAndroid Build Coastguard Worker      get_toolchain_args.append('--no-download')
553*8975f5c5SAndroid Build Coastguard Worker    subprocess.check_call(get_toolchain_args)
554*8975f5c5SAndroid Build Coastguard Worker
555*8975f5c5SAndroid Build Coastguard Worker  return 0
556*8975f5c5SAndroid Build Coastguard Worker
557*8975f5c5SAndroid Build Coastguard Worker
558*8975f5c5SAndroid Build Coastguard Workerdef NormalizePath(path):
559*8975f5c5SAndroid Build Coastguard Worker  while path.endswith('\\'):
560*8975f5c5SAndroid Build Coastguard Worker    path = path[:-1]
561*8975f5c5SAndroid Build Coastguard Worker  return path
562*8975f5c5SAndroid Build Coastguard Worker
563*8975f5c5SAndroid Build Coastguard Worker
564*8975f5c5SAndroid Build Coastguard Workerdef SetEnvironmentAndGetSDKDir():
565*8975f5c5SAndroid Build Coastguard Worker  """Gets location information about the current sdk (must have been
566*8975f5c5SAndroid Build Coastguard Worker  previously updated by 'update'). This is used for the GN build."""
567*8975f5c5SAndroid Build Coastguard Worker  SetEnvironmentAndGetRuntimeDllDirs()
568*8975f5c5SAndroid Build Coastguard Worker
569*8975f5c5SAndroid Build Coastguard Worker  # If WINDOWSSDKDIR is not set, search the default SDK path and set it.
570*8975f5c5SAndroid Build Coastguard Worker  if not 'WINDOWSSDKDIR' in os.environ:
571*8975f5c5SAndroid Build Coastguard Worker    default_sdk_path = os.path.expandvars('%ProgramFiles(x86)%'
572*8975f5c5SAndroid Build Coastguard Worker                                          '\\Windows Kits\\10')
573*8975f5c5SAndroid Build Coastguard Worker    if os.path.isdir(default_sdk_path):
574*8975f5c5SAndroid Build Coastguard Worker      os.environ['WINDOWSSDKDIR'] = default_sdk_path
575*8975f5c5SAndroid Build Coastguard Worker
576*8975f5c5SAndroid Build Coastguard Worker  return NormalizePath(os.environ['WINDOWSSDKDIR'])
577*8975f5c5SAndroid Build Coastguard Worker
578*8975f5c5SAndroid Build Coastguard Worker
579*8975f5c5SAndroid Build Coastguard Workerdef SDKIncludesIDCompositionDevice4():
580*8975f5c5SAndroid Build Coastguard Worker  """Returns true if the selected Windows SDK includes the declaration for the
581*8975f5c5SAndroid Build Coastguard Worker    IDCompositionDevice4 interface. This is essentially the equivalent checking
582*8975f5c5SAndroid Build Coastguard Worker    if a (non-preview) SDK version >=10.0.22621.2428.
583*8975f5c5SAndroid Build Coastguard Worker
584*8975f5c5SAndroid Build Coastguard Worker    We cannot check for this SDK version directly since it installs to a folder
585*8975f5c5SAndroid Build Coastguard Worker    with the minor version set to 0 (i.e. 10.0.22621.0) and the
586*8975f5c5SAndroid Build Coastguard Worker    IDCompositionDevice4 interface was added in a servicing release which did
587*8975f5c5SAndroid Build Coastguard Worker    not increment the major version.
588*8975f5c5SAndroid Build Coastguard Worker
589*8975f5c5SAndroid Build Coastguard Worker    There doesn't seem to be a straightforward and cross-platform way to get the
590*8975f5c5SAndroid Build Coastguard Worker    minor version of an installed SDK directory. To work around this, we look
591*8975f5c5SAndroid Build Coastguard Worker    for the GUID declaring the interface which implies the SDK version and
592*8975f5c5SAndroid Build Coastguard Worker    ensures the interface itself is present."""
593*8975f5c5SAndroid Build Coastguard Worker  win_sdk_dir = SetEnvironmentAndGetSDKDir()
594*8975f5c5SAndroid Build Coastguard Worker  if not win_sdk_dir:
595*8975f5c5SAndroid Build Coastguard Worker    return False
596*8975f5c5SAndroid Build Coastguard Worker
597*8975f5c5SAndroid Build Coastguard Worker  # Skip this check if we know the major version definitely includes
598*8975f5c5SAndroid Build Coastguard Worker  # IDCompositionDevice4.
599*8975f5c5SAndroid Build Coastguard Worker  if int(SDK_VERSION.split('.')[2]) > 22621:
600*8975f5c5SAndroid Build Coastguard Worker    return True
601*8975f5c5SAndroid Build Coastguard Worker
602*8975f5c5SAndroid Build Coastguard Worker  dcomp_header_path = os.path.join(win_sdk_dir, 'Include', SDK_VERSION, 'um',
603*8975f5c5SAndroid Build Coastguard Worker                                   'dcomp.h')
604*8975f5c5SAndroid Build Coastguard Worker  DECLARE_DEVICE4_LINE = ('DECLARE_INTERFACE_IID_('
605*8975f5c5SAndroid Build Coastguard Worker                          'IDCompositionDevice4, IDCompositionDevice3, '
606*8975f5c5SAndroid Build Coastguard Worker                          '"85FC5CCA-2DA6-494C-86B6-4A775C049B8A")')
607*8975f5c5SAndroid Build Coastguard Worker  with open(dcomp_header_path) as f:
608*8975f5c5SAndroid Build Coastguard Worker    for line in f.readlines():
609*8975f5c5SAndroid Build Coastguard Worker      if line.rstrip() == DECLARE_DEVICE4_LINE:
610*8975f5c5SAndroid Build Coastguard Worker        return True
611*8975f5c5SAndroid Build Coastguard Worker
612*8975f5c5SAndroid Build Coastguard Worker  return False
613*8975f5c5SAndroid Build Coastguard Worker
614*8975f5c5SAndroid Build Coastguard Worker
615*8975f5c5SAndroid Build Coastguard Workerdef GetToolchainDir():
616*8975f5c5SAndroid Build Coastguard Worker  """Gets location information about the current toolchain (must have been
617*8975f5c5SAndroid Build Coastguard Worker  previously updated by 'update'). This is used for the GN build."""
618*8975f5c5SAndroid Build Coastguard Worker  runtime_dll_dirs = SetEnvironmentAndGetRuntimeDllDirs()
619*8975f5c5SAndroid Build Coastguard Worker  win_sdk_dir = SetEnvironmentAndGetSDKDir()
620*8975f5c5SAndroid Build Coastguard Worker  version_as_year = GetVisualStudioVersion()
621*8975f5c5SAndroid Build Coastguard Worker
622*8975f5c5SAndroid Build Coastguard Worker  if not SDKIncludesIDCompositionDevice4():
623*8975f5c5SAndroid Build Coastguard Worker    print(
624*8975f5c5SAndroid Build Coastguard Worker        'Windows SDK >= 10.0.22621.2428 required. You can get it by updating '
625*8975f5c5SAndroid Build Coastguard Worker        f'Visual Studio {version_as_year} using the Visual Studio Installer.',
626*8975f5c5SAndroid Build Coastguard Worker        file=sys.stderr,
627*8975f5c5SAndroid Build Coastguard Worker    )
628*8975f5c5SAndroid Build Coastguard Worker    return 1
629*8975f5c5SAndroid Build Coastguard Worker
630*8975f5c5SAndroid Build Coastguard Worker  print('''vs_path = %s
631*8975f5c5SAndroid Build Coastguard Workersdk_version = %s
632*8975f5c5SAndroid Build Coastguard Workersdk_path = %s
633*8975f5c5SAndroid Build Coastguard Workervs_version = %s
634*8975f5c5SAndroid Build Coastguard Workerwdk_dir = %s
635*8975f5c5SAndroid Build Coastguard Workerruntime_dirs = %s
636*8975f5c5SAndroid Build Coastguard Worker''' % (ToGNString(NormalizePath(
637*8975f5c5SAndroid Build Coastguard Worker      os.environ['GYP_MSVS_OVERRIDE_PATH'])), ToGNString(SDK_VERSION),
638*8975f5c5SAndroid Build Coastguard Worker       ToGNString(win_sdk_dir), ToGNString(version_as_year),
639*8975f5c5SAndroid Build Coastguard Worker       ToGNString(NormalizePath(os.environ.get('WDK_DIR', ''))),
640*8975f5c5SAndroid Build Coastguard Worker       ToGNString(os.path.pathsep.join(runtime_dll_dirs or ['None']))))
641*8975f5c5SAndroid Build Coastguard Worker
642*8975f5c5SAndroid Build Coastguard Worker
643*8975f5c5SAndroid Build Coastguard Workerdef main():
644*8975f5c5SAndroid Build Coastguard Worker  commands = {
645*8975f5c5SAndroid Build Coastguard Worker      'update': Update,
646*8975f5c5SAndroid Build Coastguard Worker      'get_toolchain_dir': GetToolchainDir,
647*8975f5c5SAndroid Build Coastguard Worker      'copy_dlls': CopyDlls,
648*8975f5c5SAndroid Build Coastguard Worker  }
649*8975f5c5SAndroid Build Coastguard Worker  if len(sys.argv) < 2 or sys.argv[1] not in commands:
650*8975f5c5SAndroid Build Coastguard Worker    print('Expected one of: %s' % ', '.join(commands), file=sys.stderr)
651*8975f5c5SAndroid Build Coastguard Worker    return 1
652*8975f5c5SAndroid Build Coastguard Worker  return commands[sys.argv[1]](*sys.argv[2:])
653*8975f5c5SAndroid Build Coastguard Worker
654*8975f5c5SAndroid Build Coastguard Worker
655*8975f5c5SAndroid Build Coastguard Workerif __name__ == '__main__':
656*8975f5c5SAndroid Build Coastguard Worker  sys.exit(main())
657