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