Переглянути джерело

Add ninja wrappers to trigger DEPS ninja

This is the first step to deprecate ninja binaries in depot_tools.

depot_tools/{ninja, ninja.bat} will call ninja.py, which finds CIPD ninja under src/third_party in the current gclient source tree. https://crrev.com/c/3869740

If it fails to find, it does fallback to an existing ninja binary in depot_tools, until removing the references to the legacy ninja binaries.

# For those who see the deprecation message,
You need to install ninja using DEPS at your project. (https://crrev.com/c/3869740 is an example.)

If your project doesn't have DEPS, you need to install ninja and include it in PATH manually. e.g. sudo apt install ninja-build

Test:
Linux builder: https://ci.chromium.org/swarming/task/5e9cfc917aa3f110?server=chromium-swarm.appspot.com
Windows builder: https://ci.chromium.org/swarming/task/5e9d48885c460b10?server=chromium-swarm.appspot.com

Bug: 1340825
Change-Id: I70d1863d72ddfa65b87a62c9bde8ff63f7641d13
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3925341
Reviewed-by: Nico Weber <thakis@chromium.org>
Commit-Queue: Junji Watanabe <jwata@google.com>
Reviewed-by: Takuto Ikuta <tikuta@chromium.org>
Junji Watanabe 2 роки тому
батько
коміт
1f67d5573f
3 змінених файлів з 99 додано та 34 видалено
  1. 2 34
      ninja
  2. 12 0
      ninja.bat
  3. 85 0
      ninja.py

+ 2 - 34
ninja

@@ -4,37 +4,5 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-OS="$(uname -s)"
-THIS_DIR="$(dirname "${0}")"
-
-function print_help() {
-cat <<-EOF
-No prebuilt ninja binary was found for this system.
-Try building your own binary by doing:
-  cd ~
-  git clone https://github.com/ninja-build/ninja.git -b v1.8.2
-  cd ninja && ./configure.py --bootstrap
-Then add ~/ninja/ to your PATH.
-EOF
-}
-
-case "$OS" in
-  Linux)
-    MACHINE=$(uname -m)
-    case "$MACHINE" in
-      x86_64)
-        exec "${THIS_DIR}/ninja-linux64" "$@";;
-      *)
-        echo Unsupported architecture \($MACHINE\) -- unable to run ninja.
-        print_help
-        exit 1;;
-    esac
-    ;;
-  Darwin)    exec "${THIS_DIR}/ninja-mac" "$@";;
-  CYGWIN*)   exec cmd.exe /c $(cygpath -t windows $0).exe "$@";;
-  MINGW*)    cmd.exe //c $0.exe "$@";;
-  MSYS_NT*)  cmd.exe //c $0.exe "$@";;
-  *)         echo "Unsupported OS ${OS}"
-             print_help
-             exit 1;;
-esac
+base_dir=$(dirname "$0")
+PYTHONDONTWRITEBYTECODE=1 exec python3 "$base_dir/ninja.py" "$@"

+ 12 - 0
ninja.bat

@@ -0,0 +1,12 @@
+@echo off
+:: Copyright 2022 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.
+setlocal
+
+:: Ensure that "depot_tools" is somewhere in PATH so this tool can be used
+:: standalone, but allow other PATH manipulations to take priority.
+set PATH=%PATH%;%~dp0
+
+:: Defer control.
+python3 "%~dp0\ninja.py" %*

+ 85 - 0
ninja.py

@@ -0,0 +1,85 @@
+#!/usr/bin/env python3
+# Copyright 2022 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 is a wrapper around the ninja binary that is pulled to
+third_party as part of gclient sync. It will automatically find the ninja
+binary when run inside a gclient source tree, so users can just type
+"ninja" on the command line."""
+
+import os
+import subprocess
+import sys
+
+import gclient_paths
+
+DEPOT_TOOLS_ROOT = os.path.abspath(os.path.dirname(__file__))
+
+
+def fallbackToLegacyNinja(ninja_args):
+  print(
+      'depot_tools/ninja.py: Fallback to a deprecated legacy ninja binary. '
+      'Note that this ninja binary will be removed soon.\n'
+      'Please install ninja to your project using DEPS. '
+      'If your project does not have DEPS, Please install ninja in your PATH.\n'
+      'See also https://crbug.com/1340825',
+      file=sys.stderr)
+
+  exe_name = ''
+  if sys.platform == 'linux':
+    exe_name = 'ninja-linux64'
+  elif sys.platform == 'darwin':
+    exe_name = 'ninja-mac'
+  elif sys.platform in ['win32', 'cygwin']:
+    exe_name = 'ninja.exe'
+  else:
+    print('depot_tools/ninja.py: %s is not supported platform' % sys.platform)
+    return 1
+
+  ninja_path = os.path.join(DEPOT_TOOLS_ROOT, exe_name)
+  return subprocess.call([ninja_path] + ninja_args)
+
+
+def findNinjaInPath():
+  env_path = os.getenv('PATH')
+  if not env_path:
+    return
+  exe = 'ninja'
+  if sys.platform in ['win32', 'cygwin']:
+    exe += '.exe'
+  for bin_dir in env_path.split(os.pathsep):
+    if bin_dir.rstrip(os.sep).endswith('depot_tools'):
+      # skip depot_tools to avoid calling ninja.py infitely.
+      continue
+    ninja_path = os.path.join(bin_dir, exe)
+    if os.path.isfile(ninja_path):
+      return ninja_path
+
+
+def fallback(ninja_args):
+  # Try to find ninja in PATH.
+  ninja_path = findNinjaInPath()
+  if ninja_path:
+    return subprocess.call([ninja_path] + ninja_args)
+
+  # TODO(crbug.com/1340825): remove raw binaries from depot_tools.
+  return fallbackToLegacyNinja(ninja_args)
+
+
+def main(args):
+  # Get gclient root + src.
+  primary_solution_path = gclient_paths.GetPrimarySolutionPath()
+  if not primary_solution_path:
+    return fallback(args[1:])
+  gclient_root_path = gclient_paths.FindGclientRoot(os.getcwd())
+  for base_path in [primary_solution_path, gclient_root_path]:
+    ninja_path = os.path.join(base_path, 'third_party', 'ninja',
+                              'ninja' + gclient_paths.GetExeSuffix())
+    if os.path.isfile(ninja_path):
+      return subprocess.call([ninja_path] + args[1:])
+
+  return fallback(args[1:])
+
+
+if __name__ == '__main__':
+  sys.exit(main(sys.argv))