Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 1 | # Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """ |
| 6 | This script (intended to be invoked by autoninja or autoninja.bat) detects |
| 7 | whether a build is using goma. If so it runs with a large -j value, and |
| 8 | otherwise it chooses a small one. This auto-adjustment makes using goma simpler |
| 9 | and safer, and avoids errors that can cause slow goma builds or swap-storms |
| 10 | on non-goma builds. |
| 11 | """ |
| 12 | |
| 13 | import multiprocessing |
| 14 | import os |
| 15 | import re |
| 16 | import sys |
| 17 | |
Yoshisato Yanagisawa | 4b49707 | 2018-11-07 02:52:33 +0000 | [diff] [blame] | 18 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 19 | |
Yoshisato Yanagisawa | 0db62fc | 2018-11-01 01:09:53 +0000 | [diff] [blame] | 20 | # The -t tools are incompatible with -j |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame] | 21 | t_specified = False |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 22 | j_specified = False |
| 23 | output_dir = '.' |
Bruce Dawson | f3b4f06 | 2017-09-07 17:22:26 -0700 | [diff] [blame] | 24 | input_args = sys.argv |
| 25 | # On Windows the autoninja.bat script passes along the arguments enclosed in |
| 26 | # double quotes. This prevents multiple levels of parsing of the special '^' |
| 27 | # characters needed when compiling a single file but means that this script gets |
| 28 | # called with a single argument containing all of the actual arguments, |
| 29 | # separated by spaces. When this case is detected we need to do argument |
| 30 | # splitting ourselves. This means that arguments containing actual spaces are |
| 31 | # not supported by autoninja, but that is not a real limitation. |
| 32 | if (sys.platform.startswith('win') and len(sys.argv) == 2 and |
| 33 | input_args[1].count(' ') > 0): |
| 34 | input_args = sys.argv[:1] + sys.argv[1].split() |
Yoshisato Yanagisawa | f66e551 | 2018-11-15 00:40:39 +0000 | [diff] [blame] | 35 | |
| 36 | # Ninja uses getopt_long, which allow to intermix non-option arguments. |
| 37 | # To leave non supported parameters untouched, we do not use getopt. |
Bruce Dawson | f3b4f06 | 2017-09-07 17:22:26 -0700 | [diff] [blame] | 38 | for index, arg in enumerate(input_args[1:]): |
Yoshisato Yanagisawa | f66e551 | 2018-11-15 00:40:39 +0000 | [diff] [blame] | 39 | if arg.startswith('-j'): |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 40 | j_specified = True |
Yoshisato Yanagisawa | f66e551 | 2018-11-15 00:40:39 +0000 | [diff] [blame] | 41 | if arg.startswith('-t'): |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame] | 42 | t_specified = True |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 43 | if arg == '-C': |
Bruce Dawson | f3b4f06 | 2017-09-07 17:22:26 -0700 | [diff] [blame] | 44 | # + 1 to get the next argument and +1 because we trimmed off input_args[0] |
| 45 | output_dir = input_args[index + 2] |
Bruce Dawson | 6af3aa8 | 2018-10-03 16:39:42 +0000 | [diff] [blame] | 46 | elif arg.startswith('-C'): |
| 47 | # Support -Cout/Default |
| 48 | output_dir = arg[2:] |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 49 | |
| 50 | use_goma = False |
| 51 | try: |
Bruce Dawson | 85c7502 | 2018-04-17 17:49:06 -0700 | [diff] [blame] | 52 | # If GOMA_DISABLED is set (to anything) then gomacc will use the local |
| 53 | # compiler instead of doing a goma compile. This is convenient if you want |
| 54 | # to briefly disable goma. It avoids having to rebuild the world when |
| 55 | # transitioning between goma/non-goma builds. However, it is not as fast as |
| 56 | # doing a "normal" non-goma build because an extra process is created for each |
| 57 | # compile step. Checking this environment variable ensures that autoninja uses |
| 58 | # an appropriate -j value in this situation. |
| 59 | if 'GOMA_DISABLED' not in os.environ: |
| 60 | with open(os.path.join(output_dir, 'args.gn')) as file_handle: |
| 61 | for line in file_handle: |
| 62 | # This regex pattern copied from create_installer_archive.py |
| 63 | m = re.match('^\s*use_goma\s*=\s*true(\s*$|\s*#.*$)', line) |
| 64 | if m: |
| 65 | use_goma = True |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 66 | except IOError: |
| 67 | pass |
| 68 | |
Yoshisato Yanagisawa | 4b49707 | 2018-11-07 02:52:33 +0000 | [diff] [blame] | 69 | # Specify ninja.exe on Windows so that ninja.bat can call autoninja and not |
| 70 | # be called back. |
| 71 | ninja_exe = 'ninja.exe' if sys.platform.startswith('win') else 'ninja' |
Allen Bauer | 75fa855 | 2018-11-07 22:43:39 +0000 | [diff] [blame] | 72 | ninja_exe_path = os.path.join(SCRIPT_DIR, ninja_exe) |
| 73 | |
Yoshisato Yanagisawa | 4b49707 | 2018-11-07 02:52:33 +0000 | [diff] [blame] | 74 | # Use absolute path for ninja path, |
| 75 | # or fail to execute ninja if depot_tools is not in PATH. |
Allen Bauer | 75fa855 | 2018-11-07 22:43:39 +0000 | [diff] [blame] | 76 | args = [ninja_exe_path] + input_args[1:] |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 77 | |
| 78 | num_cores = multiprocessing.cpu_count() |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame] | 79 | if not j_specified and not t_specified: |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 80 | if use_goma: |
| 81 | args.append('-j') |
Takuto Ikuta | 1206a35 | 2019-02-07 22:18:08 +0000 | [diff] [blame^] | 82 | core_multiplier = int(os.environ.get("NINJA_CORE_MULTIPLIER", "40")) |
| 83 | j_value = num_cores * core_multiplier |
| 84 | |
| 85 | if sys.platform.startswith('win'): |
| 86 | # On windows, j value higher than 1000 does not improve build performance. |
| 87 | j_value = min(j_value, 1000) |
| 88 | |
| 89 | args.append('%d' % j_value) |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 90 | else: |
| 91 | core_addition = os.environ.get("NINJA_CORE_ADDITION") |
| 92 | if core_addition: |
| 93 | core_addition = int(core_addition) |
| 94 | args.append('-j') |
| 95 | args.append('%d' % (num_cores + core_addition)) |
| 96 | |
Yoshisato Yanagisawa | 43a35d2 | 2018-11-15 03:00:51 +0000 | [diff] [blame] | 97 | # On Windows, fully quote the path so that the command processor doesn't think |
| 98 | # the whole output is the command. |
| 99 | # On Linux and Mac, if people put depot_tools in directories with ' ', |
| 100 | # shell would misunderstand ' ' as a path separation. |
| 101 | # TODO(yyanagisawa): provide proper quating for Windows. |
| 102 | # see https://cs.chromium.org/chromium/src/tools/mb/mb.py |
| 103 | for i in range(len(args)): |
| 104 | if (i == 0 and sys.platform.startswith('win')) or ' ' in args[i]: |
| 105 | args[i] = '"%s"' % args[i].replace('"', '\\"') |
| 106 | |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 107 | print ' '.join(args) |