Bug 1601859 - Vendor cubeb-coreaudio. r=padenot
[gecko.git] / mach
blob68f8f871e2c61846ea23d94fd4e8bca152d40d4d
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 py2commands="
14 addtest
15 analyze
16 android
17 android-emulator
18 artifact
19 awsy-test
20 bootstrap
21 browsertime
22 build
23 build-backend
24 buildsymbols
25 busted
26 cargo
27 check-spidermonkey
28 clang-format
29 compileflags
30 configure
31 cppunittest
32 cramtest
33 crashtest
34 devtools-css-db
35 doc
36 doctor
37 empty-makefiles
38 file-info
39 firefox-ui-functional
40 fluent-migration-test
41 geckodriver
42 geckodriver-test
43 geckoview-junit
44 gradle
45 gtest
46 ide
47 import-pr
48 install
49 jit-test
50 jsapi-tests
51 jsshell-bench
52 jstestbrowser
53 jstests
54 marionette-test
55 mochitest
56 mozbuild-reference
57 mozharness
58 mozregression
59 package
60 package-multi-locale
61 pastebin
62 power
63 prettier-format
64 puppeteer-test
65 python
66 python-test
67 raptor
68 raptor-test
69 reftest
70 release
71 release-history
72 remote
73 repackage
74 resource-usage
75 robocop
76 run
77 rusttests
78 show-log
79 static-analysis
80 talos-test
81 taskcluster-build-image
82 taskcluster-load-image
83 taskgraph
84 telemetry-tests-client
85 test
86 test-info
87 tps-build
88 try
89 valgrind-test
90 visualmetrics
91 warnings-list
92 warnings-summary
93 watch
94 web-platform-tests
95 web-platform-tests-update
96 webidl-example
97 webidl-parser-test
98 webrtc-gtest
99 wpt
100 wpt-manifest-update
101 wpt-metadata-merge
102 wpt-metadata-summary
103 wpt-serve
104 wpt-test-paths
105 wpt-unittest
106 wpt-update
107 xpcshell-test
110 run_py() {
111 # Try to run a specific Python interpreter. Fall back to the system
112 # default Python if the specific interpreter couldn't be found.
113 py_executable="$1"
114 shift
115 if which "$py_executable" > /dev/null
116 then
117 exec "$py_executable" "$0" "$@"
118 elif [ "$py_executable" = "python2.7" ]; then
119 exec python "$0" "$@"
120 else
121 echo "This mach command requires $py_executable, which wasn't found on the system!"
122 exit 1
126 first_arg=$1
127 if [ "$first_arg" = "help" ]; then
128 # When running `./mach help <command>`, the correct Python for <command>
129 # needs to be used.
130 first_arg=$2
131 elif [ "$first_arg" = "mach-completion" ]; then
132 # When running `./mach mach-completion /path/to/mach <command>`, the
133 # correct Python for <command> needs to be used.
134 first_arg=$3
137 if [ -z "$first_arg" ]; then
138 # User ran `./mach` or `./mach help`, use Python 3.
139 run_py python3 "$@"
142 case "${first_arg}" in
143 "-"*)
144 # We have global arguments which are tricky to parse from this shell
145 # script. So invoke `mach` with a special --print-command argument to
146 # return the name of the command. This adds extra overhead when using
147 # global arguments, but global arguments are an edge case and this hack
148 # is only needed temporarily for the Python 3 migration. We use Python
149 # 2.7 because using Python 3 hits this error in build tasks:
150 # https://searchfox.org/mozilla-central/rev/c7e8bc4996f9/build/moz.configure/init.configure#319
151 command=`run_py python2.7 --print-command "$@" | tail -n1`
154 # In the common case, the first argument is the command.
155 command=${first_arg};
157 esac
159 # Check for the mach subcommand in the Python 2 commands list and run it
160 # with the correct interpreter.
161 case " $(echo $py2commands) " in
162 *\ $command\ *)
163 run_py python2.7 "$@"
166 run_py python3 "$@"
168 esac
170 # Run Python 3 for everything else.
171 run_py python3 "$@"
174 from __future__ import absolute_import, print_function, unicode_literals
176 import os
177 import sys
179 def ancestors(path):
180 while path:
181 yield path
182 (path, child) = os.path.split(path)
183 if child == "":
184 break
186 def load_mach(dir_path, mach_path):
187 if sys.version_info < (3, 5):
188 import imp
189 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
190 else:
191 import importlib.util
192 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
193 mach_bootstrap = importlib.util.module_from_spec(spec)
194 spec.loader.exec_module(mach_bootstrap)
196 return mach_bootstrap.bootstrap(dir_path)
199 def check_and_get_mach(dir_path):
200 bootstrap_paths = (
201 'build/mach_bootstrap.py',
202 # test package bootstrap
203 'tools/mach_bootstrap.py',
205 for bootstrap_path in bootstrap_paths:
206 mach_path = os.path.join(dir_path, bootstrap_path)
207 if os.path.isfile(mach_path):
208 return load_mach(dir_path, mach_path)
209 return None
212 def setdefaultenv(key, value):
213 """Compatibility shim to ensure the proper string type is used with
214 os.environ for the version of Python being used.
216 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
218 if sys.version_info[0] == 2:
219 if isinstance(key, unicode):
220 key = key.encode(encoding)
221 if isinstance(value, unicode):
222 value = value.encode(encoding)
223 else:
224 if isinstance(key, bytes):
225 key = key.decode(encoding)
226 if isinstance(value, bytes):
227 value = value.decode(encoding)
229 os.environ.setdefault(key, value)
232 def get_mach():
233 # Check whether the current directory is within a mach src or obj dir.
234 for dir_path in ancestors(os.getcwd()):
235 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
236 config_status_path = os.path.join(dir_path, 'config.status')
237 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
238 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
239 import json
240 info = json.load(open(mozinfo_path))
241 if 'mozconfig' in info:
242 # If the MOZCONFIG environment variable is not already set, set it
243 # to the value from mozinfo.json. This will tell the build system
244 # to look for a config file at the path in $MOZCONFIG rather than
245 # its default locations.
246 setdefaultenv('MOZCONFIG', info['mozconfig'])
248 if 'topsrcdir' in info:
249 # Continue searching for mach_bootstrap in the source directory.
250 dir_path = info['topsrcdir']
252 mach = check_and_get_mach(dir_path)
253 if mach:
254 return mach
256 # If we didn't find a source path by scanning for a mozinfo.json, check
257 # whether the directory containing this script is a source directory. We
258 # follow symlinks so mach can be run even if cwd is outside the srcdir.
259 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
261 def main(args):
262 mach = get_mach()
263 if not mach:
264 print('Could not run mach: No mach source directory found.')
265 sys.exit(1)
266 sys.exit(mach.run(args))
269 if __name__ == '__main__':
270 main(sys.argv[1:])