Bug 1680822 [wpt PR 26761] - Fix a crash PaintWorkletImageProvider::GetPaintRecordRes...
[gecko.git] / mach
blob282fba363a49814ca702fec235e93285476d4ae2
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 awsy-test
16 firefox-ui-functional
17 geckodriver
18 geckodriver-test
19 jsshell-bench
20 marionette-test
21 jstests
22 mozharness
23 raptor
24 raptor-test
25 telemetry-tests-client
26 test
27 wpt-metadata-merge
30 # Commands that are to be run with the system Python 3 instead of the
31 # virtualenv.
32 nativecmds="
33 bootstrap
34 create-mach-environment
35 install-moz-phab
38 run_py() {
39 # Try to run a specific Python interpreter.
40 py_executable="$1"
41 shift
42 if command -v "$py_executable" > /dev/null
43 then
44 exec "$py_executable" "$0" "$@"
45 else
46 echo "This mach command requires $py_executable, which wasn't found on the system!"
47 case "$py_executable" in
48 python2.7|python3) ;;
50 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."
52 esac
53 exit 1
57 get_command() {
58 # Parse the name of the mach command out of the arguments. This is necessary
59 # in the presence of global mach arguments that come before the name of the
60 # command, e.g. `mach -v build`. We dispatch to the correct Python
61 # interpreter depending on the command.
62 while true; do
63 case $1 in
64 -v|--verbose) shift;;
65 -l|--log-file)
66 if [ "$#" -lt 2 ]
67 then
68 echo
69 break
70 else
71 shift 2
74 --log-interval) shift;;
75 --log-no-times) shift;;
76 -h) shift;;
77 --debug-command) shift;;
78 --settings)
79 if [ "$#" -lt 2 ]
80 then
81 echo
82 break
83 else
84 shift 2
87 # When running `./mach help <command>`, the correct Python for <command>
88 # needs to be used.
89 help) echo $2; break;;
90 # When running `./mach mach-completion /path/to/mach <command>`, the
91 # correct Python for <command> needs to be used.
92 mach-completion) echo $3; break;;
93 "") echo; break;;
94 *) echo $1; break;;
95 esac
96 done
99 state_dir=${MOZBUILD_STATE_PATH:-~/.mozbuild}
100 command=$(get_command "$@")
102 # If MACH_USE_SYSTEM_PYTHON or MOZ_AUTOMATION are set, always use the
103 # python{2.7,3} executables and not the virtualenv locations.
104 if [ -z ${MACH_USE_SYSTEM_PYTHON} ] && [ -z ${MOZ_AUTOMATION} ]
105 then
106 case "$OSTYPE" in
107 cygwin|msys|win32) bin_path=Scripts;;
108 *) bin_path=bin;;
109 esac
110 py2executable=$state_dir/_virtualenvs/mach_py2/$bin_path/python
111 py3executable=$state_dir/_virtualenvs/mach/$bin_path/python
112 else
113 py2executable=python2.7
114 py3executable=python3
117 # Check whether we need to run with the native Python 3 interpreter.
118 case " $(echo $nativecmds) " in
119 *\ $command\ *)
120 run_py python3 "$@"
122 esac
124 # Check for the mach subcommand in the Python 2 commands list and run it
125 # with the correct interpreter.
126 case " $(echo $py2commands) " in
127 *\ $command\ *)
128 run_py "$py2executable" "$@"
131 if [ -z ${MACH_PY2} ]
132 then
133 run_py "$py3executable" "$@"
134 else
135 if [ $command != "python-test" ]
136 then
137 echo "MACH_PY2 is only valid for mach python-test; please unset MACH_PY2 to continue."
138 exit 1
140 run_py "$py2executable" "$@"
143 esac
145 # Run Python 3 for everything else.
146 run_py "$py3executable" "$@"
149 from __future__ import absolute_import, print_function, unicode_literals
151 import os
152 import sys
154 def ancestors(path):
155 while path:
156 yield path
157 (path, child) = os.path.split(path)
158 if child == "":
159 break
161 def load_mach(dir_path, mach_path):
162 if sys.version_info < (3, 5):
163 import imp
164 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
165 else:
166 import importlib.util
167 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
168 mach_bootstrap = importlib.util.module_from_spec(spec)
169 spec.loader.exec_module(mach_bootstrap)
171 return mach_bootstrap.bootstrap(dir_path)
174 def check_and_get_mach(dir_path):
175 bootstrap_paths = (
176 'build/mach_bootstrap.py',
177 # test package bootstrap
178 'tools/mach_bootstrap.py',
180 for bootstrap_path in bootstrap_paths:
181 mach_path = os.path.join(dir_path, bootstrap_path)
182 if os.path.isfile(mach_path):
183 return load_mach(dir_path, mach_path)
184 return None
187 def setdefaultenv(key, value):
188 """Compatibility shim to ensure the proper string type is used with
189 os.environ for the version of Python being used.
191 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
193 if sys.version_info[0] == 2:
194 if isinstance(key, unicode):
195 key = key.encode(encoding)
196 if isinstance(value, unicode):
197 value = value.encode(encoding)
198 else:
199 if isinstance(key, bytes):
200 key = key.decode(encoding)
201 if isinstance(value, bytes):
202 value = value.decode(encoding)
204 os.environ.setdefault(key, value)
207 def get_mach():
208 # Check whether the current directory is within a mach src or obj dir.
209 for dir_path in ancestors(os.getcwd()):
210 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
211 config_status_path = os.path.join(dir_path, 'config.status')
212 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
213 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
214 import json
215 info = json.load(open(mozinfo_path))
216 if 'mozconfig' in info:
217 # If the MOZCONFIG environment variable is not already set, set it
218 # to the value from mozinfo.json. This will tell the build system
219 # to look for a config file at the path in $MOZCONFIG rather than
220 # its default locations.
221 setdefaultenv('MOZCONFIG', info['mozconfig'])
223 if 'topsrcdir' in info:
224 # Continue searching for mach_bootstrap in the source directory.
225 dir_path = info['topsrcdir']
227 mach = check_and_get_mach(dir_path)
228 if mach:
229 return mach
231 # If we didn't find a source path by scanning for a mozinfo.json, check
232 # whether the directory containing this script is a source directory. We
233 # follow symlinks so mach can be run even if cwd is outside the srcdir.
234 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
236 def main(args):
237 mach = get_mach()
238 if not mach:
239 print('Could not run mach: No mach source directory found.')
240 sys.exit(1)
241 sys.exit(mach.run(args))
244 if __name__ == '__main__':
245 main(sys.argv[1:])