setup: the build tools come from the virtualenv first and an older system cmake (below the 3.24 CMakeLists.txt needs) counts as missing, so pip installs the right one (#3822); tests

This commit is contained in:
Niko1221 committed 2026-10-24 13:28:02 +02:00
1 parent ad9bb57ddc
commit 5bfce4bea0
3 files changed
+114 -16

No files matched your search

+48 -16
View File
@@ -2100,22 +2100,43 @@ def find_vcvars(cuda_v=None):
return v if v and v.exists() else None
CMAKE_MIN = (3, 24) # CMakeLists.txt: cmake_minimum_required(VERSION 3.24)
def _tool_version(path, name):
"""(ok, version tuple) of `path --version`: ok False when it does not run (a pip launcher whose Python is gone)."""
try:
done = subprocess.run([path, "--version"], capture_output=True, text=True, timeout=30)
except (OSError, subprocess.SubprocessError):
return False, ()
if done.returncode != 0:
return False, ()
found = re.search(r"(\d+)\.(\d+)(?:\.(\d+))?", done.stdout or "")
return True, tuple(int(x) for x in found.groups() if x) if found else ()
def find_tool(name):
"""A tool on PATH, or the one pip installed next to this Python (cmake, ninja). A PATH copy that does not run
(a pip launcher whose Python is gone) is skipped for the one next to this Python."""
p = shutil.which(name)
if p:
try:
if subprocess.run([p, "--version"], capture_output=True, timeout=30).returncode == 0:
return p
except (OSError, subprocess.SubprocessError):
pass
for d in (Path(sys.executable).parent / "Scripts", Path(sys.executable).parent,
Path.home() / ".local" / "bin"):
c = d / (name + (".exe" if WIN else ""))
if c.exists():
return str(c)
return None
"""The tool the build uses (cmake, ninja): the one pip installed next to THIS Python first (the virtualenv's own, #3822), then PATH,
then ~/.local/bin. One that does not run (a pip launcher whose Python is gone) is skipped, and so is a cmake older than the
one CMakeLists.txt needs while another is found; with only an older one it is returned and the build says so."""
exe = name + (".exe" if WIN else "")
candidates = [str(d / exe) for d in (Path(sys.executable).parent / "Scripts", Path(sys.executable).parent) if (d / exe).exists()]
on_path = shutil.which(name)
if on_path and on_path not in candidates:
candidates.append(on_path)
local = Path.home() / ".local" / "bin" / exe
if local.exists() and str(local) not in candidates:
candidates.append(str(local))
older = None
for c in candidates:
runs, version = _tool_version(c, name)
if not runs:
continue
if name == "cmake" and version and version[:2] < CMAKE_MIN:
older = older or c
continue
return c
return older
def free_gb(path):
@@ -5483,13 +5504,24 @@ def install_build_tools(gpu, yes):
return nvcc, find_vcvars(cuda_v) if WIN else None
def tool_usable(name):
"""find_tool found one that runs and (cmake) is new enough: an older system cmake counts as missing, so pip puts the right one in the venv."""
found = find_tool(name)
if found is None:
return False
if name == "cmake":
runs, version = _tool_version(found, name)
return runs and (not version or version[:2] >= CMAKE_MIN)
return True
def ensure_build_tools():
"""cmake and ninja for a local compile, installed from requirements-build.txt when missing (#3403: the Mac setup
compiles without cmake_build(), so it calls this too). Returns (cmake, ninja)."""
# The ready-made engine path never calls this function. Keep build helpers out of
# the normal serving environment, and use already installed tools offline.
build_packages = requirement_lines(BUILD_REQUIREMENTS) if BUILD_REQUIREMENTS.exists() else BUILD_PACKAGES
missing = [package for package in build_packages if find_tool(req_name(package)) is None]
missing = [package for package in build_packages if not tool_usable(req_name(package))]
if missing:
pip_install(missing, "CMake and Ninja build tools")
cmake, ninja = find_tool("cmake"), find_tool("ninja")
+65
View File
@@ -0,0 +1,65 @@
"""setup.find_tool takes the virtualenv's own cmake before a PATH one and skips a cmake older than CMakeLists.txt needs (#3822).
python -m unittest tools.test_setup_find_tool
"""
from __future__ import annotations
import sys
import tempfile
import unittest
from pathlib import Path
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
import setup # noqa: E402
class FindTool(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.bin = Path(self.tmp.name) / "venv" / "bin"
self.bin.mkdir(parents=True)
self.venv_python = self.bin / "python"
def versions(self, table):
return lambda path, name: table.get(str(path), (False, ()))
def venv_tool(self, name="cmake"):
return str(self.bin / (name + (".exe" if setup.WIN else "")))
def find(self, table, on_path, name="cmake", venv_copy=True):
if venv_copy:
Path(self.venv_tool(name)).write_text("x")
with mock.patch.object(setup.sys, "executable", str(self.venv_python)), \
mock.patch.object(setup.shutil, "which", return_value=on_path), \
mock.patch.object(setup, "_tool_version", side_effect=self.versions(table)), \
mock.patch.object(setup.Path, "home", return_value=Path(self.tmp.name) / "home"):
return setup.find_tool(name), setup.tool_usable(name)
def test_the_virtualenv_copy_wins_over_an_old_system_cmake(self):
got, usable = self.find({"/usr/bin/cmake": (True, (3, 22, 1)), self.venv_tool(): (True, (4, 1, 0))}, "/usr/bin/cmake")
self.assertEqual(got, self.venv_tool())
self.assertTrue(usable)
def test_an_old_cmake_on_its_own_is_returned_but_not_usable(self):
got, usable = self.find({"/usr/bin/cmake": (True, (3, 22, 1))}, "/usr/bin/cmake", venv_copy=False)
self.assertEqual(got, "/usr/bin/cmake") # the build would say "CMake 3.24 or higher is required"
self.assertFalse(usable) # so setup installs the pip cmake first
def test_a_new_enough_system_cmake_is_fine_without_a_venv_one(self):
got, usable = self.find({"/usr/bin/cmake": (True, (3, 28, 3))}, "/usr/bin/cmake", venv_copy=False)
self.assertEqual((got, usable), ("/usr/bin/cmake", True))
def test_ninja_has_no_version_floor(self):
got, usable = self.find({"/usr/bin/ninja": (True, (1, 10))}, "/usr/bin/ninja", "ninja", venv_copy=False)
self.assertTrue(usable)
def test_a_launcher_that_does_not_run_is_skipped(self):
got, _ = self.find({self.venv_tool(): (False, ()), "/usr/bin/cmake": (True, (3, 30, 0))}, "/usr/bin/cmake")
self.assertEqual(got, "/usr/bin/cmake")
if __name__ == "__main__":
unittest.main()
+1
View File
@@ -378,6 +378,7 @@ class Requirements(unittest.TestCase):
with mock.patch.object(setup, "WIN", False), \
mock.patch.object(setup, "find_tool", side_effect=find_tool), \
mock.patch.object(setup, "_tool_version", return_value=(True, (3, 28, 1))), \
mock.patch.object(setup, "pip_install", side_effect=pip_install), \
mock.patch.object(setup, "run", side_effect=run):
setup.cmake_build(ROOT, ROOT / "build-test", "strata", [], None, "unused.bat")