blob: a7beac5f065dd2ec8bd533b9ed87d24b44366cb0 [file] [log] [blame]
Gabriel Charette125f7cc2019-06-20 19:19:35 +00001#!/usr/bin/env vpython
Bruce Dawsonebebd952017-05-31 14:24:38 -07002# 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"""
7This script (intended to be invoked by autoninja or autoninja.bat) detects
8whether a build is using goma. If so it runs with a large -j value, and
9otherwise it chooses a small one. This auto-adjustment makes using goma simpler
10and safer, and avoids errors that can cause slow goma builds or swap-storms
11on non-goma builds.
12"""
13
Gabriel Charette125f7cc2019-06-20 19:19:35 +000014# [VPYTHON:BEGIN]
15# wheel: <
16# name: "infra/python/wheels/psutil/${vpython_platform}"
17# version: "version:5.2.2"
18# >
19# [VPYTHON:END]
20
Raul Tambre80ee78e2019-05-06 22:41:05 +000021from __future__ import print_function
22
Bruce Dawsonebebd952017-05-31 14:24:38 -070023import os
Gabriel Charette125f7cc2019-06-20 19:19:35 +000024import psutil
Bruce Dawsonebebd952017-05-31 14:24:38 -070025import re
26import sys
27
Yoshisato Yanagisawa4b497072018-11-07 02:52:33 +000028SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
29
Yoshisato Yanagisawa0db62fc2018-11-01 01:09:53 +000030# The -t tools are incompatible with -j
Bruce Dawson1f767e12017-07-07 15:10:37 -070031t_specified = False
Bruce Dawsonebebd952017-05-31 14:24:38 -070032j_specified = False
33output_dir = '.'
Bruce Dawsonf3b4f062017-09-07 17:22:26 -070034input_args = sys.argv
35# On Windows the autoninja.bat script passes along the arguments enclosed in
36# double quotes. This prevents multiple levels of parsing of the special '^'
37# characters needed when compiling a single file but means that this script gets
38# called with a single argument containing all of the actual arguments,
39# separated by spaces. When this case is detected we need to do argument
40# splitting ourselves. This means that arguments containing actual spaces are
41# not supported by autoninja, but that is not a real limitation.
42if (sys.platform.startswith('win') and len(sys.argv) == 2 and
43 input_args[1].count(' ') > 0):
44 input_args = sys.argv[:1] + sys.argv[1].split()
Yoshisato Yanagisawaf66e5512018-11-15 00:40:39 +000045
46# Ninja uses getopt_long, which allow to intermix non-option arguments.
47# To leave non supported parameters untouched, we do not use getopt.
Bruce Dawsonf3b4f062017-09-07 17:22:26 -070048for index, arg in enumerate(input_args[1:]):
Yoshisato Yanagisawaf66e5512018-11-15 00:40:39 +000049 if arg.startswith('-j'):
Bruce Dawsonebebd952017-05-31 14:24:38 -070050 j_specified = True
Yoshisato Yanagisawaf66e5512018-11-15 00:40:39 +000051 if arg.startswith('-t'):
Bruce Dawson1f767e12017-07-07 15:10:37 -070052 t_specified = True
Bruce Dawsonebebd952017-05-31 14:24:38 -070053 if arg == '-C':
Bruce Dawsonf3b4f062017-09-07 17:22:26 -070054 # + 1 to get the next argument and +1 because we trimmed off input_args[0]
55 output_dir = input_args[index + 2]
Bruce Dawson6af3aa82018-10-03 16:39:42 +000056 elif arg.startswith('-C'):
57 # Support -Cout/Default
58 output_dir = arg[2:]
Bruce Dawsonebebd952017-05-31 14:24:38 -070059
60use_goma = False
Gabriel Charette125f7cc2019-06-20 19:19:35 +000061use_jumbo_build = False
Bruce Dawsonebebd952017-05-31 14:24:38 -070062try:
Bruce Dawson85c75022018-04-17 17:49:06 -070063 # If GOMA_DISABLED is set (to anything) then gomacc will use the local
64 # compiler instead of doing a goma compile. This is convenient if you want
65 # to briefly disable goma. It avoids having to rebuild the world when
66 # transitioning between goma/non-goma builds. However, it is not as fast as
67 # doing a "normal" non-goma build because an extra process is created for each
68 # compile step. Checking this environment variable ensures that autoninja uses
69 # an appropriate -j value in this situation.
Gabriel Charette125f7cc2019-06-20 19:19:35 +000070 with open(os.path.join(output_dir, 'args.gn')) as file_handle:
71 for line in file_handle:
72 # This regex pattern copied from create_installer_archive.py
73 match_use_goma = re.match(r'^\s*use_goma\s*=\s*true(\s*$|\s*#.*$)', line)
74 if match_use_goma and 'GOMA_DISABLED' not in os.environ:
75 use_goma = True
76 continue
77 match_use_jumbo_build = re.match(
78 r'^\s*use_jumbo_build\s*=\s*true(\s*$|\s*#.*$)', line)
79 if match_use_jumbo_build:
80 use_jumbo_build = True
81 continue
Bruce Dawsonebebd952017-05-31 14:24:38 -070082except IOError:
83 pass
84
Yoshisato Yanagisawa4b497072018-11-07 02:52:33 +000085# Specify ninja.exe on Windows so that ninja.bat can call autoninja and not
86# be called back.
87ninja_exe = 'ninja.exe' if sys.platform.startswith('win') else 'ninja'
Allen Bauer75fa8552018-11-07 22:43:39 +000088ninja_exe_path = os.path.join(SCRIPT_DIR, ninja_exe)
89
Yoshisato Yanagisawa4b497072018-11-07 02:52:33 +000090# Use absolute path for ninja path,
91# or fail to execute ninja if depot_tools is not in PATH.
Allen Bauer75fa8552018-11-07 22:43:39 +000092args = [ninja_exe_path] + input_args[1:]
Bruce Dawsonebebd952017-05-31 14:24:38 -070093
Gabriel Charette125f7cc2019-06-20 19:19:35 +000094num_cores = psutil.cpu_count()
Bruce Dawson1f767e12017-07-07 15:10:37 -070095if not j_specified and not t_specified:
Bruce Dawsonebebd952017-05-31 14:24:38 -070096 if use_goma:
97 args.append('-j')
Gabriel Charette125f7cc2019-06-20 19:19:35 +000098 core_multiplier = int(os.environ.get('NINJA_CORE_MULTIPLIER', '40'))
Takuto Ikuta1206a352019-02-07 22:18:08 +000099 j_value = num_cores * core_multiplier
100
101 if sys.platform.startswith('win'):
102 # On windows, j value higher than 1000 does not improve build performance.
103 j_value = min(j_value, 1000)
Takuto Ikutada4dbf82019-03-04 03:21:58 +0000104 elif sys.platform == 'darwin':
105 # On Mac, j value higher than 500 causes 'Too many open files' error
106 # (crbug.com/936864).
107 j_value = min(j_value, 500)
Takuto Ikuta1206a352019-02-07 22:18:08 +0000108
109 args.append('%d' % j_value)
Bruce Dawsonebebd952017-05-31 14:24:38 -0700110 else:
Gabriel Charette125f7cc2019-06-20 19:19:35 +0000111 j_value = num_cores
112 # Ninja defaults to |num_cores + 2|
113 j_value += int(os.environ.get('NINJA_CORE_ADDITION', '2'))
114 if use_jumbo_build:
115 # Compiling a jumbo .o can easily use 1-2GB of memory. Leaving 2GB per
116 # process avoids memory swap/compression storms when also considering
117 # already in-use memory.
118 physical_ram = psutil.virtual_memory().total
119 GB = 1024 * 1024 * 1024
120 j_value = min(j_value, physical_ram / (2 * GB))
121 args.append('-j')
122 args.append('%d' % j_value)
Bruce Dawsonebebd952017-05-31 14:24:38 -0700123
Yoshisato Yanagisawa43a35d22018-11-15 03:00:51 +0000124# On Windows, fully quote the path so that the command processor doesn't think
125# the whole output is the command.
126# On Linux and Mac, if people put depot_tools in directories with ' ',
127# shell would misunderstand ' ' as a path separation.
128# TODO(yyanagisawa): provide proper quating for Windows.
129# see https://cs.chromium.org/chromium/src/tools/mb/mb.py
130for i in range(len(args)):
131 if (i == 0 and sys.platform.startswith('win')) or ' ' in args[i]:
132 args[i] = '"%s"' % args[i].replace('"', '\\"')
133
Raul Tambre80ee78e2019-05-06 22:41:05 +0000134print(' '.join(args))