Bug 1587081 [wpt PR 19569] - Rename lone .headers file, a=testonly
[gecko.git] / mach
blobdc7310b9efecd329e0eb06c28800081a85c29dea
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 environment
39 file-info
40 firefox-ui-functional
41 fluent-migration-test
42 geckodriver
43 geckodriver-test
44 geckoview-junit
45 gradle
46 gtest
47 ide
48 import-pr
49 install
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-safety
67 python-test
68 raptor
69 raptor-test
70 reftest
71 release
72 release-history
73 remote
74 repackage
75 resource-usage
76 robocop
77 run
78 rusttests
79 show-log
80 static-analysis
81 talos-test
82 taskcluster-build-image
83 taskcluster-load-image
84 taskgraph
85 telemetry-tests-client
86 test
87 test-info
88 tps-build
89 try
90 uuid
91 valgrind-test
92 vendor
93 visualmetrics
94 warnings-list
95 warnings-summary
96 watch
97 web-platform-tests
98 web-platform-tests-update
99 webidl-example
100 webidl-parser-test
101 webrtc-gtest
103 wpt-manifest-update
104 wpt-metadata-merge
105 wpt-metadata-summary
106 wpt-serve
107 wpt-unittest
108 wpt-update
109 xpcshell-test
112 run_py() {
113 # Try to run a specific Python interpreter. Fall back to the system
114 # default Python if the specific interpreter couldn't be found.
115 py_executable="$1"
116 shift
117 if which "$py_executable" > /dev/null
118 then
119 exec "$py_executable" "$0" "$@"
120 elif [ "$py_executable" = "python2.7" ]; then
121 exec python "$0" "$@"
122 else
123 echo "This mach command requires $py_executable, which wasn't found on the system!"
124 exit 1
128 first_arg=$1
129 if [ "$first_arg" = "help" ]; then
130 # When running `./mach help <command>`, the correct Python for <command>
131 # needs to be used.
132 first_arg=$2
135 if [ -z "$first_arg" ]; then
136 # User ran `./mach` or `./mach help`, use Python 3.
137 run_py python3 "$@"
140 case "${first_arg}" in
141 "-"*)
142 # We have global arguments which are tricky to parse from this shell
143 # script. So invoke `mach` with a special --print-command argument to
144 # return the name of the command. This adds extra overhead when using
145 # global arguments, but global arguments are an edge case and this hack
146 # is only needed temporarily for the Python 3 migration. We use Python
147 # 2.7 because using Python 3 hits this error in build tasks:
148 # https://searchfox.org/mozilla-central/rev/c7e8bc4996f9/build/moz.configure/init.configure#319
149 command=`run_py python2.7 --print-command "$@" | tail -n1`
152 # In the common case, the first argument is the command.
153 command=${first_arg};
155 esac
157 # Check for the mach subcommand in the Python 2 commands list and run it
158 # with the correct interpreter.
159 case " $(echo $py2commands) " in
160 *\ $command\ *)
161 run_py python2.7 "$@"
164 run_py python3 "$@"
166 esac
168 # Run Python 3 for everything else.
169 run_py python3 "$@"
172 from __future__ import absolute_import, print_function, unicode_literals
174 import os
175 import sys
177 def ancestors(path):
178 while path:
179 yield path
180 (path, child) = os.path.split(path)
181 if child == "":
182 break
184 def load_mach(dir_path, mach_path):
185 if sys.version_info < (3, 5):
186 import imp
187 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
188 else:
189 import importlib.util
190 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
191 mach_bootstrap = importlib.util.module_from_spec(spec)
192 spec.loader.exec_module(mach_bootstrap)
194 return mach_bootstrap.bootstrap(dir_path)
197 def check_and_get_mach(dir_path):
198 bootstrap_paths = (
199 'build/mach_bootstrap.py',
200 # test package bootstrap
201 'tools/mach_bootstrap.py',
203 for bootstrap_path in bootstrap_paths:
204 mach_path = os.path.join(dir_path, bootstrap_path)
205 if os.path.isfile(mach_path):
206 return load_mach(dir_path, mach_path)
207 return None
210 def setdefaultenv(key, value):
211 """Compatibility shim to ensure the proper string type is used with
212 os.environ for the version of Python being used.
214 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
216 if sys.version_info[0] == 2:
217 if isinstance(key, unicode):
218 key = key.encode(encoding)
219 if isinstance(value, unicode):
220 value = value.encode(encoding)
221 else:
222 if isinstance(key, bytes):
223 key = key.decode(encoding)
224 if isinstance(value, bytes):
225 value = value.decode(encoding)
227 os.environ.setdefault(key, value)
230 def get_mach():
231 # Check whether the current directory is within a mach src or obj dir.
232 for dir_path in ancestors(os.getcwd()):
233 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
234 config_status_path = os.path.join(dir_path, 'config.status')
235 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
236 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
237 import json
238 info = json.load(open(mozinfo_path))
239 if 'mozconfig' in info:
240 # If the MOZCONFIG environment variable is not already set, set it
241 # to the value from mozinfo.json. This will tell the build system
242 # to look for a config file at the path in $MOZCONFIG rather than
243 # its default locations.
244 setdefaultenv('MOZCONFIG', info['mozconfig'])
246 if 'topsrcdir' in info:
247 # Continue searching for mach_bootstrap in the source directory.
248 dir_path = info['topsrcdir']
250 mach = check_and_get_mach(dir_path)
251 if mach:
252 return mach
254 # If we didn't find a source path by scanning for a mozinfo.json, check
255 # whether the directory containing this script is a source directory. We
256 # follow symlinks so mach can be run even if cwd is outside the srcdir.
257 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
259 def main(args):
260 mach = get_mach()
261 if not mach:
262 print('Could not run mach: No mach source directory found.')
263 sys.exit(1)
264 sys.exit(mach.run(args))
267 if __name__ == '__main__':
268 main(sys.argv[1:])