Skip to content

Commit c5cde65

Browse files
mehrdadnweb-flow
andauthored
Add bazel to the PATH in setup.py (#9590)
Co-authored-by: Mehrdad <noreply@github.com>
1 parent 4a36f72 commit c5cde65

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

ci/travis/install-bazel.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE:-$0}")"; pwd)
66

77
arg1="${1-}"
88

9-
version="3.2.0"
109
achitecture="${HOSTTYPE}"
1110
platform="unknown"
1211
case "${OSTYPE}" in
@@ -30,7 +29,8 @@ esac
3029

3130
# Sanity check: Verify we have symlinks where we expect them, or Bazel can produce weird "missing input file" errors.
3231
# This is most likely to occur on Windows, where symlinks are sometimes disabled by default.
33-
{ git ls-files -s || true; } | {
32+
{ git ls-files -s 2>/dev/null || true; } | (
33+
set +x
3434
missing_symlinks=()
3535
while read -r mode digest sn path; do
3636
if [ "${mode}" = 120000 ]; then
@@ -42,8 +42,10 @@ esac
4242
echo "For a correct build, please run 'git config --local core.symlinks true' and re-run git checkout." 1>&2
4343
false
4444
fi
45-
}
45+
)
4646

47+
python="$(command -v python3 || command -v python || echo python)"
48+
version="$("${python}" -s -c "import runpy, sys; runpy.run_path(sys.argv.pop(), run_name='__api__')" bazel_version "${ROOT_DIR}/../../python/setup.py")"
4749
if [ "${OSTYPE}" = "msys" ]; then
4850
target="${MINGW_DIR-/usr}/bin/bazel.exe"
4951
mkdir -p "${target%/*}"

doc/source/development.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Ray can be built from the repository as follows.
8080
git clone https://github.com/ray-project/ray.git
8181
8282
# Install Bazel.
83+
# (Windows users: please manually place Bazel in your PATH, and point BAZEL_SH to MSYS2's Bash.)
8384
ray/ci/travis/install-bazel.sh
8485
8586
# Optionally build the dashboard

python/setup.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import glob
33
import io
4+
import logging
45
import os
56
import re
67
import shutil
@@ -16,13 +17,16 @@
1617
import urllib.parse
1718
import urllib.request
1819

20+
logger = logging.getLogger(__name__)
21+
1922
# Ideally, we could include these files by putting them in a
2023
# MANIFEST.in or using the package_data argument to setup, but the
2124
# MANIFEST.in gets applied at the very beginning when setup.py runs
2225
# before these files have been created, so we have to move the files
2326
# manually.
2427

2528
SUPPORTED_PYTHONS = [(3, 5), (3, 6), (3, 7), (3, 8)]
29+
SUPPORTED_BAZEL = (3, 2, 0)
2630

2731
ROOT_DIR = os.path.dirname(__file__)
2832
BUILD_JAVA = os.getenv("RAY_INSTALL_JAVA") == "1"
@@ -129,6 +133,28 @@ def is_invalid_windows_platform():
129133
return platform == "msys" or (platform == "win32" and ver and "GCC" in ver)
130134

131135

136+
# Calls Bazel in PATH, falling back to the standard user installatation path
137+
# (~/.bazel/bin/bazel) if it isn't found.
138+
def bazel_invoke(invoker, cmdline, *args, **kwargs):
139+
home = os.path.expanduser("~")
140+
candidates = ["bazel"]
141+
if sys.platform == "win32":
142+
mingw_dir = os.getenv("MINGW_DIR")
143+
if mingw_dir:
144+
candidates.append(mingw_dir + "/bin/bazel.exe")
145+
else:
146+
candidates.append(os.path.join(home, ".bazel", "bin", "bazel"))
147+
result = None
148+
for i, cmd in enumerate(candidates):
149+
try:
150+
result = invoker([cmd] + cmdline, *args, **kwargs)
151+
break
152+
except IOError:
153+
if i >= len(candidates) - 1:
154+
raise
155+
return result
156+
157+
132158
def download(url):
133159
try:
134160
result = urllib.request.urlopen(url).read()
@@ -221,12 +247,19 @@ def build(build_python, build_java):
221247
] + pip_packages,
222248
env=dict(os.environ, CC="gcc"))
223249

224-
bazel = os.getenv("BAZEL_EXECUTABLE", "bazel")
250+
version_info = bazel_invoke(subprocess.check_output, ["--version"])
251+
bazel_version_str = version_info.rstrip().decode("utf-8").split(" ", 1)[1]
252+
bazel_version = tuple(map(int, bazel_version_str.split(".")))
253+
if bazel_version <= SUPPORTED_BAZEL:
254+
logger.warning("Expected Bazel version {} but found {}",
255+
bazel_version, SUPPORTED_BAZEL)
256+
225257
bazel_targets = []
226258
bazel_targets += ["//:ray_pkg"] if build_python else []
227259
bazel_targets += ["//java:ray_java_pkg"] if build_java else []
228-
return subprocess.check_call(
229-
[bazel, "build", "--verbose_failures", "--"] + bazel_targets,
260+
return bazel_invoke(
261+
subprocess.check_call,
262+
["build", "--verbose_failures", "--"] + bazel_targets,
230263
env=bazel_env)
231264

232265

@@ -318,7 +351,8 @@ def pip_run(build_ext):
318351

319352
def api_main(program, *args):
320353
parser = argparse.ArgumentParser()
321-
parser.add_argument("command", type=str, choices=["build", "help"])
354+
choices = ["build", "bazel_version", "help"]
355+
parser.add_argument("command", type=str, choices=choices)
322356
parser.add_argument(
323357
"-l",
324358
"--language",
@@ -341,6 +375,8 @@ def api_main(program, *args):
341375
else:
342376
raise ValueError("invalid language: {!r}".format(lang))
343377
result = build(**kwargs)
378+
elif parsed_args.command == "bazel_version":
379+
print(".".join(map(str, SUPPORTED_BAZEL)))
344380
elif parsed_args.command == "help":
345381
parser.print_help()
346382
else:

0 commit comments

Comments
 (0)