siso.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env python3
  2. # Copyright 2023 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """This script is a wrapper around the siso binary that is pulled to
  6. third_party as part of gclient sync. It will automatically find the siso
  7. binary when run inside a gclient source tree, so users can just type
  8. "siso" on the command line."""
  9. import os
  10. import signal
  11. import shutil
  12. import subprocess
  13. import sys
  14. import gclient_paths
  15. def checkOutdir(args):
  16. subcmd = ''
  17. out_dir = "."
  18. for i, arg in enumerate(args):
  19. if not arg.startswith("-") and not subcmd:
  20. subcmd = arg
  21. continue
  22. if arg == "-C":
  23. out_dir = args[i + 1]
  24. elif arg.startswith("-C"):
  25. out_dir = arg[2:]
  26. if subcmd != "ninja":
  27. return
  28. ninja_marker = os.path.join(out_dir, ".ninja_deps")
  29. if os.path.exists(ninja_marker):
  30. print("depot_tools/siso.py: %s contains Ninja state file.\n"
  31. "Use `autoninja` to use reclient,\n"
  32. "or run `gn clean %s` to switch from ninja to siso\n" %
  33. (out_dir, out_dir),
  34. file=sys.stderr)
  35. sys.exit(1)
  36. def _is_google_corp_machine():
  37. """This assumes that corp machine has gcert binary in known location."""
  38. return shutil.which("gcert") is not None
  39. def main(args):
  40. # Do not raise KeyboardInterrupt on SIGINT so as to give siso time to run
  41. # cleanup tasks. Siso will be terminated immediately after the second
  42. # Ctrl-C.
  43. original_sigint_handler = signal.getsignal(signal.SIGINT)
  44. def _ignore(signum, frame):
  45. try:
  46. # Call the original signal handler.
  47. original_sigint_handler(signum, frame)
  48. except KeyboardInterrupt:
  49. # Do not reraise KeyboardInterrupt so as to not kill siso too early.
  50. pass
  51. signal.signal(signal.SIGINT, _ignore)
  52. if not sys.platform.startswith('win'):
  53. signal.signal(signal.SIGTERM, lambda signum, frame: None)
  54. # On Windows the siso.bat script passes along the arguments enclosed in
  55. # double quotes. This prevents multiple levels of parsing of the special '^'
  56. # characters needed when compiling a single file. When this case is
  57. # detected, we need to split the argument. This means that arguments
  58. # containing actual spaces are not supported by siso.bat, but that is not a
  59. # real limitation.
  60. if sys.platform.startswith('win') and len(args) == 2:
  61. args = args[:1] + args[1].split()
  62. # macOS's python sets CPATH, LIBRARY_PATH, SDKROOT implicitly.
  63. # https://openradar.appspot.com/radar?id=5608755232243712
  64. #
  65. # Removing those environment variables to avoid affecting clang's behaviors.
  66. if sys.platform == 'darwin':
  67. os.environ.pop("CPATH", None)
  68. os.environ.pop("LIBRARY_PATH", None)
  69. os.environ.pop("SDKROOT", None)
  70. # if user doesn't set PYTHONPYCACHEPREFIX and PYTHONDONTWRITEBYTECODE
  71. # set PYTHONDONTWRITEBYTECODE=1 not to create many *.pyc in workspace
  72. # and keep workspace clean.
  73. if not os.environ.get("PYTHONPYCACHEPREFIX"):
  74. os.environ.setdefault("PYTHONDONTWRITEBYTECODE", "1")
  75. environ = os.environ.copy()
  76. # Get gclient root + src.
  77. primary_solution_path = gclient_paths.GetPrimarySolutionPath()
  78. gclient_root_path = gclient_paths.FindGclientRoot(os.getcwd())
  79. gclient_src_root_path = None
  80. if gclient_root_path:
  81. gclient_src_root_path = os.path.join(gclient_root_path, 'src')
  82. siso_override_path = os.environ.get('SISO_PATH')
  83. if siso_override_path:
  84. print('depot_tools/siso.py: Using Siso binary from SISO_PATH: %s.' %
  85. siso_override_path,
  86. file=sys.stderr)
  87. if not os.path.isfile(siso_override_path):
  88. print(
  89. 'depot_tools/siso.py: Could not find Siso at provided '
  90. 'SISO_PATH.',
  91. file=sys.stderr)
  92. return 1
  93. for base_path in set(
  94. [primary_solution_path, gclient_root_path, gclient_src_root_path]):
  95. if not base_path:
  96. continue
  97. env = environ.copy()
  98. sisoenv_path = os.path.join(base_path, 'build', 'config', 'siso',
  99. '.sisoenv')
  100. if not os.path.exists(sisoenv_path):
  101. continue
  102. with open(sisoenv_path) as f:
  103. for line in f.readlines():
  104. k, v = line.rstrip().split('=', 1)
  105. env[k] = v
  106. backend_config_dir = os.path.join(base_path, 'build', 'config', 'siso',
  107. 'backend_config')
  108. if os.path.exists(backend_config_dir) and not os.path.exists(
  109. os.path.join(backend_config_dir, 'backend.star')):
  110. if _is_google_corp_machine():
  111. print(
  112. 'build/config/siso/backend_config/backend.star does not '
  113. 'exist.\n'
  114. 'backend.star is configured by gclient hook '
  115. 'build/config/siso/configure_siso.py.\n'
  116. 'Make sure `rbe_instance` gclient custom vars is correct.\n'
  117. 'Did you run `gclient runhooks` ?',
  118. file=sys.stderr)
  119. else:
  120. print(
  121. 'build/config/siso/backend_config/backend.star does not '
  122. 'exist.\n'
  123. 'See build/config/siso/backend_config/README.md',
  124. file=sys.stderr)
  125. return 1
  126. siso_paths = [
  127. siso_override_path,
  128. os.path.join(base_path, 'third_party', 'siso', 'cipd',
  129. 'siso' + gclient_paths.GetExeSuffix()),
  130. os.path.join(base_path, 'third_party', 'siso',
  131. 'siso' + gclient_paths.GetExeSuffix()),
  132. ]
  133. for siso_path in siso_paths:
  134. if siso_path and os.path.isfile(siso_path):
  135. checkOutdir(args[1:])
  136. return subprocess.call([siso_path] + args[1:], env=env)
  137. print(
  138. 'depot_tools/siso.py: Could not find siso in third_party/siso '
  139. 'of the current project. Did you run gclient sync?',
  140. file=sys.stderr)
  141. return 1
  142. if siso_override_path:
  143. return subprocess.call([siso_override_path] + args[1:])
  144. print(
  145. 'depot_tools/siso.py: Could not find .sisoenv under build/config/siso '
  146. 'of the current project. Did you run gclient sync?',
  147. file=sys.stderr)
  148. return 1
  149. if __name__ == '__main__':
  150. sys.exit(main(sys.argv))