From 5bfce4bea06306ab1bbccc628ab2245809523bf5 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 24 Oct 2026 13:28:02 +0200 Subject: [PATCH] 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 --- setup.py | 64 +++++++++++++++++++++++++--------- tools/test_setup_find_tool.py | 65 +++++++++++++++++++++++++++++++++++ tools/test_setup_pins.py | 1 + 3 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 tools/test_setup_find_tool.py diff --git a/setup.py b/setup.py index 40793db7..432de323 100644 --- a/setup.py +++ b/setup.py @@ -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") diff --git a/tools/test_setup_find_tool.py b/tools/test_setup_find_tool.py new file mode 100644 index 00000000..419efa7a --- /dev/null +++ b/tools/test_setup_find_tool.py @@ -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() diff --git a/tools/test_setup_pins.py b/tools/test_setup_pins.py index 83d73fb2..052c9a97 100644 --- a/tools/test_setup_pins.py +++ b/tools/test_setup_pins.py @@ -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")