Bug 1740824 remove unused support file links r=mjf
[gecko.git] / mach
blob1bcc1c56d71b8fe561b1010d71ca6a059611b1d7
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     try:
81         import distutils
82     except ModuleNotFoundError:
83         print(dedent("""
84         Mach needs the Python "distutils" module, but it's not available on your 
85         machine. This error is most common on Debian Linux (or derivatives), and can be 
86         fixed by installing a package named something like `python3-distutils`.
87         """).strip())
88         sys.exit(1)
90     # XCode python sets __PYVENV_LAUNCHER__, which overrides the executable
91     # used when a python subprocess is created. This is an issue when we want
92     # to run using our virtualenv python executables.
93     # In future Python relases, __PYVENV_LAUNCHER__ will be cleared before
94     # application code (mach) is started.
95     # https://github.com/python/cpython/pull/9516
96     os.environ.pop("__PYVENV_LAUNCHER__", None)
98     mach = check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
99     if not mach:
100         print('Could not run mach: No mach source directory found.')
101         sys.exit(1)
102     sys.exit(mach.run(args))
105 if __name__ == '__main__':
106     main(sys.argv[1:])