Bug 1514892 [wpt PR 14571] - Replace XRCoodinateSystem/FrameOfReference with XRSpace...
[gecko.git] / mach
blobb10348d09acdf1a4ed438222644ae04c7b337385
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 shell and valid python,
7 # such that the script starts with the shell and is reexecuted with
8 # the right python.
9 '''which' python2.7 > /dev/null && exec python2.7 "$0" "$@" || exec python "$0" "$@"
10 '''
12 from __future__ import print_function, unicode_literals
14 import os
15 import sys
17 def ancestors(path):
18 while path:
19 yield path
20 (path, child) = os.path.split(path)
21 if child == "":
22 break
24 def load_mach(dir_path, mach_path):
25 import imp
26 with open(mach_path, 'r') as fh:
27 imp.load_module('mach_bootstrap', fh, mach_path,
28 ('.py', 'r', imp.PY_SOURCE))
29 import mach_bootstrap
30 return mach_bootstrap.bootstrap(dir_path)
33 def check_and_get_mach(dir_path):
34 bootstrap_paths = (
35 'build/mach_bootstrap.py',
36 # test package bootstrap
37 'tools/mach_bootstrap.py',
39 for bootstrap_path in bootstrap_paths:
40 mach_path = os.path.join(dir_path, bootstrap_path)
41 if os.path.isfile(mach_path):
42 return load_mach(dir_path, mach_path)
43 return None
46 def get_mach():
47 # Check whether the current directory is within a mach src or obj dir.
48 for dir_path in ancestors(os.getcwd()):
49 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
50 config_status_path = os.path.join(dir_path, 'config.status')
51 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
52 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
53 import json
54 info = json.load(open(mozinfo_path))
55 if 'mozconfig' in info and 'MOZCONFIG' not in os.environ:
56 # If the MOZCONFIG environment variable is not already set, set it
57 # to the value from mozinfo.json. This will tell the build system
58 # to look for a config file at the path in $MOZCONFIG rather than
59 # its default locations.
61 # Note: subprocess requires native strings in os.environ on Windows
62 os.environ[b'MOZCONFIG'] = str(info['mozconfig'])
64 if 'topsrcdir' in info:
65 # Continue searching for mach_bootstrap in the source directory.
66 dir_path = info['topsrcdir']
68 mach = check_and_get_mach(dir_path)
69 if mach:
70 return mach
72 # If we didn't find a source path by scanning for a mozinfo.json, check
73 # whether the directory containing this script is a source directory. We
74 # follow symlinks so mach can be run even if cwd is outside the srcdir.
75 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
77 def main(args):
78 mach = get_mach()
79 if not mach:
80 print('Could not run mach: No mach source directory found.')
81 sys.exit(1)
82 sys.exit(mach.run(args))
85 if __name__ == '__main__':
86 main(sys.argv[1:])