blob: 07269caeaa1ebec4d19ba508c04e1a5fc38ffe00 [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
Yoshisato Yanagisawa4b497072018-11-07 02:52:33 +000018SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
19
Yoshisato Yanagisawa0db62fc2018-11-01 01:09:53 +000020# The -t tools are incompatible with -j
Bruce Dawson1f767e12017-07-07 15:10:37 -070021t_specified = False
Bruce Dawsonebebd952017-05-31 14:24:38 -070022j_specified = False
23output_dir = '.'
Bruce Dawsonf3b4f062017-09-07 17:22:26 -070024input_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.
32if (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()
35for index, arg in enumerate(input_args[1:]):
Bruce Dawsonebebd952017-05-31 14:24:38 -070036 if arg == '-j':
37 j_specified = True
Bruce Dawson1f767e12017-07-07 15:10:37 -070038 if arg == '-t':
39 t_specified = True
Bruce Dawsonebebd952017-05-31 14:24:38 -070040 if arg == '-C':
Bruce Dawsonf3b4f062017-09-07 17:22:26 -070041 # + 1 to get the next argument and +1 because we trimmed off input_args[0]
42 output_dir = input_args[index + 2]
Bruce Dawson6af3aa82018-10-03 16:39:42 +000043 elif arg.startswith('-C'):
44 # Support -Cout/Default
45 output_dir = arg[2:]
Bruce Dawsonebebd952017-05-31 14:24:38 -070046
47use_goma = False
48try:
Bruce Dawson85c75022018-04-17 17:49:06 -070049 # If GOMA_DISABLED is set (to anything) then gomacc will use the local
50 # compiler instead of doing a goma compile. This is convenient if you want
51 # to briefly disable goma. It avoids having to rebuild the world when
52 # transitioning between goma/non-goma builds. However, it is not as fast as
53 # doing a "normal" non-goma build because an extra process is created for each
54 # compile step. Checking this environment variable ensures that autoninja uses
55 # an appropriate -j value in this situation.
56 if 'GOMA_DISABLED' not in os.environ:
57 with open(os.path.join(output_dir, 'args.gn')) as file_handle:
58 for line in file_handle:
59 # This regex pattern copied from create_installer_archive.py
60 m = re.match('^\s*use_goma\s*=\s*true(\s*$|\s*#.*$)', line)
61 if m:
62 use_goma = True
Bruce Dawsonebebd952017-05-31 14:24:38 -070063except IOError:
64 pass
65
Yoshisato Yanagisawa4b497072018-11-07 02:52:33 +000066# Specify ninja.exe on Windows so that ninja.bat can call autoninja and not
67# be called back.
68ninja_exe = 'ninja.exe' if sys.platform.startswith('win') else 'ninja'
69
Allen Bauer75fa8552018-11-07 22:43:39 +000070ninja_exe_path = os.path.join(SCRIPT_DIR, ninja_exe)
71
72# On Windows, fully quote the path so that the command processor doesn't think
73# the whole output is the command.
74if sys.platform.startswith('win'):
75 ninja_exe_path = '"' + ninja_exe_path + '"'
76
Yoshisato Yanagisawa4b497072018-11-07 02:52:33 +000077# Use absolute path for ninja path,
78# or fail to execute ninja if depot_tools is not in PATH.
Allen Bauer75fa8552018-11-07 22:43:39 +000079args = [ninja_exe_path] + input_args[1:]
Bruce Dawsonebebd952017-05-31 14:24:38 -070080
81num_cores = multiprocessing.cpu_count()
Bruce Dawson1f767e12017-07-07 15:10:37 -070082if not j_specified and not t_specified:
Bruce Dawsonebebd952017-05-31 14:24:38 -070083 if use_goma:
84 args.append('-j')
85 core_multiplier = int(os.environ.get("NINJA_CORE_MULTIPLIER", "20"))
86 args.append('%d' % (num_cores * core_multiplier))
87 else:
88 core_addition = os.environ.get("NINJA_CORE_ADDITION")
89 if core_addition:
90 core_addition = int(core_addition)
91 args.append('-j')
92 args.append('%d' % (num_cores + core_addition))
93
Bruce Dawsonebebd952017-05-31 14:24:38 -070094print ' '.join(args)