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/.
9 from textwrap import dedent
12 def load_mach(dir_path, mach_path):
13 # Defer import of "importlib.util" until after Python version check has happened
14 # so that Python 2 usages fail gracefully.
16 spec = importlib.util.spec_from_file_location('mach_initialize', mach_path)
17 mach_initialize = importlib.util.module_from_spec(spec)
18 spec.loader.exec_module(mach_initialize)
19 return mach_initialize.initialize(dir_path)
22 def check_and_get_mach(dir_path):
24 # Run Thunderbird's mach_initialize.py if it exists
25 'comm/build/mach_initialize.py',
26 'build/mach_initialize.py',
27 # test package initialize
28 'tools/mach_initialize.py',
30 for initialize_path in initialize_paths:
31 mach_path = os.path.join(dir_path, initialize_path)
32 if os.path.isfile(mach_path):
33 return load_mach(dir_path, mach_path)
38 # Ensure we are running Python 3.6+. We run this check as soon as
39 # possible to avoid a cryptic import/usage error.
40 if sys.version_info < (3, 6):
41 print("Python 3.6+ is required to run mach.")
42 print("You are running Python {0}".format(platform.python_version()))
43 if sys.platform.startswith("linux"):
45 See https://firefox-source-docs.mozilla.org/setup/linux_build.html#installingpython
46 for guidance on how to install Python on your system.
48 elif sys.platform.startswith("darwin"):
50 See https://firefox-source-docs.mozilla.org/setup/macos_build.html
51 for guidance on how to prepare your system to build Firefox. Perhaps
52 you need to update Xcode, or install Python using brew?
54 elif "MOZILLABUILD" in os.environ and os.environ.get("TERM"):
56 Python is provided by MozillaBuild; ensure your MozillaBuild installation is
57 up to date. See https://firefox-source-docs.mozilla.org/setup/windows_build.html#install-mozillabuild
60 elif sys.platform.startswith("win"):
62 You probably want to be interacting with Mach from within MozillaBuild, see
63 https://firefox-source-docs.mozilla.org/setup/windows_build.html for details.
65 If you are deliberately using Mach from outside MozillaBuild, then see
66 https://firefox-source-docs.mozilla.org/mach/windows-usage-outside-mozillabuild.html#install-python
67 for guidance on installing native Python on your system.
71 We do not have specific instructions for your platform on how to
72 install Python. You may find Pyenv (https://github.com/pyenv/pyenv)
73 helpful, if your system package manager does not provide a way to
74 install a recent enough Python 3.
78 # XCode python sets __PYVENV_LAUNCHER__, which overrides the executable
79 # used when a python subprocess is created. This is an issue when we want
80 # to run using our virtualenv python executables.
81 # In future Python relases, __PYVENV_LAUNCHER__ will be cleared before
82 # application code (mach) is started.
83 # https://github.com/python/cpython/pull/9516
84 os.environ.pop("__PYVENV_LAUNCHER__", None)
86 mach = check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
88 print('Could not run mach: No mach source directory found.')
90 sys.exit(mach.run(args))
93 if __name__ == '__main__':