summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/frontend/reader.py
blob: 9d624b37ec30e746868a7f99bd865851e1d10a79 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
# 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 file contains code for reading metadata from the build system into
# data structures.

r"""Read build frontend files into data structures.

In terms of code architecture, the main interface is BuildReader. BuildReader
starts with a root mozbuild file. It creates a new execution environment for
this file, which is represented by the Sandbox class. The Sandbox class is used
to fill a Context, representing the output of an individual mozbuild file. The

The BuildReader contains basic logic for traversing a tree of mozbuild files.
It does this by examining specific variables populated during execution.
"""

import ast
import inspect
import logging
import os
import sys
import textwrap
import time
import traceback
import types
from collections import OrderedDict, defaultdict
from concurrent.futures.process import ProcessPoolExecutor
from io import StringIO
from itertools import chain
from multiprocessing import cpu_count

import mozpack.path as mozpath
import six
from mozpack.files import FileFinder
from six import string_types

from mozbuild.backend.configenvironment import ConfigEnvironment
from mozbuild.base import ExecutionSummary
from mozbuild.util import (
    EmptyValue,
    HierarchicalStringList,
    ReadOnlyDefaultDict,
    memoize,
)

from .context import (
    DEPRECATION_HINTS,
    FUNCTIONS,
    SPECIAL_VARIABLES,
    SUBCONTEXTS,
    VARIABLES,
    Context,
    ContextDerivedValue,
    Files,
    SourcePath,
    SubContext,
    TemplateContext,
)
from .sandbox import (
    Sandbox,
    SandboxError,
    SandboxExecutionError,
    SandboxLoadError,
    default_finder,
)

if six.PY2:
    type_type = types.TypeType
else:
    type_type = type


def log(logger, level, action, params, formatter):
    logger.log(level, formatter, extra={"action": action, "params": params})


class EmptyConfig(object):
    """A config object that is empty.

    This config object is suitable for using with a BuildReader on a vanilla
    checkout, without any existing configuration. The config is simply
    bootstrapped from a top source directory path.
    """

    class PopulateOnGetDict(ReadOnlyDefaultDict):
        """A variation on ReadOnlyDefaultDict that populates during .get().

        This variation is needed because CONFIG uses .get() to access members.
        Without it, None (instead of our EmptyValue types) would be returned.
        """

        def get(self, key, default=None):
            return self[key]

    default_substs = {
        # These 2 variables are used semi-frequently and it isn't worth
        # changing all the instances.
        "MOZ_APP_NAME": "empty",
        "MOZ_CHILD_PROCESS_NAME": "empty",
        # Needed to prevent js/src's config.status from loading.
        "JS_STANDALONE": "1",
    }

    def __init__(self, topsrcdir, substs=None):
        self.topsrcdir = topsrcdir
        self.topobjdir = ""

        self.substs = self.PopulateOnGetDict(EmptyValue, substs or self.default_substs)
        self.defines = self.substs
        self.error_is_fatal = False


def is_read_allowed(path, config):
    """Whether we are allowed to load a mozbuild file at the specified path.

    This is used as cheap security to ensure the build is isolated to known
    source directories.

    We are allowed to read from the main source directory and any defined
    external source directories. The latter is to allow 3rd party applications
    to hook into our build system.
    """
    assert os.path.isabs(path)
    assert os.path.isabs(config.topsrcdir)

    path = mozpath.normpath(path)
    topsrcdir = mozpath.normpath(config.topsrcdir)

    if mozpath.basedir(path, [topsrcdir]):
        return True

    return False


class SandboxCalledError(SandboxError):
    """Represents an error resulting from calling the error() function."""

    def __init__(self, file_stack, message):
        SandboxError.__init__(self, file_stack)
        self.message = message


class MozbuildSandbox(Sandbox):
    """Implementation of a Sandbox tailored for mozbuild files.

    We expose a few useful functions and expose the set of variables defining
    Mozilla's build system.

    context is a Context instance.

    metadata is a dict of metadata that can be used during the sandbox
    evaluation.
    """

    def __init__(self, context, metadata={}, finder=default_finder):
        assert isinstance(context, Context)

        Sandbox.__init__(self, context, finder=finder)

        self._log = logging.getLogger(__name__)

        self.metadata = dict(metadata)
        exports = self.metadata.get("exports", {})
        self.exports = set(exports.keys())
        context.update(exports)
        self.templates = self.metadata.setdefault("templates", {})
        self.special_variables = self.metadata.setdefault(
            "special_variables", SPECIAL_VARIABLES
        )
        self.functions = self.metadata.setdefault("functions", FUNCTIONS)
        self.subcontext_types = self.metadata.setdefault("subcontexts", SUBCONTEXTS)

    def __getitem__(self, key):
        if key in self.special_variables:
            return self.special_variables[key][0](self._context)
        if key in self.functions:
            return self._create_function(self.functions[key])
        if key in self.subcontext_types:
            return self._create_subcontext(self.subcontext_types[key])
        if key in self.templates:
            return self._create_template_wrapper(self.templates[key])
        return Sandbox.__getitem__(self, key)

    def __contains__(self, key):
        if any(
            key in d
            for d in (
                self.special_variables,
                self.functions,
                self.subcontext_types,
                self.templates,
            )
        ):
            return True

        return Sandbox.__contains__(self, key)

    def __setitem__(self, key, value):
        if key in self.special_variables and value is self[key]:
            return
        if (
            key in self.special_variables
            or key in self.functions
            or key in self.subcontext_types
        ):
            raise KeyError('Cannot set "%s" because it is a reserved keyword' % key)
        if key in self.exports:
            self._context[key] = value
            self.exports.remove(key)
            return
        Sandbox.__setitem__(self, key, value)

    def exec_file(self, path):
        """Override exec_file to normalize paths and restrict file loading.

        Paths will be rejected if they do not fall under topsrcdir or one of
        the external roots.
        """

        # realpath() is needed for true security. But, this isn't for security
        # protection, so it is omitted.
        if not is_read_allowed(path, self._context.config):
            raise SandboxLoadError(
                self._context.source_stack, sys.exc_info()[2], illegal_path=path
            )

        Sandbox.exec_file(self, path)

    def _export(self, varname):
        """Export the variable to all subdirectories of the current path."""

        exports = self.metadata.setdefault("exports", dict())
        if varname in exports:
            raise Exception("Variable has already been exported: %s" % varname)

        try:
            # Doing a regular self._context[varname] causes a set as a side
            # effect. By calling the dict method instead, we don't have any
            # side effects.
            exports[varname] = dict.__getitem__(self._context, varname)
        except KeyError:
            self.last_name_error = KeyError("global_ns", "get_unknown", varname)
            raise self.last_name_error

    def recompute_exports(self):
        """Recompute the variables to export to subdirectories with the current
        values in the subdirectory."""

        if "exports" in self.metadata:
            for key in self.metadata["exports"]:
                self.metadata["exports"][key] = self[key]

    def _include(self, path):
        """Include and exec another file within the context of this one."""

        # path is a SourcePath
        self.exec_file(path.full_path)

    def _warning(self, message):
        # FUTURE consider capturing warnings in a variable instead of printing.
        print("WARNING: %s" % message, file=sys.stderr)

    def _error(self, message):
        if self._context.error_is_fatal:
            raise SandboxCalledError(self._context.source_stack, message)
        else:
            self._warning(message)

    def _template_decorator(self, func):
        """Registers a template function."""

        if not inspect.isfunction(func):
            raise Exception(
                "`template` is a function decorator. You must "
                "use it as `@template` preceding a function declaration."
            )

        name = func.__name__

        if name in self.templates:
            raise KeyError(
                'A template named "%s" was already declared in %s.'
                % (name, self.templates[name].path)
            )

        if name.islower() or name.isupper() or name[0].islower():
            raise NameError("Template function names must be CamelCase.")

        self.templates[name] = TemplateFunction(func, self)

    @memoize
    def _create_subcontext(self, cls):
        """Return a function object that creates SubContext instances."""

        def fn(*args, **kwargs):
            return cls(self._context, *args, **kwargs)

        return fn

    @memoize
    def _create_function(self, function_def):
        """Returns a function object for use within the sandbox for the given
        function definition.

        The wrapper function does type coercion on the function arguments
        """
        func, args_def, doc = function_def

        def function(*args):
            def coerce(arg, type):
                if not isinstance(arg, type):
                    if issubclass(type, ContextDerivedValue):
                        arg = type(self._context, arg)
                    else:
                        arg = type(arg)
                return arg

            args = [coerce(arg, type) for arg, type in zip(args, args_def)]
            return func(self)(*args)

        return function

    @memoize
    def _create_template_wrapper(self, template):
        """Returns a function object for use within the sandbox for the given
        TemplateFunction instance..

        When a moz.build file contains a reference to a template call, the
        sandbox needs a function to execute. This is what this method returns.
        That function creates a new sandbox for execution of the template.
        After the template is executed, the data from its execution is merged
        with the context of the calling sandbox.
        """

        def template_wrapper(*args, **kwargs):
            context = TemplateContext(
                template=template.name,
                allowed_variables=self._context._allowed_variables,
                config=self._context.config,
            )
            context.add_source(self._context.current_path)
            for p in self._context.all_paths:
                context.add_source(p)

            sandbox = MozbuildSandbox(
                context,
                metadata={
                    # We should arguably set these defaults to something else.
                    # Templates, for example, should arguably come from the state
                    # of the sandbox from when the template was declared, not when
                    # it was instantiated. Bug 1137319.
                    "functions": self.metadata.get("functions", {}),
                    "special_variables": self.metadata.get("special_variables", {}),
                    "subcontexts": self.metadata.get("subcontexts", {}),
                    "templates": self.metadata.get("templates", {}),
                },
                finder=self._finder,
            )

            template.exec_in_sandbox(sandbox, *args, **kwargs)

            # This is gross, but allows the merge to happen. Eventually, the
            # merging will go away and template contexts emitted independently.
            klass = self._context.__class__
            self._context.__class__ = TemplateContext
            # The sandbox will do all the necessary checks for these merges.
            for key, value in context.items():
                if isinstance(value, dict):
                    self[key].update(value)
                elif isinstance(value, (list, HierarchicalStringList)):
                    self[key] += value
                else:
                    self[key] = value
            self._context.__class__ = klass

            for p in context.all_paths:
                self._context.add_source(p)

        return template_wrapper


