|
|
|
@ -27,7 +27,7 @@ import ninja
|
|
|
|
|
import ninja_reclient
|
|
|
|
|
import siso
|
|
|
|
|
|
|
|
|
|
if sys.platform in ['darwin', 'linux']:
|
|
|
|
|
if sys.platform in ["darwin", "linux"]:
|
|
|
|
|
import resource
|
|
|
|
|
|
|
|
|
|
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
@ -39,13 +39,13 @@ SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
# pylint: disable=line-too-long
|
|
|
|
|
# [1] https://learn.microsoft.com/en-us/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way # noqa
|
|
|
|
|
# [2] https://web.archive.org/web/20150815000000*/https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/set.mspx # noqa
|
|
|
|
|
_UNSAFE_FOR_CMD = set('^<>&|()%')
|
|
|
|
|
_UNSAFE_FOR_CMD = set("^<>&|()%")
|
|
|
|
|
_ALL_META_CHARS = _UNSAFE_FOR_CMD.union(set('"'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _quote_for_cmd(arg):
|
|
|
|
|
# First, escape the arg so that CommandLineToArgvW will parse it properly.
|
|
|
|
|
if arg == '' or ' ' in arg or '"' in arg:
|
|
|
|
|
if arg == "" or " " in arg or '"' in arg:
|
|
|
|
|
quote_re = re.compile(r'(\\*)"')
|
|
|
|
|
arg = '"%s"' % (quote_re.sub(lambda mo: 2 * mo.group(1) + '\\"', arg))
|
|
|
|
|
|
|
|
|
@ -53,13 +53,13 @@ def _quote_for_cmd(arg):
|
|
|
|
|
# double quotes; if it does, quote everything (including the double
|
|
|
|
|
# quotes) for safety.
|
|
|
|
|
if any(a in _UNSAFE_FOR_CMD for a in arg):
|
|
|
|
|
arg = ''.join('^' + a if a in _ALL_META_CHARS else a for a in arg)
|
|
|
|
|
arg = "".join("^" + a if a in _ALL_META_CHARS else a for a in arg)
|
|
|
|
|
return arg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _print_cmd(cmd):
|
|
|
|
|
shell_quoter = shlex.quote
|
|
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
|
shell_quoter = _quote_for_cmd
|
|
|
|
|
print(*[shell_quoter(arg) for arg in cmd], file=sys.stderr)
|
|
|
|
|
|
|
|
|
@ -70,14 +70,14 @@ def _gn_lines(output_dir, path):
|
|
|
|
|
import directives as needed.
|
|
|
|
|
"""
|
|
|
|
|
import_re = re.compile(r'\s*import\("(.*)"\)')
|
|
|
|
|
with open(path, encoding='utf-8') as f:
|
|
|
|
|
with open(path, encoding="utf-8") as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
match = import_re.match(line)
|
|
|
|
|
if match:
|
|
|
|
|
raw_import_path = match.groups()[0]
|
|
|
|
|
if raw_import_path[:2] == "//":
|
|
|
|
|
import_path = os.path.normpath(
|
|
|
|
|
os.path.join(output_dir, '..', '..',
|
|
|
|
|
os.path.join(output_dir, "..", "..",
|
|
|
|
|
raw_import_path[2:]))
|
|
|
|
|
else:
|
|
|
|
|
import_path = os.path.normpath(
|
|
|
|
@ -93,9 +93,9 @@ def main(args):
|
|
|
|
|
t_specified = False
|
|
|
|
|
j_specified = False
|
|
|
|
|
offline = False
|
|
|
|
|
output_dir = '.'
|
|
|
|
|
output_dir = "."
|
|
|
|
|
input_args = args
|
|
|
|
|
summarize_build = os.environ.get('NINJA_SUMMARIZE_BUILD') == '1'
|
|
|
|
|
summarize_build = os.environ.get("NINJA_SUMMARIZE_BUILD") == "1"
|
|
|
|
|
# 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
|
|
|
|
@ -103,29 +103,31 @@ def main(args):
|
|
|
|
|
# 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(args) == 2
|
|
|
|
|
and input_args[1].count(' ') > 0):
|
|
|
|
|
if (sys.platform.startswith("win") and len(args) == 2
|
|
|
|
|
and input_args[1].count(" ") > 0):
|
|
|
|
|
input_args = args[:1] + args[1].split()
|
|
|
|
|
|
|
|
|
|
# Ninja uses getopt_long, which allow to intermix non-option arguments.
|
|
|
|
|
# To leave non supported parameters untouched, we do not use getopt.
|
|
|
|
|
for index, arg in enumerate(input_args[1:]):
|
|
|
|
|
if arg.startswith('-j'):
|
|
|
|
|
if arg.startswith("-j"):
|
|
|
|
|
j_specified = True
|
|
|
|
|
if arg.startswith('-t'):
|
|
|
|
|
if arg.startswith("-t"):
|
|
|
|
|
t_specified = True
|
|
|
|
|
if arg == '-C':
|
|
|
|
|
if arg == "-C":
|
|
|
|
|
# + 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'):
|
|
|
|
|
elif arg.startswith("-C"):
|
|
|
|
|
# Support -Cout/Default
|
|
|
|
|
output_dir = arg[2:]
|
|
|
|
|
elif arg in ('-o', '--offline'):
|
|
|
|
|
elif arg in ("-o", "--offline"):
|
|
|
|
|
offline = True
|
|
|
|
|
elif arg == '-h':
|
|
|
|
|
print('autoninja: Use -o/--offline to temporary disable goma.',
|
|
|
|
|
file=sys.stderr)
|
|
|
|
|
elif arg == "-h":
|
|
|
|
|
print(
|
|
|
|
|
"autoninja: Use -o/--offline to temporary disable goma.",
|
|
|
|
|
file=sys.stderr,
|
|
|
|
|
)
|
|
|
|
|
print(file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
use_goma = False
|
|
|
|
@ -135,8 +137,8 @@ def main(args):
|
|
|
|
|
# Attempt to auto-detect remote build acceleration. 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')):
|
|
|
|
|
for line in _gn_lines(output_dir, os.path.join(output_dir, 'args.gn')):
|
|
|
|
|
if os.path.exists(os.path.join(output_dir, "args.gn")):
|
|
|
|
|
for line in _gn_lines(output_dir, os.path.join(output_dir, "args.gn")):
|
|
|
|
|
# use_goma, or use_remoteexec will activate build
|
|
|
|
|
# acceleration.
|
|
|
|
|
#
|
|
|
|
@ -145,62 +147,70 @@ def main(args):
|
|
|
|
|
# use_goma=false# use_goma=true This comment is ignored
|
|
|
|
|
#
|
|
|
|
|
# Anything after a comment is not consider a valid argument.
|
|
|
|
|
line_without_comment = line.split('#')[0]
|
|
|
|
|
if re.search(r'(^|\s)(use_goma)\s*=\s*true($|\s)',
|
|
|
|
|
line_without_comment = line.split("#")[0]
|
|
|
|
|
if re.search(r"(^|\s)(use_goma)\s*=\s*true($|\s)",
|
|
|
|
|
line_without_comment):
|
|
|
|
|
use_goma = True
|
|
|
|
|
continue
|
|
|
|
|
if re.search(r'(^|\s)(use_remoteexec)\s*=\s*true($|\s)',
|
|
|
|
|
line_without_comment):
|
|
|
|
|
if re.search(
|
|
|
|
|
r"(^|\s)(use_remoteexec)\s*=\s*true($|\s)",
|
|
|
|
|
line_without_comment,
|
|
|
|
|
):
|
|
|
|
|
use_remoteexec = True
|
|
|
|
|
continue
|
|
|
|
|
if re.search(r'(^|\s)(use_siso)\s*=\s*true($|\s)',
|
|
|
|
|
if re.search(r"(^|\s)(use_siso)\s*=\s*true($|\s)",
|
|
|
|
|
line_without_comment):
|
|
|
|
|
use_siso = True
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
siso_marker = os.path.join(output_dir, '.siso_deps')
|
|
|
|
|
siso_marker = os.path.join(output_dir, ".siso_deps")
|
|
|
|
|
if use_siso:
|
|
|
|
|
# autosiso generates a .ninja_log file so the mere existence of a
|
|
|
|
|
# .ninja_log file doesn't imply that a ninja build was done. However
|
|
|
|
|
# if there is a .ninja_log but no .siso_deps then that implies a
|
|
|
|
|
# ninja build.
|
|
|
|
|
ninja_marker = os.path.join(output_dir, '.ninja_log')
|
|
|
|
|
ninja_marker = os.path.join(output_dir, ".ninja_log")
|
|
|
|
|
if os.path.exists(ninja_marker) and not os.path.exists(siso_marker):
|
|
|
|
|
print('Run gn clean before switching from ninja to siso in %s' %
|
|
|
|
|
output_dir,
|
|
|
|
|
file=sys.stderr)
|
|
|
|
|
print(
|
|
|
|
|
"Run gn clean before switching from ninja to siso in %s" %
|
|
|
|
|
output_dir,
|
|
|
|
|
file=sys.stderr,
|
|
|
|
|
)
|
|
|
|
|
return 1
|
|
|
|
|
if use_goma:
|
|
|
|
|
print('Siso does not support Goma.', file=sys.stderr)
|
|
|
|
|
print('Do not use use_siso=true and use_goma=true',
|
|
|
|
|
file=sys.stderr)
|
|
|
|
|
print("Siso does not support Goma.", file=sys.stderr)
|
|
|
|
|
print(
|
|
|
|
|
"Do not use use_siso=true and use_goma=true",
|
|
|
|
|
file=sys.stderr,
|
|
|
|
|
)
|
|
|
|
|
return 1
|
|
|
|
|
if use_remoteexec:
|
|
|
|
|
return autosiso.main(['autosiso'] + input_args[1:])
|
|
|
|
|
return siso.main(['siso', 'ninja', '--offline'] + input_args[1:])
|
|
|
|
|
return autosiso.main(["autosiso"] + input_args[1:])
|
|
|
|
|
return siso.main(["siso", "ninja", "--offline"] + input_args[1:])
|
|
|
|
|
|
|
|
|
|
if os.path.exists(siso_marker):
|
|
|
|
|
print('Run gn clean before switching from siso to ninja in %s' %
|
|
|
|
|
output_dir,
|
|
|
|
|
file=sys.stderr)
|
|
|
|
|
print(
|
|
|
|
|
"Run gn clean before switching from siso to ninja in %s" %
|
|
|
|
|
output_dir,
|
|
|
|
|
file=sys.stderr,
|
|
|
|
|
)
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
for relative_path in [
|
|
|
|
|
'', # GN keeps them in the root of output_dir
|
|
|
|
|
'CMakeFiles'
|
|
|
|
|
"", # GN keeps them in the root of output_dir
|
|
|
|
|
"CMakeFiles",
|
|
|
|
|
]:
|
|
|
|
|
path = os.path.join(output_dir, relative_path, 'rules.ninja')
|
|
|
|
|
path = os.path.join(output_dir, relative_path, "rules.ninja")
|
|
|
|
|
if os.path.exists(path):
|
|
|
|
|
with open(path, encoding='utf-8') as file_handle:
|
|
|
|
|
with open(path, encoding="utf-8") as file_handle:
|
|
|
|
|
for line in file_handle:
|
|
|
|
|
if re.match(r'^\s*command\s*=\s*\S+gomacc', line):
|
|
|
|
|
if re.match(r"^\s*command\s*=\s*\S+gomacc", line):
|
|
|
|
|
use_goma = True
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# Strip -o/--offline so ninja doesn't see them.
|
|
|
|
|
input_args = [arg for arg in input_args if arg not in ('-o', '--offline')]
|
|
|
|
|
input_args = [arg for arg in input_args if arg not in ("-o", "--offline")]
|
|
|
|
|
|
|
|
|
|
# If GOMA_DISABLED is set to "true", "t", "yes", "y", or "1"
|
|
|
|
|
# (case-insensitive) then gomacc will use the local compiler instead of
|
|
|
|
@ -210,43 +220,46 @@ def main(args):
|
|
|
|
|
# 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.
|
|
|
|
|
goma_disabled_env = os.environ.get('GOMA_DISABLED', '0').lower()
|
|
|
|
|
if offline or goma_disabled_env in ['true', 't', 'yes', 'y', '1']:
|
|
|
|
|
goma_disabled_env = os.environ.get("GOMA_DISABLED", "0").lower()
|
|
|
|
|
if offline or goma_disabled_env in ["true", "t", "yes", "y", "1"]:
|
|
|
|
|
use_goma = False
|
|
|
|
|
|
|
|
|
|
if use_goma:
|
|
|
|
|
gomacc_file = 'gomacc.exe' if sys.platform.startswith(
|
|
|
|
|
'win') else 'gomacc'
|
|
|
|
|
goma_dir = os.environ.get('GOMA_DIR',
|
|
|
|
|
os.path.join(SCRIPT_DIR, '.cipd_bin'))
|
|
|
|
|
gomacc_file = ("gomacc.exe"
|
|
|
|
|
if sys.platform.startswith("win") else "gomacc")
|
|
|
|
|
goma_dir = os.environ.get("GOMA_DIR",
|
|
|
|
|
os.path.join(SCRIPT_DIR, ".cipd_bin"))
|
|
|
|
|
gomacc_path = os.path.join(goma_dir, gomacc_file)
|
|
|
|
|
# Don't invoke gomacc if it doesn't exist.
|
|
|
|
|
if os.path.exists(gomacc_path):
|
|
|
|
|
# Check to make sure that goma is running. If not, don't start the
|
|
|
|
|
# build.
|
|
|
|
|
status = subprocess.call([gomacc_path, 'port'],
|
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
|
shell=False)
|
|
|
|
|
status = subprocess.call(
|
|
|
|
|
[gomacc_path, "port"],
|
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
|
shell=False,
|
|
|
|
|
)
|
|
|
|
|
if status == 1:
|
|
|
|
|
print(
|
|
|
|
|
'Goma is not running. Use "goma_ctl ensure_start" to start '
|
|
|
|
|
'it.',
|
|
|
|
|
file=sys.stderr)
|
|
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
|
"it.",
|
|
|
|
|
file=sys.stderr,
|
|
|
|
|
)
|
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
|
# Set an exit code of 1 in the batch file.
|
|
|
|
|
print('cmd "/c exit 1"')
|
|
|
|
|
else:
|
|
|
|
|
# Set an exit code of 1 by executing 'false' in the bash
|
|
|
|
|
# script.
|
|
|
|
|
print('false')
|
|
|
|
|
print("false")
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
# A large build (with or without goma) tends to hog all system resources.
|
|
|
|
|
# Depending on the operating system, we might have mechanisms available
|
|
|
|
|
# to run at a lower priority, which improves this situation.
|
|
|
|
|
if os.environ.get('NINJA_BUILD_IN_BACKGROUND') == '1':
|
|
|
|
|
if sys.platform in ['darwin', 'linux']:
|
|
|
|
|
if os.environ.get("NINJA_BUILD_IN_BACKGROUND") == "1":
|
|
|
|
|
if sys.platform in ["darwin", "linux"]:
|
|
|
|
|
# nice-level 10 is usually considered a good default for background
|
|
|
|
|
# tasks. The niceness is inherited by child processes, so we can
|
|
|
|
|
# just set it here for us and it'll apply to the build tool we
|
|
|
|
@ -255,8 +268,8 @@ def main(args):
|
|
|
|
|
|
|
|
|
|
# Tell goma or reclient to do local compiles.
|
|
|
|
|
if offline:
|
|
|
|
|
os.environ['RBE_remote_disabled'] = '1'
|
|
|
|
|
os.environ['GOMA_DISABLED'] = '1'
|
|
|
|
|
os.environ["RBE_remote_disabled"] = "1"
|
|
|
|
|
os.environ["GOMA_DISABLED"] = "1"
|
|
|
|
|
|
|
|
|
|
# On macOS and most Linux distributions, the default limit of open file
|
|
|
|
|
# descriptors is too low (256 and 1024, respectively).
|
|
|
|
@ -264,7 +277,7 @@ def main(args):
|
|
|
|
|
# Check whether the limit can be raised to a large enough value. If yes,
|
|
|
|
|
# use `ulimit -n .... &&` as a prefix to increase the limit when running
|
|
|
|
|
# ninja.
|
|
|
|
|
if sys.platform in ['darwin', 'linux']:
|
|
|
|
|
if sys.platform in ["darwin", "linux"]:
|
|
|
|
|
# Increase the number of allowed open file descriptors to the maximum.
|
|
|
|
|
fileno_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
|
|
|
|
|
if fileno_limit < hard_limit:
|
|
|
|
@ -278,54 +291,54 @@ def main(args):
|
|
|
|
|
|
|
|
|
|
# Call ninja.py so that it can find ninja binary installed by DEPS or one in
|
|
|
|
|
# PATH.
|
|
|
|
|
ninja_path = os.path.join(SCRIPT_DIR, 'ninja.py')
|
|
|
|
|
ninja_path = os.path.join(SCRIPT_DIR, "ninja.py")
|
|
|
|
|
# If using remoteexec, use ninja_reclient.py which wraps ninja.py with
|
|
|
|
|
# starting and stopping reproxy.
|
|
|
|
|
if use_remoteexec:
|
|
|
|
|
ninja_path = os.path.join(SCRIPT_DIR, 'ninja_reclient.py')
|
|
|
|
|
ninja_path = os.path.join(SCRIPT_DIR, "ninja_reclient.py")
|
|
|
|
|
|
|
|
|
|
args = [sys.executable, ninja_path] + input_args[1:]
|
|
|
|
|
|
|
|
|
|
num_cores = multiprocessing.cpu_count()
|
|
|
|
|
if not j_specified and not t_specified:
|
|
|
|
|
if not offline and (use_goma or use_remoteexec):
|
|
|
|
|
args.append('-j')
|
|
|
|
|
args.append("-j")
|
|
|
|
|
default_core_multiplier = 80
|
|
|
|
|
if platform.machine() in ('x86_64', 'AMD64'):
|
|
|
|
|
if platform.machine() in ("x86_64", "AMD64"):
|
|
|
|
|
# Assume simultaneous multithreading and therefore half as many
|
|
|
|
|
# cores as logical processors.
|
|
|
|
|
num_cores //= 2
|
|
|
|
|
|
|
|
|
|
core_multiplier = int(
|
|
|
|
|
os.environ.get('NINJA_CORE_MULTIPLIER',
|
|
|
|
|
os.environ.get("NINJA_CORE_MULTIPLIER",
|
|
|
|
|
default_core_multiplier))
|
|
|
|
|
j_value = num_cores * core_multiplier
|
|
|
|
|
|
|
|
|
|
core_limit = int(os.environ.get('NINJA_CORE_LIMIT', j_value))
|
|
|
|
|
core_limit = int(os.environ.get("NINJA_CORE_LIMIT", j_value))
|
|
|
|
|
j_value = min(j_value, core_limit)
|
|
|
|
|
|
|
|
|
|
# On Windows, a -j higher than 1000 doesn't improve build times.
|
|
|
|
|
# On macOS, ninja is limited to at most FD_SETSIZE (1024) open file
|
|
|
|
|
# descriptors.
|
|
|
|
|
if sys.platform in ['darwin', 'win32']:
|
|
|
|
|
if sys.platform in ["darwin", "win32"]:
|
|
|
|
|
j_value = min(j_value, 1000)
|
|
|
|
|
|
|
|
|
|
# Use a j value that reliably works with the open file descriptors
|
|
|
|
|
# limit.
|
|
|
|
|
if sys.platform in ['darwin', 'linux']:
|
|
|
|
|
if sys.platform in ["darwin", "linux"]:
|
|
|
|
|
j_value = min(j_value, int(fileno_limit * 0.8))
|
|
|
|
|
|
|
|
|
|
args.append('%d' % j_value)
|
|
|
|
|
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'))
|
|
|
|
|
args.append('-j')
|
|
|
|
|
args.append('%d' % j_value)
|
|
|
|
|
j_value += int(os.environ.get("NINJA_CORE_ADDITION", "2"))
|
|
|
|
|
args.append("-j")
|
|
|
|
|
args.append("%d" % j_value)
|
|
|
|
|
|
|
|
|
|
if summarize_build:
|
|
|
|
|
# Enable statistics collection in Ninja.
|
|
|
|
|
args += ['-d', 'stats']
|
|
|
|
|
args += ["-d", "stats"]
|
|
|
|
|
# Print the command-line to reassure the user that the right settings
|
|
|
|
|
# are being used.
|
|
|
|
|
_print_cmd(args)
|
|
|
|
@ -335,7 +348,7 @@ def main(args):
|
|
|
|
|
return ninja.main(args[1:])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
try:
|
|
|
|
|
sys.exit(main(sys.argv))
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|