mach 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/.
  5. # The beginning of this script is both valid shell and valid python,
  6. # such that the script starts with the shell and is reexecuted with
  7. # the right python.
  8. '''which' python2.7 > /dev/null && exec python2.7 "$0" "$@" || exec python "$0" "$@"
  9. '''
  10. from __future__ import print_function, unicode_literals
  11. import os
  12. import sys
  13. def ancestors(path):
  14. while path:
  15. yield path
  16. (path, child) = os.path.split(path)
  17. if child == "":
  18. break
  19. def load_mach(dir_path, mach_path):
  20. import imp
  21. with open(mach_path, 'r') as fh:
  22. imp.load_module('mach_bootstrap', fh, mach_path,
  23. ('.py', 'r', imp.PY_SOURCE))
  24. import mach_bootstrap
  25. return mach_bootstrap.bootstrap(dir_path)
  26. def check_and_get_mach(dir_path):
  27. bootstrap_paths = (
  28. 'build/mach_bootstrap.py',
  29. # test package bootstrap
  30. 'tools/mach_bootstrap.py',
  31. )
  32. for bootstrap_path in bootstrap_paths:
  33. mach_path = os.path.join(dir_path, bootstrap_path)
  34. if os.path.isfile(mach_path):
  35. return load_mach(dir_path, mach_path)
  36. return None
  37. def get_mach():
  38. # Check whether the current directory is within a mach src or obj dir.
  39. for dir_path in ancestors(os.getcwd()):
  40. # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
  41. config_status_path = os.path.join(dir_path, 'config.status')
  42. mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
  43. if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
  44. import json
  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.
  51. #
  52. # Note: subprocess requires native strings in os.environ on Windows
  53. os.environ[b'MOZCONFIG'] = str(info['mozconfig'])
  54. if 'topsrcdir' in info:
  55. # Continue searching for mach_bootstrap in the source directory.
  56. dir_path = info['topsrcdir']
  57. mach = check_and_get_mach(dir_path)
  58. if mach:
  59. return mach
  60. # If we didn't find a source path by scanning for a mozinfo.json, check
  61. # whether the directory containing this script is a source directory. We
  62. # follow symlinks so mach can be run even if cwd is outside the srcdir.
  63. return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
  64. def main(args):
  65. mach = get_mach()
  66. if not mach:
  67. print('Could not run mach: No mach source directory found.')
  68. sys.exit(1)
  69. sys.exit(mach.run(args))
  70. if __name__ == '__main__':
  71. if sys.platform == 'win32':
  72. # This is a complete hack to work around the fact that Windows
  73. # multiprocessing needs to import the original module (ie: this
  74. # file), but only works if it has a .py extension.
  75. #
  76. # We do this by a sort of two-level function interposing. The first
  77. # level interposes forking.get_command_line() with our version defined
  78. # in my_get_command_line(). Our version of get_command_line will
  79. # replace the command string with the contents of the fork_interpose()
  80. # function to be used in the subprocess.
  81. #
  82. # The subprocess then gets an interposed imp.find_module(), which we
  83. # hack up to find 'mach' without the .py extension, since we already
  84. # know where it is (it's us!). If we're not looking for 'mach', then
  85. # the original find_module will suffice.
  86. #
  87. # See also: http://bugs.python.org/issue19946
  88. # And: https://bugzilla.mozilla.org/show_bug.cgi?id=914563
  89. import inspect
  90. from multiprocessing import forking
  91. global orig_command_line
  92. def fork_interpose():
  93. import imp
  94. import os
  95. import sys
  96. orig_find_module = imp.find_module
  97. def my_find_module(name, dirs):
  98. if name == 'mach':
  99. path = os.path.join(dirs[0], 'mach')
  100. f = open(path)
  101. return (f, path, ('', 'r', imp.PY_SOURCE))
  102. return orig_find_module(name, dirs)
  103. # Don't allow writing bytecode file for mach module.
  104. orig_load_module = imp.load_module
  105. def my_load_module(name, file, path, description):
  106. # multiprocess.forking invokes imp.load_module manually and
  107. # hard-codes the name __parents_main__ as the module name.
  108. if name == '__parents_main__':
  109. old_bytecode = sys.dont_write_bytecode
  110. sys.dont_write_bytecode = True
  111. try:
  112. return orig_load_module(name, file, path, description)
  113. finally:
  114. sys.dont_write_bytecode = old_bytecode
  115. return orig_load_module(name, file, path, description)
  116. imp.find_module = my_find_module
  117. imp.load_module = my_load_module
  118. from multiprocessing.forking import main; main()
  119. def my_get_command_line():
  120. fork_code, lineno = inspect.getsourcelines(fork_interpose)
  121. # Remove the first line (for 'def fork_interpose():') and the three
  122. # levels of indentation (12 spaces).
  123. fork_string = ''.join(x[12:] for x in fork_code[1:])
  124. cmdline = orig_command_line()
  125. cmdline[2] = fork_string
  126. return cmdline
  127. orig_command_line = forking.get_command_line
  128. forking.get_command_line = my_get_command_line
  129. main(sys.argv[1:])