class TemplateFunction(object):
    def __init__(self, func, sandbox):
        self.path = func.__code__.co_filename
        self.name = func.__name__

        code = func.__code__
        firstlineno = code.co_firstlineno
        lines = sandbox._current_source.splitlines(True)
        if lines:
            # Older versions of python 2.7 had a buggy inspect.getblock() that
            # would ignore the last line if it didn't terminate with a newline.
            if not lines[-1].endswith("\n"):
                lines[-1] += "\n"
        lines = inspect.getblock(lines[firstlineno - 1 :])

        # The code lines we get out of inspect.getsourcelines look like
        #   @template
        #   def Template(*args, **kwargs):
        #       VAR = 'value'
        #       ...
        func_ast = ast.parse("".join(lines), self.path)
        # Remove decorators
        func_ast.body[0].decorator_list = []
        # Adjust line numbers accordingly
        ast.increment_lineno(func_ast, firstlineno - 1)

        # When using a custom dictionary for function globals/locals, Cpython
        # actually never calls __getitem__ and __setitem__, so we need to
        # modify the AST so that accesses to globals are properly directed
        # to a dict. AST wants binary_type for this in Py2 and text_type for
        # this in Py3, so cast to str.
        self._global_name = str("_data")
        # In case '_data' is a name used for a variable in the function code,
        # prepend more underscores until we find an unused name.
        while (
            self._global_name in code.co_names or self._global_name in code.co_varnames
        ):
            self._global_name += str("_")
        func_ast = self.RewriteName(sandbox, self._global_name).visit(func_ast)

        # Execute the rewritten code. That code now looks like:
        #   def Template(*args, **kwargs):
        #       _data['VAR'] = 'value'
        #       ...
        # The result of executing this code is the creation of a 'Template'
        # function object in the global namespace.
        glob = {"__builtins__": sandbox._builtins}
        func = types.FunctionType(
            compile(func_ast, self.path, "exec"),
            glob,
            self.name,
            func.__defaults__,
            func.__closure__,
        )
        func()

        self._func = glob[self.name]

    def exec_in_sandbox(self, sandbox, *args, **kwargs):
        """Executes the template function in the given sandbox."""
        # Create a new function object associated with the execution sandbox
        glob = {self._global_name: sandbox, "__builtins__": sandbox._builtins}
        func = types.FunctionType(
            self._func.__code__,
            glob,
            self.name,
            self._func.__defaults__,
            self._func.__closure__,
        )
        sandbox.exec_function(func, args, kwargs, self.path, becomes_current_path=False)

    class RewriteName(ast.NodeTransformer):
        """AST Node Transformer to rewrite variable accesses to go through
        a dict.
        """

        def __init__(self, sandbox, global_name):
            self._sandbox = sandbox
            self._global_name = global_name

        def visit_Str(self, node):
            node.s = six.ensure_text(node.s)
            return node

        def visit_Name(self, node):
            # Modify uppercase variable references and names known to the
            # sandbox as if they were retrieved from a dict instead.
            if not node.id.isupper() and node.id not in self._sandbox:
                return node

            def c(new_node):
                return ast.copy_location(new_node, node)

            return c(
                ast.Subscript(
                    value=c(ast.Name(id=self._global_name, ctx=ast.Load())),
                    slice=c(ast.Index(value=c(ast.Str(s=node.id)))),
                    ctx=node.ctx,
                )
            )


class SandboxValidationError(Exception):
    """Represents an error encountered when validating sandbox results."""

    def __init__(self, message, context):
        Exception.__init__(self, message)
        self.context = context

    def __str__(self):
        s = StringIO()

        delim = "=" * 30
        s.write("\n%s\nFATAL ERROR PROCESSING MOZBUILD FILE\n%s\n\n" % (delim, delim))

        s.write("The error occurred while processing the following file or ")
        s.write("one of the files it includes:\n")
        s.write("\n")
        s.write("    %s/moz.build\n" % self.context.srcdir)
        s.write("\n")

        s.write("The error occurred when validating the result of ")
        s.write("the execution. The reported error is:\n")
        s.write("\n")
        s.write(
            "".join(
                "    %s\n" % l
                for l in super(SandboxValidationError, self).__str__().splitlines()
            )
        )
        s.write("\n")

        return s.getvalue()


