autoninja.py 22 KB

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