|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2021-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +# This module implements basic PEP 517 backend support, see e.g. |
| 6 | +# - https://peps.python.org/pep-0517/ |
| 7 | +# - https://setuptools.pypa.io/en/latest/build_meta.html#dynamic-build-dependencies-and-other-build-meta-tweaks |
| 8 | +# Specifically, there are 5 APIs required to create a proper build backend, see below. |
| 9 | +# For now it's mostly a pass-through to setuptools, except that we need to determine |
| 10 | +# some dependencies at build time. |
| 11 | +# |
| 12 | +# TODO: also implement PEP-660 API hooks |
| 13 | + |
| 14 | +import os |
| 15 | +import re |
| 16 | +import subprocess # nosec: B404 |
| 17 | + |
| 18 | +from setuptools import build_meta as _build_meta |
| 19 | + |
| 20 | +prepare_metadata_for_build_wheel = _build_meta.prepare_metadata_for_build_wheel |
| 21 | +build_wheel = _build_meta.build_wheel |
| 22 | +build_sdist = _build_meta.build_sdist |
| 23 | +get_requires_for_build_sdist = _build_meta.get_requires_for_build_sdist |
| 24 | + |
| 25 | + |
| 26 | +def _get_proper_cuda_bindings_major_version() -> str: |
| 27 | + # for local development (with/without build isolation) |
| 28 | + try: |
| 29 | + import cuda.bindings |
| 30 | + |
| 31 | + return cuda.bindings.__version__.split(".")[0] |
| 32 | + except ImportError: |
| 33 | + pass |
| 34 | + |
| 35 | + # for custom overwrite, e.g. in CI |
| 36 | + cuda_major = os.environ.get("CUDA_CORE_BUILD_MAJOR") |
| 37 | + if cuda_major is not None: |
| 38 | + return cuda_major |
| 39 | + |
| 40 | + # also for local development |
| 41 | + try: |
| 42 | + out = subprocess.run("nvidia-smi", env=os.environ, capture_output=True, check=True) # nosec: B603, B607 |
| 43 | + m = re.search(r"CUDA Version:\s*([\d\.]+)", out.stdout.decode()) |
| 44 | + if m: |
| 45 | + return m.group(1).split(".")[0] |
| 46 | + except FileNotFoundError: |
| 47 | + # the build machine has no driver installed |
| 48 | + pass |
| 49 | + |
| 50 | + # default fallback |
| 51 | + return "13" |
| 52 | + |
| 53 | + |
| 54 | +# Note: this function returns a list of *build-time* dependencies, so it's not affected |
| 55 | +# by "--no-deps" based on the PEP-517 design. |
| 56 | +def get_requires_for_build_wheel(config_settings=None): |
| 57 | + cuda_major = _get_proper_cuda_bindings_major_version() |
| 58 | + cuda_bindings_require = [f"cuda-bindings=={cuda_major}.*"] |
| 59 | + return _build_meta.get_requires_for_build_wheel(config_settings) + cuda_bindings_require |
0 commit comments