Skip to content

Commit 90b973a

Browse files
XuehaiPanpytorchmergebot
authored andcommitted
[BE] parse CMake version from cmake -E capabilities instead of cmake --version (pytorch#157073)
`cmake -E capabilities` produces a JSON format that is more machine-friendly. ```console $ cmake --version cmake version 4.0.3 CMake suite maintained and supported by Kitware (kitware.com/cmake). $ cmake -E capabilities | jq '.version.string' "4.0.3" $ cmake -E capabilities | jq { "debugger": true, "fileApi": { "requests": [ { "kind": "codemodel", "version": [ { "major": 2, "minor": 8 } ] }, { "kind": "configureLog", "version": [ { "major": 1, "minor": 0 } ] }, { "kind": "cache", "version": [ { "major": 2, "minor": 0 } ] }, { "kind": "cmakeFiles", "version": [ { "major": 1, "minor": 1 } ] }, { "kind": "toolchains", "version": [ { "major": 1, "minor": 0 } ] } ] }, "generators": [ { "extraGenerators": [], "name": "Watcom WMake", "platformSupport": false, "toolsetSupport": false }, { "extraGenerators": [ "Kate" ], "name": "Ninja Multi-Config", "platformSupport": false, "toolsetSupport": false }, { "extraGenerators": [ "CodeBlocks", "CodeLite", "Eclipse CDT4", "Kate", "Sublime Text 2" ], "name": "Ninja", "platformSupport": false, "toolsetSupport": false }, { "extraGenerators": [], "name": "Xcode", "platformSupport": false, "toolsetSupport": true }, { "extraGenerators": [ "CodeBlocks", "CodeLite", "Eclipse CDT4", "Kate", "Sublime Text 2" ], "name": "Unix Makefiles", "platformSupport": false, "toolsetSupport": false } ], "serverMode": false, "tls": true, "version": { "isDirty": false, "major": 4, "minor": 0, "patch": 3, "string": "4.0.3", "suffix": "" } } ``` Pull Request resolved: pytorch#157073 Approved by: https://github.com/Skylion007
1 parent 772d590 commit 90b973a

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

.ci/pytorch/windows/internal/install_python.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ start /wait "" python-amd64.exe /quiet InstallAllUsers=1 PrependPath=0 Include_t
1818
if errorlevel 1 exit /b 1
1919

2020
set "PATH=%CD%\Python\Scripts;%CD%\Python;%PATH%"
21+
%PYTHON_EXEC% -m pip install --upgrade pip setuptools packaging wheel
22+
if errorlevel 1 exit /b 1

pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ requires = [
3131
"setuptools>=62.3.0,<80.0",
3232
"wheel",
3333
"astunparse",
34-
"numpy",
34+
"cmake",
3535
"ninja",
36+
"numpy",
37+
"packaging",
3638
"pyyaml",
37-
"cmake",
38-
"typing-extensions>=4.10.0",
3939
"requests",
40+
"typing-extensions>=4.10.0",
4041
]
4142
# Use legacy backend to import local packages in setup.py
4243
build-backend = "setuptools.build_meta:__legacy__"

tools/setup_helpers/cmake.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,32 @@
22

33
from __future__ import annotations
44

5+
import json
56
import multiprocessing
67
import os
78
import platform
89
import sys
910
import sysconfig
10-
from distutils.version import LooseVersion
1111
from pathlib import Path
12-
from subprocess import CalledProcessError, check_call, check_output
13-
from typing import Any, cast
12+
from subprocess import CalledProcessError, check_call, check_output, DEVNULL
13+
from typing import cast
1414

1515
from . import which
1616
from .cmake_utils import CMakeValue, get_cmake_cache_variables_from_file
1717
from .env import BUILD_DIR, check_negative_env_flag, IS_64BIT, IS_DARWIN, IS_WINDOWS
1818

1919

20+
try:
21+
from packaging.version import Version
22+
except ImportError:
23+
try:
24+
from setuptools.dist import Version # type: ignore[attr-defined,no-redef]
25+
except ImportError:
26+
from distutils.version import ( # type: ignore[assignment,no-redef]
27+
LooseVersion as Version,
28+
)
29+
30+
2031
def _mkdir_p(d: str) -> None:
2132
try:
2233
os.makedirs(d, exist_ok=True)
@@ -60,7 +71,7 @@ def _get_cmake_command() -> str:
6071
cmake3_version = CMake._get_version(which("cmake3"))
6172
cmake_version = CMake._get_version(which("cmake"))
6273

63-
_cmake_min_version = LooseVersion("3.27.0")
74+
_cmake_min_version = Version("3.27.0")
6475
if all(
6576
ver is None or ver < _cmake_min_version
6677
for ver in [cmake_version, cmake3_version]
@@ -82,15 +93,26 @@ def _get_cmake_command() -> str:
8293
return cmake_command
8394

8495
@staticmethod
85-
def _get_version(cmd: str | None) -> Any:
86-
"Returns cmake version."
96+
def _get_version(cmd: str | None) -> Version | None:
97+
"""Returns cmake version."""
8798

8899
if cmd is None:
89100
return None
90-
for line in check_output([cmd, "--version"]).decode("utf-8").split("\n"):
91-
if "version" in line:
92-
return LooseVersion(line.strip().split(" ")[2])
93-
raise RuntimeError("no version found")
101+
102+
try:
103+
cmake_capabilities = json.loads(
104+
check_output(
105+
[cmd, "-E", "capabilities"],
106+
stderr=DEVNULL,
107+
text=True,
108+
),
109+
)
110+
except (OSError, CalledProcessError, json.JSONDecodeError):
111+
cmake_capabilities = {}
112+
cmake_version = cmake_capabilities.get("version", {}).get("string")
113+
if cmake_version is not None:
114+
return Version(cmake_version)
115+
raise RuntimeError(f"Failed to get CMake version from command: {cmd}")
94116

95117
def run(self, args: list[str], env: dict[str, str]) -> None:
96118
"Executes cmake with arguments and an environment."

0 commit comments

Comments
 (0)