blob: 056b9c1f2148959e3bec29439be4f09ea8270c9f [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
18j_specified = False
19output_dir = '.'
20for index, arg in enumerate(sys.argv[1:]):
21 if arg == '-j':
22 j_specified = True
23 if arg == '-C':
24 # + 1 to get the next argument and +1 because we trimmed off sys.argv[0]
25 output_dir = sys.argv[index + 2]
26
27use_goma = False
28try:
29 with open(os.path.join(output_dir, 'args.gn')) as file_handle:
30 for line in file_handle:
31 # This regex pattern copied from create_installer_archive.py
32 m = re.match('^\s*use_goma\s*=\s*true(\s*$|\s*#.*$)', line)
33 if m:
34 use_goma = True
35except IOError:
36 pass
37
38if sys.platform.startswith('win'):
39 # Specify ninja.exe on Windows so that ninja.bat can call autoninja and not
40 # be called back.
41 args = ['ninja.exe'] + sys.argv[1:]
42else:
43 args = ['ninja'] + sys.argv[1:]
44
45num_cores = multiprocessing.cpu_count()
46if not j_specified:
47 if use_goma:
48 args.append('-j')
49 core_multiplier = int(os.environ.get("NINJA_CORE_MULTIPLIER", "20"))
50 args.append('%d' % (num_cores * core_multiplier))
51 else:
52 core_addition = os.environ.get("NINJA_CORE_ADDITION")
53 if core_addition:
54 core_addition = int(core_addition)
55 args.append('-j')
56 args.append('%d' % (num_cores + core_addition))
57
58# Specify a maximum CPU load so that running builds in two different command
59# prompts won't overload the system too much. This is not reliable enough to
60# be used to auto-adjust between goma/non-goma loads, but it is a nice
61# fallback load balancer.
62args.append('-l')
63args.append('%d' % num_cores)
64
65print ' '.join(args)