| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | """Shared setuptools-rust build entry for Rust artifacts.""" |
| 5 | |
| 6 | from __future__ import annotations |
| 7 | |
| 8 | import os |
| 9 | import sys |
| 10 | from pathlib import Path |
| 11 | |
| 12 | from setuptools import setup |
| 13 | from setuptools_rust import Binding, RustExtension |
| 14 | from setuptools_scm import get_version |
| 15 | |
| 16 | ROOT_DIR = Path(__file__).resolve().parents[1] |
| 17 | VLLM_RS_BUILD_VERSION = "VLLM_RS_BUILD_VERSION" |
| 18 | |
| 19 | # Select vLLM release tags, excluding crate tags such as "proto-v0.3.0". |
| 20 | VLLM_GIT_DESCRIBE_COMMAND = [ |
| 21 | "git", |
| 22 | "describe", |
| 23 | "--dirty", |
| 24 | "--tags", |
| 25 | "--long", |
| 26 | "--abbrev=40", |
| 27 | "--match", |
| 28 | "v[0-9]*", |
| 29 | ] |
| 30 | |
| 31 | |
| 32 | def prepare_build_environment() -> str | None: |
| 33 | """Set the device-independent vLLM source version for Rust artifacts.""" |
| 34 | version = os.getenv(VLLM_RS_BUILD_VERSION) or None |
| 35 | if version is None: |
| 36 | try: |
| 37 | version = get_version( |
| 38 | root=ROOT_DIR, |
| 39 | git_describe_command=VLLM_GIT_DESCRIBE_COMMAND, |
| 40 | ) |
| 41 | except LookupError: |
| 42 | return None |
| 43 | |
| 44 | os.environ[VLLM_RS_BUILD_VERSION] = version |
| 45 | return version |
| 46 | |
| 47 | |
| 48 | def rust_extensions(*, optional: bool = False) -> list[RustExtension]: |
| 49 | return [ |
| 50 | RustExtension( |
| 51 | target="vllm.vllm-rs", |
| 52 | path="rust/src/cmd/Cargo.toml", |
| 53 | args=["--bin", "vllm-rs"], |
| 54 | features=["native-tls-vendored"], |
| 55 | binding=Binding.Exec, |
| 56 | optional=optional, |
| 57 | ), |
| 58 | RustExtension( |
| 59 | target="vllm._rust_tool_parser", |
| 60 | path="rust/src/parser/python/Cargo.toml", |
| 61 | features=["pyo3/abi3-py38"], |
| 62 | binding=Binding.PyO3, |
| 63 | optional=optional, |
| 64 | py_limited_api=True, |
| 65 | ), |
| 66 | ] |
| 67 | |
| 68 | |
| 69 | def rust_py_extension_module_names() -> list[str]: |
| 70 | module_names = [] |
| 71 | for extension in rust_extensions(): |
| 72 | if extension.binding != Binding.PyO3: |
| 73 | continue |
| 74 | |
| 75 | for target_name in extension.target.values(): |
| 76 | if target_name.startswith("vllm._rust_"): |
| 77 | module_names.append(target_name.rsplit(".", 1)[-1]) |
| 78 | |
| 79 | return module_names |
| 80 | |
| 81 | |
| 82 | def build_binary(build_rust_args: list[str]) -> None: |
| 83 | os.chdir(ROOT_DIR) |
| 84 | (ROOT_DIR / "vllm").mkdir(exist_ok=True) |
| 85 | setup( |
| 86 | name="vllm-rust-frontend-build", |
| 87 | packages=[], |
| 88 | rust_extensions=rust_extensions(optional=False), |
| 89 | script_args=["build_rust", "--quiet", "--inplace", *build_rust_args], |
| 90 | ) |
| 91 | |
| 92 | |
| 93 | def main() -> None: |
| 94 | prepare_build_environment() |
| 95 | build_binary(sys.argv[1:]) |
| 96 | |
| 97 | |
| 98 | if __name__ == "__main__": |
| 99 | main() |