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 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 21TOOLCHAIN_VERSION = '2015' 22TOOLCHAIN_HASH = 'd3cb0e37bdd120ad0ac4650b674b09e81be45616' 23 24 25def 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 """ 30 vs_runtime_dll_dirs = None 31 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'] 41 win_sdk = toolchain_data.get('win_sdk') 42 if not win_sdk: 43 win_sdk = toolchain_data['win8sdk'] 44 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 48 vs_runtime_dll_dirs = toolchain_data['runtime_dirs'] 49 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')) 57 gyp_defines_dict['windows_sdk_path'] = win_sdk 58 os.environ['GYP_DEFINES'] = ' '.join('%s=%s' % (k, pipes.quote(str(v))) 59 for k, v in gyp_defines_dict.iteritems()) 60 os.environ['WINDOWSSDKDIR'] = win_sdk 61 os.environ['WDK_DIR'] = wdk 62 # Include the VS runtime in the PATH in case it's not machine-installed. 63 runtime_path = ';'.join(vs_runtime_dll_dirs) 64 os.environ['PATH'] = runtime_path + ';' + os.environ['PATH'] 65 return vs_runtime_dll_dirs 66 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() 85 # 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 88 get_toolchain_args = [ 89 sys.executable, 90 os.path.join(depot_tools_path, 91 'win_toolchain', 92 'get_toolchain_if_necessary.py'), 93 '--output-json', json_data_file, TOOLCHAIN_HASH, 94 ] 95 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()) 114