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 | |
Junji Watanabe | 1f67d55 | 2022-11-18 00:53:50 +0000 | [diff] [blame] | 16 | |
| 17 | def findNinjaInPath(): |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 18 | env_path = os.getenv("PATH") |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 19 | if not env_path: |
| 20 | return |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 21 | exe = "ninja" |
| 22 | if sys.platform in ("win32", "cygwin"): |
| 23 | exe += ".exe" |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 24 | for bin_dir in env_path.split(os.pathsep): |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 25 | if bin_dir.rstrip(os.sep).endswith("depot_tools"): |
Junji Watanabe | c550588 | 2023-09-25 01:32:45 +0000 | [diff] [blame] | 26 | # skip depot_tools to avoid calling ninja.py infinitely. |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 27 | continue |
| 28 | ninja_path = os.path.join(bin_dir, exe) |
| 29 | if os.path.isfile(ninja_path): |
| 30 | return ninja_path |
Junji Watanabe | 1f67d55 | 2022-11-18 00:53:50 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | def fallback(ninja_args): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 34 | # Try to find ninja in PATH. |
| 35 | ninja_path = findNinjaInPath() |
| 36 | if ninja_path: |
| 37 | return subprocess.call([ninja_path] + ninja_args) |
Junji Watanabe | 1f67d55 | 2022-11-18 00:53:50 +0000 | [diff] [blame] | 38 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 39 | print( |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 40 | "depot_tools/ninja.py: Could not find Ninja in the third_party of " |
| 41 | "the current project, nor in your PATH.\n" |
| 42 | "Please take one of the following actions to install Ninja:\n" |
| 43 | "- If your project has DEPS, add a CIPD Ninja dependency to DEPS.\n" |
| 44 | "- Otherwise, add Ninja to your PATH *after* depot_tools.", |
| 45 | file=sys.stderr, |
| 46 | ) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 47 | return 1 |
Junji Watanabe | 1f67d55 | 2022-11-18 00:53:50 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | def main(args): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 51 | # On Windows the ninja.bat script passes along the arguments enclosed in |
| 52 | # double quotes. This prevents multiple levels of parsing of the special '^' |
| 53 | # characters needed when compiling a single file. When this case is |
| 54 | # detected, we need to split the argument. This means that arguments |
| 55 | # containing actual spaces are not supported by ninja.bat, but that is not a |
| 56 | # real limitation. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 57 | if sys.platform.startswith("win") and len(args) == 2: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 58 | args = args[:1] + args[1].split() |
Junji Watanabe | 2b1aa8d | 2023-01-10 02:20:13 +0000 | [diff] [blame] | 59 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 60 | # macOS's python sets CPATH, LIBRARY_PATH, SDKROOT implicitly. |
| 61 | # https://openradar.appspot.com/radar?id=5608755232243712 |
| 62 | # |
| 63 | # Removing those environment variables to avoid affecting clang's behaviors. |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 64 | if sys.platform == "darwin": |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 65 | os.environ.pop("CPATH", None) |
| 66 | os.environ.pop("LIBRARY_PATH", None) |
| 67 | os.environ.pop("SDKROOT", None) |
Junji Watanabe | 3e7206f | 2023-01-11 02:33:26 +0000 | [diff] [blame] | 68 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 69 | # Get gclient root + src. |
| 70 | primary_solution_path = gclient_paths.GetPrimarySolutionPath() |
| 71 | gclient_root_path = gclient_paths.FindGclientRoot(os.getcwd()) |
| 72 | gclient_src_root_path = None |
| 73 | if gclient_root_path: |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 74 | gclient_src_root_path = os.path.join(gclient_root_path, "src") |
Shelley Vohr | 106754b | 2023-02-01 11:59:40 +0000 | [diff] [blame] | 75 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 76 | for base_path in set( |
| 77 | [primary_solution_path, gclient_root_path, gclient_src_root_path]): |
| 78 | if not base_path: |
| 79 | continue |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 80 | ninja_path = os.path.join( |
| 81 | base_path, |
| 82 | "third_party", |
| 83 | "ninja", |
| 84 | "ninja" + gclient_paths.GetExeSuffix(), |
| 85 | ) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 86 | if os.path.isfile(ninja_path): |
| 87 | return subprocess.call([ninja_path] + args[1:]) |
Junji Watanabe | 1f67d55 | 2022-11-18 00:53:50 +0000 | [diff] [blame] | 88 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 89 | return fallback(args[1:]) |
Junji Watanabe | 1f67d55 | 2022-11-18 00:53:50 +0000 | [diff] [blame] | 90 | |
| 91 | |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 92 | if __name__ == "__main__": |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 93 | try: |
| 94 | sys.exit(main(sys.argv)) |
| 95 | except KeyboardInterrupt: |
| 96 | sys.exit(1) |