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
9 '''which' python2.7
> /dev
/null
&& exec python2.7
"$0" "$@" ||
exec python
"$0" "$@"
12 from __future__ import print_function, unicode_literals
20 (path, child) = os.path.split(path)
24 def load_mach(topsrcdir):
25 sys.path[0:0] = [os.path.join(topsrcdir, "build")]
27 return mach_bootstrap.bootstrap(topsrcdir)
30 def check_and_run_mach(dir_path, args):
31 # If we find the mach bootstrap module, we are in the srcdir.
32 mach_path = os.path.join(dir_path, 'build
/mach_bootstrap.py
')
33 if os.path.isfile(mach_path):
34 mach = load_mach(dir_path)
35 sys.exit(mach.run(args))
39 # Check whether the current directory is within a mach src or obj dir.
40 for dir_path in ancestors(os.getcwd()):
41 # If we find a "mozinfo.json" file, we are in the objdir.
42 mozinfo_path = os.path.join(dir_path, 'mozinfo.json
')
43 if os.path.isfile(mozinfo_path):
45 info = json.load(open(mozinfo_path))
46 if 'mozconfig
' in info and 'MOZCONFIG
' not in os.environ:
47 # If the MOZCONFIG environment variable is not already set, set it
48 # to the value from mozinfo.json. This will tell the build system
49 # to look for a config file at the path in $MOZCONFIG rather than
50 # its default locations.
52 # Note: subprocess requires native strings in os.environ on Windows
53 os.environ[b'MOZCONFIG
'] = str(info['mozconfig
'])
55 if 'topsrcdir
' in info:
56 # Continue searching for mach_bootstrap in the source directory.
57 dir_path = info['topsrcdir
']
59 check_and_run_mach(dir_path, args)
61 # If we didn't
find a
source path by scanning
for a mozinfo.json
, check
62 # whether the directory containing this script is a source directory.
63 check_and_run_mach
(os.path.
dirname(__file__
), args
)
65 print
('Could not run mach: No mach source directory found.')
69 if __name__
== '__main__':
70 if sys.platform
== 'win32':
71 # This is a complete hack to work around the fact that Windows
72 # multiprocessing needs to import the original module (ie: this
73 # file), but only works if it has a .py extension.
75 # We do this by a sort of two-level function interposing. The first
76 # level interposes forking.get_command_line() with our version defined
77 # in my_get_command_line(). Our version of get_command_line will
78 # replace the command string with the contents of the fork_interpose()
79 # function to be used in the subprocess.
81 # The subprocess then gets an interposed imp.find_module(), which we
82 # hack up to find 'mach' without the .py extension, since we already
83 # know where it is (it's us!). If we're not looking for 'mach', then
84 # the original find_module will suffice.
86 # See also: http://bugs.python.org/issue19946
87 # And: https://bugzilla.mozilla.org/show_bug.cgi?id=914563
89 from multiprocessing import forking
90 global orig_command_line
96 orig_find_module
= imp.find_module
97 def my_find_module
(name
, dirs):
99 path
= os.path.
join(dirs[0], 'mach')
101 return (f
, path
, ('', 'r', imp.PY_SOURCE
))
102 return orig_find_module
(name
, dirs)
104 # Don't allow writing bytecode file for mach module.
105 orig_load_module
= imp.load_module
106 def my_load_module
(name
, file, path
, description
):
107 # multiprocess.forking invokes imp.load_module manually and
108 # hard-codes the name __parents_main__ as the module name.
109 if name
== '__parents_main__':
110 old_bytecode
= sys.dont_write_bytecode
111 sys.dont_write_bytecode
= True
113 return orig_load_module
(name
, file, path
, description
)
115 sys.dont_write_bytecode
= old_bytecode
117 return orig_load_module
(name
, file, path
, description
)
119 imp.find_module
= my_find_module
120 imp.load_module
= my_load_module
121 from multiprocessing.forking import main
; main
()
123 def my_get_command_line
():
124 fork_code
, lineno
= inspect.getsourcelines
(fork_interpose
)
125 # Remove the first line (for 'def fork_interpose():') and the three
126 # levels of indentation (12 spaces).
127 fork_string
= ''.
join(x
[12:] for x
in fork_code
[1:])
128 cmdline
= orig_command_line
()
129 cmdline
[2] = fork_string
131 orig_command_line
= forking.get_command_line
132 forking.get_command_line
= my_get_command_line