version_provider.py 2.66 KB
Newer Older
1
2
3
4
5
6
from __future__ import annotations

import os
import platform
import subprocess
from pathlib import Path
7
from functools import lru_cache
8
9
10

ROOT = Path(__file__).parent

11
base_version = (ROOT / "VERSION").read_text().strip()
12
13
14
15
16
# When installing a sdist,
# the installed version needs to match the sdist version,
# so pip will complain when we install `tilelang-0.1.6.post2+gitxxxx.tar.gz`.
# To workaround that, when building sdist,
# we do not add version label and use a file to store the git hash instead.
17
git_pin = ROOT / ".git_commit.txt"
18
19
20
21
22


def _read_cmake_bool(i: str | None, default=False):
    if i is None:
        return default
23
    return i.lower() not in ("0", "false", "off", "no", "n", "")
24
25


26
@lru_cache(maxsize=1)
27
def get_git_commit_id() -> str | None:
28
29
    """Get the current git commit hash by running git in the current file's directory."""

30
    r = subprocess.run(["git", "rev-parse", "HEAD"], cwd=ROOT, capture_output=True, encoding="utf-8")
31
    if r.returncode == 0:
32
33
34
35
36
        _git = r.stdout.strip()
        git_pin.write_text(_git)
        return _git
    elif git_pin.exists():
        return git_pin.read_text().strip()
37
    else:
38
        return None
39
40


41
42
def dynamic_metadata(field: str, settings: dict[str, object] | None = None) -> str:
    assert field == "version"
43
44
45

    version = base_version

46
47
48
    # generate git version for sdist
    get_git_commit_id()

49
    if not _read_cmake_bool(os.environ.get("NO_VERSION_LABEL")):
50
51
        exts = []
        backend = None
52
        if _read_cmake_bool(os.environ.get("NO_TOOLCHAIN_VERSION")):
53
            pass
54
        elif platform.system() == "Darwin":
55
56
57
            # only on macosx_11_0_arm64, not necessary
            # backend = 'metal'
            pass
58
59
60
61
        elif _read_cmake_bool(os.environ.get("USE_ROCM", "")):
            backend = "rocm"
        elif "USE_CUDA" in os.environ and not _read_cmake_bool(os.environ.get("USE_CUDA")):
            backend = "cpu"
62
63
64
65
        else:  # cuda
            # Read nvcc version from env.
            # This is not exactly how it should be,
            # but works for now if building in a nvidia/cuda image.
66
67
68
            if cuda_version := os.environ.get("CUDA_VERSION"):
                major, minor, *_ = cuda_version.split(".")
                backend = f"cu{major}{minor}"
69
            else:
70
                backend = "cuda"
71
72
73
        if backend:
            exts.append(backend)

74
        if _read_cmake_bool(os.environ.get("NO_GIT_VERSION")):
75
76
            pass
        elif git_hash := get_git_commit_id():
77
            exts.append(f"git{git_hash[:8]}")
78
        else:
79
            exts.append("gitunknown")
80
81

        if exts:
82
            version += "+" + ".".join(exts)
83
84
85
86
87

    return version


__all__ = ["dynamic_metadata"]