bazel.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env vpython3
  2. # Copyright 2023 The ChromiumOS Authors
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. # [VPYTHON:BEGIN]
  6. # python_version: "3.8"
  7. # [VPYTHON:END]
  8. """Bazel launcher wrapper.
  9. This script starts Bazel appropriate for the project you're working in. It's
  10. currently used by ChromiumOS, but is intended for use and to be updated by any
  11. depot_tools users who are using Bazel.
  12. In the case this script is not able to detect which project you're working in,
  13. it will fall back to using the next "bazel" executable in your PATH.
  14. """
  15. import itertools
  16. import os
  17. from pathlib import Path
  18. import shutil
  19. import sys
  20. from typing import List, Optional
  21. def _find_bazel_cros() -> Optional[Path]:
  22. """Find the bazel launcher for ChromiumOS."""
  23. cwd = Path.cwd()
  24. for parent in itertools.chain([cwd], cwd.parents):
  25. bazel_launcher = parent / "chromite" / "bin" / "bazel"
  26. if bazel_launcher.exists():
  27. return bazel_launcher
  28. return None
  29. def _find_next_bazel_in_path() -> Optional[Path]:
  30. """The fallback method: search the remainder of PATH for bazel."""
  31. # Remove depot_tools from PATH if present.
  32. depot_tools = Path(__file__).resolve().parent
  33. path_env = os.environ.get("PATH", os.defpath)
  34. search_paths = []
  35. for path in path_env.split(os.pathsep):
  36. if Path(path).resolve() != depot_tools:
  37. search_paths.append(path)
  38. new_path_env = os.pathsep.join(search_paths)
  39. bazel = shutil.which("bazel", path=new_path_env)
  40. if bazel:
  41. return Path(bazel)
  42. return None
  43. # All functions used to search for Bazel (in order of search).
  44. _SEARCH_FUNCTIONS = (
  45. _find_bazel_cros,
  46. _find_next_bazel_in_path,
  47. )
  48. _FIND_FAILURE_MSG = """\
  49. ERROR: The depot_tools bazel launcher was unable to find an appropriate bazel
  50. executable to use.
  51. For ChromiumOS developers:
  52. Make sure your current directory is inside a ChromiumOS checkout (e.g.,
  53. ~/chromiumos). If you're already in a ChromiumOS checkout, it may be because
  54. you're working on a branch that's too old (i.e., prior to Bazel).
  55. If you're not working on any of the above listed projects, this launcher assumes
  56. that you have Bazel installed on your system somewhere else in PATH. Check that
  57. it's actually installed."""
  58. def main(argv: List[str]) -> int:
  59. """Main."""
  60. for search_func in _SEARCH_FUNCTIONS:
  61. bazel = search_func()
  62. if bazel:
  63. os.execv(bazel, [str(bazel), *argv])
  64. print(_FIND_FAILURE_MSG, file=sys.stderr)
  65. return 1
  66. if __name__ == "__main__":
  67. sys.exit(main(sys.argv[1:]))