blob: 01dcc30b734236da4edc7c9581270a60e03fd159 [file] [log] [blame]
Bruce Dawsonebebd952017-05-31 14:24:38 -07001# 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"""
6This script (intended to be invoked by autoninja or autoninja.bat) detects
7whether a build is using goma. If so it runs with a large -j value, and
8otherwise it chooses a small one. This auto-adjustment makes using goma simpler
9and safer, and avoids errors that can cause slow goma builds or swap-storms
10on non-goma builds.
11"""
12
13import multiprocessing
14import os
15import re
16import sys
17
Bruce Dawson1f767e12017-07-07 15:10:37 -070018# The -t tools are incompatible with -j and -l
19t_specified = False
Bruce Dawsonebebd952017-05-31 14:24:38 -070020j_specified = False
21output_dir = '.'
Bruce Dawsonf3b4f062017-09-07 17:22:26 -070022input_args = sys.argv
23# On Windows the autoninja.bat script passes along the arguments enclosed in
24# double quotes. This prevents multiple levels of parsing of the special '^'
25# characters needed when compiling a single file but means that this script gets
26# called with a single argument containing all of the actual arguments,
27# separated by spaces. When this case is detected we need to do argument
28# splitting ourselves. This means that arguments containing actual spaces are
29# not supported by autoninja, but that is not a real limitation.
30if (sys.platform.startswith('win') and len(sys.argv) == 2 and
31 input_args[1].count(' ') > 0):
32 input_args = sys.argv[:1] + sys.argv[1].split()
33for index, arg in enumerate(input_args[1:]):
Bruce Dawsonebebd952017-05-31 14:24:38 -070034 if arg == '-j':
35 j_specified = True
Bruce Dawson1f767e12017-07-07 15:10:37 -070036 if arg == '-t':
37 t_specified = True
Bruce Dawsonebebd952017-05-31 14:24:38 -070038 if arg == '-C':
Bruce Dawsonf3b4f062017-09-07 17:22:26 -070039 # + 1 to get the next argument and +1 because we trimmed off input_args[0]
40 output_dir = input_args[index + 2]
Bruce Dawsonebebd952017-05-31 14:24:38 -070041
42use_goma = False
43try:
Bruce Dawson85c75022018-04-17 17:49:06 -070044 # If GOMA_DISABLED is set (to anything) then gomacc will use the local
45 # compiler instead of doing a goma compile. This is convenient if you want
46 # to briefly disable goma. It avoids having to rebuild the world when
47 # transitioning between goma/non-goma builds. However, it is not as fast as
48 # doing a "normal" non-goma build because an extra process is created for each
49 # compile step. Checking this environment variable ensures that autoninja uses
50 # an appropriate -j value in this situation.
51 if 'GOMA_DISABLED' not in os.environ:
52 with open(os.path.join(output_dir, 'args.gn')) as file_handle:
53 for line in file_handle:
54 # This regex pattern copied from create_installer_archive.py
55 m = re.match('^\s*use_goma\s*=\s*true(\s*$|\s*#.*$)', line)
56 if m:
57 use_goma = True
Bruce Dawsonebebd952017-05-31 14:24:38 -070058except IOError:
59 pass
60
61if sys.platform.startswith('win'):
62 # Specify ninja.exe on Windows so that ninja.bat can call autoninja and not
63 # be called back.
Bruce Dawsonf3b4f062017-09-07 17:22:26 -070064 args = ['ninja.exe'] + input_args[1:]
Bruce Dawsonebebd952017-05-31 14:24:38 -070065else:
Bruce Dawsonf3b4f062017-09-07 17:22:26 -070066 args = ['ninja'] + input_args[1:]
Bruce Dawsonebebd952017-05-31 14:24:38 -070067
68num_cores = multiprocessing.cpu_count()
Bruce Dawson1f767e12017-07-07 15:10:37 -070069if not j_specified and not t_specified:
Bruce Dawsonebebd952017-05-31 14:24:38 -070070 if use_goma:
71 args.append('-j')
72 core_multiplier = int(os.environ.get("NINJA_CORE_MULTIPLIER", "20"))
73 args.append('%d' % (num_cores * core_multiplier))
74 else:
75 core_addition = os.environ.get("NINJA_CORE_ADDITION")
76 if core_addition:
77 core_addition = int(core_addition)
78 args.append('-j')
79 args.append('%d' % (num_cores + core_addition))
80
Bruce Dawson1f767e12017-07-07 15:10:37 -070081if not t_specified:
82 # Specify a maximum CPU load so that running builds in two different command
83 # prompts won't overload the system too much. This is not reliable enough to
84 # be used to auto-adjust between goma/non-goma loads, but it is a nice
85 # fallback load balancer.
86 args.append('-l')
87 args.append('%d' % num_cores)
Bruce Dawsonebebd952017-05-31 14:24:38 -070088
89print ' '.join(args)