michael@0: #!/bin/sh michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: # The beginning of this script is both valid shell and valid python, michael@0: # such that the script starts with the shell and is reexecuted with michael@0: # the right python. michael@0: '''which' python2.7 > /dev/null && exec python2.7 "$0" "$@" || exec python "$0" "$@" michael@0: ''' michael@0: michael@0: from __future__ import print_function, unicode_literals michael@0: michael@0: import os michael@0: import sys michael@0: michael@0: def ancestors(path): michael@0: while path: michael@0: yield path michael@0: (path, child) = os.path.split(path) michael@0: if child == "": michael@0: break michael@0: michael@0: def load_mach(topsrcdir): michael@0: sys.path[0:0] = [os.path.join(topsrcdir, "build")] michael@0: import mach_bootstrap michael@0: return mach_bootstrap.bootstrap(topsrcdir) michael@0: michael@0: michael@0: def check_and_run_mach(dir_path, args): michael@0: # If we find the mach bootstrap module, we are in the srcdir. michael@0: mach_path = os.path.join(dir_path, 'build/mach_bootstrap.py') michael@0: if os.path.isfile(mach_path): michael@0: mach = load_mach(dir_path) michael@0: sys.exit(mach.run(args)) michael@0: michael@0: michael@0: def main(args): michael@0: # Check whether the current directory is within a mach src or obj dir. michael@0: for dir_path in ancestors(os.getcwd()): michael@0: # If we find a "mozinfo.json" file, we are in the objdir. michael@0: mozinfo_path = os.path.join(dir_path, 'mozinfo.json') michael@0: if os.path.isfile(mozinfo_path): michael@0: import json michael@0: info = json.load(open(mozinfo_path)) michael@0: if 'mozconfig' in info and 'MOZCONFIG' not in os.environ: michael@0: # If the MOZCONFIG environment variable is not already set, set it michael@0: # to the value from mozinfo.json. This will tell the build system michael@0: # to look for a config file at the path in $MOZCONFIG rather than michael@0: # its default locations. michael@0: # michael@0: # Note: subprocess requires native strings in os.environ on Windows michael@0: os.environ[b'MOZCONFIG'] = str(info['mozconfig']) michael@0: michael@0: if 'topsrcdir' in info: michael@0: # Continue searching for mach_bootstrap in the source directory. michael@0: dir_path = info['topsrcdir'] michael@0: michael@0: check_and_run_mach(dir_path, args) michael@0: michael@0: # If we didn't find a source path by scanning for a mozinfo.json, check michael@0: # whether the directory containing this script is a source directory. michael@0: check_and_run_mach(os.path.dirname(__file__), args) michael@0: michael@0: print('Could not run mach: No mach source directory found.') michael@0: sys.exit(1) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: main(sys.argv[1:])