David Benjamin | 05a5beb | 2015-03-12 20:13:41 -0400 | [diff] [blame] | 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 | |
| 5 | import json |
| 6 | import os |
| 7 | import pipes |
| 8 | import shutil |
| 9 | import subprocess |
| 10 | import sys |
| 11 | |
| 12 | |
| 13 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 14 | sys.path.insert(0, os.path.join(script_dir, 'gyp', 'pylib')) |
| 15 | json_data_file = os.path.join(script_dir, 'win_toolchain.json') |
| 16 | |
| 17 | |
| 18 | import gyp |
| 19 | |
| 20 | |
David Benjamin | 8861daa | 2016-05-02 15:22:34 -0400 | [diff] [blame] | 21 | TOOLCHAIN_VERSION = '2015' |
David Benjamin | a93bc11 | 2017-06-29 15:26:05 -0400 | [diff] [blame] | 22 | TOOLCHAIN_HASH = 'f53e4598951162bad6330f7a167486c7ae5db1e5' |
David Benjamin | 8861daa | 2016-05-02 15:22:34 -0400 | [diff] [blame] | 23 | |
| 24 | |
David Benjamin | 05a5beb | 2015-03-12 20:13:41 -0400 | [diff] [blame] | 25 | def 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 Benjamin | 8861daa | 2016-05-02 15:22:34 -0400 | [diff] [blame] | 30 | vs_runtime_dll_dirs = None |
David Benjamin | 05a5beb | 2015-03-12 20:13:41 -0400 | [diff] [blame] | 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'] |
David Benjamin | efad697 | 2015-06-02 16:59:27 -0400 | [diff] [blame] | 41 | win_sdk = toolchain_data.get('win_sdk') |
| 42 | if not win_sdk: |
| 43 | win_sdk = toolchain_data['win8sdk'] |
David Benjamin | 05a5beb | 2015-03-12 20:13:41 -0400 | [diff] [blame] | 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 |
David Benjamin | 8861daa | 2016-05-02 15:22:34 -0400 | [diff] [blame] | 48 | vs_runtime_dll_dirs = toolchain_data['runtime_dirs'] |
David Benjamin | 05a5beb | 2015-03-12 20:13:41 -0400 | [diff] [blame] | 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')) |
David Benjamin | efad697 | 2015-06-02 16:59:27 -0400 | [diff] [blame] | 57 | gyp_defines_dict['windows_sdk_path'] = win_sdk |
David Benjamin | 05a5beb | 2015-03-12 20:13:41 -0400 | [diff] [blame] | 58 | os.environ['GYP_DEFINES'] = ' '.join('%s=%s' % (k, pipes.quote(str(v))) |
| 59 | for k, v in gyp_defines_dict.iteritems()) |
David Benjamin | efad697 | 2015-06-02 16:59:27 -0400 | [diff] [blame] | 60 | os.environ['WINDOWSSDKDIR'] = win_sdk |
David Benjamin | 05a5beb | 2015-03-12 20:13:41 -0400 | [diff] [blame] | 61 | os.environ['WDK_DIR'] = wdk |
| 62 | # Include the VS runtime in the PATH in case it's not machine-installed. |
David Benjamin | 8861daa | 2016-05-02 15:22:34 -0400 | [diff] [blame] | 63 | runtime_path = ';'.join(vs_runtime_dll_dirs) |
David Benjamin | 05a5beb | 2015-03-12 20:13:41 -0400 | [diff] [blame] | 64 | os.environ['PATH'] = runtime_path + ';' + os.environ['PATH'] |
David Benjamin | 8861daa | 2016-05-02 15:22:34 -0400 | [diff] [blame] | 65 | return vs_runtime_dll_dirs |
David Benjamin | 05a5beb | 2015-03-12 20:13:41 -0400 | [diff] [blame] | 66 | |
| 67 | |
| 68 | def 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 | |
| 76 | def 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 Benjamin | 8861daa | 2016-05-02 15:22:34 -0400 | [diff] [blame] | 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 |
David Benjamin | 05a5beb | 2015-03-12 20:13:41 -0400 | [diff] [blame] | 88 | get_toolchain_args = [ |
| 89 | sys.executable, |
| 90 | os.path.join(depot_tools_path, |
| 91 | 'win_toolchain', |
| 92 | 'get_toolchain_if_necessary.py'), |
David Benjamin | 8861daa | 2016-05-02 15:22:34 -0400 | [diff] [blame] | 93 | '--output-json', json_data_file, TOOLCHAIN_HASH, |
| 94 | ] |
David Benjamin | 05a5beb | 2015-03-12 20:13:41 -0400 | [diff] [blame] | 95 | subprocess.check_call(get_toolchain_args) |
| 96 | |
| 97 | return 0 |
| 98 | |
| 99 | |
| 100 | def 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 | |
| 112 | if __name__ == '__main__': |
| 113 | sys.exit(main()) |