Bug 1794307 - Update HarfBuzz to 5.3.0. r=jfkthame
[gecko.git] / mach
blob16c9589d74b7a79210e412bc762baebb053313c5
1 #!/usr/bin/env python3
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 from __future__ import absolute_import, print_function, unicode_literals
8 import os
9 import platform
10 import sys
11 from textwrap import dedent
14 def load_mach(dir_path, mach_path):
15     # Defer import of "importlib.util" until after Python version check has happened
16     # so that Python 2 usages fail gracefully.
17     import importlib.util
18     spec = importlib.util.spec_from_file_location('mach_initialize', mach_path)
19     mach_initialize = importlib.util.module_from_spec(spec)
20     spec.loader.exec_module(mach_initialize)
21     return mach_initialize.initialize(dir_path)
24 def check_and_get_mach(dir_path):
25     initialize_paths = (
26         # Run Thunderbird's mach_initialize.py if it exists
27         'comm/build/mach_initialize.py',
28         'build/mach_initialize.py',
29         # test package initialize
30         'tools/mach_initialize.py',
31     )
32     for initialize_path in initialize_paths:
33         mach_path = os.path.join(dir_path, initialize_path)
34         if os.path.isfile(mach_path):
35             return load_mach(dir_path, mach_path)
36     return None
39 def main(args):
40     # Ensure we are running Python 3.6+. We run this check as soon as
41     # possible to avoid a cryptic import/usage error.
42     if sys.version_info < (3, 6):
43         print("Python 3.6+ is required to run mach.")
44         print("You are running Python", platform.python_version())
45         if sys.platform.startswith("linux"):
46             print(dedent("""
47             See https://firefox-source-docs.mozilla.org/setup/linux_build.html#installingpython
48             for guidance on how to install Python on your system.
49             """).strip())
50         elif sys.platform.startswith("darwin"):
51             print(dedent("""
52             See https://firefox-source-docs.mozilla.org/setup/macos_build.html
53             for guidance on how to prepare your system to build Firefox. Perhaps
54             you need to update Xcode, or install Python using brew?
55             """).strip())
56         elif "MOZILLABUILD" in os.environ and os.environ.get("TERM"):
57             print(dedent("""
58             Python is provided by MozillaBuild; ensure your MozillaBuild installation is
59             up to date. See https://firefox-source-docs.mozilla.org/setup/windows_build.html#install-mozillabuild
60             for details.
61             """).strip())
62         elif sys.platform.startswith("win"):
63             print(dedent("""
64             You probably want to be interacting with Mach from within MozillaBuild, see
65             https://firefox-source-docs.mozilla.org/setup/windows_build.html for details.
66             
67             If you are deliberately using Mach from outside MozillaBuild, then see
68             https://firefox-source-docs.mozilla.org/mach/windows-usage-outside-mozillabuild.html#install-python
69             for guidance on installing native Python on your system.
70             """).strip())
71         else:
72             print(dedent("""
73             We do not have specific instructions for your platform on how to
74             install Python. You may find Pyenv (https://github.com/pyenv/pyenv)
75             helpful, if your system package manager does not provide a way to
76             install a recent enough Python 3.
77             """).strip())
78         sys.exit(1)
80     if sys.version_info >= (3, 10) and sys.platform.startswith("darwin"):
81         version = f"{sys.version_info[0]}.{sys.version_info[1]}"
82         if f"python@{version}" in sys.executable:
83             print(dedent(f"""
84             You are using python {version} from homebrew.
85             There is a bug in virtualenv that prevents us from starting up mach in that situation.
87             Please uninstall it with the following and try again:
88               brew unlink python@{version}
90             If you need python {version} later, you can re-link it with:
91               brew link python@{version}
92             """).strip())
93             sys.exit(1)
95     # XCode python sets __PYVENV_LAUNCHER__, which overrides the executable
96     # used when a python subprocess is created. This is an issue when we want
97     # to run using our virtualenv python executables.
98     # In future Python relases, __PYVENV_LAUNCHER__ will be cleared before
99     # application code (mach) is started.
100     # https://github.com/python/cpython/pull/9516
101     os.environ.pop("__PYVENV_LAUNCHER__", None)
103     mach = check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
104     if not mach:
105         print('Could not run mach: No mach source directory found.')
106         sys.exit(1)
107     sys.exit(mach.run(args))
110 if __name__ == '__main__':
111     main(sys.argv[1:])