summaryrefslogtreecommitdiff
path: root/build/windows_toolchain.py
blob: 22e656eff3b015c720ab63e4b5c8432004b21cb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python2.7
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# This script is used to create and manipulate archives containing
# files necessary to build Firefox on Windows (referred to as the
# "Windows toolchain").
#
# When updating behavior of this script, remember to update the docs
# in ``build/docs/toolchains.rst``.

from __future__ import absolute_import, unicode_literals

import hashlib
import os
import sys

from mozpack.files import (
    FileFinder,
)
from mozpack.mozjar import (
    JarWriter,
)
import mozpack.path as mozpath


# mozpack.match patterns for files under "Microsoft Visual Studio 14.0".
VS_PATTERNS = [
    {
        'pattern': 'DIA SDK/bin/**',
        'ignore': (
            'DIA SDK/bin/arm/**',
        ),
    },
    {
        'pattern': 'DIA SDK/idl/**',
    },
    {
        'pattern': 'DIA SDK/include/**',
    },
    {
        'pattern': 'DIA SDK/lib/**',
        'ignore': (
            'DIA SDK/lib/arm/**',
        ),
    },
    # ATL is needed by Breakpad.
    {
        'pattern': 'VC/atlmfc/include/**',
    },
    {
        'pattern': 'VC/atlmfc/lib/atls.*',
    },
    {
        'pattern': 'VC/atlmfc/lib/amd64/atls.*',
    },
    {
        'pattern': 'VC/bin/**',
        # We only care about compiling on amd64 for amd64 or x86 targets.
        'ignore': (
            'VC/bin/amd64_arm/**',
            'VC/bin/arm/**',
            'VC/bin/x86_arm/**',
            'VC/bin/x86_amd64/**',
        ),
    },
    {
        'pattern': 'VC/include/**',
    },
    {
        'pattern': 'VC/lib/**',
        'ignore': (
            'VC/lib/arm/**',
            'VC/lib/onecore/**',
            'VC/lib/store/**',
        ),
    },
    {
        'pattern': 'VC/redist/x64/Microsoft.VC140.CRT/**',
    },
    {
        'pattern': 'VC/redist/x86/Microsoft.VC140.CRT/**',
    },
]

SDK_RELEASE = '10.0.14393.0'

# Files from the Windows 10 SDK to install.
SDK_PATTERNS = [
    {
        'pattern': 'bin/x64/**',
    },
    {
        'pattern': 'Include/%s/**' % SDK_RELEASE,
    },
    {
        'pattern': 'Lib/%s/ucrt/x64/**' % SDK_RELEASE,
    },
    {
        'pattern': 'Lib/%s/ucrt/x86/**' % SDK_RELEASE,
    },
    {
        'pattern': 'Lib/%s/um/x64/**' % SDK_RELEASE,
    },
    {
        'pattern': 'Lib/%s/um/x86/**' % SDK_RELEASE,
    },
    {
        'pattern': 'Redist/D3D/**',
    },
    {
        'pattern': 'Redist/ucrt/DLLs/x64/**',
    },
    {
        'pattern': 'Redist/ucrt/DLLs/x86/**',
    },
]


def find_vs_paths():
    """Resolve source locations of files.

    Returns a 2-tuple of (Visual Studio Path, SDK Path).
    """
    pf = os.environ.get('ProgramFiles(x86)')
    if not pf:
        raise Exception('No "ProgramFiles(x86)" environment variable. '
                        'Not running on 64-bit Windows?')

    vs_path = os.path.join(pf, 'Microsoft Visual Studio 14.0')
    if not os.path.exists(vs_path):
        raise Exception('%s does not exist; Visual Studio 2015 not installed?' %
                        vs_path)

    sdk_path = os.path.join(pf, 'Windows Kits', '10')
    if not os.path.exists(sdk_path):
        raise Exception('%s does not exist; Windows 10 SDK not installed?' %
                        sdk_path)

    return vs_path, sdk_path


def resolve_files():
    """Resolve the files that constitute a standalone toolchain.

    This is a generator of (dest path, file) where the destination
    path is relative and the file instance is a BaseFile from mozpack.
    """
    vs_path, sdk_path = find_vs_paths()

    for entry in VS_PATTERNS:
        finder = FileFinder(vs_path, find_executables=False,
                            ignore=entry.get('ignore', []))
        for p, f in finder.find(entry['pattern']):
            assert p.startswith(('VC/', 'DIA SDK/'))

            yield p.encode('utf-8'), f

    for entry in SDK_PATTERNS:
        finder = FileFinder(sdk_path, find_executables=False,
                            ignore=entry.get('ignore', []))
        for p, f in finder.find(entry['pattern']):
            relpath = 'SDK/%s' % p

            yield relpath.encode('utf-8'), f


def resolve_files_and_hash(manifest):
    """Resolve files and hash their data.

    This is a generator of 3-tuples of (relpath, data, mode).

    As data is read, the manifest is populated with metadata.
    Keys are set to the relative file path. Values are 2-tuples
    of (data length, sha-256).
    """
    assert manifest == {}
    for p, f in resolve_files():
        data = f.read()

        sha256 = hashlib.sha256()
        sha256.update(data)
        manifest[p] = (len(data), sha256.hexdigest())

        yield p, data, f.mode


def format_manifest(manifest):
    """Return formatted SHA-256 manifests as a byte strings."""
    sha256_lines = []
    for path, (length, sha256) in sorted(manifest.items()):
        sha256_lines.append(b'%s\t%d\t%s' % (sha256, length, path))

    # Trailing newline.
    sha256_lines.append(b'')

    return b'\n'.join(sha256_lines)


def write_zip(zip_path, prefix=None):
    """Write toolchain data to a zip file."""
    if isinstance(prefix, unicode):
        prefix = prefix.encode('utf-8')

    with JarWriter(file=zip_path, optimize=False, compress=5) as zip:
        manifest = {}
        for p, data, mode in resolve_files_and_hash(manifest):
            print(p)
            if prefix:
                p = mozpath.join(prefix, p)

            zip.add(p, data, mode=mode)

        sha256_manifest = format_manifest(manifest)

        sdk_path = b'SDK_VERSION'
        sha256_path = b'MANIFEST.SHA256'
        if prefix:
            sdk_path = mozpath.join(prefix, sdk_path)
            sha256_path = mozpath.join(prefix, sha256_path)

        zip.add(sdk_path, SDK_RELEASE.encode('utf-8'))
        zip.add(sha256_path, sha256_manifest)


if __name__ == '__main__':
    if len(sys.argv) != 3:
        print('usage: %s create-zip <path-prefix>' % sys.argv[0])
        sys.exit(1)

    assert sys.argv[1] == 'create-zip'
    prefix = os.path.basename(sys.argv[2])
    destzip = '%s.zip' % sys.argv[2]
    write_zip(destzip, prefix=prefix)

    sha1 = hashlib.sha1()
    sha256 = hashlib.sha256()
    sha512 = hashlib.sha512()

    with open(destzip, 'rb') as fh:
        data = fh.read()
        sha1.update(data)
        sha256.update(data)
        sha512.update(data)

    print('Hashes of %s (size=%d)' % (destzip, len(data)))
    print('SHA-1:   %s' % sha1.hexdigest())
    print('SHA-256: %s' % sha256.hexdigest())
    print('SHA-512: %s' % sha512.hexdigest())