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