blob: 97b51d5a1d051b93f07c35769481e42cbb657267 [file] [log] [blame]
Junji Watanabe1f67d552022-11-18 00:53:50 +00001#!/usr/bin/env python3
2# Copyright 2022 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"""This script is a wrapper around the ninja binary that is pulled to
6third_party as part of gclient sync. It will automatically find the ninja
7binary when run inside a gclient source tree, so users can just type
8"ninja" on the command line."""
9
10import os
11import subprocess
12import sys
13
14import gclient_paths
15
16DEPOT_TOOLS_ROOT = os.path.abspath(os.path.dirname(__file__))
17
18
19def fallbackToLegacyNinja(ninja_args):
20 print(
21 'depot_tools/ninja.py: Fallback to a deprecated legacy ninja binary. '
22 'Note that this ninja binary will be removed soon.\n'
23 'Please install ninja to your project using DEPS. '
24 'If your project does not have DEPS, Please install ninja in your PATH.\n'
25 'See also https://crbug.com/1340825',
26 file=sys.stderr)
27
28 exe_name = ''
29 if sys.platform == 'linux':
30 exe_name = 'ninja-linux64'
31 elif sys.platform == 'darwin':
32 exe_name = 'ninja-mac'
33 elif sys.platform in ['win32', 'cygwin']:
34 exe_name = 'ninja.exe'
35 else:
36 print('depot_tools/ninja.py: %s is not supported platform' % sys.platform)
37 return 1
38
39 ninja_path = os.path.join(DEPOT_TOOLS_ROOT, exe_name)
40 return subprocess.call([ninja_path] + ninja_args)
41
42
43def findNinjaInPath():
44 env_path = os.getenv('PATH')
45 if not env_path:
46 return
47 exe = 'ninja'
48 if sys.platform in ['win32', 'cygwin']:
49 exe += '.exe'
50 for bin_dir in env_path.split(os.pathsep):
51 if bin_dir.rstrip(os.sep).endswith('depot_tools'):
52 # skip depot_tools to avoid calling ninja.py infitely.
53 continue
54 ninja_path = os.path.join(bin_dir, exe)
55 if os.path.isfile(ninja_path):
56 return ninja_path
57
58
59def fallback(ninja_args):
60 # Try to find ninja in PATH.
61 ninja_path = findNinjaInPath()
62 if ninja_path:
63 return subprocess.call([ninja_path] + ninja_args)
64
65 # TODO(crbug.com/1340825): remove raw binaries from depot_tools.
66 return fallbackToLegacyNinja(ninja_args)
67
68
69def main(args):
70 # Get gclient root + src.
71 primary_solution_path = gclient_paths.GetPrimarySolutionPath()
Junji Watanabe1f67d552022-11-18 00:53:50 +000072 gclient_root_path = gclient_paths.FindGclientRoot(os.getcwd())
73 for base_path in [primary_solution_path, gclient_root_path]:
Brian Osmana0cf4322022-11-18 17:17:21 +000074 if not base_path:
75 continue
Junji Watanabe1f67d552022-11-18 00:53:50 +000076 ninja_path = os.path.join(base_path, 'third_party', 'ninja',
77 'ninja' + gclient_paths.GetExeSuffix())
78 if os.path.isfile(ninja_path):
79 return subprocess.call([ninja_path] + args[1:])
80
81 return fallback(args[1:])
82
83
84if __name__ == '__main__':
Junji Watanabe22bbba12022-12-09 02:18:05 +000085 try:
86 sys.exit(main(sys.argv))
87 except KeyboardInterrupt:
88 sys.exit(1)