Junji Watanabe | 1f67d55 | 2022-11-18 00:53:50 +0000 | [diff] [blame] | 1 | #!/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 |
| 6 | third_party as part of gclient sync. It will automatically find the ninja |
| 7 | binary when run inside a gclient source tree, so users can just type |
| 8 | "ninja" on the command line.""" |
| 9 | |
| 10 | import os |
| 11 | import subprocess |
| 12 | import sys |
| 13 | |
| 14 | import gclient_paths |
| 15 | |
| 16 | DEPOT_TOOLS_ROOT = os.path.abspath(os.path.dirname(__file__)) |
| 17 | |
| 18 | |
| 19 | def 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 | |
| 43 | def 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 | |
| 59 | def 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 | |
| 69 | def main(args): |
| 70 | # Get gclient root + src. |
| 71 | primary_solution_path = gclient_paths.GetPrimarySolutionPath() |
| 72 | if not primary_solution_path: |
| 73 | return fallback(args[1:]) |
| 74 | gclient_root_path = gclient_paths.FindGclientRoot(os.getcwd()) |
| 75 | for base_path in [primary_solution_path, gclient_root_path]: |
| 76 | 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 | |
| 84 | if __name__ == '__main__': |
| 85 | sys.exit(main(sys.argv)) |