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