You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
depot_tools/autoninja.py

145 lines
5.5 KiB
Python

#!/usr/bin/env vpython
# Copyright (c) 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
This script (intended to be invoked by autoninja or autoninja.bat) detects
whether a build is using goma. If so it runs with a large -j value, and
otherwise it chooses a small one. This auto-adjustment makes using goma simpler
and safer, and avoids errors that can cause slow goma builds or swap-storms
on non-goma builds.
"""
# [VPYTHON:BEGIN]
# wheel: <
# name: "infra/python/wheels/psutil/${vpython_platform}"
6 years ago
# version: "version:5.6.2"
# >
# [VPYTHON:END]
from __future__ import print_function
import os
import psutil
import re
import sys
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
# The -t tools are incompatible with -j
t_specified = False
j_specified = False
output_dir = '.'
Fix autoninja to allow compiling one source file This is the second attempt at fixing autoninja to allow passing '^^' to it to specify that ninja should build the outputs of the specified file instead of building that file itself. The problem is that '^' is a special character and when extra layers of indirection are added the number of '^' characters needed grows exponentially in some poorly understood way. The first fix attempt just quoted the arguments that autoninja.bat passed to autoninja.py, but that meant they came in as one argument. This fix expands on that by modifying autoninja.py to understand how to deal with the monolithic argument. With this change this once again works: autoninja -C out\debug_component ..\..\base\win\enum_variant.cc^^ It can be convenient to have a ninja.bat file which starts goma and lets users keep typing the same build commands. However even with this fix the previously recommended ninja.bat file must be invoked with four '^' characters. If that is too much then the new recommended ninja.bat is to copy autoninja.bat and modify as needed, perhaps like this: @echo off call python c:\goma\goma-win64\goma_ctl.py ensure_start >nul FOR /f "usebackq tokens=*" %%a in (`python c:\src\depot_tools\autoninja.py "%*"`) do echo %%a & %%a BUG: 758725 Change-Id: Ieee9cf343ee5f22e9988a1969cb7a7a90687666b Reviewed-on: https://chromium-review.googlesource.com/656478 Reviewed-by: Sébastien Marchand <sebmarchand@chromium.org> Reviewed-by: Dirk Pranke <dpranke@chromium.org> Commit-Queue: Bruce Dawson <brucedawson@chromium.org>
8 years ago
input_args = sys.argv
# On Windows the autoninja.bat script passes along the arguments enclosed in
# double quotes. This prevents multiple levels of parsing of the special '^'
# characters needed when compiling a single file but means that this script gets
# called with a single argument containing all of the actual arguments,
# separated by spaces. When this case is detected we need to do argument
# splitting ourselves. This means that arguments containing actual spaces are
# not supported by autoninja, but that is not a real limitation.
if (sys.platform.startswith('win') and len(sys.argv) == 2 and
input_args[1].count(' ') > 0):
input_args = sys.argv[:1] + sys.argv[1].split()
# Ninja uses getopt_long, which allow to intermix non-option arguments.
# To leave non supported parameters untouched, we do not use getopt.
Fix autoninja to allow compiling one source file This is the second attempt at fixing autoninja to allow passing '^^' to it to specify that ninja should build the outputs of the specified file instead of building that file itself. The problem is that '^' is a special character and when extra layers of indirection are added the number of '^' characters needed grows exponentially in some poorly understood way. The first fix attempt just quoted the arguments that autoninja.bat passed to autoninja.py, but that meant they came in as one argument. This fix expands on that by modifying autoninja.py to understand how to deal with the monolithic argument. With this change this once again works: autoninja -C out\debug_component ..\..\base\win\enum_variant.cc^^ It can be convenient to have a ninja.bat file which starts goma and lets users keep typing the same build commands. However even with this fix the previously recommended ninja.bat file must be invoked with four '^' characters. If that is too much then the new recommended ninja.bat is to copy autoninja.bat and modify as needed, perhaps like this: @echo off call python c:\goma\goma-win64\goma_ctl.py ensure_start >nul FOR /f "usebackq tokens=*" %%a in (`python c:\src\depot_tools\autoninja.py "%*"`) do echo %%a & %%a BUG: 758725 Change-Id: Ieee9cf343ee5f22e9988a1969cb7a7a90687666b Reviewed-on: https://chromium-review.googlesource.com/656478 Reviewed-by: Sébastien Marchand <sebmarchand@chromium.org> Reviewed-by: Dirk Pranke <dpranke@chromium.org> Commit-Queue: Bruce Dawson <brucedawson@chromium.org>
8 years ago
for index, arg in enumerate(input_args[1:]):
if arg.startswith('-j'):
j_specified = True
if arg.startswith('-t'):
t_specified = True
if arg == '-C':
Fix autoninja to allow compiling one source file This is the second attempt at fixing autoninja to allow passing '^^' to it to specify that ninja should build the outputs of the specified file instead of building that file itself. The problem is that '^' is a special character and when extra layers of indirection are added the number of '^' characters needed grows exponentially in some poorly understood way. The first fix attempt just quoted the arguments that autoninja.bat passed to autoninja.py, but that meant they came in as one argument. This fix expands on that by modifying autoninja.py to understand how to deal with the monolithic argument. With this change this once again works: autoninja -C out\debug_component ..\..\base\win\enum_variant.cc^^ It can be convenient to have a ninja.bat file which starts goma and lets users keep typing the same build commands. However even with this fix the previously recommended ninja.bat file must be invoked with four '^' characters. If that is too much then the new recommended ninja.bat is to copy autoninja.bat and modify as needed, perhaps like this: @echo off call python c:\goma\goma-win64\goma_ctl.py ensure_start >nul FOR /f "usebackq tokens=*" %%a in (`python c:\src\depot_tools\autoninja.py "%*"`) do echo %%a & %%a BUG: 758725 Change-Id: Ieee9cf343ee5f22e9988a1969cb7a7a90687666b Reviewed-on: https://chromium-review.googlesource.com/656478 Reviewed-by: Sébastien Marchand <sebmarchand@chromium.org> Reviewed-by: Dirk Pranke <dpranke@chromium.org> Commit-Queue: Bruce Dawson <brucedawson@chromium.org>
8 years ago
# + 1 to get the next argument and +1 because we trimmed off input_args[0]
output_dir = input_args[index + 2]
elif arg.startswith('-C'):
# Support -Cout/Default
output_dir = arg[2:]
use_goma = False
use_jumbo_build = False
# Attempt to auto-detect goma usage. We support gn-based builds, where we
# look for args.gn in the build tree, and cmake-based builds where we look for
# rules.ninja.
if os.path.exists(os.path.join(output_dir, 'args.gn')):
with open(os.path.join(output_dir, 'args.gn')) as file_handle:
for line in file_handle:
# This regex pattern copied from create_installer_archive.py
if re.match(r'^\s*use_goma\s*=\s*true(\s*$|\s*#.*$)', line):
use_goma = True
continue
match_use_jumbo_build = re.match(
r'^\s*use_jumbo_build\s*=\s*true(\s*$|\s*#.*$)', line)
if match_use_jumbo_build:
use_jumbo_build = True
continue
elif os.path.exists(os.path.join(output_dir, 'rules.ninja')):
with open(os.path.join(output_dir, 'rules.ninja')) as file_handle:
for line in file_handle:
if re.match(r'^\s*command\s*=\s*\S+gomacc', line):
use_goma = True
break
# If GOMA_DISABLED is set (to anything) then gomacc will use the local
# compiler instead of doing a goma compile. This is convenient if you want
# to briefly disable goma. It avoids having to rebuild the world when
# transitioning between goma/non-goma builds. However, it is not as fast as
# doing a "normal" non-goma build because an extra process is created for each
# compile step. Checking this environment variable ensures that autoninja uses
# an appropriate -j value in this situation.
if 'GOMA_DISABLED' in os.environ:
use_goma = False
# Specify ninja.exe on Windows so that ninja.bat can call autoninja and not
# be called back.
ninja_exe = 'ninja.exe' if sys.platform.startswith('win') else 'ninja'
ninja_exe_path = os.path.join(SCRIPT_DIR, ninja_exe)
# Use absolute path for ninja path,
# or fail to execute ninja if depot_tools is not in PATH.
args = [ninja_exe_path] + input_args[1:]
num_cores = psutil.cpu_count()
if not j_specified and not t_specified:
if use_goma:
args.append('-j')
core_multiplier = int(os.environ.get('NINJA_CORE_MULTIPLIER', '40'))
j_value = num_cores * core_multiplier
if sys.platform.startswith('win'):
# On windows, j value higher than 1000 does not improve build performance.
j_value = min(j_value, 1000)
elif sys.platform == 'darwin':
# On Mac, j value higher than 500 causes 'Too many open files' error
# (crbug.com/936864).
j_value = min(j_value, 500)
args.append('%d' % j_value)
else:
j_value = num_cores
# Ninja defaults to |num_cores + 2|
j_value += int(os.environ.get('NINJA_CORE_ADDITION', '2'))
if use_jumbo_build:
# Compiling a jumbo .o can easily use 1-2GB of memory. Leaving 2GB per
# process avoids memory swap/compression storms when also considering
# already in-use memory.
physical_ram = psutil.virtual_memory().total
GB = 1024 * 1024 * 1024
j_value = min(j_value, physical_ram / (2 * GB))
args.append('-j')
args.append('%d' % j_value)
# On Windows, fully quote the path so that the command processor doesn't think
# the whole output is the command.
# On Linux and Mac, if people put depot_tools in directories with ' ',
# shell would misunderstand ' ' as a path separation.
# TODO(yyanagisawa): provide proper quating for Windows.
# see https://cs.chromium.org/chromium/src/tools/mb/mb.py
for i in range(len(args)):
if (i == 0 and sys.platform.startswith('win')) or ' ' in args[i]:
args[i] = '"%s"' % args[i].replace('"', '\\"')
print(' '.join(args))