import collections
import glob
import json
import os
import platform
import re
import shutil
import stat
import subprocess
import sys
from gn_helpers import ToGNString
TOOLCHAIN_HASH = 'e66617bc68'
SDK_VERSION = '10.0.26100.0'
MSVS_VERSIONS = collections.OrderedDict([
('2026', '18.0'), ('2022', '17.0'),
('2019', '16.0'),
('2017', '15.0'),
])
MSVC_TOOLSET_VERSION = {
'2026': 'VC145',
'2022': 'VC143',
'2019': 'VC142',
'2017': 'VC141',
}
script_dir = os.path.dirname(os.path.realpath(__file__))
json_data_file = os.path.join(script_dir, 'win_toolchain.json')
def _HostIsWindows():
return sys.platform in ('win32', 'cygwin')
def SetEnvironmentAndGetRuntimeDllDirs():
vs_runtime_dll_dirs = None
depot_tools_win_toolchain = \
bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', '1')))
if ((_HostIsWindows() or os.path.exists(json_data_file))
and depot_tools_win_toolchain):
if ShouldUpdateToolchain():
if len(sys.argv) > 1 and sys.argv[1] == 'update':
update_result = Update()
else:
update_result = Update(no_download=True)
if update_result != 0:
raise Exception('Failed to update, error code %d.' % update_result)
with open(json_data_file, 'r') as tempf:
toolchain_data = json.load(tempf)
toolchain = toolchain_data['path']
version = toolchain_data['version']
win_sdk = toolchain_data.get('win_sdk')
wdk = toolchain_data['wdk']
vs_runtime_dll_dirs = toolchain_data['runtime_dirs']
if len(vs_runtime_dll_dirs) == 2:
vs_runtime_dll_dirs.append('Arm64Unused')
os.environ['GYP_MSVS_OVERRIDE_PATH'] = toolchain
os.environ['WINDOWSSDKDIR'] = win_sdk
os.environ['WDK_DIR'] = wdk
runtime_path = os.path.pathsep.join(vs_runtime_dll_dirs)
os.environ['PATH'] = runtime_path + os.path.pathsep + os.environ['PATH']
elif sys.platform == 'win32' and not depot_tools_win_toolchain:
if not 'GYP_MSVS_OVERRIDE_PATH' in os.environ:
os.environ['GYP_MSVS_OVERRIDE_PATH'] = DetectVisualStudioPath()
bitness = platform.architecture()[0]
x64_path = 'System32' if bitness == '64bit' else 'Sysnative'
x64_path = os.path.join(os.path.expandvars('%windir%'), x64_path)
vs_runtime_dll_dirs = [x64_path,
os.path.join(os.path.expandvars('%windir%'),
'SysWOW64'),
'Arm64Unused']
return vs_runtime_dll_dirs
def _RegistryGetValueUsingWinReg(key, value):
import _winreg
try:
root, subkey = key.split('\\', 1)
assert root == 'HKLM' with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey) as hkey:
return _winreg.QueryValueEx(hkey, value)[0]
except WindowsError:
return None
def _RegistryGetValue(key, value):
try:
return _RegistryGetValueUsingWinReg(key, value)
except ImportError:
raise Exception('The python library _winreg not found.')
def _GenerateCandidatePaths(version):
path = os.environ.get('vs%s_install' % version)
if path:
yield path
MSVC_LOCATION = {
'2026': ['%ProgramFiles%', '18'],
'2022': ['%ProgramFiles%', '2022'],
'2019': ['%ProgramFiles(x86)%', '2019'],
'2017': ['%ProgramFiles(x86)%', '2017'],
}
MSVC_EDITIONS = ['Enterprise', 'Professional', 'Community', 'Preview', 'BuildTools']
path = os.path.expandvars(MSVC_LOCATION[version][0] +
'/Microsoft Visual Studio/%s' % MSVC_LOCATION[version][1])
if path:
for edition in MSVC_EDITIONS:
yield os.path.join(path, edition)
def _CheckIfVersionInstalled(version):
return any(
os.path.exists(path)
for path in _GenerateCandidatePaths(version))
def GetVisualStudioVersion():
supported_versions = list(MSVS_VERSIONS.keys())
if bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', '1'))):
return supported_versions[0]
supported_versions_str = ', '.join('{} ({})'.format(v,k)
for k,v in MSVS_VERSIONS.items())
available_versions = []
for version in supported_versions:
if _CheckIfVersionInstalled(version):
available_versions.append(version)
break
if not available_versions:
raise Exception('No supported Visual Studio can be found.'
' Supported versions are: %s.' % supported_versions_str)
return available_versions[0]
def DetectVisualStudioPath():
version_as_year = GetVisualStudioVersion()
for path in _GenerateCandidatePaths(version_as_year):
if path and os.path.exists(path):
return path
raise Exception('Visual Studio Version %s not found.' % version_as_year)
def _CopyRuntimeImpl(target, source, verbose=True):
if (os.path.isdir(os.path.dirname(target)) and
(not os.path.isfile(target) or
abs(os.stat(target).st_mtime - os.stat(source).st_mtime) >= 0.01)):
if verbose:
print('Copying %s to %s...' % (source, target))
if os.path.exists(target):
os.chmod(target, stat.S_IWRITE | stat.S_IREAD)
os.unlink(target)
shutil.copy2(source, target)
os.chmod(target, stat.S_IWRITE | stat.S_IREAD)
def _SortByHighestVersionNumberFirst(list_of_str_versions):
def to_int_if_int(x):
try:
return int(x)
except ValueError:
return x
def to_number_sequence(x):
part_sequence = re.split(r'[\\/\.]', x)
return [to_int_if_int(x) for x in part_sequence]
list_of_str_versions.sort(key=to_number_sequence, reverse=True)
def _CopyUCRTRuntime(target_dir, source_dir, target_cpu, debug):
if target_cpu == 'arm64':
vc_redist_root = FindVCRedistRoot()
if not debug:
vc_toolset_dir = 'Microsoft.{}.CRT' \
.format(MSVC_TOOLSET_VERSION[GetVisualStudioVersion()])
source_dir = os.path.join(vc_redist_root,
'arm64', vc_toolset_dir)
else:
vc_toolset_dir = 'Microsoft.{}.DebugCRT' \
.format(MSVC_TOOLSET_VERSION[GetVisualStudioVersion()])
source_dir = os.path.join(vc_redist_root, 'debug_nonredist',
'arm64', vc_toolset_dir)
def d(s):
return s + 'd' if debug else s
file_parts = (d('msvcp140'), d('msvcp140') + '_atomic_wait', d('vccorlib140'),
d('vcruntime140'))
if target_cpu == 'x64' and GetVisualStudioVersion() != '2017':
file_parts = file_parts + (d('vcruntime140_1'), )
for file_part in file_parts:
dll = file_part + '.dll'
target = os.path.join(target_dir, dll)
source = os.path.join(source_dir, dll)
_CopyRuntimeImpl(target, source)
if debug:
win_sdk_dir = os.path.normpath(
os.environ.get(
'WINDOWSSDKDIR',
os.path.expandvars('%ProgramFiles(x86)%'
'\\Windows Kits\\10')))
sdk_bin_root = os.path.join(win_sdk_dir, 'bin')
sdk_bin_sub_dirs = glob.glob(os.path.join(sdk_bin_root, '10.*'))
_SortByHighestVersionNumberFirst(sdk_bin_sub_dirs)
for directory in sdk_bin_sub_dirs:
sdk_redist_root_version = os.path.join(sdk_bin_root, directory)
if not os.path.isdir(sdk_redist_root_version):
continue
source_dir = os.path.join(sdk_redist_root_version, target_cpu, 'ucrt')
if not os.path.isdir(source_dir):
continue
break
_CopyRuntimeImpl(os.path.join(target_dir,
d('ucrtbase') + '.dll'),
os.path.join(source_dir,
d('ucrtbase') + '.dll'))
def FindVCComponentRoot(component):
SetEnvironmentAndGetRuntimeDllDirs()
assert ('GYP_MSVS_OVERRIDE_PATH' in os.environ)
vc_component_msvc_root = os.path.join(os.environ['GYP_MSVS_OVERRIDE_PATH'],
'VC', component, 'MSVC')
vc_component_msvc_contents = glob.glob(
os.path.join(vc_component_msvc_root, '14.*'))
_SortByHighestVersionNumberFirst(vc_component_msvc_contents)
for directory in vc_component_msvc_contents:
if os.path.isdir(directory):
return directory
raise Exception('Unable to find the VC %s directory.' % component)
def FindVCRedistRoot():
return FindVCComponentRoot('Redist')
def _CopyRuntime(target_dir, source_dir, target_cpu, debug):
_CopyUCRTRuntime(target_dir, source_dir, target_cpu, debug)
def CopyDlls(target_dir, configuration, target_cpu):
vs_runtime_dll_dirs = SetEnvironmentAndGetRuntimeDllDirs()
if not vs_runtime_dll_dirs:
return
x64_runtime, x86_runtime, arm64_runtime = vs_runtime_dll_dirs
if target_cpu == 'x64':
runtime_dir = x64_runtime
elif target_cpu == 'x86':
runtime_dir = x86_runtime
elif target_cpu == 'arm64':
runtime_dir = arm64_runtime
else:
raise Exception('Unknown target_cpu: ' + target_cpu)
_CopyRuntime(target_dir, runtime_dir, target_cpu, debug=False)
if configuration == 'Debug':
_CopyRuntime(target_dir, runtime_dir, target_cpu, debug=True)
_CopyDebugger(target_dir, target_cpu)
if target_cpu == 'arm64':
target_dir = os.path.join(target_dir, 'win_clang_x64')
target_cpu = 'x64'
runtime_dir = x64_runtime
os.makedirs(target_dir, exist_ok=True)
_CopyRuntime(target_dir, runtime_dir, target_cpu, debug=False)
if configuration == 'Debug':
_CopyRuntime(target_dir, runtime_dir, target_cpu, debug=True)
_CopyDebugger(target_dir, target_cpu)
def _CopyDebugger(target_dir, target_cpu):
win_sdk_dir = SetEnvironmentAndGetSDKDir()
if not win_sdk_dir:
return
debug_files = [('dbghelp.dll', False), ('dbgcore.dll', True),
('symsrv.dll', True)]
for debug_file, is_optional in debug_files:
full_path = os.path.join(win_sdk_dir, 'Debuggers', target_cpu, debug_file)
if not os.path.exists(full_path):
if is_optional:
continue
else:
raise Exception('%s not found in "%s"\r\nYou must install '
'Windows 10 SDK version %s including the '
'"Debugging Tools for Windows" feature.' %
(debug_file, full_path, SDK_VERSION))
target_path = os.path.join(target_dir, debug_file)
_CopyRuntimeImpl(target_path, full_path)
dia_path = os.path.join(NormalizePath(os.environ['GYP_MSVS_OVERRIDE_PATH']),
'DIA SDK', 'bin', 'amd64', 'msdia140.dll')
_CopyRuntimeImpl(os.path.join(target_dir, 'msdia140.dll'), dia_path)
def _GetDesiredVsToolchainHashes():
toolchain_hash_mapping_key = 'GYP_MSVS_HASH_%s' % TOOLCHAIN_HASH
return [os.environ.get(toolchain_hash_mapping_key, TOOLCHAIN_HASH)]
def ShouldUpdateToolchain():
if not os.path.exists(json_data_file):
return True
with open(json_data_file, 'r') as tempf:
toolchain_data = json.load(tempf)
version = toolchain_data['version']
env_version = GetVisualStudioVersion()
return version != env_version
def Update(force=False, no_download=False):
if force != False and force != '--force':
print('Unknown parameter "%s"' % force, file=sys.stderr)
return 1
if force == '--force' or os.path.exists(json_data_file):
force = True
depot_tools_win_toolchain = \
bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', '1')))
if (_HostIsWindows() or force) and depot_tools_win_toolchain:
import find_depot_tools
depot_tools_path = find_depot_tools.add_depot_tools_to_path()
toolchain_dir = os.path.join(depot_tools_path, 'win_toolchain', 'vs_files')
if sys.platform.startswith('linux') and not os.path.ismount(toolchain_dir):
ciopfs = shutil.which('ciopfs')
if not ciopfs:
ciopfs = os.path.join(script_dir, 'ciopfs')
if not os.path.isdir(toolchain_dir):
try:
os.mkdir(toolchain_dir)
except FileExistsError:
subprocess.check_call(["fusermount", "-u", toolchain_dir])
if not os.path.isdir(toolchain_dir + '.ciopfs'):
os.mkdir(toolchain_dir + '.ciopfs')
subprocess.check_call([
ciopfs, '-o', 'use_ino', toolchain_dir + '.ciopfs', toolchain_dir])
get_toolchain_args = [
sys.executable,
os.path.join(depot_tools_path,
'win_toolchain',
'get_toolchain_if_necessary.py'),
'--output-json', json_data_file,
] + _GetDesiredVsToolchainHashes()
if force:
get_toolchain_args.append('--force')
if no_download:
get_toolchain_args.append('--no-download')
subprocess.check_call(get_toolchain_args)
return 0
def NormalizePath(path):
while path.endswith('\\'):
path = path[:-1]
return path
def SetEnvironmentAndGetSDKDir():
SetEnvironmentAndGetRuntimeDllDirs()
if not 'WINDOWSSDKDIR' in os.environ:
default_sdk_path = os.path.expandvars('%ProgramFiles(x86)%'
'\\Windows Kits\\10')
if os.path.isdir(default_sdk_path):
os.environ['WINDOWSSDKDIR'] = default_sdk_path
return NormalizePath(os.environ['WINDOWSSDKDIR'])
def SDKIncludesIDCompositionDevice4():
win_sdk_dir = SetEnvironmentAndGetSDKDir()
if not win_sdk_dir:
return False
if int(SDK_VERSION.split('.')[2]) > 22621:
return True
dcomp_header_path = os.path.join(win_sdk_dir, 'Include', SDK_VERSION, 'um',
'dcomp.h')
DECLARE_DEVICE4_LINE = ('DECLARE_INTERFACE_IID_('
'IDCompositionDevice4, IDCompositionDevice3, '
'"85FC5CCA-2DA6-494C-86B6-4A775C049B8A")')
with open(dcomp_header_path) as f:
for line in f.readlines():
if line.rstrip() == DECLARE_DEVICE4_LINE:
return True
return False
def GetToolchainDir():
runtime_dll_dirs = SetEnvironmentAndGetRuntimeDllDirs()
win_sdk_dir = SetEnvironmentAndGetSDKDir()
version_as_year = GetVisualStudioVersion()
if not SDKIncludesIDCompositionDevice4():
print(
'Windows SDK >= 10.0.22621.2428 required. You can get it by updating '
f'Visual Studio {version_as_year} using the Visual Studio Installer.',
file=sys.stderr,
)
return 1
print('''vs_path = %s
sdk_version = %s
sdk_path = %s
vs_version = %s
wdk_dir = %s
runtime_dirs = %s
''' % (ToGNString(NormalizePath(
os.environ['GYP_MSVS_OVERRIDE_PATH'])), ToGNString(SDK_VERSION),
ToGNString(win_sdk_dir), ToGNString(version_as_year),
ToGNString(NormalizePath(os.environ.get('WDK_DIR', ''))),
ToGNString(os.path.pathsep.join(runtime_dll_dirs or ['None']))))
def main():
commands = {
'update': Update,
'get_toolchain_dir': GetToolchainDir,
'copy_dlls': CopyDlls,
}
if len(sys.argv) < 2 or sys.argv[1] not in commands:
print('Expected one of: %s' % ', '.join(commands), file=sys.stderr)
return 1
return commands[sys.argv[1]](*sys.argv[2:])
if __name__ == '__main__':
sys.exit(main())