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