Auto-install libclang for bindgen on Windows + NVIDIA RTX

The DLSS build failed on Windows at dlss_wgpu's bindgen step: libclang.dll
is neither in the DLSS SDK nor in the Vulkan headers. It is now part of the
same zero-manual toolchain.

Detection first, download last: an explicit LIBCLANG_PATH (directory with
libclang.dll or clang.dll) wins and disables auto-install; otherwise PATH,
Program Files\LLVM\bin (both Program Files roots) and LLVM shipped with
Visual Studio (vswhere -find VC\Tools\Llvm\x64\bin\libclang.dll) are
checked, then the cache.

When nothing is found, the NuGet package libclang.runtime.win-x64 is
downloaded (~34 MB, version from the flat-container index with pinned
23.1.5 fallback) and only runtimes/win-x64/native/libclang.dll (~89 MB) is
extracted to .horizon-cache/libclang/<version>/ behind the existing
staging + airdef-install.json marker pattern. The resolved directory is
passed to cargo as LIBCLANG_PATH.

libclang is resolved only when the DLSS SDK and Vulkan headers are already
available, so a missing SDK never triggers a useless download. Any
detection/download/extract failure warns and degrades to mods/TAAU, and
non-Windows/non-RTX still returns before any network access.

Tests cover PATH/Program Files/vswhere detection, explicit precedence,
numeric NuGet version choice and fallback, selective extraction and marker,
cache hit, damaged cache reinstall, package without the DLL, plan env
wiring and failure to mods-only.

Co-Authored-By: Kimi Code <noreply@moonshot.ai>
This commit is contained in:
2026-11-21 15:55:12 +03:00
parent e61b2e8f64
commit 67e8a19e2b
2 changed files with 410 additions and 28 deletions
+178 -15
View File
@@ -2,7 +2,7 @@
"""AirDef: сборка, установка и запуск на Windows, macOS и Linux. Только стандартная библиотека Python.
python3 tools/airdef.py fork # инициализировать закреплённый сабмодуль Horizon (horizon/)
python3 tools/airdef.py game # собрать игру (форк) — нужен Rust; на Windows + NVIDIA RTX сам скачивает DLSS SDK
python3 tools/airdef.py game # собрать игру (форк) — нужен Rust; на Windows + NVIDIA RTX сам скачивает DLSS SDK, Vulkan-заголовки и libclang
python3 tools/airdef.py mod # собрать мод airdef.hmod — нужен Rust с wasm32
python3 tools/airdef.py install # положить мод в папку игры и включить
python3 tools/airdef.py play [аргументы игры] # запустить форк на папке игры
@@ -13,9 +13,10 @@
Переменные окружения: HORIZON_HOME (папка игры), HORIZON_RESOURCES (подготовленные ресурсы
Minecraft 26.3), AIRDEF_GAME (готовый horizon-client вместо собранного).
На Windows + NVIDIA RTX сборка с DLSS (feature dlss) сама скачивает DLSS SDK и заголовки Vulkan
в .horizon-cache/ и кладёт nvngx_dlss.dll рядом с horizon-client.exe. Явные DLSS_SDK и VULKAN_SDK
по-прежнему имеют приоритет над автоскачиванием; при сбое сети собирается обычный mods/TAAU.
На Windows + NVIDIA RTX сборка с DLSS (feature dlss) сама скачивает DLSS SDK, заголовки Vulkan и
libclang для bindgen в .horizon-cache/ и кладёт nvngx_dlss.dll рядом с horizon-client.exe. Явные
DLSS_SDK, VULKAN_SDK и LIBCLANG_PATH по-прежнему имеют приоритет над автоскачиванием; при сбое сети
собирается обычный mods/TAAU.
"""
import glob
import hashlib
@@ -139,9 +140,19 @@ VULKAN_HEADERS = (
"vk_video/vulkan_video_codec_vp9std_decode.h", "vk_video/vulkan_video_codecs_common.h",
)
# bindgen из dlss_wgpu ищет libclang.dll; в DLSS SDK и Vulkan-заголовках его нет.
LIBCLANG_NUGET = "libclang.runtime.win-x64"
LIBCLANG_INDEX = "https://api.nuget.org/v3-flatcontainer/libclang.runtime.win-x64/index.json"
LIBCLANG_PACKAGE = ("https://api.nuget.org/v3-flatcontainer/libclang.runtime.win-x64/{version}/"
"libclang.runtime.win-x64.{version}.nupkg")
LIBCLANG_VERSION_PINNED = "23.1.5"
LIBCLANG_NATIVE = "runtimes/win-x64/native/libclang.dll"
LIBCLANG_URL = "https://www.nuget.org/packages/libclang.runtime.win-x64"
CACHE = ROOT / ".horizon-cache"
DLSS_CACHE = CACHE / "dlss-sdk"
VULKAN_CACHE = CACHE / "vulkan-headers"
LIBCLANG_CACHE = CACHE / "libclang"
INSTALL_MARKER = "airdef-install.json"
@@ -154,7 +165,8 @@ def open_url(url):
return urllib.request.urlopen(request, timeout=120)
def github_json(url):
def fetch_json(url):
"""JSON с GitHub API или nuget.org: общий транспорт для версий и списков файлов."""
with open_url(url) as response:
return json.load(response)
@@ -195,7 +207,7 @@ def download(url, dest):
def dlss_latest_tag():
"""Новейший стабильный тег v310.9.x с GitHub; при сбое API — закреплённый v310.9.1."""
try:
tags = github_json(DLSS_TAGS_API)
tags = fetch_json(DLSS_TAGS_API)
except Exception as exc:
print(f"Внимание: не удалось получить теги DLSS ({exc}); беру {DLSS_TAG_PINNED}.", flush=True)
return DLSS_TAG_PINNED
@@ -214,7 +226,7 @@ def dlss_latest_tag():
def dlss_include_files(tag):
"""Список include/*.h тега из API; при сбое — закреплённый набор v310.9.x."""
try:
entries = github_json(f"https://api.github.com/repos/NVIDIA/DLSS/contents/include?ref={tag}")
entries = fetch_json(f"https://api.github.com/repos/NVIDIA/DLSS/contents/include?ref={tag}")
names = [entry["name"] for entry in entries if isinstance(entry, dict)
and str(entry.get("name", "")).endswith(".h")]
if names:
@@ -333,6 +345,132 @@ def install_vulkan_headers(tag=VULKAN_TAG_PINNED):
return dest
def version_key(text):
"""Числовой ключ версии NuGet: 23.1.10 новее 23.1.5 (строкой было бы наоборот)."""
numbers = tuple(int(part) for part in re.findall(r"\d+", str(text)))
return numbers or (0,)
def libclang_root(path):
"""Каталог, в котором bindgen найдёт libclang.dll (или clang.dll), либо None."""
if not path:
return None
path = Path(path)
return path if any((path / name).is_file() for name in ("libclang.dll", "clang.dll")) else None
def vs_libclang():
"""LLVM из Visual Studio (VC\\Tools\\Llvm\\x64\\bin) через vswhere; вне Windows — None."""
if platform.system() != "Windows":
return None
base = os.environ.get("ProgramFiles(x86)") or os.environ.get("ProgramFiles")
vswhere = Path(base) / "Microsoft Visual Studio" / "Installer" / "vswhere.exe" if base else None
if vswhere is None or not vswhere.is_file():
return None
flags = getattr(subprocess, "CREATE_NO_WINDOW", 0)
try:
out = subprocess.run([str(vswhere), "-products", "*", "-latest",
"-find", "VC\\Tools\\Llvm\\x64\\bin\\libclang.dll"],
capture_output=True, text=True, timeout=30,
creationflags=flags, errors="replace")
except (OSError, ValueError, subprocess.SubprocessError):
return None
for line in out.stdout.splitlines():
found = libclang_root(Path(line.strip()).parent)
if found:
return found
return None
def system_libclang():
"""Уже установленный libclang: PATH, Program Files\\LLVM\\bin, LLVM из Visual Studio."""
for entry in os.environ.get("PATH", "").split(os.pathsep):
found = libclang_root(entry)
if found:
return found
for env in ("ProgramFiles", "ProgramFiles(x86)"):
if os.environ.get(env):
found = libclang_root(Path(os.environ[env]) / "LLVM" / "bin")
if found:
return found
return vs_libclang()
def libclang_latest_version():
"""Последняя стабильная версия libclang.runtime.win-x64 с NuGet; pinned при сбое API."""
try:
data = fetch_json(LIBCLANG_INDEX)
versions = [v for v in data.get("versions", [])
if isinstance(v, str) and "-" not in v and version_key(v) != (0,)]
except Exception as exc:
print(f"Внимание: не удалось получить версии libclang ({exc}); беру {LIBCLANG_VERSION_PINNED}.",
flush=True)
return LIBCLANG_VERSION_PINNED
if not versions:
print(f"Внимание: NuGet не вернул стабильные версии libclang; беру {LIBCLANG_VERSION_PINNED}.",
flush=True)
return LIBCLANG_VERSION_PINNED
return max(versions, key=version_key)
def libclang_cache_ok(path, version):
"""Кэш libclang: маркер этой версии и DLL того же размера (иначе — перекачать)."""
state = read_marker(path)
dll = Path(path) / "libclang.dll"
return bool(state) and state.get("tag") == version and dll.is_file() \
and state.get("dll_bytes") == dll.stat().st_size
def cached_libclang():
try:
candidates = sorted(LIBCLANG_CACHE.iterdir(), key=lambda p: version_key(p.name), reverse=True)
except OSError:
return None
for path in candidates:
if libclang_cache_ok(path, path.name):
return path
return None
def install_libclang(version=None):
"""libclang.dll из NuGet-пакета libclang.runtime.win-x64 в .horizon-cache/libclang/<ver>.
Пакет (~34 МБ) распаковывается выборочно: нужен только runtimes/win-x64/native/libclang.dll.
"""
version = version or libclang_latest_version()
dest = LIBCLANG_CACHE / version
if libclang_cache_ok(dest, version):
print(f"libclang {version}: уже в кэше {dest}", flush=True)
return dest
url = LIBCLANG_PACKAGE.format(version=version)
print(f"libclang {version}: скачиваю {LIBCLANG_NUGET} (~34 МБ) с nuget.org", flush=True)
staging = LIBCLANG_CACHE / f".{version}.part"
shutil.rmtree(staging, ignore_errors=True)
package = staging / f"libclang.runtime.win-x64.{version}.nupkg"
try:
download(url, package)
with zipfile.ZipFile(package) as archive:
with archive.open(LIBCLANG_NATIVE) as src, open(staging / "libclang.dll", "wb") as out:
shutil.copyfileobj(src, out)
package.unlink()
dll = staging / "libclang.dll"
if not dll.is_file() or dll.stat().st_size == 0:
raise DownloadError(f"libclang {version}: в пакете нет {LIBCLANG_NATIVE}")
(staging / INSTALL_MARKER).write_text(json.dumps({
"tag": version, "source": LIBCLANG_URL,
"dll_bytes": dll.stat().st_size}, indent=2) + "\n", encoding="utf-8")
shutil.rmtree(dest, ignore_errors=True)
staging.rename(dest)
except (KeyError, zipfile.BadZipFile) as exc:
shutil.rmtree(staging, ignore_errors=True)
raise DownloadError(f"libclang {version}: {exc}") from exc
except Exception:
shutil.rmtree(staging, ignore_errors=True)
raise
print(f"libclang {version}: готов, {dest / 'libclang.dll'}", flush=True)
return dest
def gpu_names():
"""Имена видеоадаптеров Windows. Сбой опроса — пустой список (это «нет DLSS», а не ошибка сборки)."""
if platform.system() != "Windows":
@@ -391,11 +529,12 @@ def _auto_install(install, label):
def dlss_build_plan(system=None, gpus=None, sdk=None) -> dict:
"""Features сборки и окружение DLSS_SDK/VULKAN_SDK для Windows + NVIDIA RTX.
"""Features сборки и окружение DLSS_SDK/VULKAN_SDK/LIBCLANG_PATH для Windows + NVIDIA RTX.
Возвращает {"features", "env", "sdk", "dll"}. Без явного SDK на Windows + RTX сам скачивает
DLSS SDK и заголовки Vulkan в .horizon-cache. Явные DLSS_SDK/VULKAN_SDK (или аргумент sdk)
имеют приоритет и отключают автоскачивание; неполный явный путь — предупреждение и mods.
DLSS SDK, заголовки Vulkan и libclang для bindgen в .horizon-cache. Явные
DLSS_SDK/VULKAN_SDK/LIBCLANG_PATH (или аргумент sdk) имеют приоритет и отключают автоскачивание
своего компонента; неполный явный путь — предупреждение и mods.
"""
plan = {"features": "mods", "env": {}, "sdk": None, "dll": None}
system = platform.system() if system is None else system
@@ -427,27 +566,51 @@ def dlss_build_plan(system=None, gpus=None, sdk=None) -> dict:
if vulkan_root is None:
vulkan_root = _auto_install(install_vulkan_headers, "Vulkan-заголовки")
# libclang нужен только bindgen'у из dlss_wgpu, поэтому качаем его лишь когда SDK и Vulkan уже есть.
env_libclang = os.environ.get("LIBCLANG_PATH")
explicit_libclang = bool(env_libclang)
clang_root = None
if sdk_root is not None and vulkan_root is not None:
if env_libclang:
clang_root = libclang_root(env_libclang)
else:
clang_root = system_libclang() or cached_libclang()
if clang_root is None:
clang_root = _auto_install(install_libclang, "libclang для bindgen")
missing = []
if sdk_root is None:
missing.append("NVIDIA DLSS SDK")
if vulkan_root is None:
missing.append("заголовки Vulkan")
if sdk_root is not None and vulkan_root is not None and clang_root is None:
missing.append("libclang для bindgen")
if missing:
print("Внимание: NVIDIA RTX найден, но DLSS-сборка пропущена — нет " + " и ".join(missing) + ".",
flush=True)
if explicit_sdk or explicit_vulkan:
print(" DLSS_SDK/VULKAN_SDK заданы, но путь неполный. Убери их, и airdef.py скачает SDK сам.",
incomplete = []
if explicit_sdk and sdk_root is None:
incomplete.append("DLSS_SDK")
if explicit_vulkan and vulkan_root is None:
incomplete.append("VULKAN_SDK")
if explicit_libclang and clang_root is None and sdk_root is not None and vulkan_root is not None:
incomplete.append("LIBCLANG_PATH")
if incomplete:
print(f" {', '.join(incomplete)}: путь неполный. Убери переменную, и airdef.py скачает всё сам.",
flush=True)
else:
print(" Автоскачивание не удалось: проверь сеть/прокси и повтори сборку.", flush=True)
print(f" Вручную: DLSS SDK {DLSS_SDK_URL} (переменная DLSS_SDK), Vulkan SDK или только "
f"заголовки (VULKAN_SDK, {VULKAN_SDK_URL}).", flush=True)
f"заголовки (VULKAN_SDK, {VULKAN_SDK_URL}); libclang — winget install LLVM.LLVM "
"(переменная LIBCLANG_PATH).", flush=True)
print(" Собираю --features mods: DLSS выключен, native TAAU остаётся.", flush=True)
return plan
plan.update(features="mods,dlss", sdk=sdk_root, dll=Path(sdk_root) / DLSS_RUNTIME,
env={"DLSS_SDK": str(sdk_root), "VULKAN_SDK": str(vulkan_root)})
print(f"DLSS: NVIDIA RTX, SDK {sdk_root}; собираю --features mods,dlss", flush=True)
env={"DLSS_SDK": str(sdk_root), "VULKAN_SDK": str(vulkan_root),
"LIBCLANG_PATH": str(clang_root)})
print(f"DLSS: NVIDIA RTX, SDK {sdk_root}, libclang {clang_root}; собираю --features mods,dlss",
flush=True)
print(" nvngx_dlss.dll из SDK кладу рядом с horizon-client.exe после сборки; DLSS включён по умолчанию — переключатель в настройках мода (N → DLSS), HORIZON_DLSS=1|0 переопределяет.", flush=True)
return plan