Bug 1500142 - Remove inspector bindings from toolbox. r=yulia,ochameau
[gecko.git] / mach
blob63191c8e78c2f16a18c6cea505587d5f191e3db6
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 compare-locales
30 compileflags
31 configure
32 cppunittest
33 cramtest
34 crashtest
35 devtools-css-db
36 doc
37 doctor
38 empty-makefiles
39 environment
40 eslint
41 file-info
42 firefox-ui-functional
43 fluent-migration-test
44 geckodriver
45 geckodriver-test
46 geckoview-junit
47 gradle
48 gtest
49 ide
50 import-pr
51 install
52 install-android
53 install-desktop
54 jsapi-tests
55 jsshell-bench
56 jstestbrowser
57 jstests
58 lint
59 marionette-test
60 mochitest
61 mozbuild-reference
62 mozharness
63 mozregression
64 package
65 package-multi-locale
66 pastebin
67 power
68 prettier-format
69 puppeteer-test
70 python
71 python-safety
72 python-test
73 raptor
74 raptor-test
75 reftest
76 release
77 release-history
78 remote
79 repackage
80 resource-usage
81 robocop
82 run
83 run-android
84 run-desktop
85 rusttests
86 show-log
87 static-analysis
88 talos-test
89 taskcluster-build-image
90 taskcluster-load-image
91 taskgraph
92 telemetry-tests-client
93 test
94 test-info
95 tps-build
96 try
97 uuid
98 valgrind-test
99 vcs-setup
100 vendor
101 visualmetrics
102 warnings-list
103 warnings-summary
104 watch
105 web-platform-tests
106 web-platform-tests-update
107 webidl-example
108 webidl-parser-test
109 webrtc-gtest
111 wpt-manifest-update
112 wpt-metadata-merge
113 wpt-metadata-summary
114 wpt-serve
115 wpt-unittest
116 wpt-update
117 xpcshell-test
120 run_py() {
121 # Try to run a specific Python interpreter. Fall back to the system
122 # default Python if the specific interpreter couldn't be found.
123 py_executable="$1"
124 shift
125 if which "$py_executable" > /dev/null
126 then
127 exec "$py_executable" "$0" "$@"
128 elif [ "$py_executable" = "python2.7" ]; then
129 exec python "$0" "$@"
130 else
131 echo "This mach command requires $py_executable, which wasn't found on the system!"
132 exit 1
136 first_arg=$1
137 if [ "$first_arg" = "help" ]; then
138 # When running `./mach help <command>`, the correct Python for <command>
139 # needs to be used.
140 first_arg=$2
143 if [ -z "$first_arg" ]; then
144 # User ran `./mach` or `./mach help`, use Python 3.
145 run_py python3 "$@"
148 case "${first_arg}" in
149 "-"*)
150 # We have global arguments which are tricky to parse from this shell
151 # script. So invoke `mach` with a special --print-command argument to
152 # return the name of the command. This adds extra overhead when using
153 # global arguments, but global arguments are an edge case and this hack
154 # is only needed temporarily for the Python 3 migration. We use Python
155 # 2.7 because using Python 3 hits this error in build tasks:
156 # https://searchfox.org/mozilla-central/rev/c7e8bc4996f9/build/moz.configure/init.configure#319
157 command=`run_py python2.7 --print-command "$@" | tail -n1`
160 # In the common case, the first argument is the command.
161 command=${first_arg};
163 esac
165 # Check for the mach subcommand in the Python 2 commands list and run it
166 # with the correct interpreter.
167 case " $(echo $py2commands) " in
168 *\ $command\ *)
169 run_py python2.7 "$@"
172 run_py python3 "$@"
174 esac
176 # Run Python 3 for everything else.
177 run_py python3 "$@"
180 from __future__ import absolute_import, print_function, unicode_literals
182 import os
183 import sys
185 def ancestors(path):
186 while path:
187 yield path
188 (path, child) = os.path.split(path)
189 if child == "":
190 break
192 def load_mach(dir_path, mach_path):
193 if sys.version_info < (3, 5):
194 import imp
195 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
196 else:
197 import importlib.util
198 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
199 mach_bootstrap = importlib.util.module_from_spec(spec)
200 spec.loader.exec_module(mach_bootstrap)
202 return mach_bootstrap.bootstrap(dir_path)
205 def check_and_get_mach(dir_path):
206 bootstrap_paths = (
207 'build/mach_bootstrap.py',
208 # test package bootstrap
209 'tools/mach_bootstrap.py',
211 for bootstrap_path in bootstrap_paths:
212 mach_path = os.path.join(dir_path, bootstrap_path)
213 if os.path.isfile(mach_path):
214 return load_mach(dir_path, mach_path)
215 return None
218 def get_mach():
219 # Check whether the current directory is within a mach src or obj dir.
220 for dir_path in ancestors(os.getcwd()):
221 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
222 config_status_path = os.path.join(dir_path, 'config.status')
223 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
224 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
225 import json
226 info = json.load(open(mozinfo_path))
227 if 'mozconfig' in info and 'MOZCONFIG' not in os.environ:
228 # If the MOZCONFIG environment variable is not already set, set it
229 # to the value from mozinfo.json. This will tell the build system
230 # to look for a config file at the path in $MOZCONFIG rather than
231 # its default locations.
233 # Note: subprocess requires native strings in os.environ on Windows
234 os.environ[b'MOZCONFIG'] = str(info['mozconfig'])
236 if 'topsrcdir' in info:
237 # Continue searching for mach_bootstrap in the source directory.
238 dir_path = info['topsrcdir']
240 mach = check_and_get_mach(dir_path)
241 if mach:
242 return mach
244 # If we didn't find a source path by scanning for a mozinfo.json, check
245 # whether the directory containing this script is a source directory. We
246 # follow symlinks so mach can be run even if cwd is outside the srcdir.
247 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
249 def main(args):
250 mach = get_mach()
251 if not mach:
252 print('Could not run mach: No mach source directory found.')
253 sys.exit(1)
254 sys.exit(mach.run(args))
257 if __name__ == '__main__':
258 main(sys.argv[1:])