diff --git a/docs/gpu_test_results.md b/docs/gpu_test_results.md index 25a73c7f5d..fbe362cd50 100644 --- a/docs/gpu_test_results.md +++ b/docs/gpu_test_results.md @@ -4,6 +4,12 @@ This page keeps a local record of GPU test runs. The project's public tracker is [#166](https://github.com/ItsNotRexed/Leonida/issues/166) (one comment per GPU and driver); post counts and pass/fail lines there, never game shaders, logs or private paths. +Name the Vulkan headers a run was built against: configure with `-DLEONIDA_VULKAN_HEADERS_FETCH=ON`, which uses the +repository's pinned Khronos Vulkan-Headers whatever else is installed, and quote the configure's `-- Vulkan headers:` +line in the entry. Other headers can change the counts ([testing.md](testing.md#build-the-test-programs)). Until +`CMAKE-VULKAN-HEADERS-PIN` the flag lost to any Vulkan headers already on the machine, so an older entry whose setup +names it may have been built against those instead. + | GPU | Driver, OS | Commit | build.py | check.py | run_gpu_tests.py | Failed | Entry | |---|---|---|---|---|---|---|---| | NVIDIA GeForce RTX 3080 Ti (Ampere), subgroup 32..32 | NVIDIA 616.92, Windows 11 26300.9974 | `d478a92` | not run; pre-disassembled subset | run via run_gpu_tests.py | **Subset:** 81/97 passed, 16 skipped (native); 97/97 passed (`LEONIDA_W64_EMULATE=1`); Python assembler, no spirv-val | none in the subset | [local note](../gpu-test-results/RTX3080Ti-61692.txt) | diff --git a/docs/testing.md b/docs/testing.md index ffe729d24f..26d1db964b 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -94,6 +94,21 @@ cmake --build runtime/build/linux --parallel On macOS, add `-DVulkan_INCLUDE_DIR=/opt/homebrew/include` to the first command. +For a result you will post, add `-DLEONIDA_VULKAN_HEADERS_FETCH=ON` to the first command. It builds against the +repository's pinned Khronos Vulkan-Headers (downloaded once at configure time and hash-checked; +`-DFETCHCONTENT_SOURCE_DIR_VULKANHEADERS=` uses a local copy instead), even when `VULKAN_SDK`, +`Vulkan_INCLUDE_DIR` or installed headers name others, so two records of the same GPU compare. When something else +would supply other headers in place of the pin (a dependency provider, or a stale download kept by +`FETCHCONTENT_FULLY_DISCONNECTED`), the configure fails instead. The headers decide what the test programs can ask the +driver for: built against conda-forge's Vulkan 1.3 headers instead of the pin, a Radeon 780M skipped `ps_texoff`, +`ps_texoff_forms` and `ps_texoff_implicit` (225/248 passed instead of 228/248, +[#14](https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7122653687)). Every configure names the +headers it uses in one line; quote it in the record: + +```text +-- Vulkan headers: /_deps/vulkanheaders-src/include (VK_HEADER_VERSION 363), the pin: Khronos Vulkan-Headers 6802bb4733b63ed5efd3adb308a6c885ef180ea1, hash-checked +``` + The test programs and `scripts/run_gpu_tests.py` should come from the same commit. Some checks tolerate an older binary (`unpinned_subgroup_skip`), but mixing them can report false failures in the compute tests: 28 `cs_*` failures on both an RTX 5070 Ti Laptop and a Radeon 610M with the `426023a` script and `48ad23f` binaries, where diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 2763e9d1b0..66ab1ef5ed 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1744,11 +1744,54 @@ if(BUILD_TESTING) endif() # GPU test and pipeline tools load Vulkan at run time, so only the Vulkan headers are needed at build time. -# Nothing is downloaded unless asked: set VULKAN_SDK, install the Vulkan headers, pass -# -DLEONIDA_VULKAN_HEADERS_FETCH=ON to download the pinned Khronos Vulkan-Headers release once at configure time -# (hash-checked), or pass -DFETCHCONTENT_SOURCE_DIR_VULKANHEADERS= to use a local copy without downloading. -option(LEONIDA_VULKAN_HEADERS_FETCH "Download the pinned Khronos Vulkan-Headers release if no Vulkan headers are found" OFF) +# Nothing is downloaded unless asked. -DLEONIDA_VULKAN_HEADERS_FETCH=ON downloads the pinned Khronos Vulkan-Headers +# release once at configure time (hash-checked), and -DFETCHCONTENT_SOURCE_DIR_VULKANHEADERS= uses a local +# copy of it without downloading. Either one wins over any other Vulkan headers on the machine (VULKAN_SDK, +# Vulkan_INCLUDE_DIR, conda-forge's vulkan-headers, /usr/include), so GPU test results built with it compare across +# machines. Without them the headers come from VULKAN_SDK, Vulkan_INCLUDE_DIR or an install. The configure names the +# headers it uses in a "Vulkan headers:" line (cmake/vulkan_headers_report.cmake). +option(LEONIDA_VULKAN_HEADERS_FETCH "Build against the pinned Khronos Vulkan-Headers release (downloaded once), even when other Vulkan headers are installed" OFF) option(LEONIDA_REQUIRE_VULKAN "Fail the configure instead of warning when no Vulkan headers are found" OFF) +# The pinned Khronos Vulkan-Headers commit, which the "Vulkan headers:" line names, and its VK_HEADER_VERSION. +set(LEONIDA_VULKAN_HEADERS_PIN 6802bb4733b63ed5efd3adb308a6c885ef180ea1) +set(LEONIDA_VULKAN_HEADERS_PIN_VERSION 363) +include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/vulkan_headers_report.cmake") +set(leonida_vulkan_headers_origin FOUND) +if(LEONIDA_VULKAN_HEADERS_FETCH OR FETCHCONTENT_SOURCE_DIR_VULKANHEADERS) + # Before find_package(Vulkan): it would define Vulkan::Headers from whatever headers it finds first, and the pin + # would never be used. FindVulkan leaves an existing Vulkan::Headers alone. + include(FetchContent) + # OVERRIDE_FIND_PACKAGE: FetchContent_MakeAvailable never tries find_package(VulkanHeaders) in place of the pin, even + # with -DFETCHCONTENT_TRY_FIND_PACKAGE_MODE=ALWAYS, and a later find_package(VulkanHeaders) gets the pin. + FetchContent_Declare(VulkanHeaders + # vulkan-sdk-1.4.363.0 (same commit pinned in .github/workflows/runtime.yml) + URL https://github.com/KhronosGroup/Vulkan-Headers/archive/${LEONIDA_VULKAN_HEADERS_PIN}.tar.gz + URL_HASH SHA256=9044fbb356160e43201dc2094acb44c8a906722a63a35f48fc0f051579db9bb6 + DOWNLOAD_EXTRACT_TIMESTAMP TRUE + OVERRIDE_FIND_PACKAGE) + set(VULKAN_HEADERS_ENABLE_TESTS OFF CACHE BOOL "" FORCE) + set(VULKAN_HEADERS_ENABLE_INSTALL OFF CACHE BOOL "" FORCE) + set(VULKAN_HEADERS_ENABLE_MODULE OFF CACHE BOOL "" FORCE) + FetchContent_MakeAvailable(VulkanHeaders) + FetchContent_GetProperties(VulkanHeaders) + if(FETCHCONTENT_SOURCE_DIR_VULKANHEADERS) + set(leonida_vulkan_headers_origin SOURCE_DIR) + elseif(FETCHCONTENT_FULLY_DISCONNECTED) + # FetchContent reuses what is already in FETCHCONTENT_BASE_DIR and checks no archive. + set(leonida_vulkan_headers_origin PIN_CACHED) + else() + set(leonida_vulkan_headers_origin PIN) + endif() + # A dependency provider or an earlier FetchContent_Declare(VulkanHeaders) can still answer FetchContent_MakeAvailable + # with other headers; fail rather than build against them and call them the pin. + set(leonida_vk_include_dirs "") + if(TARGET Vulkan::Headers) + get_target_property(leonida_vk_include_dirs Vulkan::Headers INTERFACE_INCLUDE_DIRECTORIES) + endif() + leonida_vulkan_headers_check("${leonida_vk_include_dirs}" "${vulkanheaders_SOURCE_DIR}" + ${leonida_vulkan_headers_origin}) +endif() +# Also finds glslangValidator (Vulkan_GLSLANG_VALIDATOR_EXECUTABLE), which the shader-building tests below use. find_package(Vulkan QUIET) if(NOT TARGET Vulkan::Headers) find_path(Vulkan_INCLUDE_DIR @@ -1760,20 +1803,11 @@ if(NOT TARGET Vulkan::Headers) set_target_properties(Vulkan::Headers PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${Vulkan_INCLUDE_DIR}" ) - elseif(LEONIDA_VULKAN_HEADERS_FETCH OR FETCHCONTENT_SOURCE_DIR_VULKANHEADERS) - include(FetchContent) - FetchContent_Declare(VulkanHeaders - # vulkan-sdk-1.4.363.0 (same commit pinned in .github/workflows/runtime.yml) - URL https://github.com/KhronosGroup/Vulkan-Headers/archive/6802bb4733b63ed5efd3adb308a6c885ef180ea1.tar.gz - URL_HASH SHA256=9044fbb356160e43201dc2094acb44c8a906722a63a35f48fc0f051579db9bb6 - DOWNLOAD_EXTRACT_TIMESTAMP TRUE) - set(VULKAN_HEADERS_ENABLE_TESTS OFF CACHE BOOL "" FORCE) - set(VULKAN_HEADERS_ENABLE_INSTALL OFF CACHE BOOL "" FORCE) - set(VULKAN_HEADERS_ENABLE_MODULE OFF CACHE BOOL "" FORCE) - FetchContent_MakeAvailable(VulkanHeaders) endif() endif() if(TARGET Vulkan::Headers) + get_target_property(leonida_vk_include_dirs Vulkan::Headers INTERFACE_INCLUDE_DIRECTORIES) + leonida_vulkan_headers_report("${leonida_vk_include_dirs}" ${leonida_vulkan_headers_origin}) add_executable(vulkan_device_fault_test test/vulkan_device_fault_test.cpp) target_link_libraries(vulkan_device_fault_test PRIVATE Vulkan::Headers) add_test(NAME vulkan_device_fault COMMAND vulkan_device_fault_test) diff --git a/runtime/README.md b/runtime/README.md index aa9b2d3081..4c846524ae 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -375,7 +375,8 @@ ctest --test-dir runtime/build -C RelWithDebInfo --output-on-failure On Windows the build configures the CPU tests that do not need a GPU or game files, including the root-signature forwarding test (`root_sig_test`) and the clear-command test. The Vulkan tools and D3D12-over-Vulkan COM targets are built when Vulkan headers are found (via `VULKAN_SDK`, an installed -SDK, or `-DLEONIDA_VULKAN_HEADERS_FETCH=ON` to download the pinned Khronos Vulkan-Headers release): +SDK, or `-DLEONIDA_VULKAN_HEADERS_FETCH=ON` to download the pinned Khronos Vulkan-Headers release, which then wins +over any installed headers; the configure's `-- Vulkan headers:` line names the ones in use): - `vk_harness` — hand-written RDNA2 shader programs on a real Vulkan device. - `vk_texture_test`, `vk_compute_test`, `vk_store_test` — texture, LDS/compute, and buffer-store paths. diff --git a/runtime/cmake/vulkan_headers_report.cmake b/runtime/cmake/vulkan_headers_report.cmake new file mode 100644 index 0000000000..aa30ea637f --- /dev/null +++ b/runtime/cmake/vulkan_headers_report.cmake @@ -0,0 +1,104 @@ +# Names the Vulkan headers a configure builds against, in one STATUS line a GPU test record can quote: +# -- Vulkan headers: (VK_HEADER_VERSION ), +# The headers decide what the GPU test tools can ask the driver for, so two records of the same GPU and driver differ +# when their headers do: rebuilt against conda-forge's Vulkan 1.3 headers instead of the pin, run_gpu_tests.py on a +# Radeon 780M skipped ps_texoff, ps_texoff_forms and ps_texoff_implicit (225/248 passed instead of 228/248). +# Kept in its own file so tests/test_cmake_vulkan_headers_pin.py can run it with `cmake -P`. + +# Sets dir_var to the first of include_dirs (the INTERFACE_INCLUDE_DIRECTORIES of Vulkan::Headers) that holds +# vulkan/vulkan_core.h or vulkan/vulkan.h, or to "" when none does, and version_var to its VK_HEADER_VERSION, or to +# "unknown". Generator expressions such as $ (FetchContent's target) are unwrapped. +function(leonida_vulkan_headers_find include_dirs dir_var version_var) + set(dir "") + set(header "") + foreach(d IN LISTS include_dirs) + string(REGEX REPLACE "^\\$$" "\\1" d "${d}") + if(NOT d OR d MATCHES "^\\$<") + continue() + endif() + foreach(name vulkan_core.h vulkan.h) + if(EXISTS "${d}/vulkan/${name}") + set(dir "${d}") + set(header "${d}/vulkan/${name}") + break() + endif() + endforeach() + if(dir) + break() + endif() + endforeach() + set(version "unknown") + if(header) + file(STRINGS "${header}" line REGEX "^#define VK_HEADER_VERSION [0-9]+" LIMIT_COUNT 1) + if(line MATCHES "VK_HEADER_VERSION ([0-9]+)") + set(version "${CMAKE_MATCH_1}") + endif() + endif() + set(${dir_var} "${dir}" PARENT_SCOPE) + set(${version_var} "${version}" PARENT_SCOPE) +endfunction() + +# include_dirs: the INTERFACE_INCLUDE_DIRECTORIES of Vulkan::Headers. origin: PIN (the hash-checked download of +# LEONIDA_VULKAN_HEADERS_PIN), PIN_CACHED (a copy already in FETCHCONTENT_BASE_DIR, which +# FETCHCONTENT_FULLY_DISCONNECTED=ON uses without downloading or checking an archive; leonida_vulkan_headers_check +# has confirmed only its version), SOURCE_DIR (FETCHCONTENT_SOURCE_DIR_VULKANHEADERS) or FOUND (anything else). +function(leonida_vulkan_headers_report include_dirs origin) + leonida_vulkan_headers_find("${include_dirs}" dir version) + if(NOT dir) + set(dir "${include_dirs}") + endif() + # string(CONCAT) on quoted pieces keeps a ";" in the text, which a list would split on. + if(origin STREQUAL "PIN") + set(what "the pin: Khronos Vulkan-Headers ${LEONIDA_VULKAN_HEADERS_PIN}, hash-checked") + elseif(origin STREQUAL "PIN_CACHED") + string(CONCAT what "the pinned version, cached source: Khronos Vulkan-Headers ${LEONIDA_VULKAN_HEADERS_PIN}; " + "archive not checked in this configure (FETCHCONTENT_FULLY_DISCONNECTED=ON)") + elseif(origin STREQUAL "SOURCE_DIR") + string(CONCAT what "from FETCHCONTENT_SOURCE_DIR_VULKANHEADERS: a local copy used in place of the pin, not " + "checked against it") + else() + string(CONCAT what "found on this machine, not the pin (-DLEONIDA_VULKAN_HEADERS_FETCH=ON builds against the " + "pinned Khronos Vulkan-Headers)") + endif() + message(STATUS "Vulkan headers: ${dir} (VK_HEADER_VERSION ${version}), ${what}") +endfunction() + +# Fails the configure unless Vulkan::Headers (include_dirs) is the Vulkan-Headers copy FetchContent populated in +# source_dir (empty when it populated none). FetchContent_MakeAvailable(VulkanHeaders) can return other headers +# without populating anything: a dependency provider can answer it, and so can find_package() when an earlier +# FetchContent_Declare(VulkanHeaders) asked for that or when FETCHCONTENT_TRY_FIND_PACKAGE_MODE=ALWAYS applies (an +# installed VulkanHeadersConfig.cmake with VK_HEADER_VERSION 111 was named as the pin that way). origin PIN or +# PIN_CACHED also needs the pinned version, LEONIDA_VULKAN_HEADERS_PIN_VERSION, which a build folder populated before +# the pin changed and configured with FETCHCONTENT_FULLY_DISCONNECTED=ON does not have. +function(leonida_vulkan_headers_check include_dirs source_dir origin) + leonida_vulkan_headers_find("${include_dirs}" dir version) + set(same FALSE) + if(dir AND source_dir) + file(REAL_PATH "${dir}" used) + file(REAL_PATH "${source_dir}/include" wanted) + if(used STREQUAL wanted) + set(same TRUE) + endif() + endif() + if(NOT same) + if(source_dir) + set(wanted "${source_dir}/include") + else() + set(wanted "a folder FetchContent populated (it populated none)") + endif() + message(FATAL_ERROR + "-DLEONIDA_VULKAN_HEADERS_FETCH=ON or FETCHCONTENT_SOURCE_DIR_VULKANHEADERS asks for the Vulkan headers in " + "${wanted}, but Vulkan::Headers includes \"${include_dirs}\". Something other than that download answered " + "FetchContent_MakeAvailable(VulkanHeaders): a dependency provider (CMAKE_PROJECT_TOP_LEVEL_INCLUDES) or an " + "earlier FetchContent_Declare(VulkanHeaders). Remove it, or configure without either of the two to build " + "against the installed headers.") + endif() + if((origin STREQUAL "PIN" OR origin STREQUAL "PIN_CACHED") AND + NOT version STREQUAL "${LEONIDA_VULKAN_HEADERS_PIN_VERSION}") + message(FATAL_ERROR + "${dir} has VK_HEADER_VERSION ${version}, but the pinned Khronos Vulkan-Headers ${LEONIDA_VULKAN_HEADERS_PIN} " + "are version ${LEONIDA_VULKAN_HEADERS_PIN_VERSION}, so ${source_dir} is not the pin. A dependency provider may " + "have populated it, or the build folder was populated before the pin changed and FETCHCONTENT_FULLY_DISCONNECTED " + "is ON, which keeps the old copy: configure once with -DFETCHCONTENT_FULLY_DISCONNECTED=OFF to download the pin.") + endif() +endfunction() diff --git a/tests/test_cmake_vulkan_headers_pin.py b/tests/test_cmake_vulkan_headers_pin.py new file mode 100644 index 0000000000..afa183ee65 --- /dev/null +++ b/tests/test_cmake_vulkan_headers_pin.py @@ -0,0 +1,390 @@ +"""-DLEONIDA_VULKAN_HEADERS_FETCH=ON (or a FETCHCONTENT_SOURCE_DIR_VULKANHEADERS copy) must win over other headers. + +runtime/CMakeLists.txt used to run find_package(Vulkan) and find_path(Vulkan_INCLUDE_DIR) before the fetch, so any +Vulkan headers on the machine (an SDK, conda-forge's vulkan-headers, /usr/include) defined Vulkan::Headers first and +the pinned Khronos Vulkan-Headers were never used, even with the flag on. That changes GPU results: against +conda-forge's Vulkan 1.3 headers run_gpu_tests.py on a Radeon 780M skipped ps_texoff, ps_texoff_forms and +ps_texoff_implicit (225/248 passed instead of 228/248 with the pin). The configure now also names its headers in a +"Vulkan headers:" line (runtime/cmake/vulkan_headers_report.cmake) that a GPU record can quote. + +FetchContent_MakeAvailable(VulkanHeaders) can also hand back other headers without populating the pin: with +-DFETCHCONTENT_TRY_FIND_PACKAGE_MODE=ALWAYS it tried find_package(VulkanHeaders) first, and an installed +VulkanHeadersConfig.cmake with VK_HEADER_VERSION 111 was used and named as the pin. The declaration now carries +OVERRIDE_FIND_PACKAGE, and the configure fails when a dependency provider or another declaration still supplies +other headers, or when the populated copy is not the pinned version. + +The configure cases run CMake on runtime/ with hand-written stand-ins for an installed SDK (VULKAN_SDK), an installed +Vulkan-Headers package (VulkanHeaders_DIR) and the pinned release (FETCHCONTENT_SOURCE_DIR_VULKANHEADERS, or a copy +already in FETCHCONTENT_BASE_DIR with FETCHCONTENT_FULLY_DISCONNECTED=ON), so nothing is downloaded and no real Vulkan +headers are needed. They need cmake and a C++ compiler and are skipped without them; the cmake -P cases need cmake +only. +""" +import os +import re +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +RUNTIME = ROOT / "runtime" +MODULE = RUNTIME / "cmake" / "vulkan_headers_report.cmake" +CMAKE_LISTS = RUNTIME / "CMakeLists.txt" +CMAKE = shutil.which("cmake") + +SDK_VERSION = 231 # conda-forge's vulkan-headers next to llvm-tools +PACKAGE_VERSION = 111 # an installed Vulkan-Headers package found through VulkanHeaders_DIR +# The VK_HEADER_VERSION of the pin (363 for vulkan-sdk-1.4.363.0), which a stand-in for the pin must have. +_PIN_VERSION = re.search(r"^set\(LEONIDA_VULKAN_HEADERS_PIN_VERSION ([0-9]+)\)$", + CMAKE_LISTS.read_text(encoding="utf-8"), re.M) +PIN_VERSION = int(_PIN_VERSION.group(1)) if _PIN_VERSION else None + +# Runs at the end of the top-level directory (cmake_language DEFER) and writes the include directories of the +# Vulkan::Headers target the tools link, which is what the compiler gets, independent of the "Vulkan headers:" line. +HOOK = """\ +function(leonida_test_dump_vulkan_headers) + if(TARGET Vulkan::Headers) + get_target_property(dirs Vulkan::Headers INTERFACE_INCLUDE_DIRECTORIES) + else() + set(dirs NONE) + endif() + file(WRITE "${CMAKE_BINARY_DIR}/leonida_test_vulkan_headers.txt" "${dirs}") +endfunction() +cmake_language(DEFER CALL leonida_test_dump_vulkan_headers) +""" + +# A stand-in for the Khronos Vulkan-Headers project: the same target names and include directory layout. +FAKE_PROJECT = """\ +cmake_minimum_required(VERSION 3.24) +project(Vulkan-Headers LANGUAGES NONE) +add_library(Vulkan-Headers INTERFACE) +add_library(Vulkan::Headers ALIAS Vulkan-Headers) +target_include_directories(Vulkan-Headers INTERFACE "$") +""" + +# A stand-in for an installed Khronos Vulkan-Headers package: the same config file and target names. +FAKE_PACKAGE = """\ +if(NOT TARGET Vulkan::Headers) + add_library(Vulkan::Headers INTERFACE IMPORTED) + set_target_properties(Vulkan::Headers PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_DIR}/include") +endif() +""" + +# A dependency provider (CMAKE_PROJECT_TOP_LEVEL_INCLUDES) that answers FetchContent_MakeAvailable(VulkanHeaders) with +# the installed package's headers, as a package manager's provider would. +PROVIDER = """\ +function(leonida_test_provider method name) + if(name STREQUAL "VulkanHeaders") + if(NOT TARGET Vulkan::Headers) + add_library(Vulkan::Headers INTERFACE IMPORTED GLOBAL) + set_target_properties(Vulkan::Headers PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "@INCLUDE@") + endif() + FetchContent_SetPopulated(${name}) + endif() +endfunction() +cmake_language(SET_DEPENDENCY_PROVIDER leonida_test_provider SUPPORTED_METHODS FETCHCONTENT_MAKEAVAILABLE_SERIAL) +""" + +CHECK_FAILED = "Something other than that download answered" + + +def write(path, text): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(text, encoding="utf-8", newline="\n") + + +def write_headers(include, version): + """vulkan/vulkan.h and vulkan/vulkan_core.h under include; version None writes no VK_HEADER_VERSION line.""" + line = f"#define VK_HEADER_VERSION {version}\n" if version is not None else "" + write(include / "vulkan" / "vulkan_core.h", f"#ifndef VULKAN_CORE_H_\n#define VULKAN_CORE_H_ 1\n{line}#endif\n") + write(include / "vulkan" / "vulkan.h", '#include "vulkan_core.h"\n') + + +def same_dir(a, b): + return os.path.normcase(os.path.realpath(a)) == os.path.normcase(os.path.realpath(b)) + + +def included(text): + """The directories of an INTERFACE_INCLUDE_DIRECTORIES value, with $ unwrapped.""" + dirs = [] + for d in text.split(";"): + m = re.fullmatch(r"\$", d) + if m: + d = m.group(1) + if d and not d.startswith("$<"): + dirs.append(d) + return dirs + + +def headers_line(output): + lines = [ln for ln in output.splitlines() if "Vulkan headers:" in ln] + return lines[-1] if lines else "" + + +def run_module(call): + with tempfile.TemporaryDirectory() as tmp: + script = Path(tmp) / "driver.cmake" + write(script, f'include("{MODULE.as_posix()}")\n' + 'set(LEONIDA_VULKAN_HEADERS_PIN 0123456789abcdef0123456789abcdef01234567)\n' + f'set(LEONIDA_VULKAN_HEADERS_PIN_VERSION {PIN_VERSION})\n' + f'{call}\n') + return subprocess.run([CMAKE, "-P", str(script)], capture_output=True, text=True, timeout=60) + + +def run_report(dirs, origin): + return run_module(f'leonida_vulkan_headers_report("{dirs}" {origin})') + + +def run_check(dirs, source_dir, origin): + return run_module(f'leonida_vulkan_headers_check("{dirs}" "{source_dir}" {origin})') + + +@unittest.skipUnless(CMAKE, "cmake not on PATH") +class ReportLineTest(unittest.TestCase): + def setUp(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + self.include = Path(tmp.name) / "include" + + def test_found_headers_are_named_and_not_called_the_pin(self): + write_headers(self.include, SDK_VERSION) + r = run_report(self.include.as_posix(), "FOUND") + self.assertEqual(r.returncode, 0, r.stderr) + line = headers_line(r.stdout) + self.assertIn(f"Vulkan headers: {self.include.as_posix()} (VK_HEADER_VERSION {SDK_VERSION})", line) + self.assertIn("not the pin", line) + self.assertIn("-DLEONIDA_VULKAN_HEADERS_FETCH=ON", line) + + def test_pin_names_the_commit(self): + write_headers(self.include, PIN_VERSION) + r = run_report(f"$", "PIN") + self.assertEqual(r.returncode, 0, r.stderr) + line = headers_line(r.stdout) + self.assertIn(f"Vulkan headers: {self.include.as_posix()} (VK_HEADER_VERSION {PIN_VERSION})", line) + self.assertIn("the pin: Khronos Vulkan-Headers 0123456789abcdef0123456789abcdef01234567", line) + + def test_a_cached_pin_does_not_claim_a_hash_check(self): + # FETCHCONTENT_FULLY_DISCONNECTED=ON: the copy in FETCHCONTENT_BASE_DIR is used and no archive is checked. + write_headers(self.include, PIN_VERSION) + r = run_report(f"$", "PIN_CACHED") + self.assertEqual(r.returncode, 0, r.stderr) + line = headers_line(r.stdout) + self.assertIn(f"(VK_HEADER_VERSION {PIN_VERSION}), the pinned version, cached source: Khronos Vulkan-Headers " + "0123456789abcdef0123456789abcdef01234567; archive not checked in this configure", line) + self.assertNotIn("hash-checked", line) + + def test_source_dir_copy_is_not_vouched_for(self): + write_headers(self.include, PIN_VERSION) + r = run_report(self.include.as_posix(), "SOURCE_DIR") + self.assertEqual(r.returncode, 0, r.stderr) + line = headers_line(r.stdout) + self.assertIn("FETCHCONTENT_SOURCE_DIR_VULKANHEADERS", line) + self.assertIn("not checked against it", line) + + def test_header_without_a_version_says_unknown(self): + write_headers(self.include, None) + r = run_report(self.include.as_posix(), "FOUND") + self.assertEqual(r.returncode, 0, r.stderr) + self.assertIn("(VK_HEADER_VERSION unknown)", headers_line(r.stdout)) + + +@unittest.skipUnless(CMAKE, "cmake not on PATH") +class CheckTest(unittest.TestCase): + """leonida_vulkan_headers_check: Vulkan::Headers must be the copy FetchContent populated, and the pin's version.""" + + def setUp(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + base = Path(tmp.name) + self.source = base / "vulkan-headers" + self.package = base / "package" + write_headers(self.package / "include", PACKAGE_VERSION) + + def test_the_populated_copy_passes(self): + write_headers(self.source / "include", PIN_VERSION) + r = run_check(f"$", self.source.as_posix(), "PIN") + self.assertEqual(r.returncode, 0, r.stderr) + + def test_other_headers_fail(self): + write_headers(self.source / "include", PIN_VERSION) + r = run_check((self.package / "include").as_posix(), self.source.as_posix(), "PIN") + self.assertNotEqual(r.returncode, 0) + self.assertIn(CHECK_FAILED, " ".join(r.stderr.split())) + + def test_nothing_populated_fails(self): + # find_package() or a provider answered FetchContent_MakeAvailable: vulkanheaders_SOURCE_DIR is empty. + r = run_check((self.package / "include").as_posix(), "", "PIN") + self.assertNotEqual(r.returncode, 0) + self.assertIn("it populated none", " ".join(r.stderr.split())) + + def test_another_version_is_not_the_pin(self): + write_headers(self.source / "include", PIN_VERSION - 1) + for origin in ("PIN", "PIN_CACHED"): + with self.subTest(origin=origin): + r = run_check((self.source / "include").as_posix(), self.source.as_posix(), origin) + self.assertNotEqual(r.returncode, 0) + self.assertIn(f"has VK_HEADER_VERSION {PIN_VERSION - 1}", " ".join(r.stderr.split())) + + def test_a_source_dir_copy_may_be_another_version(self): + write_headers(self.source / "include", PIN_VERSION - 1) + r = run_check((self.source / "include").as_posix(), self.source.as_posix(), "SOURCE_DIR") + self.assertEqual(r.returncode, 0, r.stderr) + + +@unittest.skipUnless(CMAKE, "cmake not on PATH") +class ConfigureTest(unittest.TestCase): + """Configures runtime/ with a fake installed SDK on VULKAN_SDK and a fake copy of the pinned release.""" + + def setUp(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + base = Path(tmp.name).resolve() + self.sdk_include = base / "sdk" / "include" + write_headers(self.sdk_include, SDK_VERSION) + self.pin_source = base / "vulkan-headers" + self.pin_include = self.pin_source / "include" + write_headers(self.pin_include, PIN_VERSION) + write(self.pin_source / "CMakeLists.txt", FAKE_PROJECT) + self.package = base / "package" + self.package_include = self.package / "include" + write_headers(self.package_include, PACKAGE_VERSION) + write(self.package / "VulkanHeadersConfig.cmake", FAKE_PACKAGE) + self.hook = base / "hook.cmake" + write(self.hook, HOOK) + self.base = base + self.env = dict(os.environ, VULKAN_SDK=str(base / "sdk")) + + def run_configure(self, build, *args): + cmd = [CMAKE, "-S", str(RUNTIME), "-B", str(build), f"-DCMAKE_PROJECT_gta6rt_INCLUDE={self.hook.as_posix()}", + "-DBUILD_TESTING=OFF", "-DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF", *args] + r = subprocess.run(cmd, capture_output=True, text=True, timeout=600, env=self.env) + out = r.stdout + r.stderr + if r.returncode and "CMAKE_CXX_COMPILER" in out and not (build / "leonida_test_vulkan_headers.txt").exists(): + self.skipTest("no working C++ compiler for CMake") + return r.returncode, out + + def configure(self, build, *args): + returncode, out = self.run_configure(build, *args) + self.assertEqual(returncode, 0, out[-4000:]) + dirs = included((build / "leonida_test_vulkan_headers.txt").read_text(encoding="utf-8")) + return dirs, headers_line(out) + + def assert_uses(self, dirs, include): + self.assertTrue(any(same_dir(d, include) for d in dirs), f"Vulkan::Headers includes {dirs}, not {include}") + + def assert_pin(self, dirs, line): + self.assert_uses(dirs, self.pin_include) + self.assertFalse(any(same_dir(d, self.sdk_include) for d in dirs), dirs) + self.assertIn(f"(VK_HEADER_VERSION {PIN_VERSION})", line) + self.assertIn("FETCHCONTENT_SOURCE_DIR_VULKANHEADERS", line) + + def test_fetch_wins_over_installed_headers(self): + dirs, line = self.configure(self.base / "build", "-DLEONIDA_VULKAN_HEADERS_FETCH=ON", + f"-DFETCHCONTENT_SOURCE_DIR_VULKANHEADERS={self.pin_source.as_posix()}") + self.assert_pin(dirs, line) + + def seeded_base_dir(self, version): + """A FETCHCONTENT_BASE_DIR that already holds a downloaded copy, for FETCHCONTENT_FULLY_DISCONNECTED=ON.""" + deps = self.base / "deps" + write_headers(deps / "vulkanheaders-src" / "include", version) + write(deps / "vulkanheaders-src" / "CMakeLists.txt", FAKE_PROJECT) + return deps + + def test_always_mode_does_not_swap_in_an_installed_package(self): + # FETCHCONTENT_TRY_FIND_PACKAGE_MODE=ALWAYS used to make FetchContent_MakeAvailable take the installed + # VulkanHeadersConfig.cmake and populate nothing, and the line still said "the pin". + deps = self.seeded_base_dir(PIN_VERSION) + dirs, line = self.configure(self.base / "build", "-DLEONIDA_VULKAN_HEADERS_FETCH=ON", + "-DFETCHCONTENT_TRY_FIND_PACKAGE_MODE=ALWAYS", + f"-DVulkanHeaders_DIR={self.package.as_posix()}", + f"-DFETCHCONTENT_BASE_DIR={deps.as_posix()}", "-DFETCHCONTENT_FULLY_DISCONNECTED=ON") + self.assert_uses(dirs, deps / "vulkanheaders-src" / "include") + self.assertFalse(any(same_dir(d, self.package_include) for d in dirs), dirs) + # The preseeded copy is the pinned version, but no archive was checked in this configure. + self.assertIn(f"(VK_HEADER_VERSION {PIN_VERSION}), the pinned version, cached source: Khronos Vulkan-Headers ", + line) + self.assertIn("archive not checked in this configure (FETCHCONTENT_FULLY_DISCONNECTED=ON)", line) + self.assertNotIn("hash-checked", line) + + def test_a_dependency_provider_cannot_stand_in_for_the_pin(self): + provider = self.base / "provider.cmake" + write(provider, PROVIDER.replace("@INCLUDE@", self.package_include.as_posix())) + returncode, out = self.run_configure(self.base / "build", "-DLEONIDA_VULKAN_HEADERS_FETCH=ON", + f"-DCMAKE_PROJECT_TOP_LEVEL_INCLUDES={provider.as_posix()}") + self.assertNotEqual(returncode, 0, headers_line(out)) + self.assertIn(CHECK_FAILED, " ".join(out.split())) + + def test_a_stale_download_is_not_called_the_pin(self): + # A build folder populated before the pin changed keeps that copy while FETCHCONTENT_FULLY_DISCONNECTED is ON. + deps = self.seeded_base_dir(PIN_VERSION - 1) + returncode, out = self.run_configure(self.base / "build", "-DLEONIDA_VULKAN_HEADERS_FETCH=ON", + f"-DFETCHCONTENT_BASE_DIR={deps.as_posix()}", + "-DFETCHCONTENT_FULLY_DISCONNECTED=ON") + self.assertNotEqual(returncode, 0, headers_line(out)) + self.assertIn(f"has VK_HEADER_VERSION {PIN_VERSION - 1}", " ".join(out.split())) + + def test_turning_fetch_on_in_a_build_that_found_headers(self): + build = self.base / "build" + # The installed SDK is visible: without the fetch it is used (and Vulkan_INCLUDE_DIR is cached). + dirs, line = self.configure(build) + self.assert_uses(dirs, self.sdk_include) + self.assertIn(f"(VK_HEADER_VERSION {SDK_VERSION}), found on this machine, not the pin", line) + # The same build folder with the fetch turned on: the pin, not the cached lookup. + dirs, line = self.configure(build, "-DLEONIDA_VULKAN_HEADERS_FETCH=ON", + f"-DFETCHCONTENT_SOURCE_DIR_VULKANHEADERS={self.pin_source.as_posix()}") + self.assert_pin(dirs, line) + # And off again: back to the installed headers. + dirs, line = self.configure(build, "-DLEONIDA_VULKAN_HEADERS_FETCH=OFF", + "-UFETCHCONTENT_SOURCE_DIR_VULKANHEADERS") + self.assert_uses(dirs, self.sdk_include) + self.assertIn("not the pin", line) + + +class CMakeListsTest(unittest.TestCase): + def setUp(self): + self.text = CMAKE_LISTS.read_text(encoding="utf-8") + + def test_fetch_comes_before_find_package(self): + fetch = re.search(r"^if\(LEONIDA_VULKAN_HEADERS_FETCH OR FETCHCONTENT_SOURCE_DIR_VULKANHEADERS\)$", self.text, + re.M) + self.assertIsNotNone(fetch, "the top-level fetch branch is missing from runtime/CMakeLists.txt") + self.assertLess(fetch.start(), self.text.index("\nfind_package(Vulkan QUIET)\n")) + + def fetch_branch(self): + m = re.search(r"^if\(LEONIDA_VULKAN_HEADERS_FETCH OR FETCHCONTENT_SOURCE_DIR_VULKANHEADERS\)\n(.*?)^endif\(\)$", + self.text, re.S | re.M) + self.assertIsNotNone(m, "the top-level fetch branch is missing from runtime/CMakeLists.txt") + return m + + def test_headers_line_is_printed_with_the_tools(self): + m = re.search(r"^if\(TARGET Vulkan::Headers\)\n(.*?)^ foreach\(tool ", self.text, re.S | re.M) + self.assertIsNotNone(m, "if(TARGET Vulkan::Headers) ... foreach(tool ...) not found") + self.assertIn("leonida_vulkan_headers_report(", m.group(1)) + self.assertLess(self.text.index("/cmake/vulkan_headers_report.cmake\")"), self.fetch_branch().start()) + + def test_the_fetch_is_checked(self): + branch = self.fetch_branch().group(1) + declare = re.search(r"^ FetchContent_Declare\(VulkanHeaders\n(.*?)^ FetchContent_MakeAvailable\(VulkanHeaders\)$", + branch, re.S | re.M) + self.assertIsNotNone(declare, "FetchContent_Declare(VulkanHeaders ...) not found in the fetch branch") + code = "\n".join(ln for ln in declare.group(1).splitlines() if not ln.lstrip().startswith("#")) + self.assertIn("OVERRIDE_FIND_PACKAGE", code) + self.assertLess(branch.index("FetchContent_MakeAvailable(VulkanHeaders)"), + branch.index("leonida_vulkan_headers_check(")) + + def test_pin_matches_the_ci_checkout(self): + m = re.search(r"^set\(LEONIDA_VULKAN_HEADERS_PIN ([0-9a-f]{40})\)$", self.text, re.M) + self.assertIsNotNone(m, "set(LEONIDA_VULKAN_HEADERS_PIN ) not found") + self.assertIn("Vulkan-Headers/archive/${LEONIDA_VULKAN_HEADERS_PIN}.tar.gz", self.text) + ci = (ROOT / ".github" / "workflows" / "runtime.yml").read_text(encoding="utf-8") + refs = re.findall(r"^\s*ref: ([0-9a-f]{40}) # vulkan-sdk-[0-9]+\.[0-9]+\.([0-9]+)\.[0-9]+$", ci, re.M) + self.assertTrue(refs, "no Vulkan-Headers checkout in runtime.yml") + self.assertEqual({commit for commit, _ in refs}, {m.group(1)}) + # The SDK version's third number is the VK_HEADER_VERSION the configure checks the download against. + self.assertIsNotNone(PIN_VERSION, "set(LEONIDA_VULKAN_HEADERS_PIN_VERSION ) not found") + self.assertEqual({int(version) for _, version in refs}, {PIN_VERSION}) + + +if __name__ == "__main__": + unittest.main()