setup.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. import argparse
  2. import errno
  3. import io
  4. import logging
  5. import os
  6. import pathlib
  7. import re
  8. import shlex
  9. import shutil
  10. import subprocess
  11. import sys
  12. import urllib.error
  13. import urllib.parse
  14. import urllib.request
  15. import warnings
  16. from enum import Enum
  17. from itertools import chain
  18. # Workaround for setuptools_scm (used on macos) adding junk files
  19. # https://stackoverflow.com/a/61274968/8162137
  20. try:
  21. import setuptools_scm.integration
  22. setuptools_scm.integration.find_files = lambda _: []
  23. except ImportError:
  24. pass
  25. logger = logging.getLogger(__name__)
  26. SUPPORTED_PYTHONS = [(3, 9), (3, 10), (3, 11), (3, 12)]
  27. # When the bazel version is updated, make sure to update it
  28. # in WORKSPACE file as well.
  29. ROOT_DIR = os.path.dirname(__file__)
  30. BUILD_JAVA = os.getenv("RAY_INSTALL_JAVA") == "1"
  31. SKIP_BAZEL_BUILD = os.getenv("SKIP_BAZEL_BUILD") == "1"
  32. BAZEL_ARGS = os.getenv("BAZEL_ARGS")
  33. BAZEL_LIMIT_CPUS = os.getenv("BAZEL_LIMIT_CPUS")
  34. THIRDPARTY_SUBDIR = os.path.join("ray", "thirdparty_files")
  35. RUNTIME_ENV_AGENT_THIRDPARTY_SUBDIR = os.path.join(
  36. "ray", "_private", "runtime_env", "agent", "thirdparty_files"
  37. )
  38. CLEANABLE_SUBDIRS = [
  39. THIRDPARTY_SUBDIR,
  40. RUNTIME_ENV_AGENT_THIRDPARTY_SUBDIR,
  41. ]
  42. # In automated builds, we do a few adjustments before building. For instance,
  43. # the bazel environment is set up slightly differently, and symlinks are
  44. # replaced with junctions in Windows. This variable is set e.g. in our conda-forge
  45. # feedstock.
  46. is_automated_build = bool(int(os.environ.get("IS_AUTOMATED_BUILD", "0")))
  47. exe_suffix = ".exe" if sys.platform == "win32" else ""
  48. # .pyd is the extension Python requires on Windows for shared libraries.
  49. # https://docs.python.org/3/faq/windows.html#is-a-pyd-file-the-same-as-a-dll
  50. pyd_suffix = ".pyd" if sys.platform == "win32" else ".so"
  51. def find_version(*filepath):
  52. # Extract version information from filepath
  53. with open(os.path.join(ROOT_DIR, *filepath)) as fp:
  54. version_match = re.search(r"^version = ['\"]([^'\"]*)['\"]", fp.read(), re.M)
  55. if version_match:
  56. return version_match.group(1)
  57. raise RuntimeError("Unable to find version string.")
  58. class SetupType(Enum):
  59. RAY = 1
  60. RAY_CPP = 2
  61. class BuildType(Enum):
  62. DEFAULT = 1
  63. DEBUG = 2
  64. ASAN = 3
  65. TSAN = 4
  66. class SetupSpec:
  67. def __init__(
  68. self, type: SetupType, name: str, description: str, build_type: BuildType
  69. ):
  70. self.type: SetupType = type
  71. self.name: str = name
  72. version = find_version("ray", "_version.py")
  73. # add .dbg suffix if debug mode is on.
  74. if build_type == BuildType.DEBUG:
  75. self.version: str = f"{version}+dbg"
  76. elif build_type == BuildType.ASAN:
  77. self.version: str = f"{version}+asan"
  78. elif build_type == BuildType.TSAN:
  79. self.version: str = f"{version}+tsan"
  80. else:
  81. self.version = version
  82. self.description: str = description
  83. self.build_type: BuildType = build_type
  84. self.files_to_include: list = []
  85. self.install_requires: list = []
  86. self.extras: dict = {}
  87. def get_packages(self):
  88. if self.type == SetupType.RAY:
  89. return setuptools.find_packages(exclude=("tests", "*.tests", "*.tests.*"))
  90. else:
  91. return []
  92. build_type = os.getenv("RAY_DEBUG_BUILD")
  93. if build_type == "debug":
  94. BUILD_TYPE = BuildType.DEBUG
  95. elif build_type == "asan":
  96. BUILD_TYPE = BuildType.ASAN
  97. elif build_type == "tsan":
  98. BUILD_TYPE = BuildType.TSAN
  99. else:
  100. BUILD_TYPE = BuildType.DEFAULT
  101. if os.getenv("RAY_INSTALL_CPP") == "1":
  102. # "ray-cpp" wheel package.
  103. setup_spec = SetupSpec(
  104. SetupType.RAY_CPP,
  105. "ray-cpp",
  106. "A subpackage of Ray which provides the Ray C++ API.",
  107. BUILD_TYPE,
  108. )
  109. else:
  110. # "ray" primary wheel package.
  111. setup_spec = SetupSpec(
  112. SetupType.RAY,
  113. "ray",
  114. "Ray provides a simple, "
  115. "universal API for building distributed applications.",
  116. BUILD_TYPE,
  117. )
  118. RAY_EXTRA_CPP = True
  119. # Disable extra cpp for the development versions.
  120. if "dev" in setup_spec.version or os.getenv("RAY_DISABLE_EXTRA_CPP") == "1":
  121. RAY_EXTRA_CPP = False
  122. # Ideally, we could include these files by putting them in a
  123. # MANIFEST.in or using the package_data argument to setup, but the
  124. # MANIFEST.in gets applied at the very beginning when setup.py runs
  125. # before these files have been created, so we have to move the files
  126. # manually.
  127. # NOTE: The lists below must be kept in sync with ray/BUILD.bazel.
  128. ray_files = [
  129. "ray/_raylet" + pyd_suffix,
  130. "ray/core/src/ray/gcs/gcs_server" + exe_suffix,
  131. "ray/core/src/ray/raylet/raylet" + exe_suffix,
  132. ]
  133. if sys.platform == "linux":
  134. ray_files.append("ray/core/libjemalloc.so")
  135. if BUILD_JAVA or os.path.exists(os.path.join(ROOT_DIR, "ray/jars/ray_dist.jar")):
  136. ray_files.append("ray/jars/ray_dist.jar")
  137. if setup_spec.type == SetupType.RAY_CPP:
  138. setup_spec.files_to_include += ["ray/cpp/default_worker" + exe_suffix]
  139. # C++ API library and project template files.
  140. setup_spec.files_to_include += [
  141. os.path.join(dirpath, filename)
  142. for dirpath, dirnames, filenames in os.walk("ray/cpp")
  143. for filename in filenames
  144. ]
  145. # These are the directories where automatically generated Python protobuf
  146. # bindings are created.
  147. generated_python_directories = [
  148. "ray/core/generated",
  149. "ray/serve/generated",
  150. ]
  151. ray_files.append("ray/nightly-wheels.yaml")
  152. # Autoscaler files.
  153. ray_files += [
  154. "ray/autoscaler/aws/defaults.yaml",
  155. "ray/autoscaler/aws/cloudwatch/prometheus.yml",
  156. "ray/autoscaler/aws/cloudwatch/ray_prometheus_waiter.sh",
  157. "ray/autoscaler/azure/defaults.yaml",
  158. "ray/autoscaler/spark/defaults.yaml",
  159. "ray/autoscaler/_private/_azure/azure-vm-template.json",
  160. "ray/autoscaler/_private/_azure/azure-config-template.json",
  161. "ray/autoscaler/gcp/defaults.yaml",
  162. "ray/autoscaler/local/defaults.yaml",
  163. "ray/autoscaler/vsphere/defaults.yaml",
  164. "ray/autoscaler/ray-schema.json",
  165. ]
  166. # Dashboard files.
  167. ray_files += [
  168. os.path.join(dirpath, filename)
  169. for dirpath, dirnames, filenames in os.walk("ray/dashboard/client/build")
  170. for filename in filenames
  171. ]
  172. # Dashboard metrics files.
  173. ray_files += [
  174. os.path.join(dirpath, filename)
  175. for dirpath, dirnames, filenames in os.walk("ray/dashboard/modules/metrics/export")
  176. for filename in filenames
  177. ]
  178. ray_files += [
  179. os.path.join(dirpath, filename)
  180. for dirpath, dirnames, filenames in os.walk(
  181. "ray/dashboard/modules/metrics/dashboards"
  182. )
  183. for filename in filenames
  184. if filename.endswith(".json")
  185. ]
  186. # html templates for notebook integration
  187. ray_files += [
  188. p.as_posix() for p in pathlib.Path("ray/widgets/templates/").glob("*.html.j2")
  189. ]
  190. # If you're adding dependencies for ray extras, please
  191. # also update the matching section of requirements/requirements.txt
  192. # in this directory
  193. if setup_spec.type == SetupType.RAY:
  194. pandas_dep = "pandas >= 1.3"
  195. numpy_dep = "numpy >= 1.20"
  196. pyarrow_dep = "pyarrow >= 6.0.1"
  197. setup_spec.extras = {
  198. "data": [
  199. numpy_dep,
  200. pandas_dep,
  201. pyarrow_dep,
  202. "fsspec",
  203. ],
  204. "default": [
  205. # If adding dependencies necessary to launch the dashboard api server,
  206. # please add it to dashboard/optional_deps.py as well.
  207. "aiohttp >= 3.7",
  208. "aiohttp_cors",
  209. "colorful",
  210. "py-spy >= 0.2.0",
  211. "requests",
  212. "grpcio >= 1.32.0; python_version < '3.10'", # noqa:E501
  213. "grpcio >= 1.42.0; python_version >= '3.10'", # noqa:E501
  214. "opencensus",
  215. "pydantic!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,<3",
  216. "prometheus_client >= 0.7.1",
  217. "smart_open",
  218. "virtualenv >=20.0.24, !=20.21.1", # For pip runtime env.
  219. "memray; sys_platform != 'win32'",
  220. ],
  221. "client": [
  222. # The Ray client needs a specific range of gRPC to work:
  223. # Tracking issues: https://github.com/grpc/grpc/issues/33714
  224. "grpcio != 1.56.0"
  225. if sys.platform == "darwin"
  226. else "grpcio",
  227. ],
  228. "serve": [
  229. "uvicorn[standard]",
  230. "requests",
  231. "starlette",
  232. "fastapi",
  233. "watchfiles",
  234. ],
  235. "tune": ["pandas", "tensorboardX>=1.9", "requests", pyarrow_dep, "fsspec"],
  236. "observability": [
  237. "opentelemetry-api",
  238. "opentelemetry-sdk",
  239. "opentelemetry-exporter-otlp",
  240. ],
  241. }
  242. # Ray Serve depends on the Ray dashboard components.
  243. setup_spec.extras["serve"] = list(
  244. set(setup_spec.extras["serve"] + setup_spec.extras["default"])
  245. )
  246. # Ensure gRPC library exists for Ray Serve gRPC support.
  247. setup_spec.extras["serve-grpc"] = list(
  248. set(
  249. setup_spec.extras["serve"]
  250. + [
  251. "grpcio >= 1.32.0; python_version < '3.10'", # noqa:E501
  252. "grpcio >= 1.42.0; python_version >= '3.10'", # noqa:E501
  253. ]
  254. )
  255. )
  256. if RAY_EXTRA_CPP:
  257. setup_spec.extras["cpp"] = ["ray-cpp==" + setup_spec.version]
  258. setup_spec.extras["rllib"] = setup_spec.extras["tune"] + [
  259. "dm_tree",
  260. "gymnasium==0.28.1",
  261. "lz4",
  262. "scikit-image",
  263. "pyyaml",
  264. "scipy",
  265. "typer",
  266. "rich",
  267. ]
  268. setup_spec.extras["train"] = setup_spec.extras["tune"]
  269. # Ray AI Runtime should encompass Data, Tune, and Serve.
  270. setup_spec.extras["air"] = list(
  271. set(
  272. setup_spec.extras["tune"]
  273. + setup_spec.extras["data"]
  274. + setup_spec.extras["train"]
  275. + setup_spec.extras["serve"]
  276. )
  277. )
  278. setup_spec.extras["all"] = list(
  279. set(chain.from_iterable(setup_spec.extras.values()))
  280. )
  281. # These are the main dependencies for users of ray. This list
  282. # should be carefully curated. If you change it, please reflect
  283. # the change in the matching section of requirements/requirements.txt
  284. #
  285. # NOTE: if you add any unbounded dependency, please also update
  286. # install-core-prerelease-dependencies.sh so we can test
  287. # new releases candidates.
  288. if setup_spec.type == SetupType.RAY:
  289. setup_spec.install_requires = [
  290. "click >= 7.0",
  291. "filelock",
  292. "jsonschema",
  293. "msgpack >= 1.0.0, < 2.0.0",
  294. "packaging",
  295. "protobuf >= 3.15.3, != 3.19.5",
  296. "pyyaml",
  297. "aiosignal",
  298. "frozenlist",
  299. "requests",
  300. ]
  301. def is_native_windows_or_msys():
  302. """Check to see if we are running on native Windows,
  303. but NOT WSL (which is seen as Linux)."""
  304. return sys.platform == "msys" or sys.platform == "win32"
  305. def is_invalid_windows_platform():
  306. # 'GCC' check is how you detect MinGW:
  307. # https://github.com/msys2/MINGW-packages/blob/abd06ca92d876b9db05dd65f27d71c4ebe2673a9/mingw-w64-python2/0410-MINGW-build-extensions-with-GCC.patch#L53
  308. platform = sys.platform
  309. ver = sys.version
  310. return platform == "msys" or (platform == "win32" and ver and "GCC" in ver)
  311. # Calls Bazel in PATH, falling back to the standard user installation path
  312. # (~/bin/bazel) if it isn't found.
  313. def bazel_invoke(invoker, cmdline, *args, **kwargs):
  314. home = os.path.expanduser("~")
  315. first_candidate = os.getenv("BAZEL_PATH", "bazel")
  316. candidates = [first_candidate]
  317. if sys.platform == "win32":
  318. mingw_dir = os.getenv("MINGW_DIR")
  319. if mingw_dir:
  320. candidates.append(mingw_dir + "/bin/bazel.exe")
  321. else:
  322. candidates.append(os.path.join(home, "bin", "bazel"))
  323. result = None
  324. for i, cmd in enumerate(candidates):
  325. try:
  326. result = invoker([cmd] + cmdline, *args, **kwargs)
  327. break
  328. except IOError:
  329. if i >= len(candidates) - 1:
  330. raise
  331. return result
  332. def download(url):
  333. try:
  334. result = urllib.request.urlopen(url).read()
  335. except urllib.error.URLError:
  336. # This fallback is necessary on Python 3.5 on macOS due to TLS 1.2.
  337. curl_args = ["curl", "-s", "-L", "-f", "-o", "-", url]
  338. result = subprocess.check_output(curl_args)
  339. return result
  340. def patch_isdir():
  341. """
  342. Python on Windows is having hard times at telling if a symlink is
  343. a directory - it can "guess" wrong at times, which bites when
  344. finding packages. Replace with a fixed version which unwraps links first.
  345. """
  346. orig_isdir = os.path.isdir
  347. def fixed_isdir(path):
  348. while os.path.islink(path):
  349. try:
  350. link = os.readlink(path)
  351. except OSError:
  352. break
  353. path = os.path.abspath(os.path.join(os.path.dirname(path), link))
  354. return orig_isdir(path)
  355. os.path.isdir = fixed_isdir
  356. def replace_symlinks_with_junctions():
  357. """
  358. Per default Windows requires admin access to create symlinks, while
  359. junctions (which behave similarly) can be created by users.
  360. This function replaces symlinks (which might be broken when checked
  361. out without admin rights) with junctions so Ray can be built both
  362. with and without admin access.
  363. """
  364. assert is_native_windows_or_msys()
  365. # Update this list if new symlinks are introduced to the source tree
  366. _LINKS = {
  367. r"ray\rllib": "../../rllib",
  368. }
  369. root_dir = os.path.dirname(__file__)
  370. for link, default in _LINKS.items():
  371. path = os.path.join(root_dir, link)
  372. try:
  373. out = subprocess.check_output(
  374. "DIR /A:LD /B", shell=True, cwd=os.path.dirname(path)
  375. )
  376. except subprocess.CalledProcessError:
  377. out = b""
  378. if os.path.basename(path) in out.decode("utf8").splitlines():
  379. logger.info(f"'{link}' is already converted to junction point")
  380. else:
  381. logger.info(f"Converting '{link}' to junction point...")
  382. if os.path.isfile(path):
  383. with open(path) as inp:
  384. target = inp.read()
  385. os.unlink(path)
  386. elif os.path.isdir(path):
  387. target = default
  388. try:
  389. # unlink() works on links as well as on regular files,
  390. # and links to directories are considered directories now
  391. os.unlink(path)
  392. except OSError as err:
  393. # On Windows attempt to unlink a regular directory results
  394. # in a PermissionError with errno set to errno.EACCES.
  395. if err.errno != errno.EACCES:
  396. raise
  397. # For regular directories deletion is done with rmdir call.
  398. os.rmdir(path)
  399. else:
  400. raise ValueError(f"Unexpected type of entry: '{path}'")
  401. target = os.path.abspath(os.path.join(os.path.dirname(path), target))
  402. logger.info("Setting {} -> {}".format(link, target))
  403. subprocess.check_call(
  404. f'MKLINK /J "{os.path.basename(link)}" "{target}"',
  405. shell=True,
  406. cwd=os.path.dirname(path),
  407. )
  408. if is_automated_build and is_native_windows_or_msys():
  409. # Automated replacements should only happen in automatic build
  410. # contexts for now
  411. patch_isdir()
  412. replace_symlinks_with_junctions()
  413. def build(build_python, build_java, build_cpp):
  414. if tuple(sys.version_info[:2]) not in SUPPORTED_PYTHONS:
  415. msg = (
  416. "Detected Python version {}, which is not supported. "
  417. "Only Python {} are supported."
  418. ).format(
  419. ".".join(map(str, sys.version_info[:2])),
  420. ", ".join(".".join(map(str, v)) for v in SUPPORTED_PYTHONS),
  421. )
  422. raise RuntimeError(msg)
  423. if is_invalid_windows_platform():
  424. msg = (
  425. "Please use official native CPython on Windows,"
  426. " not Cygwin/MSYS/MSYS2/MinGW/etc.\n"
  427. + "Detected: {}\n at: {!r}".format(sys.version, sys.executable)
  428. )
  429. raise OSError(msg)
  430. bazel_env = dict(os.environ, PYTHON3_BIN_PATH=sys.executable)
  431. if is_native_windows_or_msys():
  432. SHELL = bazel_env.get("SHELL")
  433. if SHELL:
  434. bazel_env.setdefault("BAZEL_SH", os.path.normpath(SHELL))
  435. BAZEL_SH = bazel_env.get("BAZEL_SH", "")
  436. SYSTEMROOT = os.getenv("SystemRoot")
  437. wsl_bash = os.path.join(SYSTEMROOT, "System32", "bash.exe")
  438. if (not BAZEL_SH) and SYSTEMROOT and os.path.isfile(wsl_bash):
  439. msg = (
  440. "You appear to have Bash from WSL,"
  441. " which Bazel may invoke unexpectedly. "
  442. "To avoid potential problems,"
  443. " please explicitly set the {name!r}"
  444. " environment variable for Bazel."
  445. ).format(name="BAZEL_SH")
  446. raise RuntimeError(msg)
  447. # Note: We are passing in sys.executable so that we use the same
  448. # version of Python to build packages inside the build.sh script. Note
  449. # that certain flags will not be passed along such as --user or sudo.
  450. # TODO(rkn): Fix this.
  451. if not os.getenv("SKIP_THIRDPARTY_INSTALL"):
  452. pip_packages = ["psutil", "setproctitle==1.2.2", "colorama"]
  453. subprocess.check_call(
  454. [
  455. sys.executable,
  456. "-m",
  457. "pip",
  458. "install",
  459. "-q",
  460. "--target=" + os.path.join(ROOT_DIR, THIRDPARTY_SUBDIR),
  461. ]
  462. + pip_packages,
  463. env=dict(os.environ, CC="gcc"),
  464. )
  465. # runtime env agent dependenceis
  466. runtime_env_agent_pip_packages = ["aiohttp"]
  467. subprocess.check_call(
  468. [
  469. sys.executable,
  470. "-m",
  471. "pip",
  472. "install",
  473. "-q",
  474. "--target=" + os.path.join(ROOT_DIR, RUNTIME_ENV_AGENT_THIRDPARTY_SUBDIR),
  475. ]
  476. + runtime_env_agent_pip_packages
  477. )
  478. bazel_flags = ["--verbose_failures"]
  479. if BAZEL_ARGS:
  480. bazel_flags.extend(shlex.split(BAZEL_ARGS))
  481. if BAZEL_LIMIT_CPUS:
  482. n = int(BAZEL_LIMIT_CPUS) # the value must be an int
  483. bazel_flags.append(f"--local_cpu_resources={n}")
  484. warnings.warn(
  485. "Setting BAZEL_LIMIT_CPUS is deprecated and will be removed in a future"
  486. " version. Please use BAZEL_ARGS instead.",
  487. FutureWarning,
  488. )
  489. if is_automated_build:
  490. src_dir = os.environ.get("SRC_DIR", False) or os.getcwd()
  491. src_dir = os.path.abspath(src_dir)
  492. if is_native_windows_or_msys():
  493. drive = os.path.splitdrive(src_dir)[0] + "\\"
  494. root_dir = os.path.join(drive, "bazel-root")
  495. out_dir = os.path.join(drive, "b-o")
  496. bazel_flags.append("--enable_runfiles=false")
  497. else:
  498. root_dir = os.path.join(src_dir, "..", "bazel-root")
  499. out_dir = os.path.join(src_dir, "..", "b-o")
  500. for d in (root_dir, out_dir):
  501. if not os.path.exists(d):
  502. os.makedirs(d)
  503. bazel_precmd_flags = [
  504. "--output_user_root=" + root_dir,
  505. "--output_base=" + out_dir,
  506. ]
  507. else:
  508. bazel_precmd_flags = []
  509. bazel_targets = []
  510. bazel_targets += ["//:ray_pkg"] if build_python else []
  511. bazel_targets += ["//cpp:ray_cpp_pkg"] if build_cpp else []
  512. bazel_targets += ["//java:ray_java_pkg"] if build_java else []
  513. if setup_spec.build_type == BuildType.DEBUG:
  514. bazel_flags.extend(["--config", "debug"])
  515. if setup_spec.build_type == BuildType.ASAN:
  516. bazel_flags.extend(["--config=asan-build"])
  517. if setup_spec.build_type == BuildType.TSAN:
  518. bazel_flags.extend(["--config=tsan"])
  519. return bazel_invoke(
  520. subprocess.check_call,
  521. bazel_precmd_flags + ["build"] + bazel_flags + ["--"] + bazel_targets,
  522. env=bazel_env,
  523. )
  524. def _walk_thirdparty_dir(directory):
  525. file_list = []
  526. for root, dirs, filenames in os.walk(directory):
  527. # Exclude generated bytecode cache directories and tests directories
  528. # from vendored packages.
  529. for exclude_dir in ["__pycache__", "tests"]:
  530. if exclude_dir in dirs:
  531. dirs.remove(exclude_dir)
  532. for name in filenames:
  533. file_list.append(os.path.join(root, name))
  534. return file_list
  535. def copy_file(target_dir, filename, rootdir):
  536. # TODO(rkn): This feels very brittle. It may not handle all cases. See
  537. # https://github.com/apache/arrow/blob/master/python/setup.py for an
  538. # example.
  539. # File names can be absolute paths, e.g. from _walk_thirdparty_dir().
  540. source = os.path.relpath(filename, rootdir)
  541. destination = os.path.join(target_dir, source)
  542. # Create the target directory if it doesn't already exist.
  543. os.makedirs(os.path.dirname(destination), exist_ok=True)
  544. if not os.path.exists(destination):
  545. if sys.platform == "win32":
  546. # Does not preserve file mode (needed to avoid read-only bit)
  547. shutil.copyfile(source, destination, follow_symlinks=True)
  548. else:
  549. # Preserves file mode (needed to copy executable bit)
  550. shutil.copy(source, destination, follow_symlinks=True)
  551. return 1
  552. return 0
  553. def add_system_dlls(dlls, target_dir):
  554. """
  555. Copy any required dlls required by the c-extension module and not already
  556. provided by python. They will end up in the wheel next to the c-extension
  557. module which will guarentee they are available at runtime.
  558. """
  559. for dll in dlls:
  560. # Installing Visual Studio will copy the runtime dlls to system32
  561. src = os.path.join(r"c:\Windows\system32", dll)
  562. assert os.path.exists(src)
  563. shutil.copy(src, target_dir)
  564. def pip_run(build_ext):
  565. if SKIP_BAZEL_BUILD:
  566. build(False, False, False)
  567. else:
  568. build(True, BUILD_JAVA, True)
  569. if setup_spec.type == SetupType.RAY:
  570. setup_spec.files_to_include += ray_files
  571. thirdparty_dir = os.path.join(ROOT_DIR, THIRDPARTY_SUBDIR)
  572. setup_spec.files_to_include += _walk_thirdparty_dir(thirdparty_dir)
  573. runtime_env_agent_thirdparty_dir = os.path.join(
  574. ROOT_DIR, RUNTIME_ENV_AGENT_THIRDPARTY_SUBDIR
  575. )
  576. setup_spec.files_to_include += _walk_thirdparty_dir(
  577. runtime_env_agent_thirdparty_dir
  578. )
  579. # Copy over the autogenerated protobuf Python bindings.
  580. for directory in generated_python_directories:
  581. for filename in os.listdir(directory):
  582. if filename[-3:] == ".py":
  583. setup_spec.files_to_include.append(
  584. os.path.join(directory, filename)
  585. )
  586. copied_files = 0
  587. for filename in setup_spec.files_to_include:
  588. copied_files += copy_file(build_ext.build_lib, filename, ROOT_DIR)
  589. if sys.platform == "win32":
  590. # _raylet.pyd links to some MSVC runtime DLLS, this one may not be
  591. # present on a user's machine. While vcruntime140.dll and
  592. # vcruntime140_1.dll are also required, they are provided by CPython.
  593. runtime_dlls = ["msvcp140.dll"]
  594. add_system_dlls(runtime_dlls, os.path.join(build_ext.build_lib, "ray"))
  595. copied_files += len(runtime_dlls)
  596. print("# of files copied to {}: {}".format(build_ext.build_lib, copied_files))
  597. def api_main(program, *args):
  598. parser = argparse.ArgumentParser()
  599. choices = ["build", "python_versions", "clean", "help"]
  600. parser.add_argument("command", type=str, choices=choices)
  601. parser.add_argument(
  602. "-l",
  603. "--language",
  604. default="python,cpp",
  605. type=str,
  606. help="A list of languages to build native libraries. "
  607. 'Supported languages include "python" and "java". '
  608. "If not specified, only the Python library will be built.",
  609. )
  610. parsed_args = parser.parse_args(args)
  611. result = None
  612. if parsed_args.command == "build":
  613. kwargs = dict(build_python=False, build_java=False, build_cpp=False)
  614. for lang in parsed_args.language.split(","):
  615. if "python" in lang:
  616. kwargs.update(build_python=True)
  617. elif "java" in lang:
  618. kwargs.update(build_java=True)
  619. elif "cpp" in lang:
  620. kwargs.update(build_cpp=True)
  621. else:
  622. raise ValueError("invalid language: {!r}".format(lang))
  623. result = build(**kwargs)
  624. elif parsed_args.command == "python_versions":
  625. for version in SUPPORTED_PYTHONS:
  626. # NOTE: On Windows this will print "\r\n" on the command line.
  627. # Strip it out by piping to tr -d "\r".
  628. print(".".join(map(str, version)))
  629. elif parsed_args.command == "clean":
  630. def onerror(function, path, excinfo):
  631. nonlocal result
  632. if excinfo[1].errno != errno.ENOENT:
  633. msg = excinfo[1].strerror
  634. logger.error("cannot remove {}: {}".format(path, msg))
  635. result = 1
  636. for subdir in CLEANABLE_SUBDIRS:
  637. shutil.rmtree(os.path.join(ROOT_DIR, subdir), onerror=onerror)
  638. elif parsed_args.command == "help":
  639. parser.print_help()
  640. else:
  641. raise ValueError("Invalid command: {!r}".format(parsed_args.command))
  642. return result
  643. if __name__ == "__api__":
  644. api_main(*sys.argv)
  645. if __name__ == "__main__":
  646. import setuptools
  647. import setuptools.command.build_ext
  648. class build_ext(setuptools.command.build_ext.build_ext):
  649. def run(self):
  650. return pip_run(self)
  651. class BinaryDistribution(setuptools.Distribution):
  652. def has_ext_modules(self):
  653. return True
  654. # Ensure no remaining lib files.
  655. build_dir = os.path.join(ROOT_DIR, "build")
  656. if os.path.isdir(build_dir):
  657. shutil.rmtree(build_dir)
  658. setuptools.setup(
  659. name=setup_spec.name,
  660. version=setup_spec.version,
  661. author="Ray Team",
  662. author_email="ray-dev@googlegroups.com",
  663. description=(setup_spec.description),
  664. long_description=io.open(
  665. os.path.join(ROOT_DIR, os.path.pardir, "README.rst"), "r", encoding="utf-8"
  666. ).read(),
  667. url="https://github.com/ray-project/ray",
  668. keywords=(
  669. "ray distributed parallel machine-learning hyperparameter-tuning"
  670. "reinforcement-learning deep-learning serving python"
  671. ),
  672. python_requires=">=3.8",
  673. classifiers=[
  674. "Programming Language :: Python :: 3.8",
  675. "Programming Language :: Python :: 3.9",
  676. "Programming Language :: Python :: 3.10",
  677. "Programming Language :: Python :: 3.11",
  678. ],
  679. packages=setup_spec.get_packages(),
  680. cmdclass={"build_ext": build_ext},
  681. # The BinaryDistribution argument triggers build_ext.
  682. distclass=BinaryDistribution,
  683. install_requires=setup_spec.install_requires,
  684. setup_requires=["cython >= 0.29.32", "wheel"],
  685. extras_require=setup_spec.extras,
  686. entry_points={
  687. "console_scripts": [
  688. "ray=ray.scripts.scripts:main",
  689. "rllib=ray.rllib.scripts:cli [rllib]",
  690. "tune=ray.tune.cli.scripts:cli",
  691. "serve=ray.serve.scripts:cli",
  692. ]
  693. },
  694. package_data={
  695. "ray": ["includes/*.pxd", "*.pxd", "data/_internal/logging.yaml"],
  696. },
  697. include_package_data=True,
  698. exclude_package_data={
  699. # Empty string means "any package".
  700. # Therefore, exclude BUILD from every package:
  701. "": ["BUILD"],
  702. },
  703. zip_safe=False,
  704. license="Apache 2.0",
  705. ) if __name__ == "__main__" else None