class BuildReaderError(Exception):
    """Represents errors encountered during BuildReader execution.

    The main purpose of this class is to facilitate user-actionable error
    messages. Execution errors should say:

      - Why they failed
      - Where they failed
      - What can be done to prevent the error

    A lot of the code in this class should arguably be inside sandbox.py.
    However, extraction is somewhat difficult given the additions
    MozbuildSandbox has over Sandbox (e.g. the concept of included files -
    which affect error messages, of course).
    """

    def __init__(
        self,
        file_stack,
        trace,
        sandbox_exec_error=None,
        sandbox_load_error=None,
        validation_error=None,
        other_error=None,
        sandbox_called_error=None,
    ):

        self.file_stack = file_stack
        self.trace = trace
        self.sandbox_called_error = sandbox_called_error
        self.sandbox_exec = sandbox_exec_error
        self.sandbox_load = sandbox_load_error
        self.validation_error = validation_error
        self.other = other_error

    @property
    def main_file(self):
        return self.file_stack[-1]

    @property
    def actual_file(self):
        # We report the file that called out to the file that couldn't load.
        if self.sandbox_load is not None:
            if len(self.sandbox_load.file_stack) > 1:
                return self.sandbox_load.file_stack[-2]

            if len(self.file_stack) > 1:
                return self.file_stack[-2]

        if self.sandbox_error is not None and len(self.sandbox_error.file_stack):
            return self.sandbox_error.file_stack[-1]

        return self.file_stack[-1]

    @property
    def sandbox_error(self):
        return self.sandbox_exec or self.sandbox_load or self.sandbox_called_error

    def __str__(self):
        s = StringIO()

        delim = "=" * 30
        s.write("\n%s\nFATAL ERROR PROCESSING MOZBUILD FILE\n%s\n\n" % (delim, delim))

        s.write("The error occurred while processing the following file:\n")
        s.write("\n")
        s.write("    %s\n" % self.actual_file)
        s.write("\n")

        if self.actual_file != self.main_file and not self.sandbox_load:
            s.write("This file was included as part of processing:\n")
            s.write("\n")
            s.write("    %s\n" % self.main_file)
            s.write("\n")

        if self.sandbox_error is not None:
            self._print_sandbox_error(s)
        elif self.validation_error is not None:
            s.write("The error occurred when validating the result of ")
            s.write("the execution. The reported error is:\n")
            s.write("\n")
            s.write(
                "".join(
                    "    %s\n" % l
                    for l in six.text_type(self.validation_error).splitlines()
                )
            )
            s.write("\n")
        else:
            s.write("The error appears to be part of the %s " % __name__)
            s.write("Python module itself! It is possible you have stumbled ")
            s.write("across a legitimate bug.\n")
            s.write("\n")

            for l in traceback.format_exception(
                type(self.other), self.other, self.trace
            ):
                s.write(six.ensure_text(l))

        return s.getvalue()

    def _print_sandbox_error(self, s):
        # Try to find the frame of the executed code.
        script_frame = None

        # We don't currently capture the trace for SandboxCalledError.
        # Therefore, we don't get line numbers from the moz.build file.
        # FUTURE capture this.
        trace = getattr(self.sandbox_error, "trace", None)
        frames = []
        if trace:
            frames = traceback.extract_tb(trace)
        for frame in frames:
            if frame[0] == self.actual_file:
                script_frame = frame

            # Reset if we enter a new execution context. This prevents errors
            # in this module from being attributes to a script.
            elif frame[0] == __file__ and frame[2] == "exec_function":
                script_frame = None

        if script_frame is not None:
            s.write("The error was triggered on line %d " % script_frame[1])
            s.write("of this file:\n")
            s.write("\n")
            s.write("    %s\n" % script_frame[3])
            s.write("\n")

        if self.sandbox_called_error is not None:
            self._print_sandbox_called_error(s)
            return

        if self.sandbox_load is not None:
            self._print_sandbox_load_error(s)
            return

        self._print_sandbox_exec_error(s)

    def _print_sandbox_called_error(self, s):
        assert self.sandbox_called_error is not None

        s.write("A moz.build file called the error() function.\n")
        s.write("\n")
        s.write("The error it encountered is:\n")
        s.write("\n")
        s.write("    %s\n" % self.sandbox_called_error.message)
        s.write("\n")
        s.write("Correct the error condition and try again.\n")

    def _print_sandbox_load_error(self, s):
        assert self.sandbox_load is not None

        if self.sandbox_load.illegal_path is not None:
            s.write("The underlying problem is an illegal file access. ")
            s.write("This is likely due to trying to access a file ")
            s.write("outside of the top source directory.\n")
            s.write("\n")
            s.write("The path whose access was denied is:\n")
            s.write("\n")
            s.write("    %s\n" % self.sandbox_load.illegal_path)
            s.write("\n")
            s.write("Modify the script to not access this file and ")
            s.write("try again.\n")
            return

        if self.sandbox_load.read_error is not None:
            if not os.path.exists(self.sandbox_load.read_error):
                s.write("The underlying problem is we referenced a path ")
                s.write("that does not exist. That path is:\n")
                s.write("\n")
                s.write("    %s\n" % self.sandbox_load.read_error)
                s.write("\n")
                s.write("Either create the file if it needs to exist or ")
                s.write("do not reference it.\n")
            else:
                s.write("The underlying problem is a referenced path could ")
                s.write("not be read. The trouble path is:\n")
                s.write("\n")
                s.write("    %s\n" % self.sandbox_load.read_error)
                s.write("\n")
                s.write("It is possible the path is not correct. Is it ")
                s.write("pointing to a directory? It could also be a file ")
                s.write("permissions issue. Ensure that the file is ")
                s.write("readable.\n")

            return

        # This module is buggy if you see this.
        raise AssertionError("SandboxLoadError with unhandled properties!")

    def _print_sandbox_exec_error(self, s):
        assert self.sandbox_exec is not None

        inner = self.sandbox_exec.exc_value

        if isinstance(inner, SyntaxError):
            s.write("The underlying problem is a Python syntax error ")
            s.write("on line %d:\n" % inner.lineno)
            s.write("\n")
            s.write("    %s\n" % inner.text)
            if inner.offset:
                s.write((" " * (inner.offset + 4)) + "^\n")
            s.write("\n")
            s.write("Fix the syntax error and try again.\n")
            return

        if isinstance(inner, KeyError):
            self._print_keyerror(inner, s)
        elif isinstance(inner, ValueError):
            self._print_valueerror(inner, s)
        else:
            self._print_exception(inner, s)

    def _print_keyerror(self, inner, s):
        if not inner.args or inner.args[0] not in ("global_ns", "local_ns"):
            self._print_exception(inner, s)
            return

        if inner.args[0] == "global_ns":
            import difflib

            verb = None
            if inner.args[1] == "get_unknown":
                verb = "read"
            elif inner.args[1] == "set_unknown":
                verb = "write"
            elif inner.args[1] == "reassign":
                s.write("The underlying problem is an attempt to reassign ")
                s.write("a reserved UPPERCASE variable.\n")
                s.write("\n")
                s.write("The reassigned variable causing the error is:\n")
                s.write("\n")
                s.write("    %s\n" % inner.args[2])
                s.write("\n")
                s.write('Maybe you meant "+=" instead of "="?\n')
                return
            else:
                raise AssertionError("Unhandled global_ns: %s" % inner.args[1])

            s.write("The underlying problem is an attempt to %s " % verb)
            s.write("a reserved UPPERCASE variable that does not exist.\n")
            s.write("\n")
            s.write("The variable %s causing the error is:\n" % verb)
            s.write("\n")
            s.write("    %s\n" % inner.args[2])
            s.write("\n")
            close_matches = difflib.get_close_matches(
                inner.args[2], VARIABLES.keys(), 2
            )
            if close_matches:
                s.write("Maybe you meant %s?\n" % " or ".join(close_matches))
                s.write("\n")

            if inner.args[2] in DEPRECATION_HINTS:
                s.write(
                    "%s\n" % textwrap.dedent(DEPRECATION_HINTS[inner.args[2]]).strip()
                )
                return

            s.write("Please change the file to not use this variable.\n")
            s.write("\n")
            s.write("For reference, the set of valid variables is:\n")
            s.write("\n")
            s.write(", ".join(sorted(VARIABLES.keys())) + "\n")
            return

        s.write("The underlying problem is a reference to an undefined ")
        s.write("local variable:\n")
        s.write("\n")
        s.write("    %s\n" % inner.args[2])
        s.write("\n")
        s.write("Please change the file to not reference undefined ")
        s.write("variables and try again.\n")

    def _print_valueerror(self, inner, s):
        if not inner.args or inner.args[0] not in ("global_ns", "local_ns"):
            self._print_exception(inner, s)
            return

        assert inner.args[1] == "set_type"

        s.write("The underlying problem is an attempt to write an illegal ")
        s.write("value to a special variable.\n")
        s.write("\n")
        s.write("The variable whose value was rejected is:\n")
        s.write("\n")
        s.write("    %s" % inner.args[2])
        s.write("\n")
        s.write("The value being written to it was of the following type:\n")
        s.write("\n")
        s.write("    %s\n" % type(inner.args[3]).__name__)
        s.write("\n")
        s.write("This variable expects the following type(s):\n")
        s.write("\n")
        if type(inner.args[4]) == type_type:
            s.write("    %s\n" % inner.args[4].__name__)
        else:
            for t in inner.args[4]:
                s.write("    %s\n" % t.__name__)
        s.write("\n")
        s.write("Change the file to write a value of the appropriate type ")
        s.write("and try again.\n")

    def _print_exception(self, e, s):
        s.write("An error was encountered as part of executing the file ")
        s.write("itself. The error appears to be the fault of the script.\n")
        s.write("\n")
        s.write("The error as reported by Python is:\n")
        s.write("\n")
        s.write("    %s\n" % traceback.format_exception_only(type(e), e))


