Bug 1698786: part 2) Change some compile-time dependent `printf`s to `MOZ_LOG` in...
[gecko.git] / mach
blob2671a8e33f9c5f0aa1b15e4f665698b162ca798c
1 #!/bin/sh
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 # The beginning of this script is both valid POSIX shell and valid Python,
7 # such that the script starts with the shell and is reexecuted with
8 # the right Python.
10 # Embeds a shell script inside a Python triple quote. This pattern is valid
11 # shell because `''':'`, `':'` and `:` are all equivalent, and `:` is a no-op.
12 ''':'
13 # Commands that are to be run with Python 2.
14 py2commands="
15 raptor
16 raptor-test
17 telemetry-tests-client
20 # Commands that are to be run with the system Python 3 instead of the
21 # virtualenv.
22 nativecmds="
23 bootstrap
24 create-mach-environment
25 install-moz-phab
28 run_py() {
29 # Try to run a specific Python interpreter.
30 py_executable="$1"
31 shift
32 if command -v "$py_executable" > /dev/null
33 then
34 exec "$py_executable" "$0" "$@"
35 else
36 echo "This mach command requires $py_executable, which wasn't found on the system!"
37 case "$py_executable" in
38 python2.7|python3) ;;
40 echo "Consider running 'mach bootstrap' or 'mach create-mach-environment' to create the mach virtualenvs, or set MACH_USE_SYSTEM_PYTHON to use the system Python installation over a virtualenv."
42 esac
43 exit 1
47 get_command() {
48 # Parse the name of the mach command out of the arguments. This is necessary
49 # in the presence of global mach arguments that come before the name of the
50 # command, e.g. `mach -v build`. We dispatch to the correct Python
51 # interpreter depending on the command.
52 while true; do
53 case $1 in
54 -v|--verbose) shift;;
55 -l|--log-file)
56 if [ "$#" -lt 2 ]
57 then
58 echo
59 break
60 else
61 shift 2
64 --no-interactive) shift;;
65 --log-interval) shift;;
66 --log-no-times) shift;;
67 -h) shift;;
68 --debug-command) shift;;
69 --settings)
70 if [ "$#" -lt 2 ]
71 then
72 echo
73 break
74 else
75 shift 2
78 # When running `./mach help <command>`, the correct Python for <command>
79 # needs to be used.
80 help) echo $2; break;;
81 # When running `./mach mach-completion /path/to/mach <command>`, the
82 # correct Python for <command> needs to be used.
83 mach-completion) echo $3; break;;
84 "") echo; break;;
85 *) echo $1; break;;
86 esac
87 done
90 state_dir=${MOZBUILD_STATE_PATH:-~/.mozbuild}
91 command=$(get_command "$@")
93 # If MACH_USE_SYSTEM_PYTHON or MOZ_AUTOMATION are set, always use the
94 # python{2.7,3} executables and not the virtualenv locations.
95 if [ -z ${MACH_USE_SYSTEM_PYTHON} ] && [ -z ${MOZ_AUTOMATION} ]
96 then
97 case "$OSTYPE" in
98 cygwin|msys|win32) bin_path=Scripts;;
99 *) bin_path=bin;;
100 esac
101 py2executable=$state_dir/_virtualenvs/mach_py2/$bin_path/python
102 py3executable=$state_dir/_virtualenvs/mach/$bin_path/python
103 else
104 py2executable=python2.7
105 py3executable=python3
108 # Check whether we need to run with the native Python 3 interpreter.
109 case " $(echo $nativecmds) " in
110 *\ $command\ *)
111 run_py python3 "$@"
113 esac
115 # Check for the mach subcommand in the Python 2 commands list and run it
116 # with the correct interpreter.
117 case " $(echo $py2commands) " in
118 *\ $command\ *)
119 run_py "$py2executable" "$@"
122 if [ -z ${MACH_PY2} ]
123 then
124 run_py "$py3executable" "$@"
125 else
126 if [ $command != "python-test" ]
127 then
128 echo "MACH_PY2 is only valid for mach python-test; please unset MACH_PY2 to continue."
129 exit 1
131 run_py "$py2executable" "$@"
134 esac
136 # Run Python 3 for everything else.
137 run_py "$py3executable" "$@"
140 from __future__ import absolute_import, print_function, unicode_literals
142 import os
143 import sys
145 def load_mach(dir_path, mach_path):
146 if sys.version_info < (3, 5):
147 import imp
148 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
149 else:
150 import importlib.util
151 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
152 mach_bootstrap = importlib.util.module_from_spec(spec)
153 spec.loader.exec_module(mach_bootstrap)
155 return mach_bootstrap.bootstrap(dir_path)
158 def check_and_get_mach(dir_path):
159 bootstrap_paths = (
160 'build/mach_bootstrap.py',
161 # test package bootstrap
162 'tools/mach_bootstrap.py',
164 for bootstrap_path in bootstrap_paths:
165 mach_path = os.path.join(dir_path, bootstrap_path)
166 if os.path.isfile(mach_path):
167 return load_mach(dir_path, mach_path)
168 return None
171 def main(args):
172 # XCode python sets __PYVENV_LAUNCHER__, which overrides the executable
173 # used when a python subprocess is created. This is an issue when we want
174 # to run using our virtualenv python executables.
175 # In future Python relases, __PYVENV_LAUNCHER__ will be cleared before
176 # application code (mach) is started.
177 # https://github.com/python/cpython/pull/9516
178 os.environ.pop("__PYVENV_LAUNCHER__", None)
180 mach = check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
181 if not mach:
182 print('Could not run mach: No mach source directory found.')
183 sys.exit(1)
184 sys.exit(mach.run(args))
187 if __name__ == '__main__':
188 main(sys.argv[1:])