Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Extracts a Windows VS2013 toolchain from various downloadable pieces.""" | |
| 7 | |
| 8 | |
| 9 import ctypes | |
| 10 import optparse | |
| 11 import os | |
| 12 import shutil | |
| 13 import subprocess | |
| 14 import sys | |
| 15 import tempfile | |
| 16 import urllib2 | |
| 17 | |
| 18 | |
| 19 BASEDIR = os.path.dirname(os.path.abspath(__file__)) | |
| 20 g_temp_dirs = [] | |
| 21 | |
| 22 | |
| 23 def GetLongPathName(path): | |
| 24 """Converts any 8dot3 names in the path to the full name.""" | |
| 25 buf = ctypes.create_unicode_buffer(260) | |
| 26 size = ctypes.windll.kernel32.GetLongPathNameW(unicode(path), buf, 260) | |
| 27 if (size > 260): | |
| 28 raise SystemExit('Long form of path longer than 260 chars: %s' % path) | |
|
iannucci
2014/01/14 10:14:11
fyi, this can also be said like:
sys.exit('Long f
scottmg
2014/01/14 17:09:36
Done.
| |
| 29 return buf.value | |
| 30 | |
| 31 | |
| 32 def RunOrDie(command): | |
| 33 rc = subprocess.call(command, shell=True) | |
| 34 if rc != 0: | |
| 35 raise SystemExit('%s failed.' % command) | |
|
iannucci
2014/01/14 10:14:11
Isn't this just subprocess.check_call?
scottmg
2014/01/14 17:09:36
Done.
iannucci
2014/01/15 21:55:26
I was more meaning... inline RunOrDie?
| |
| 36 | |
| 37 | |
| 38 def TempDir(): | |
| 39 """Generates a temporary directory (for downloading or extracting to) and keep | |
| 40 track of the directory that's created for cleaning up later. | |
| 41 """ | |
| 42 global g_temp_dirs | |
| 43 temp = tempfile.mkdtemp() | |
| 44 g_temp_dirs.append(temp) | |
|
iannucci
2014/01/14 10:14:11
since you're not actually assigning g_temp_dirs, y
scottmg
2014/01/14 17:09:36
Yeah, it feels a bit nicer to make it obvious that
iannucci
2014/01/15 21:55:26
'sfine
| |
| 45 return temp | |
| 46 | |
| 47 | |
| 48 def DeleteAllTempDirs(): | |
| 49 """Removes all temporary directories created by |TempDir()|.""" | |
| 50 global g_temp_dirs | |
| 51 if g_temp_dirs: | |
| 52 sys.stdout.write('Cleaning up temporaries...\n') | |
| 53 for temp in g_temp_dirs: | |
| 54 # shutil.rmtree errors out on read only attributes. | |
| 55 RunOrDie('rmdir /s/q "%s"' % temp) | |
| 56 g_temp_dirs = [] | |
|
iannucci
2014/01/14 10:14:11
you could also get rid of the global line here by
scottmg
2014/01/14 17:09:36
Same.
| |
| 57 | |
| 58 | |
| 59 def GetIsoUrl(pro): | |
| 60 """Gets the .iso URL. | |
| 61 | |
| 62 If |pro| is False, downloads the Express edition. | |
| 63 """ | |
| 64 prefix = 'http://download.microsoft.com/download/' | |
| 65 if pro: | |
| 66 return (prefix + | |
| 67 'A/F/1/AF128362-A6A8-4DB3-A39A-C348086472CC/VS2013_RTM_PRO_ENU.iso') | |
| 68 else: | |
| 69 return (prefix + | |
| 70 '7/2/E/72E0F986-D247-4289-B9DC-C4FB07374894/VS2013_RTM_DskExp_ENU.iso') | |
| 71 | |
| 72 | |
| 73 def Download(url, local_path): | |
| 74 """Downloads a large-ish binary file and print some status information while | |
| 75 doing so. | |
| 76 """ | |
| 77 sys.stdout.write('Downloading %s...\n' % url) | |
| 78 req = urllib2.urlopen(url) | |
| 79 content_length = int(req.headers.get('Content-Length', 0)) | |
| 80 bytes_read = 0L | |
| 81 terminator = '\r' if sys.stdout.isatty() else '\n' | |
| 82 with open(local_path, 'wb') as file: | |
| 83 while True: | |
| 84 chunk = req.read(1024 * 1024) | |
| 85 if not chunk: | |
| 86 break | |
| 87 bytes_read += len(chunk) | |
| 88 file.write(chunk) | |
| 89 sys.stdout.write('... %d/%d%s' % (bytes_read, content_length, terminator)) | |
| 90 sys.stdout.flush() | |
| 91 sys.stdout.write('\n') | |
| 92 if content_length and content_length != bytes_read: | |
| 93 raise SystemExit('Got incorrect number of bytes downloading %s' % url) | |
| 94 | |
| 95 | |
| 96 def ExtractIso(iso_path): | |
| 97 """Uses 7zip to extract the contents of the given .iso (or self-extracting | |
| 98 .exe). | |
| 99 """ | |
| 100 target_path = TempDir() | |
| 101 sys.stdout.write('Extracting %s...\n' % iso_path) | |
| 102 sys.stdout.flush() | |
| 103 # TODO(scottmg): Do this (and exe) manually with python code. | |
| 104 # Note that at the beginning of main() we set the working directory to 7z's | |
| 105 # location so that 7z can find its codec dll. | |
| 106 RunOrDie('7z x "%s" -y "-o%s" >nul' % (iso_path, target_path)) | |
| 107 return target_path | |
| 108 | |
| 109 | |
| 110 def ExtractMsi(msi_path): | |
| 111 """Uses msiexec to extract the contents of the given .msi file.""" | |
| 112 sys.stdout.write('Extracting %s...\n' % msi_path) | |
| 113 target_path = TempDir() | |
| 114 RunOrDie('msiexec /a "%s" /qn TARGETDIR="%s"' % (msi_path, target_path)) | |
| 115 return target_path | |
| 116 | |
| 117 | |
| 118 def DownloadMainIso(url): | |
| 119 temp_dir = TempDir() | |
| 120 target_path = os.path.join(temp_dir, os.path.basename(url)) | |
| 121 Download(url, target_path) | |
| 122 return target_path | |
| 123 | |
| 124 | |
| 125 def GetSourceImage(local_dir, pro): | |
| 126 url = GetIsoUrl(pro) | |
| 127 if local_dir: | |
| 128 return os.path.join(local_dir, os.path.basename(url)) | |
| 129 else: | |
| 130 return DownloadMainIso(url) | |
| 131 | |
| 132 | |
| 133 def ExtractMsiList(iso_dir, packages): | |
| 134 """Extracts the contents of a list of .msi files from an already extracted | |
| 135 .iso file. | |
| 136 | |
| 137 |packages| is a list of pairs (msi, required). If required is not True, the | |
| 138 msi is optional (this is set for packages that are in Pro but not Express). | |
| 139 """ | |
| 140 results = [] | |
| 141 for (package, required) in packages: | |
| 142 path_to_package = os.path.join(iso_dir, 'packages', package) | |
| 143 if not os.path.exists(path_to_package) and not required: | |
| 144 continue | |
| 145 results.append(ExtractMsi(path_to_package)) | |
| 146 return results | |
| 147 | |
| 148 | |
| 149 def ExtractComponents(image): | |
| 150 packages = [ | |
| 151 (r'vcRuntimeAdditional_amd64\vc_runtimeAdditional_x64.msi', True), | |
| 152 (r'vcRuntimeAdditional_x86\vc_runtimeAdditional_x86.msi', True), | |
| 153 (r'vcRuntimeDebug_amd64\vc_runtimeDebug_x64.msi', True), | |
| 154 (r'vcRuntimeDebug_x86\vc_runtimeDebug_x86.msi', True), | |
| 155 (r'vcRuntimeMinimum_amd64\vc_runtimeMinimum_x64.msi', True), | |
| 156 (r'vcRuntimeMinimum_x86\vc_runtimeMinimum_x86.msi', True), | |
| 157 (r'vc_compilerCore86\vc_compilerCore86.msi', True), | |
| 158 (r'vc_compilerCore86res\vc_compilerCore86res.msi', True), | |
| 159 (r'vc_compilerx64nat\vc_compilerx64nat.msi', False), | |
| 160 (r'vc_compilerx64natres\vc_compilerx64natres.msi', False), | |
| 161 (r'vc_compilerx64x86\vc_compilerx64x86.msi', False), | |
| 162 (r'vc_compilerx64x86res\vc_compilerx64x86res.msi', False), | |
| 163 (r'vc_librarycore86\vc_librarycore86.msi', True), | |
| 164 (r'vc_libraryDesktop\x64\vc_LibraryDesktopX64.msi', True), | |
| 165 (r'vc_libraryDesktop\x86\vc_LibraryDesktopX86.msi', True), | |
| 166 (r'vc_libraryextended\vc_libraryextended.msi', False), | |
| 167 (r'Windows_SDK\Windows Software Development Kit-x86_en-us.msi', True), | |
| 168 ('Windows_SDK\\' | |
| 169 r'Windows Software Development Kit for Metro style Apps-x86_en-us.msi', | |
| 170 True), | |
| 171 ] | |
| 172 extracted_iso = ExtractIso(image) | |
| 173 return ExtractMsiList(extracted_iso, packages) | |
| 174 | |
| 175 | |
| 176 def CopyToFinalLocation(extracted_dirs, target_dir): | |
| 177 sys.stdout.write('Copying to final location...\n') | |
| 178 mappings = { | |
| 179 'Program Files\\Microsoft Visual Studio 12.0\\': '.\\', | |
| 180 'System64\\': 'sys64\\', | |
| 181 'System\\': 'sys32\\', | |
| 182 'Windows Kits\\8.0\\': 'win8sdk\\', | |
| 183 } | |
| 184 matches = [] | |
| 185 for extracted_dir in extracted_dirs: | |
| 186 for root, dirnames, filenames in os.walk(extracted_dir): | |
| 187 for filename in filenames: | |
| 188 matches.append((extracted_dir, os.path.join(root, filename))) | |
| 189 | |
| 190 copies = [] | |
| 191 for prefix, full_path in matches: | |
| 192 # +1 for trailing \. | |
| 193 partial_path = full_path[len(prefix) + 1:] | |
| 194 for map_from, map_to in mappings.iteritems(): | |
| 195 if partial_path.startswith(map_from): | |
| 196 target_path = os.path.join(map_to, partial_path[len(map_from):]) | |
| 197 copies.append((full_path, os.path.join(target_dir, target_path))) | |
| 198 | |
| 199 for full_source, full_target in copies: | |
| 200 target_dir = os.path.dirname(full_target) | |
| 201 if not os.path.isdir(target_dir): | |
| 202 os.makedirs(target_dir) | |
| 203 shutil.copy2(full_source, full_target) | |
| 204 | |
| 205 | |
| 206 def GenerateSetEnvCmd(target_dir, pro): | |
| 207 """Generate a batch file that gyp expects to exist to set up the compiler | |
| 208 environment. | |
| 209 | |
| 210 This is normally generated by a full install of the SDK, but we | |
| 211 do it here manually since we do not do a full install.""" | |
| 212 with open(os.path.join( | |
| 213 target_dir, r'win8sdk\bin\SetEnv.cmd'), 'w') as f: | |
| 214 f.write('@echo off\n' | |
| 215 ':: Generated by win_toolchain\\toolchain2013.py.\n' | |
| 216 # Common to x86 and x64 | |
| 217 'set PATH=%~dp0..\\..\\Common7\\IDE;%PATH%\n' | |
| 218 'set INCLUDE=%~dp0..\\..\\win8sdk\\Include\\um;' | |
| 219 '%~dp0..\\..\\win8sdk\\Include\\shared;' | |
| 220 '%~dp0..\\..\\VC\\include;' | |
| 221 '%~dp0..\\..\\VC\\atlmfc\\include\n' | |
| 222 'if "%1"=="/x64" goto x64\n') | |
| 223 | |
| 224 # x86. If we're Pro, then use the amd64_x86 cross (we don't support x86 | |
| 225 # host at all). | |
| 226 if pro: | |
| 227 f.write('set PATH=%~dp0..\\..\\win8sdk\\bin\\x86;' | |
| 228 '%~dp0..\\..\\VC\\bin\\amd64_x86;' | |
| 229 '%~dp0..\\..\\VC\\bin\\amd64;' # Needed for mspdb120.dll. | |
| 230 '%PATH%\n') | |
| 231 else: | |
| 232 f.write('set PATH=%~dp0..\\..\\win8sdk\\bin\\x86;' | |
| 233 '%~dp0..\\..\\VC\\bin;%PATH%\n') | |
| 234 f.write('set LIB=%~dp0..\\..\\VC\\lib;' | |
| 235 '%~dp0..\\..\\win8sdk\\Lib\\win8\\um\\x86;' | |
| 236 '%~dp0..\\..\\VC\\atlmfc\\lib\n' | |
| 237 'goto :EOF\n') | |
| 238 | |
| 239 # Express does not include a native 64 bit compiler, so we have to use | |
| 240 # the x86->x64 cross. | |
| 241 if not pro: | |
| 242 # x86->x64 cross. | |
| 243 f.write(':x64\n' | |
| 244 'set PATH=%~dp0..\\..\\win8sdk\\bin\\x64;' | |
| 245 '%~dp0..\\..\\VC\\bin\\x86_amd64;' | |
| 246 '%PATH%\n') | |
| 247 else: | |
| 248 # x64 native. | |
| 249 f.write(':x64\n' | |
| 250 'set PATH=%~dp0..\\..\\win8sdk\\bin\\x64;' | |
| 251 '%~dp0..\\..\\VC\\bin\\amd64;' | |
| 252 '%PATH%\n') | |
| 253 f.write('set LIB=%~dp0..\\..\\VC\\lib\\amd64;' | |
| 254 '%~dp0..\\..\\win8sdk\\Lib\\win8\\um\\x64;' | |
| 255 '%~dp0..\\..\\VC\\atlmfc\\lib\\amd64\n') | |
| 256 | |
| 257 | |
| 258 def main(): | |
| 259 parser = optparse.OptionParser(description=sys.modules[__name__].__doc__) | |
| 260 parser.add_option('--targetdir', metavar='DIR', | |
| 261 help='put toolchain into DIR', | |
| 262 default=os.path.join(BASEDIR, 'win_toolchain_2013')) | |
| 263 parser.add_option('--noclean', action='store_false', dest='clean', | |
| 264 help='do not remove temp files', | |
| 265 default=True) | |
| 266 parser.add_option('--local', metavar='DIR', | |
| 267 help='use downloaded files from DIR') | |
| 268 parser.add_option('--express', | |
| 269 help='use VS Express instead of Pro', action='store_true') | |
| 270 options, args = parser.parse_args() | |
| 271 try: | |
| 272 target_dir = os.path.abspath(options.targetdir) | |
| 273 if os.path.exists(target_dir): | |
| 274 parser.error('%s already exists. Please [re]move it or use ' | |
| 275 '--targetdir to select a different target.\n' % | |
| 276 target_dir) | |
| 277 # Set the working directory to 7z subdirectory. 7-zip doesn't find its | |
| 278 # codec dll very well, so this is the simplest way to make sure it runs | |
| 279 # correctly, as we don't otherwise care about working directory. | |
| 280 os.chdir(os.path.join(BASEDIR, '7z')) | |
| 281 image = GetSourceImage(options.local, not options.express) | |
| 282 extracted = ExtractComponents(image) | |
| 283 CopyToFinalLocation(extracted, target_dir) | |
| 284 | |
| 285 GenerateSetEnvCmd(target_dir, not options.express) | |
| 286 finally: | |
| 287 if options.clean: | |
| 288 DeleteAllTempDirs() | |
| 289 | |
| 290 | |
| 291 if __name__ == '__main__': | |
| 292 sys.exit(main()) | |
| OLD | NEW |