Bug 1509907 - Add telemetry to track flexbox highlighter usage. r=miker
[gecko.git] / build / windows_toolchain.py
blob0631ce5780e667f67e5b1b16c131e07fa77f82eb
1 #!/usr/bin/env python2.7
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 # This script is used to create and manipulate archives containing
7 # files necessary to build Firefox on Windows (referred to as the
8 # "Windows toolchain").
10 # When updating behavior of this script, remember to update the docs
11 # in ``build/docs/toolchains.rst``.
13 from __future__ import absolute_import, unicode_literals
15 import hashlib
16 import os
17 import sys
19 from mozpack.files import (
20 FileFinder,
22 from mozpack.mozjar import (
23 JarWriter,
25 import mozpack.path as mozpath
27 SDK_RELEASE = '10.0.17134.0'
29 PATTERNS = [
31 'srcdir': '%(vs_path)s/DIA SDK',
32 'dstdir': 'DIA SDK',
33 'files': [
35 'pattern': 'bin/**',
36 'ignore': (
37 'bin/arm/**',
41 'pattern': 'idl/**',
44 'pattern': 'include/**',
47 'pattern': 'lib/**',
48 'ignore': (
49 'lib/arm/**',
55 'srcdir': '%(vs_path)s/VC/Tools/MSVC/14.15.26726',
56 'dstdir': 'VC',
57 'files': [
58 # ATL is needed by Breakpad.
60 'pattern': 'atlmfc/include/**',
63 'pattern': 'atlmfc/lib/x86/atls.*',
66 'pattern': 'atlmfc/lib/x64/atls.*',
69 'pattern': 'bin/Hostx64/**',
71 # 32-bit PGO-instrumented builds require 32-bit pgort140.dll.
73 'pattern': 'bin/Hostx86/x86/pgort140.dll',
76 'pattern': 'include/**',
79 'pattern': 'lib/**',
80 'ignore': (
81 'lib/onecore/**',
82 'lib/x64/store/**',
83 'lib/x86/store/**',
89 'srcdir': '%(vs_path)s/VC/Redist/MSVC/14.15.26706',
90 'dstdir': 'VC/redist',
91 'files': [
93 'pattern': 'x64/Microsoft.VC141.CRT/**',
96 'pattern': 'x86/Microsoft.VC141.CRT/**',
101 'srcdir': '%(sdk_path)s',
102 'dstdir': 'SDK',
103 'files': [
105 'pattern': 'bin/%s/x64/**' % SDK_RELEASE,
108 'pattern': 'Include/%s/**' % SDK_RELEASE,
111 'pattern': 'Lib/%s/ucrt/x64/**' % SDK_RELEASE,
114 'pattern': 'Lib/%s/ucrt/x86/**' % SDK_RELEASE,
117 'pattern': 'Lib/%s/um/x64/**' % SDK_RELEASE,
120 'pattern': 'Lib/%s/um/x86/**' % SDK_RELEASE,
123 'pattern': 'Redist/D3D/**',
126 'pattern': 'Redist/ucrt/DLLs/x64/**',
129 'pattern': 'Redist/ucrt/DLLs/x86/**',
136 def find_vs_paths():
137 """Resolve source locations of files.
139 Returns a 2-tuple of (Visual Studio Path, SDK Path).
141 pf = os.environ.get('ProgramFiles(x86)')
142 if not pf:
143 raise Exception('No "ProgramFiles(x86)" environment variable. '
144 'Not running on 64-bit Windows?')
146 vs_path = os.path.join(pf, 'Microsoft Visual Studio', '2017', 'Community')
147 if not os.path.exists(vs_path):
148 raise Exception('%s does not exist; Visual Studio 2017 not installed?' %
149 vs_path)
151 sdk_path = os.path.join(pf, 'Windows Kits', '10')
152 if not os.path.exists(sdk_path):
153 raise Exception('%s does not exist; Windows 10 SDK not installed?' %
154 sdk_path)
156 sdk_fullver_path = os.path.join(sdk_path, 'Include', SDK_RELEASE)
157 if not os.path.exists(sdk_fullver_path):
158 raise Exception('%s does not exist; Wrong SDK version installed?' %
159 sdk_fullver_path)
161 return vs_path, sdk_path
164 def resolve_files():
165 """Resolve the files that constitute a standalone toolchain.
167 This is a generator of (dest path, file) where the destination
168 path is relative and the file instance is a BaseFile from mozpack.
170 vs_path, sdk_path = find_vs_paths()
172 for entry in PATTERNS:
173 fullpath = entry['srcdir'] % {
174 'vs_path': vs_path,
175 'sdk_path': sdk_path,
177 for pattern in entry['files']:
178 finder = FileFinder(fullpath, ignore=pattern.get('ignore', []))
179 for p, f in finder.find(pattern['pattern']):
180 dstpath = '%s/%s' % (entry['dstdir'], p)
181 yield dstpath.encode('utf-8'), f
184 def resolve_files_and_hash(manifest):
185 """Resolve files and hash their data.
187 This is a generator of 3-tuples of (relpath, data, mode).
189 As data is read, the manifest is populated with metadata.
190 Keys are set to the relative file path. Values are 2-tuples
191 of (data length, sha-256).
193 assert manifest == {}
194 for p, f in resolve_files():
195 data = f.read()
197 sha256 = hashlib.sha256()
198 sha256.update(data)
199 manifest[p] = (len(data), sha256.hexdigest())
201 yield p, data, f.mode
204 def format_manifest(manifest):
205 """Return formatted SHA-256 manifests as a byte strings."""
206 sha256_lines = []
207 for path, (length, sha256) in sorted(manifest.items()):
208 sha256_lines.append(b'%s\t%d\t%s' % (sha256, length, path))
210 # Trailing newline.
211 sha256_lines.append(b'')
213 return b'\n'.join(sha256_lines)
216 def write_zip(zip_path, prefix=None):
217 """Write toolchain data to a zip file."""
218 if isinstance(prefix, unicode): # noqa Special case for Python 2
219 prefix = prefix.encode('utf-8')
221 with JarWriter(file=zip_path, optimize=False, compress_level=5) as zip:
222 manifest = {}
223 for p, data, mode in resolve_files_and_hash(manifest):
224 print(p)
225 if prefix:
226 p = mozpath.join(prefix, p)
228 zip.add(p, data, mode=mode)
230 sha256_manifest = format_manifest(manifest)
232 sdk_path = b'SDK_VERSION'
233 sha256_path = b'MANIFEST.SHA256'
234 if prefix:
235 sdk_path = mozpath.join(prefix, sdk_path)
236 sha256_path = mozpath.join(prefix, sha256_path)
238 zip.add(sdk_path, SDK_RELEASE.encode('utf-8'))
239 zip.add(sha256_path, sha256_manifest)
242 if __name__ == '__main__':
243 if len(sys.argv) != 3:
244 print('usage: %s create-zip <path-prefix>' % sys.argv[0])
245 sys.exit(1)
247 assert sys.argv[1] == 'create-zip'
248 prefix = os.path.basename(sys.argv[2])
249 destzip = '%s.zip' % sys.argv[2]
250 write_zip(destzip, prefix=prefix)
252 sha1 = hashlib.sha1()
253 sha256 = hashlib.sha256()
254 sha512 = hashlib.sha512()
256 with open(destzip, 'rb') as fh:
257 data = fh.read()
258 sha1.update(data)
259 sha256.update(data)
260 sha512.update(data)
262 print('Hashes of %s (size=%d)' % (destzip, len(data)))
263 print('SHA-1: %s' % sha1.hexdigest())
264 print('SHA-256: %s' % sha256.hexdigest())
265 print('SHA-512: %s' % sha512.hexdigest())