class BuildReader(object):
    """Read a tree of mozbuild files into data structures.

    This is where the build system starts. You give it a tree configuration
    (the output of configuration) and it executes the moz.build files and
    collects the data they define.

    The reader can optionally call a callable after each sandbox is evaluated
    but before its evaluated content is processed. This gives callers the
    opportunity to modify contexts before side-effects occur from their
    content. This callback receives the ``Context`` containing the result of
    each sandbox evaluation. Its return value is ignored.
    """

    def __init__(self, config, finder=default_finder):
        self.config = config

        self._log = logging.getLogger(__name__)
        self._read_files = set()
        self._execution_stack = []
        self.finder = finder

        # Finder patterns to ignore when searching for moz.build files.
        ignores = {
            # Ignore fake moz.build files used for testing moz.build.
            "python/mozbuild/mozbuild/test",
            "testing/mozbase/moztest/tests/data",
            # Ignore object directories.
            "obj*",
        }

        self._relevant_mozbuild_finder = FileFinder(
            self.config.topsrcdir, ignore=ignores
        )

        # Also ignore any other directories that could be objdirs, they don't
        # necessarily start with the string 'obj'.
        for path, f in self._relevant_mozbuild_finder.find("*/config.status"):
            self._relevant_mozbuild_finder.ignore.add(os.path.dirname(path))

        max_workers = cpu_count()
        if sys.platform.startswith("win"):
            # In python 3, on Windows, ProcessPoolExecutor uses
            # _winapi.WaitForMultipleObjects, which doesn't work on large
            # number of objects. It also has some automatic capping to avoid
            # _winapi.WaitForMultipleObjects being unhappy as a consequence,
            # but that capping is actually insufficient in python 3.7 and 3.8
            # (as well as inexistent in older versions). So we cap ourselves
            # to 60, see https://bugs.python.org/issue26903#msg365886.
            max_workers = min(max_workers, 60)
        self._gyp_worker_pool = ProcessPoolExecutor(max_workers=max_workers)
        self._gyp_processors = []
        self._execution_time = 0.0
        self._file_count = 0
        self._gyp_execution_time = 0.0
        self._gyp_file_count = 0

    def summary(self):
        return ExecutionSummary(
            "Finished reading {file_count:d} moz.build files in "
            "{execution_time:.2f}s",
            file_count=self._file_count,
            execution_time=self._execution_time,
        )

    def gyp_summary(self):
        return ExecutionSummary(
            "Read {file_count:d} gyp files in parallel contributing "
            "{execution_time:.2f}s to total wall time",
            file_count=self._gyp_file_count,
            execution_time=self._gyp_execution_time,
        )

    def read_topsrcdir(self):
        """Read the tree of linked moz.build files.

        This starts with the tree's top-most moz.build file and descends into
        all linked moz.build files until all relevant files have been evaluated.

        This is a generator of Context instances. As each moz.build file is
        read, a new Context is created and emitted.
        """
        path = mozpath.join(self.config.topsrcdir, "moz.build")
        for r in self.read_mozbuild(path, self.config):
            yield r
        all_gyp_paths = set()
        for g in self._gyp_processors:
            for gyp_context in g.results:
                all_gyp_paths |= gyp_context.all_paths
                yield gyp_context
            self._gyp_execution_time += g.execution_time
        self._gyp_file_count += len(all_gyp_paths)
        self._gyp_worker_pool.shutdown()

    def all_mozbuild_paths(self):
        """Iterator over all available moz.build files.

        This method has little to do with the reader. It should arguably belong
        elsewhere.
        """
        # In the future, we may traverse moz.build files by looking
        # for DIRS references in the AST, even if a directory is added behind
        # a conditional. For now, just walk the filesystem.
        for path, f in self._relevant_mozbuild_finder.find("**/moz.build"):
            yield path

    def find_variables_from_ast(self, variables, path=None):
        """Finds all assignments to the specified variables by parsing
        moz.build abstract syntax trees.

        This function only supports two cases, as detailed below.

        1) A dict. Keys and values should both be strings, e.g:

            VARIABLE['foo'] = 'bar'

        This is an `Assign` node with a `Subscript` target. The `Subscript`'s
        value is a `Name` node with id "VARIABLE". The slice of this target is
        an `Index` node and its value is a `Str` with value "foo".

        2) A simple list. Values should be strings, e.g: The target of the
        assignment should be a Name node. Values should be a List node,
        whose elements are Str nodes. e.g:

            VARIABLE += ['foo']

        This is an `AugAssign` node with a `Name` target with id "VARIABLE".
        The value is a `List` node containing one `Str` element whose value is
        "foo".

        With a little work, this function could support other types of
        assignment. But if we end up writing a lot of AST code, it might be
        best to import a high-level AST manipulation library into the tree.

        Args:
            variables (list): A list of variable assignments to capture.
            path (str): A path relative to the source dir. If specified, only
                `moz.build` files relevant to this path will be parsed. Otherwise
                all `moz.build` files are parsed.

        Returns:
            A generator that generates tuples of the form `(<moz.build path>,
            <variable name>, <key>, <value>)`. The `key` will only be
            defined if the variable is an object, otherwise it is `None`.
        """

        if isinstance(variables, string_types):
            variables = [variables]

        def assigned_variable(node):
            # This is not correct, but we don't care yet.
            if hasattr(node, "targets"):
                # Nothing in moz.build does multi-assignment (yet). So error if
                # we see it.
                assert len(node.targets) == 1

                target = node.targets[0]
            else:
                target = node.target

            if isinstance(target, ast.Subscript):
                if not isinstance(target.value, ast.Name):
                    return None, None
                name = target.value.id
            elif isinstance(target, ast.Name):
                name = target.id
            else:
                return None, None

            if name not in variables:
                return None, None

            key = None
            if isinstance(target, ast.Subscript):
                # We need to branch to deal with python version differences.
                if isinstance(target.slice, ast.Constant):
                    # Python >= 3.9
                    assert isinstance(target.slice.value, str)
                    key = target.slice.value
                else:
                    # Others
                    assert isinstance(target.slice, ast.Index)
                    assert isinstance(target.slice.value, ast.Str)
                    key = target.slice.value.s

            return name, key

        def assigned_values(node):
            value = node.value
            if isinstance(value, ast.List):
                for v in value.elts:
                    assert isinstance(v, ast.Str)
                    yield v.s
            else:
                assert isinstance(value, ast.Str)
                yield value.s

        assignments = []

        class Visitor(ast.NodeVisitor):
            def helper(self, node):
                name, key = assigned_variable(node)
                if not name:
                    return

                for v in assigned_values(node):
                    assignments.append((name, key, v))

            def visit_Assign(self, node):
                self.helper(node)

            def visit_AugAssign(self, node):
                self.helper(node)

        if path:
            mozbuild_paths = chain(*self._find_relevant_mozbuilds([path]).values())
        else:
            mozbuild_paths = self.all_mozbuild_paths()

        for p in mozbuild_paths:
            assignments[:] = []
            full = os.path.join(self.config.topsrcdir, p)

            with open(full, "rb") as fh:
                source = fh.read()

            tree = ast.parse(source, full)
            Visitor().visit(tree)

            for name, key, value in assignments:
                yield p, name, key, value

    def read_mozbuild(self, path, config, descend=True, metadata={}):
        """Read and process a mozbuild file, descending into children.

        This starts with a single mozbuild file, executes it, and descends into
        other referenced files per our traversal logic.

        The traversal logic is to iterate over the ``*DIRS`` variables, treating
        each element as a relative directory path. For each encountered
        directory, we will open the moz.build file located in that
        directory in a new Sandbox and process it.

        If descend is True (the default), we will descend into child
        directories and files per variable values.

        Arbitrary metadata in the form of a dict can be passed into this
        function. This feature is intended to facilitate the build reader
        injecting state and annotations into moz.build files that is
        independent of the sandbox's execution context.

        Traversal is performed depth first (for no particular reason).
        """
        self._execution_stack.append(path)
        try:
            for s in self._read_mozbuild(
                path, config, descend=descend, metadata=metadata
            ):
                yield s

        except BuildReaderError as bre:
            raise bre

        except SandboxCalledError as sce:
            raise BuildReaderError(
                list(self._execution_stack), sys.exc_info()[2], sandbox_called_error=sce
            )

        except SandboxExecutionError as se:
            raise BuildReaderError(
                list(self._execution_stack), sys.exc_info()[2], sandbox_exec_error=se
            )

        except SandboxLoadError as sle:
            raise BuildReaderError(
                list(self._execution_stack), sys.exc_info()[2], sandbox_load_error=sle
            )

        except SandboxValidationError as ve:
            raise BuildReaderError(
                list(self._execution_stack), sys.exc_info()[2], validation_error=ve
            )

        except Exception as e:
            raise BuildReaderError(
                list(self._execution_stack), sys.exc_info()[2], other_error=e
            )

    def _read_mozbuild(self, path, config, descend, metadata):
        path = mozpath.normpath(path)
        log(
            self._log,
            logging.DEBUG,
            "read_mozbuild",
            {"path": path},
            "Reading file: {path}".format(path=path),
        )

        if path in self._read_files:
            log(
                self._log,
                logging.WARNING,
                "read_already",
                {"path": path},
                "File already read. Skipping: {path}".format(path=path),
            )
            return

        self._read_files.add(path)

        time_start = time.monotonic()

        topobjdir = config.topobjdir

        relpath = mozpath.relpath(path, config.topsrcdir)
        reldir = mozpath.dirname(relpath)

        if mozpath.dirname(relpath) == "js/src" and not config.substs.get(
            "JS_STANDALONE"
        ):
            config = ConfigEnvironment.from_config_status(
                mozpath.join(topobjdir, reldir, "config.status")
            )
            config.topobjdir = topobjdir

        context = Context(VARIABLES, config, self.finder)
        sandbox = MozbuildSandbox(context, metadata=metadata, finder=self.finder)
        sandbox.exec_file(path)
        self._execution_time += time.monotonic() - time_start
        self._file_count += len(context.all_paths)

        # Yield main context before doing any processing. This gives immediate
        # consumers an opportunity to change state before our remaining
        # processing is performed.
        yield context

        # We need the list of directories pre-gyp processing for later.
        dirs = list(context.get("DIRS", []))

        curdir = mozpath.dirname(path)

        for target_dir in context.get("GYP_DIRS", []):
            gyp_dir = context["GYP_DIRS"][target_dir]
            for v in ("input", "variables"):
                if not getattr(gyp_dir, v):
                    raise SandboxValidationError(
                        "Missing value for " 'GYP_DIRS["%s"].%s' % (target_dir, v),
                        context,
                    )

            # The make backend assumes contexts for sub-directories are
            # emitted after their parent, so accumulate the gyp contexts.
            # We could emit the parent context before processing gyp
            # configuration, but we need to add the gyp objdirs to that context
            # first.
            from .gyp_reader import GypProcessor

            non_unified_sources = set()
            for s in gyp_dir.non_unified_sources:
                source = SourcePath(context, s)
                if not self.finder.get(source.full_path):
                    raise SandboxValidationError("Cannot find %s." % source, context)
                non_unified_sources.add(source)
            action_overrides = {}
            for action, script in six.iteritems(gyp_dir.action_overrides):
                action_overrides[action] = SourcePath(context, script)

            gyp_processor = GypProcessor(
                context.config,
                gyp_dir,
                mozpath.join(curdir, gyp_dir.input),
                mozpath.join(context.objdir, target_dir),
                self._gyp_worker_pool,
                action_overrides,
                non_unified_sources,
            )
            self._gyp_processors.append(gyp_processor)

        for subcontext in sandbox.subcontexts:
            yield subcontext

        # Traverse into referenced files.

        # It's very tempting to use a set here. Unfortunately, the recursive
        # make backend needs order preserved. Once we autogenerate all backend
        # files, we should be able to convert this to a set.
        recurse_info = OrderedDict()
        for d in dirs:
            if d in recurse_info:
                raise SandboxValidationError(
                    "Directory (%s) registered multiple times"
                    % (mozpath.relpath(d.full_path, context.srcdir)),
                    context,
                )

            recurse_info[d] = {}
            for key in sandbox.metadata:
                if key == "exports":
                    sandbox.recompute_exports()

                recurse_info[d][key] = dict(sandbox.metadata[key])

        for path, child_metadata in recurse_info.items():
            child_path = path.join("moz.build").full_path

            # Ensure we don't break out of the topsrcdir. We don't do realpath
            # because it isn't necessary. If there are symlinks in the srcdir,
            # that's not our problem. We're not a hosted application: we don't
            # need to worry about security too much.
            if not is_read_allowed(child_path, context.config):
                raise SandboxValidationError(
                    "Attempting to process file outside of allowed paths: %s"
                    % child_path,
                    context,
                )

            if not descend:
                continue

            for res in self.read_mozbuild(
                child_path, context.config, metadata=child_metadata
            ):
                yield res

        self._execution_stack.pop()

    def _find_relevant_mozbuilds(self, paths):
        """Given a set of filesystem paths, find all relevant moz.build files.

        We assume that a moz.build file in the directory ancestry of a given path
        is relevant to that path. Let's say we have the following files on disk::

           moz.build
           foo/moz.build
           foo/baz/moz.build
           foo/baz/file1
           other/moz.build
           other/file2

        If ``foo/baz/file1`` is passed in, the relevant moz.build files are
        ``moz.build``, ``foo/moz.build``, and ``foo/baz/moz.build``. For
        ``other/file2``, the relevant moz.build files are ``moz.build`` and
        ``other/moz.build``.

        Returns a dict of input paths to a list of relevant moz.build files.
        The root moz.build file is first and the leaf-most moz.build is last.
        """
        root = self.config.topsrcdir
        result = {}

        @memoize
        def exists(path):
            return self._relevant_mozbuild_finder.get(path) is not None

        def itermozbuild(path):
            subpath = ""
            yield "moz.build"
            for part in mozpath.split(path):
                subpath = mozpath.join(subpath, part)
                yield mozpath.join(subpath, "moz.build")

        for path in sorted(paths):
            path = mozpath.normpath(path)
            if os.path.isabs(path):
                if not mozpath.basedir(path, [root]):
                    raise Exception("Path outside topsrcdir: %s" % path)
                path = mozpath.relpath(path, root)

            result[path] = [p for p in itermozbuild(path) if exists(p)]

        return result

    def read_relevant_mozbuilds(self, paths):
        """Read and process moz.build files relevant for a set of paths.

        For an iterable of relative-to-root filesystem paths ``paths``,
        find all moz.build files that may apply to them based on filesystem
        hierarchy and read those moz.build files.

        The return value is a 2-tuple. The first item is a dict mapping each
        input filesystem path to a list of Context instances that are relevant
        to that path. The second item is a list of all Context instances. Each
        Context instance is in both data structures.
        """
        relevants = self._find_relevant_mozbuilds(paths)

        topsrcdir = self.config.topsrcdir

        # Source moz.build file to directories to traverse.
        dirs = defaultdict(set)
        # Relevant path to absolute paths of relevant contexts.
        path_mozbuilds = {}

        # There is room to improve this code (and the code in
        # _find_relevant_mozbuilds) to better handle multiple files in the same
        # directory. Bug 1136966 tracks.
        for path, mbpaths in relevants.items():
            path_mozbuilds[path] = [mozpath.join(topsrcdir, p) for p in mbpaths]

            for i, mbpath in enumerate(mbpaths[0:-1]):
                source_dir = mozpath.dirname(mbpath)
                target_dir = mozpath.dirname(mbpaths[i + 1])

                d = mozpath.normpath(mozpath.join(topsrcdir, mbpath))
                dirs[d].add(mozpath.relpath(target_dir, source_dir))

        # Exporting doesn't work reliably in tree traversal mode. Override
        # the function to no-op.
        functions = dict(FUNCTIONS)

        def export(sandbox):
            return lambda varname: None

        functions["export"] = tuple([export] + list(FUNCTIONS["export"][1:]))

        metadata = {
            "functions": functions,
        }

        contexts = defaultdict(list)
        all_contexts = []
        for context in self.read_mozbuild(
            mozpath.join(topsrcdir, "moz.build"), self.config, metadata=metadata
        ):
            # Explicitly set directory traversal variables to override default
            # traversal rules.
            if not isinstance(context, SubContext):
                for v in ("DIRS", "GYP_DIRS"):
                    context[v][:] = []

                context["DIRS"] = sorted(dirs[context.main_path])

            contexts[context.main_path].append(context)
            all_contexts.append(context)

        result = {}
        for path, paths in path_mozbuilds.items():
            result[path] = six.moves.reduce(
                lambda x, y: x + y, (contexts[p] for p in paths), []
            )

        return result, all_contexts

    def files_info(self, paths):
        """Obtain aggregate data from Files for a set of files.

        Given a set of input paths, determine which moz.build files may
        define metadata for them, evaluate those moz.build files, and
        apply file metadata rules defined within to determine metadata
        values for each file requested.

        Essentially, for each input path:

        1. Determine the set of moz.build files relevant to that file by
           looking for moz.build files in ancestor directories.
        2. Evaluate moz.build files starting with the most distant.
        3. Iterate over Files sub-contexts.
        4. If the file pattern matches the file we're seeking info on,
           apply attribute updates.
        5. Return the most recent value of attributes.
        """
        paths, _ = self.read_relevant_mozbuilds(paths)

        r = {}

        # Only do wildcard matching if the '*' character is present.
        # Otherwise, mozpath.match will match directories, which we've
        # arbitrarily chosen to not allow.
        def path_matches_pattern(relpath, pattern):
            if pattern == relpath:
                return True

            return "*" in pattern and mozpath.match(relpath, pattern)

        for path, ctxs in paths.items():
            # Should be normalized by read_relevant_mozbuilds.
            assert "\\" not in path

            flags = Files(Context())

            for ctx in ctxs:
                if not isinstance(ctx, Files):
                    continue

                # read_relevant_mozbuilds() normalizes paths and ensures that
                # the contexts have paths in the ancestry of the path. When
                # iterating over tens of thousands of paths, mozpath.relpath()
                # can be very expensive. So, given our assumptions about paths,
                # we implement an optimized version.
                ctx_rel_dir = ctx.relsrcdir
                if ctx_rel_dir:
                    assert path.startswith(ctx_rel_dir)
                    relpath = path[len(ctx_rel_dir) + 1 :]
                else:
                    relpath = path

                if any(path_matches_pattern(relpath, p) for p in ctx.patterns):
                    flags += ctx

            r[path] = flags

        return r