Bug 1870059 - Add a moz.build hook. r=firefox-build-system-reviewers,sergesanspaille

Differential Revision: https://phabricator.services.mozilla.com/D211105
This commit is contained in:
Mike Hommey 2024-05-23 01:13:55 +00:00
parent bb2e05764d
commit 38a03e2836
6 changed files with 70 additions and 10 deletions

View File

@ -48,6 +48,25 @@ def imply_disable_compile_environment(value):
return False
option(
env="MOZ_BUILD_HOOK",
nargs=1,
help="moz.build file that will be executed as if it were appended to"
" every moz.build in the tree.",
)
@depends_if("MOZ_BUILD_HOOK")
@imports("os")
def moz_build_hook(value):
if not os.path.exists(value[0]):
die(f"MOZ_BUILD_HOOK set to {value[0]} but the file doesn't exist")
return os.path.abspath(value[0])
set_config("MOZ_BUILD_HOOK", moz_build_hook)
option(
env="MOZ_COPY_PDBS",
help="For builds that do not support symbols in the normal fashion,"

View File

@ -125,6 +125,10 @@ def is_read_allowed(path, config):
if mozpath.basedir(path, [topsrcdir]):
return True
hook = config.substs.get("MOZ_BUILD_HOOK")
if hook and path == hook:
return True
return False
@ -206,7 +210,7 @@ class MozbuildSandbox(Sandbox):
return
Sandbox.__setitem__(self, key, value)
def exec_file(self, path):
def exec_file(self, path, becomes_current_path=True):
"""Override exec_file to normalize paths and restrict file loading.
Paths will be rejected if they do not fall under topsrcdir or one of
@ -220,7 +224,7 @@ class MozbuildSandbox(Sandbox):
self._context.source_stack, sys.exc_info()[2], illegal_path=path
)
Sandbox.exec_file(self, path)
Sandbox.exec_file(self, path, becomes_current_path)
def _export(self, varname):
"""Export the variable to all subdirectories of the current path."""
@ -1145,6 +1149,9 @@ class BuildReader(object):
context = Context(VARIABLES, config, self.finder)
sandbox = MozbuildSandbox(context, metadata=metadata, finder=self.finder)
sandbox.exec_file(path)
hook = config.substs.get("MOZ_BUILD_HOOK")
if hook:
sandbox.exec_file(hook, becomes_current_path=False)
self._execution_time += time.monotonic() - time_start
self._file_count += len(context.all_paths)

View File

@ -144,7 +144,7 @@ class Sandbox(dict):
def _context(self):
return self._active_contexts[-1]
def exec_file(self, path):
def exec_file(self, path, becomes_current_path=True):
"""Execute code at a path in the sandbox.
The path must be absolute.
@ -158,9 +158,9 @@ class Sandbox(dict):
self._context.source_stack, sys.exc_info()[2], read_error=path
)
self.exec_source(source, path)
self.exec_source(source, path, becomes_current_path)
def exec_source(self, source, path=""):
def exec_source(self, source, path="", becomes_current_path=True):
"""Execute Python code within a string.
The passed string should contain Python code to be executed. The string
@ -185,7 +185,9 @@ class Sandbox(dict):
finally:
self._current_source = old_source
self.exec_function(execute, path=path)
self.exec_function(
execute, path=path, becomes_current_path=becomes_current_path
)
def exec_function(
self, func, args=(), kwargs={}, path="", becomes_current_path=True

View File

@ -0,0 +1,10 @@
DEFINES["ALL"] = True
if RELATIVEDIR == "foo":
DEFINES["FOO_ONLY"] = True
if RELATIVEDIR.startswith("foo"):
DEFINES["FOO"] = True
if RELATIVEDIR == "bar":
DEFINES["BAR_ONLY"] = True

View File

@ -534,6 +534,26 @@ class TestBuildReader(unittest.TestCase):
set(info["win.and.osx"]["SCHEDULES"].exclusive), set(["macosx", "windows"])
)
def test_hook(self):
extra = {
"MOZ_BUILD_HOOK": mozpath.abspath(self.file_path("mozbuild.hook")),
}
config = self.config(
"traversal-simple", extra_substs=extra, error_is_fatal=True
)
reader = BuildReader(config)
contexts = {ctx.relsrcdir: ctx for ctx in reader.read_topsrcdir()}
self.assertEqual(len(contexts), 4)
self.assertEqual(contexts[""]["DEFINES"], {"ALL": True})
self.assertEqual(
contexts["foo"]["DEFINES"], {"ALL": True, "FOO": True, "FOO_ONLY": True}
)
self.assertEqual(contexts["foo/biz"]["DEFINES"], {"ALL": True, "FOO": True})
self.assertEqual(contexts["bar"]["DEFINES"], {"ALL": True, "BAR_ONLY": True})
if __name__ == "__main__":
main()

View File

@ -124,12 +124,14 @@ class TestedSandbox(MozbuildSandbox):
def source_path(self, path):
return SourcePath(self._context, path)
def exec_file(self, path):
super(TestedSandbox, self).exec_file(self.normalize_path(path))
def exec_file(self, path, becomes_current_path=True):
super(TestedSandbox, self).exec_file(
self.normalize_path(path), becomes_current_path
)
def exec_source(self, source, path=""):
def exec_source(self, source, path="", becomes_current_path=True):
super(TestedSandbox, self).exec_source(
source, self.normalize_path(path) if path else ""
source, self.normalize_path(path) if path else "", becomes_current_path
)