blob: d86f72579a77e6748ed74abdfa16257f98141a71 [file] [log] [blame]
David Benjamin05a5beb2015-03-12 20:13:41 -04001# 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 pipes
8import shutil
9import subprocess
10import sys
11
12
13script_dir = os.path.dirname(os.path.realpath(__file__))
14sys.path.insert(0, os.path.join(script_dir, 'gyp', 'pylib'))
15json_data_file = os.path.join(script_dir, 'win_toolchain.json')
16
17
18import gyp
19
20
David Benjamin8861daa2016-05-02 15:22:34 -040021TOOLCHAIN_VERSION = '2015'
David Benjamina93bc112017-06-29 15:26:05 -040022TOOLCHAIN_HASH = 'f53e4598951162bad6330f7a167486c7ae5db1e5'
David Benjamin8861daa2016-05-02 15:22:34 -040023
24
David Benjamin05a5beb2015-03-12 20:13:41 -040025def SetEnvironmentAndGetRuntimeDllDirs():
26 """Sets up os.environ to use the depot_tools VS toolchain with gyp, and
27 returns the location of the VS runtime DLLs so they can be copied into
28 the output directory after gyp generation.
29 """
David Benjamin8861daa2016-05-02 15:22:34 -040030 vs_runtime_dll_dirs = None
David Benjamin05a5beb2015-03-12 20:13:41 -040031 depot_tools_win_toolchain = \
32 bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', '1')))
33 if sys.platform in ('win32', 'cygwin') and depot_tools_win_toolchain:
34 if not os.path.exists(json_data_file):
35 Update()
36 with open(json_data_file, 'r') as tempf:
37 toolchain_data = json.load(tempf)
38
39 toolchain = toolchain_data['path']
40 version = toolchain_data['version']
David Benjaminefad6972015-06-02 16:59:27 -040041 win_sdk = toolchain_data.get('win_sdk')
42 if not win_sdk:
43 win_sdk = toolchain_data['win8sdk']
David Benjamin05a5beb2015-03-12 20:13:41 -040044 wdk = toolchain_data['wdk']
45 # TODO(scottmg): The order unfortunately matters in these. They should be
46 # split into separate keys for x86 and x64. (See CopyVsRuntimeDlls call
47 # below). http://crbug.com/345992
David Benjamin8861daa2016-05-02 15:22:34 -040048 vs_runtime_dll_dirs = toolchain_data['runtime_dirs']
David Benjamin05a5beb2015-03-12 20:13:41 -040049
50 os.environ['GYP_MSVS_OVERRIDE_PATH'] = toolchain
51 os.environ['GYP_MSVS_VERSION'] = version
52 # We need to make sure windows_sdk_path is set to the automated
53 # toolchain values in GYP_DEFINES, but don't want to override any
54 # otheroptions.express
55 # values there.
56 gyp_defines_dict = gyp.NameValueListToDict(gyp.ShlexEnv('GYP_DEFINES'))
David Benjaminefad6972015-06-02 16:59:27 -040057 gyp_defines_dict['windows_sdk_path'] = win_sdk
David Benjamin05a5beb2015-03-12 20:13:41 -040058 os.environ['GYP_DEFINES'] = ' '.join('%s=%s' % (k, pipes.quote(str(v)))
59 for k, v in gyp_defines_dict.iteritems())
David Benjaminefad6972015-06-02 16:59:27 -040060 os.environ['WINDOWSSDKDIR'] = win_sdk
David Benjamin05a5beb2015-03-12 20:13:41 -040061 os.environ['WDK_DIR'] = wdk
62 # Include the VS runtime in the PATH in case it's not machine-installed.
David Benjamin8861daa2016-05-02 15:22:34 -040063 runtime_path = ';'.join(vs_runtime_dll_dirs)
David Benjamin05a5beb2015-03-12 20:13:41 -040064 os.environ['PATH'] = runtime_path + ';' + os.environ['PATH']
David Benjamin8861daa2016-05-02 15:22:34 -040065 return vs_runtime_dll_dirs
David Benjamin05a5beb2015-03-12 20:13:41 -040066
67
68def FindDepotTools():
69 """Returns the path to depot_tools in $PATH."""
70 for path in os.environ['PATH'].split(os.pathsep):
71 if os.path.isfile(os.path.join(path, 'gclient.py')):
72 return path
73 raise Exception("depot_tools not found!")
74
75
76def Update():
77 """Requests an update of the toolchain to the specific hashes we have at
78 this revision. The update outputs a .json of the various configuration
79 information required to pass to gyp which we use in |GetToolchainDir()|.
80 """
81 depot_tools_win_toolchain = \
82 bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', '1')))
83 if sys.platform in ('win32', 'cygwin') and depot_tools_win_toolchain:
84 depot_tools_path = FindDepotTools()
David Benjamin8861daa2016-05-02 15:22:34 -040085 # Necessary so that get_toolchain_if_necessary.py will put the VS toolkit
86 # in the correct directory.
87 os.environ['GYP_MSVS_VERSION'] = TOOLCHAIN_VERSION
David Benjamin05a5beb2015-03-12 20:13:41 -040088 get_toolchain_args = [
89 sys.executable,
90 os.path.join(depot_tools_path,
91 'win_toolchain',
92 'get_toolchain_if_necessary.py'),
David Benjamin8861daa2016-05-02 15:22:34 -040093 '--output-json', json_data_file, TOOLCHAIN_HASH,
94 ]
David Benjamin05a5beb2015-03-12 20:13:41 -040095 subprocess.check_call(get_toolchain_args)
96
97 return 0
98
99
100def main():
101 if not sys.platform.startswith(('win32', 'cygwin')):
102 return 0
103 commands = {
104 'update': Update,
105 }
106 if len(sys.argv) < 2 or sys.argv[1] not in commands:
107 print >>sys.stderr, 'Expected one of: %s' % ', '.join(commands)
108 return 1
109 return commands[sys.argv[1]](*sys.argv[2:])
110
111
112if __name__ == '__main__':
113 sys.exit(main())