Support bazelisk or bazel in tools/eclipse/project.py

It is recommended to use bazelisk instead of bazel directly, however
it is also perfectly reasonable to use bazel directly. Update the
eclipse script to default to looking for bazelisk then bazel, but to
continue to allow explicit overrides. This should allow people who
do not have bazelisk to not be broken, but people who have installed
bazelisk to have it be the program run by default.

This also causes whichever of the two was found to to go into
.bazel_path which will influence what eclipse uses.

Based on the change from Monty Taylor [1] in core Gerrit.

[1] https://gerrit-review.googlesource.com/c/gerrit/+/234727

Bug: Issue 11359
Change-Id: I2490e479d16bdad64c771a38e6cce73decb062a6
diff --git a/tools/eclipse/project.py b/tools/eclipse/project.py
index c7789ff..9abfbef 100755
--- a/tools/eclipse/project.py
+++ b/tools/eclipse/project.py
@@ -34,6 +34,11 @@
 opts.add_argument('-x', '--exclude', action='append', help='Exclude paths')
 opts.add_argument('-b', '--batch', action='store_true',
                   dest='batch', help='Bazel batch option')
+opts.add_argument('--bazel',
+                  help=('name of the bazel executable. Defaults to using'
+                        ' bazelisk if found, or bazel if bazelisk is not'
+                        ' found.'),
+                  action='store', default=None, dest='bazel_exe')
 args = opts.parse_args()
 
 if not args.root:
@@ -47,8 +52,31 @@
 
 batch_option = '--batch' if args.batch else None
 
+def find_bazel():
+  if args.bazel_exe:
+    try:
+      return subprocess.check_output(
+        ['which', args.bazel_exe]).strip().decode('UTF-8')
+    except subprocess.CalledProcessError:
+      print('Bazel command: %s not found' % args.bazel_exe, file=sys.stderr)
+      sys.exit(1)
+  try:
+    return subprocess.check_output(
+      ['which', 'bazelisk']).strip().decode('UTF-8')
+  except subprocess.CalledProcessError:
+    try:
+      return subprocess.check_output(
+        ['which', 'bazel']).strip().decode('UTF-8')
+    except subprocess.CalledProcessError:
+      print("Neither bazelisk nor bazel found. Please see"
+            " Documentation/dev-bazel for instructions on installing"
+            " one of them.")
+      sys.exit(1)
+
+bazel_exe = find_bazel()
+
 def _build_bazel_cmd(*args):
-  cmd = ['bazel']
+  cmd = [bazel_exe]
   if batch_option:
     cmd.append('--batch')
   for arg in args: