autoninja.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2017 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. """
  6. This script (intended to be invoked by autoninja or autoninja.bat) detects
  7. whether a build is accelerated using a service like RBE. If so, it runs with a
  8. large -j value, and otherwise it chooses a small one. This auto-adjustment
  9. makes using remote build acceleration simpler and safer, and avoids errors that
  10. can cause slow RBE builds, or swap-storms on unaccelerated builds.
  11. autoninja tries to detect relevant build settings such as use_remoteexec, and it
  12. does handle import statements, but it can't handle conditional setting of build
  13. settings.
  14. """
  15. import importlib.util
  16. import logging
  17. import multiprocessing
  18. import os
  19. import platform
  20. import re
  21. import shlex
  22. import shutil
  23. import subprocess
  24. import sys
  25. import time
  26. import uuid
  27. import warnings
  28. import build_telemetry
  29. import gclient_paths
  30. import gclient_utils
  31. import gn_helper
  32. import ninja
  33. import ninjalog_uploader
  34. import reclient_helper
  35. import siso
  36. if sys.platform in ["darwin", "linux"]:
  37. import resource
  38. _SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
  39. _NINJALOG_UPLOADER = os.path.join(_SCRIPT_DIR, "ninjalog_uploader.py")
  40. # See [1] and [2] for the painful details of this next section, which handles
  41. # escaping command lines so that they can be copied and pasted into a cmd
  42. # window.
  43. #
  44. # pylint: disable=line-too-long
  45. # [1] https://learn.microsoft.com/en-us/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way # noqa
  46. # [2] https://web.archive.org/web/20150815000000*/https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/set.mspx # noqa
  47. _UNSAFE_FOR_CMD = set("^<>&|()%")
  48. _ALL_META_CHARS = _UNSAFE_FOR_CMD.union(set('"'))
  49. def _import_from_path(module_name, file_path):
  50. try:
  51. spec = importlib.util.spec_from_file_location(module_name, file_path)
  52. module = importlib.util.module_from_spec(spec)
  53. sys.modules[module_name] = module
  54. spec.loader.exec_module(module)
  55. except:
  56. raise ImportError(
  57. 'Could not import module "{}" from "{}"'.format(
  58. module_name, file_path),
  59. name=module_name,
  60. path=file_path,
  61. )
  62. return module
  63. def _is_google_corp_machine():
  64. """This assumes that corp machine has gcert binary in known location."""
  65. return shutil.which("gcert") is not None
  66. def _reclient_rbe_project():
  67. """Returns RBE project used by reclient."""
  68. instance = os.environ.get('RBE_instance')
  69. if instance:
  70. m = re.match(instance, r'projects/([^/]*)/instances/.*')
  71. if m:
  72. return m[1]
  73. reproxy_cfg_path = reclient_helper.find_reclient_cfg()
  74. if not reproxy_cfg_path:
  75. return ""
  76. with open(reproxy_cfg_path) as f:
  77. for line in f:
  78. m = re.match(r'instance\s*=\s*projects/([^/]*)/instances/.*', line)
  79. if m:
  80. return m[1]
  81. return ""
  82. def _siso_rbe_project():
  83. """Returns RBE project used by siso."""
  84. siso_project = os.environ.get('SISO_PROJECT')
  85. if siso_project:
  86. return siso_project
  87. root_dir = gclient_paths.GetPrimarySolutionPath()
  88. if not root_dir:
  89. return ""
  90. sisoenv_path = os.path.join(root_dir, 'build/config/siso/.sisoenv')
  91. if not os.path.exists(sisoenv_path):
  92. return ""
  93. with open(sisoenv_path) as f:
  94. for line in f:
  95. m = re.match(r'SISO_PROJECT=\s*(\S*)\s*', line)
  96. if m:
  97. return m[1]
  98. return ""
  99. def _quote_for_cmd(arg):
  100. # First, escape the arg so that CommandLineToArgvW will parse it properly.
  101. if arg == "" or " " in arg or '"' in arg:
  102. quote_re = re.compile(r'(\\*)"')
  103. arg = '"%s"' % (quote_re.sub(lambda mo: 2 * mo.group(1) + '\\"', arg))
  104. # Then check to see if the arg contains any metacharacters other than
  105. # double quotes; if it does, quote everything (including the double
  106. # quotes) for safety.
  107. if any(a in _UNSAFE_FOR_CMD for a in arg):
  108. arg = "".join("^" + a if a in _ALL_META_CHARS else a for a in arg)
  109. return arg
  110. def _print_cmd(cmd):
  111. shell_quoter = shlex.quote
  112. if sys.platform.startswith("win"):
  113. shell_quoter = _quote_for_cmd
  114. print(*[shell_quoter(arg) for arg in cmd], file=sys.stderr)
  115. def _get_use_reclient_value(output_dir):
  116. root_dir = gclient_paths.GetPrimarySolutionPath()
  117. if not root_dir:
  118. return None
  119. script_path = os.path.join(root_dir,
  120. "build/toolchain/use_reclient_value.py")
  121. if not os.path.exists(script_path):
  122. return None
  123. script = _import_from_path("use_reclient_value", script_path)
  124. try:
  125. r = script.use_reclient_value(output_dir)
  126. except:
  127. raise RuntimeError(
  128. 'Could not call method "use_reclient_value" in {}"'.format(
  129. script_path))
  130. if not isinstance(r, bool):
  131. raise TypeError(
  132. 'Method "use_reclient_defualt" in "{}" returns invalid result. Expected bool, got "{}" (type "{}")'
  133. .format(script_path, r, type(r)))
  134. return r
  135. def _get_use_siso_default(output_dir):
  136. # TODO(379584977): move this in depot_tools
  137. # once gn rule for action_remote.py, which check use_siso` is removed.
  138. root_dir = gclient_paths.GetPrimarySolutionPath()
  139. if not root_dir:
  140. return None
  141. script_path = os.path.join(root_dir, "build/toolchain/use_siso_default.py")
  142. if not os.path.exists(script_path):
  143. return None
  144. script = _import_from_path("use_siso_default", script_path)
  145. try:
  146. r = script.use_siso_default(output_dir)
  147. except:
  148. raise RuntimeError(
  149. 'Could not call method "use_siso_default" in {}"'.format(
  150. script_path))
  151. if not isinstance(r, bool):
  152. raise TypeError(
  153. 'Method "use_siso_default" in "{}" returns invalid result. Expected bool, got "{}" (type "{}")'
  154. .format(script_path, r, type(r)))
  155. return r
  156. def _main_inner(input_args, build_id, should_collect_logs=False):
  157. # if user doesn't set PYTHONPYCACHEPREFIX and PYTHONDONTWRITEBYTECODE
  158. # set PYTHONDONTWRITEBYTECODE=1 not to create many *.pyc in workspace
  159. # and keep workspace clean.
  160. if not os.environ.get("PYTHONPYCACHEPREFIX"):
  161. os.environ.setdefault("PYTHONDONTWRITEBYTECODE", "1")
  162. # The -t tools are incompatible with -j
  163. t_specified = False
  164. j_specified = False
  165. offline = False
  166. output_dir = "."
  167. summarize_build = os.environ.get("NINJA_SUMMARIZE_BUILD") == "1"
  168. project = None
  169. # Ninja uses getopt_long, which allow to intermix non-option arguments.
  170. # To leave non supported parameters untouched, we do not use getopt.
  171. for index, arg in enumerate(input_args[1:]):
  172. if arg.startswith("-j"):
  173. j_specified = True
  174. if arg.startswith("-t"):
  175. t_specified = True
  176. if arg == "-C":
  177. # + 1 to get the next argument and +1 because we trimmed off
  178. # input_args[0]
  179. output_dir = input_args[index + 2]
  180. elif arg.startswith("-C"):
  181. # Support -Cout/Default
  182. output_dir = arg[2:]
  183. elif arg in ("-o", "--offline"):
  184. offline = True
  185. elif arg in ("--project", "-project"):
  186. project = input_args[index + 2]
  187. elif arg.startswith("--project="):
  188. project = arg[len("--project="):]
  189. elif arg.startswith("-project="):
  190. project = arg[len("-project="):]
  191. elif arg in ("-h", "--help"):
  192. print(
  193. "autoninja: Use -o/--offline to temporary disable remote execution.",
  194. file=sys.stderr,
  195. )
  196. print(file=sys.stderr)
  197. use_remoteexec = False
  198. use_reclient = _get_use_reclient_value(output_dir)
  199. use_siso = _get_use_siso_default(output_dir)
  200. # Attempt to auto-detect remote build acceleration. We support gn-based
  201. # builds, where we look for args.gn in the build tree, and cmake-based
  202. # builds where we look for rules.ninja.
  203. if gn_helper.exists(output_dir):
  204. for k, v in gn_helper.args(output_dir):
  205. # use_remoteexec will activate build acceleration.
  206. #
  207. # This test can match multi-argument lines. Examples of this
  208. # are: is_debug=false use_remoteexec=true is_official_build=false
  209. # use_remoteexec=false# use_remoteexec=true This comment is ignored
  210. #
  211. # Anything after a comment is not consider a valid argument.
  212. if k == "use_remoteexec" and v == "true":
  213. use_remoteexec = True
  214. continue
  215. if k == "use_remoteexec" and v == "false":
  216. use_remoteexec = False
  217. continue
  218. if k == "use_siso" and v == "true":
  219. use_siso = True
  220. continue
  221. if k == "use_siso" and v == "false":
  222. use_siso = False
  223. continue
  224. if k == "use_reclient" and v == "true":
  225. use_reclient = True
  226. continue
  227. if k == "use_reclient" and v == "false":
  228. use_reclient = False
  229. continue
  230. if use_reclient is None:
  231. use_reclient = use_remoteexec
  232. if use_remoteexec:
  233. if use_reclient:
  234. project = _reclient_rbe_project()
  235. elif use_siso and project is None:
  236. # siso runs locally if empty project is given
  237. # even if use_remoteexec=true is set.
  238. project = _siso_rbe_project()
  239. if _is_google_corp_machine():
  240. # user may login on non-@google.com account on corp,
  241. # but need to use @google.com and rbe-chrome-untrusted
  242. # on corp machine.
  243. if project == 'rbe-chromium-untrusted':
  244. print(
  245. "You can't use rbe-chromium-untrusted on corp "
  246. "machine.\n"
  247. "Please use rbe-chrome-untrusted and @google.com "
  248. "account instead to build chromium.\n",
  249. file=sys.stderr,
  250. )
  251. return 1
  252. else:
  253. # only @google.com is allowed to use rbe-chrome-untrusted
  254. # and use @google.com on non-corp machine is not allowed
  255. # by corp security policy.
  256. if project == 'rbe-chrome-untrusted':
  257. print(
  258. "You can't use rbe-chrome-untrusted on non-corp "
  259. "machine.\n"
  260. "Plase use rbe-chromium-untrusted and non-@google.com "
  261. "account instead to build chromium.",
  262. file=sys.stderr,
  263. )
  264. return 1
  265. if gclient_utils.IsEnvCog():
  266. if not use_remoteexec or use_reclient or not use_siso:
  267. print(
  268. "WARNING: You're not using Siso's built-in remote "
  269. "execution. The build will be slow.\n"
  270. "You should set the following in args.gn to get better "
  271. "performance:\n"
  272. " use_remoteexec=true\n"
  273. " use_reclient=false\n"
  274. " use_siso=true\n",
  275. file=sys.stderr,
  276. )
  277. siso_marker = os.path.join(output_dir, ".siso_deps")
  278. if use_siso:
  279. # siso generates a .ninja_log file so the mere existence of a
  280. # .ninja_log file doesn't imply that a ninja build was done. However
  281. # if there is a .ninja_log but no .siso_deps then that implies a
  282. # ninja build.
  283. ninja_marker = os.path.join(output_dir, ".ninja_log")
  284. if os.path.exists(ninja_marker) and not os.path.exists(siso_marker):
  285. print(
  286. "Run gn clean before switching from ninja to siso in %s" %
  287. output_dir,
  288. file=sys.stderr,
  289. )
  290. return 1
  291. # Build ID consistently used in other tools. e.g. Reclient, ninjalog.
  292. os.environ.setdefault("SISO_BUILD_ID", build_id)
  293. if use_remoteexec:
  294. if use_reclient and not t_specified:
  295. return reclient_helper.run_siso(
  296. [
  297. 'siso',
  298. 'ninja',
  299. # Do not authenticate when using Reproxy.
  300. '-project=',
  301. '-reapi_instance=',
  302. ] + input_args[1:],
  303. should_collect_logs)
  304. return siso.main(["siso", "ninja"] + input_args[1:])
  305. return siso.main(["siso", "ninja", "--offline"] + input_args[1:])
  306. if os.path.exists(siso_marker):
  307. print(
  308. "Run gn clean before switching from siso to ninja in %s" %
  309. output_dir,
  310. file=sys.stderr,
  311. )
  312. return 1
  313. # Strip -o/--offline so ninja doesn't see them.
  314. input_args = [arg for arg in input_args if arg not in ("-o", "--offline")]
  315. # A large build (with or without RBE) tends to hog all system resources.
  316. # Depending on the operating system, we might have mechanisms available
  317. # to run at a lower priority, which improves this situation.
  318. if os.environ.get("NINJA_BUILD_IN_BACKGROUND") == "1":
  319. if sys.platform in ["darwin", "linux"]:
  320. # nice-level 10 is usually considered a good default for background
  321. # tasks. The niceness is inherited by child processes, so we can
  322. # just set it here for us and it'll apply to the build tool we
  323. # spawn later.
  324. os.nice(10)
  325. # If --offline is set, then reclient will use the local compiler instead of
  326. # doing a remote compile. This is convenient if you want to briefly disable
  327. # remote compile. It avoids having to rebuild the world when transitioning
  328. # between RBE/non-RBE builds. However, it is not as fast as doing a "normal"
  329. # non-RBE build because an extra process is created for each compile step.
  330. if offline:
  331. # Tell reclient to do local compiles.
  332. os.environ["RBE_remote_disabled"] = "1"
  333. # On macOS and most Linux distributions, the default limit of open file
  334. # descriptors is too low (256 and 1024, respectively).
  335. # This causes a large j value to result in 'Too many open files' errors.
  336. # Check whether the limit can be raised to a large enough value. If yes,
  337. # use `ulimit -n .... &&` as a prefix to increase the limit when running
  338. # ninja.
  339. if sys.platform in ["darwin", "linux"]:
  340. # Increase the number of allowed open file descriptors to the maximum.
  341. fileno_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
  342. if fileno_limit < hard_limit:
  343. try:
  344. resource.setrlimit(resource.RLIMIT_NOFILE,
  345. (hard_limit, hard_limit))
  346. except Exception:
  347. pass
  348. fileno_limit, hard_limit = resource.getrlimit(
  349. resource.RLIMIT_NOFILE)
  350. ninja_args = ['ninja']
  351. num_cores = multiprocessing.cpu_count()
  352. if not j_specified and not t_specified:
  353. if not offline and use_remoteexec:
  354. ninja_args.append("-j")
  355. default_core_multiplier = 80
  356. if platform.machine() in ("x86_64", "AMD64"):
  357. # Assume simultaneous multithreading and therefore half as many
  358. # cores as logical processors.
  359. num_cores //= 2
  360. core_multiplier = int(
  361. os.environ.get("NINJA_CORE_MULTIPLIER",
  362. default_core_multiplier))
  363. j_value = num_cores * core_multiplier
  364. core_limit = int(os.environ.get("NINJA_CORE_LIMIT", j_value))
  365. j_value = min(j_value, core_limit)
  366. # On Windows, a -j higher than 1000 doesn't improve build times.
  367. # On macOS, ninja is limited to at most FD_SETSIZE (1024) open file
  368. # descriptors.
  369. if sys.platform in ["darwin", "win32"]:
  370. j_value = min(j_value, 1000)
  371. # Use a j value that reliably works with the open file descriptors
  372. # limit.
  373. if sys.platform in ["darwin", "linux"]:
  374. j_value = min(j_value, int(fileno_limit * 0.8))
  375. ninja_args.append("%d" % j_value)
  376. else:
  377. j_value = num_cores
  378. # Ninja defaults to |num_cores + 2|
  379. j_value += int(os.environ.get("NINJA_CORE_ADDITION", "2"))
  380. ninja_args.append("-j")
  381. ninja_args.append("%d" % j_value)
  382. if summarize_build:
  383. # Enable statistics collection in Ninja.
  384. ninja_args += ["-d", "stats"]
  385. ninja_args += input_args[1:]
  386. if summarize_build:
  387. # Print the command-line to reassure the user that the right settings
  388. # are being used.
  389. _print_cmd(ninja_args)
  390. if use_reclient and not t_specified:
  391. return reclient_helper.run_ninja(ninja_args, should_collect_logs)
  392. return ninja.main(ninja_args)
  393. def _upload_ninjalog(args, exit_code, build_duration):
  394. warnings.simplefilter("ignore", ResourceWarning)
  395. # Run upload script without wait.
  396. creationflags = 0
  397. if platform.system() == "Windows":
  398. creationflags = subprocess.CREATE_NEW_PROCESS_GROUP
  399. cmd = [
  400. sys.executable,
  401. _NINJALOG_UPLOADER,
  402. "--exit_code",
  403. str(exit_code),
  404. "--build_duration",
  405. str(int(build_duration)),
  406. "--cmdline",
  407. ] + args[1:]
  408. subprocess.Popen(
  409. cmd,
  410. stdout=subprocess.DEVNULL,
  411. stderr=subprocess.DEVNULL,
  412. creationflags=creationflags,
  413. )
  414. def main(args):
  415. start = time.time()
  416. # Generate Build ID randomly.
  417. # This ID is expected to be used consistently in all build tools.
  418. build_id = os.environ.get("AUTONINJA_BUILD_ID")
  419. if not build_id:
  420. build_id = str(uuid.uuid4())
  421. os.environ.setdefault("AUTONINJA_BUILD_ID", build_id)
  422. # Check the log collection opt-in/opt-out status, and display notice if necessary.
  423. should_collect_logs = build_telemetry.enabled()
  424. # On Windows the autoninja.bat script passes along the arguments enclosed in
  425. # double quotes. This prevents multiple levels of parsing of the special '^'
  426. # characters needed when compiling a single file but means that this script
  427. # gets called with a single argument containing all of the actual arguments,
  428. # separated by spaces. When this case is detected we need to do argument
  429. # splitting ourselves. This means that arguments containing actual spaces
  430. # are not supported by autoninja, but that is not a real limitation.
  431. input_args = args
  432. if sys.platform.startswith("win") and len(args) == 2:
  433. input_args = args[:1] + args[1].split()
  434. try:
  435. exit_code = _main_inner(input_args, build_id, should_collect_logs)
  436. except KeyboardInterrupt:
  437. exit_code = 1
  438. finally:
  439. if should_collect_logs:
  440. elapsed = time.time() - start
  441. _upload_ninjalog(input_args, exit_code, elapsed)
  442. return exit_code
  443. if __name__ == "__main__":
  444. sys.exit(main(sys.argv))