Source code

Revision control

Copy as Markdown

Other Tools

# 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
# 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 functools
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
from mozpack.files import FileFinder
from mozbuild.backend.configenvironment import ConfigEnvironment
from mozbuild.base import ExecutionSummary
from mozbuild.util import (
from .context import (
from .sandbox import (
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",
# Needed to prevent js/src's config.status from loading.
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
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())
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 (
return True
return Sandbox.__contains__(self, key)
def __setitem__(self, key, value):
if key in self.special_variables and value is self[key]:
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
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)
# 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
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)
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)
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
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)
arg = type(arg)
return arg
args = [coerce(arg, type) for arg, type in zip(args, args_def)]
return func(self)(*args)
return function
def _create_template_wrapper(self, template):
"""Returns a function object for use within the sandbox for the given
TemplateFunction instance..
When a 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(,
for p in self._context.all_paths:
sandbox = MozbuildSandbox(
# 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", {}),
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):
elif isinstance(value, (list, HierarchicalStringList)):
self[key] += value
self[key] = value
self._context.__class__ = klass
for p in context.all_paths:
return template_wrapper
class TemplateFunction(object):
def __init__(self, func, sandbox):
self.path = func.__code__.co_filename = 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.
self._global_name = "_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 += "_"
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"),
self._func = glob[]
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(
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_Name(self, node):
# Modify uppercase variable references and names known to the
# sandbox as if they were retrieved from a dict instead.
if not and not in self._sandbox:
return node
def c(new_node):
return ast.copy_location(new_node, node)
return c(
value=c(ast.Name(id=self._global_name, ctx=ast.Load())),
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(" %s/\n" % self.context.srcdir)
s.write("The error occurred when validating the result of ")
s.write("the execution. The reported error is:\n")
" %s\n" % l
for l in super(SandboxValidationError, self).__str__().splitlines()
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
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 = 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
def main_file(self):
return self.file_stack[-1]
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]
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(" %s\n" % self.actual_file)
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(" %s\n" % self.main_file)
if self.sandbox_error is not None:
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")
"".join(" %s\n" % l for l in str(self.validation_error).splitlines())
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")
for l in traceback.format_exception(
type(self.other), self.other, self.trace
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 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(" %s\n" % script_frame[3])
if self.sandbox_called_error is not None:
if self.sandbox_load is not None:
def _print_sandbox_called_error(self, s):
assert self.sandbox_called_error is not None
s.write("A file called the error() function.\n")
s.write("The error it encountered is:\n")
s.write(" %s\n" % self.sandbox_called_error.message)
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("The path whose access was denied is:\n")
s.write(" %s\n" % self.sandbox_load.illegal_path)
s.write("Modify the script to not access this file and ")
s.write("try again.\n")
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(" %s\n" % self.sandbox_load.read_error)
s.write("Either create the file if it needs to exist or ")
s.write("do not reference it.\n")
s.write("The underlying problem is a referenced path could ")
s.write("not be read. The trouble path is:\n")
s.write(" %s\n" % self.sandbox_load.read_error)
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 ")
# 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(" %s\n" % inner.text)
if inner.offset:
s.write((" " * (inner.offset + 4)) + "^\n")
s.write("Fix the syntax error and try again.\n")
if isinstance(inner, KeyError):
self._print_keyerror(inner, s)
elif isinstance(inner, ValueError):
self._print_valueerror(inner, s)
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)
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("The reassigned variable causing the error is:\n")
s.write(" %s\n" % inner.args[2])
s.write('Maybe you meant "+=" instead of "="?\n')
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("The variable %s causing the error is:\n" % verb)
s.write(" %s\n" % inner.args[2])
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))
if inner.args[2] in DEPRECATION_HINTS:
"%s\n" % textwrap.dedent(DEPRECATION_HINTS[inner.args[2]]).strip()
s.write("Please change the file to not use this variable.\n")
s.write("For reference, the set of valid variables is:\n")
s.write(", ".join(sorted(VARIABLES.keys())) + "\n")
s.write("The underlying problem is a reference to an undefined ")
s.write("local variable:\n")
s.write(" %s\n" % inner.args[2])
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)
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("The variable whose value was rejected is:\n")
s.write(" %s" % inner.args[2])
s.write("The value being written to it was of the following type:\n")
s.write(" %s\n" % type(inner.args[3]).__name__)
s.write("This variable expects the following type(s):\n")
if type(inner.args[4]) == type:
s.write(" %s\n" % inner.args[4].__name__)
for t in inner.args[4]:
s.write(" %s\n" % t.__name__)
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("The error as reported by Python is:\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 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 = {}
self._execution_stack = []
self.finder = finder
# Finder patterns to ignore when searching for files.
ignores = {
# Ignore fake files used for testing
# Ignore object directories.
# Also ignore any other directories that could be objdirs, but don't
# necessarily start with the string 'obj'.
objdir_finder = FileFinder(self.config.topsrcdir, ignore=ignores)
for path, _file in objdir_finder.find("*/config.status"):
del objdir_finder
self._relevant_mozbuild_finder = FileFinder(
self.config.topsrcdir, ignore=ignores
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
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} files in "
def gyp_summary(self):
return ExecutionSummary(
"Read {file_count:d} gyp files in parallel contributing "
"{execution_time:.2f}s to total wall time",
def read_topsrcdir(self):
"""Read the tree of linked files.
This starts with the tree's top-most file and descends into
all linked files until all relevant files have been evaluated.
This is a generator of Context instances. As each file is
read, a new Context is created and emitted.
path = mozpath.join(self.config.topsrcdir, "")
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)
def all_mozbuild_paths(self):
"""Iterator over all available files.
This method has little to do with the reader. It should arguably belong
# In the future, we may traverse 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("**/"):
yield path
def find_variables_from_ast(self, variables, path=None):
"""Finds all assignments to the specified variables by parsing 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
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.
variables (list): A list of variable assignments to capture.
path (str): A path relative to the source dir. If specified, only
`` files relevant to this path will be parsed. Otherwise
all `` files are parsed.
A generator that generates tuples of the form `(< path>,
<variable name>, <key>, <value>)`. The `key` will only be
defined if the variable is an object, otherwise it is `None`.
if isinstance(variables, str):
variables = [variables]
def assigned_variable(node):
# This is not correct, but we don't care yet.
if hasattr(node, "targets"):
# Nothing in does multi-assignment (yet). So error if
# we see it.
assert len(node.targets) == 1
target = node.targets[0]
target =
if isinstance(target, ast.Subscript):
if not isinstance(target.value, ast.Name):
return None, None
name =
elif isinstance(target, ast.Name):
name =
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
# 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
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:
for v in assigned_values(node):
assignments.append((name, key, v))
def visit_Assign(self, node):
def visit_AugAssign(self, node):
if path:
mozbuild_paths = chain(*self._find_relevant_mozbuilds([path]).values())
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 =
tree = ast.parse(source, full)
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 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 files that is
independent of the sandbox's execution context.
Traversal is performed depth first (for no particular reason).
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)
{"path": path},
"Reading file: {path}".format(path=path),
time_start = time.monotonic()
topobjdir = config.topobjdir
relpath = mozpath.relpath(path, config.topsrcdir)
reldir = mozpath.dirname(relpath)
# NOTE: descend case is handled in the loop below.
if not descend:
if relpath in self._read_files:
{"path": path},
"File already read. Skipping: {path}".format(path=path),
self._read_files[relpath] = (relpath, "")
if mozpath.dirname(relpath) == "js/src" and not config.substs.get(
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)
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),
# 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)
action_overrides = {}
for action, script in gyp_dir.action_overrides.items():
action_overrides[action] = SourcePath(context, script)
gyp_processor = GypProcessor(
mozpath.join(curdir, gyp_dir.input),
mozpath.join(context.objdir, target_dir),
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)),
recurse_info[d] = {}
for key in sandbox.metadata:
if key == "exports":
recurse_info[d][key] = dict(sandbox.metadata[key])
for path, child_metadata in recurse_info.items():
child_path = path.join("").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,
if not descend:
child_relpath = mozpath.relpath(child_path, self.config.topsrcdir)
if child_relpath in self._read_files:
(prev_parent, prev_path) = self._read_files[child_relpath]
raise Exception(
f"File already read. A directory should not be added to DIRS twice: {child_relpath} is referred from {prev_parent} as '{prev_path}', and {relpath} as '{path}'"
self._read_files[child_relpath] = (relpath, path)
for res in self.read_mozbuild(
child_path, context.config, metadata=child_metadata
yield res
def _find_relevant_mozbuilds(self, paths):
"""Given a set of filesystem paths, find all relevant files.
We assume that a file in the directory ancestry of a given path
is relevant to that path. Let's say we have the following files on disk::
If ``foo/baz/file1`` is passed in, the relevant files are
````, ``foo/``, and ``foo/baz/``. For
``other/file2``, the relevant files are ```` and
Returns a dict of input paths to a list of relevant files.
The root file is first and the leaf-most is last.
root = self.config.topsrcdir
result = {}
def exists(path):
return self._relevant_mozbuild_finder.get(path) is not None
def itermozbuild(path):
subpath = ""
yield ""
for part in mozpath.split(path):
subpath = mozpath.join(subpath, part)
yield mozpath.join(subpath, "")
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 files relevant for a set of paths.
For an iterable of relative-to-root filesystem paths ``paths``,
find all files that may apply to them based on filesystem
hierarchy and read those 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 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, ""), 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])
result = {}
for path, paths in path_mozbuilds.items():
result[path] = functools.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 files may
define metadata for them, evaluate those 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 files relevant to that file by
looking for files in ancestor directories.
2. Evaluate 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):
# 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 :]
relpath = path
if any(path_matches_pattern(relpath, p) for p in ctx.patterns):
flags += ctx
r[path] = flags
return r