114 lines
4.5 KiB
Python
114 lines
4.5 KiB
Python
# !/usr/bin/env python3
|
||
"""
|
||
SDL3_GPU Slang Compiler - Slangc Finder
|
||
查找和验证Slangc编译器路径
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
import shutil
|
||
import subprocess
|
||
|
||
class SlangcFinder:
|
||
@staticmethod
|
||
def find_slangc() -> str:
|
||
"""查找slangc编译器路径"""
|
||
|
||
# 方法1: 使用shutil.which (推荐)
|
||
slangc_path = shutil.which('slangc')
|
||
if slangc_path:
|
||
try:
|
||
subprocess.run([slangc_path, '-v'],
|
||
capture_output=True, check=True, timeout=10)
|
||
print(f"Found slangc at: {slangc_path}")
|
||
return slangc_path
|
||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
|
||
pass
|
||
|
||
# 方法2: 从环境变量获取
|
||
slangc_from_env = os.environ.get('SLANGC_PATH')
|
||
if slangc_from_env and os.path.isfile(slangc_from_env):
|
||
try:
|
||
subprocess.run([slangc_from_env, '-v'],
|
||
capture_output=True, check=True, timeout=10)
|
||
print(f"Found slangc from SLANGC_PATH: {slangc_from_env}")
|
||
return slangc_from_env
|
||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
|
||
pass
|
||
|
||
# 方法3: 在常见位置搜索
|
||
common_paths = [
|
||
r'C:\Program Files\Slang\bin\slangc.exe',
|
||
r'C:\Program Files (x86)\Slang\bin\slangc.exe',
|
||
r'C:\tools\slang\bin\slangc.exe',
|
||
r'C:\slang\bin\slangc.exe'
|
||
]
|
||
|
||
# 也尝试从PATH中的每个目录查找
|
||
path_dirs = os.environ.get('PATH', '').split(os.pathsep)
|
||
for path_dir in path_dirs:
|
||
if path_dir: # 跳过空路径
|
||
potential_path = os.path.join(path_dir, 'slangc.exe')
|
||
common_paths.append(potential_path)
|
||
|
||
for path in common_paths:
|
||
if os.path.isfile(path):
|
||
try:
|
||
subprocess.run([path, '-v'],
|
||
capture_output=True, check=True, timeout=10)
|
||
print(f"Found slangc at: {path}")
|
||
return path
|
||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
|
||
continue
|
||
|
||
# 方法4: 尝试直接调用 (有时候PATH在Python中不完整)
|
||
try:
|
||
result = subprocess.run(['slangc', '-v'],
|
||
capture_output=True, check=True, timeout=10)
|
||
print("Found slangc in PATH")
|
||
return 'slangc'
|
||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired, FileNotFoundError):
|
||
pass
|
||
|
||
# 如果都失败了,显示调试信息
|
||
print("Debug information:")
|
||
print(f"Current working directory: {os.getcwd()}")
|
||
print(f"Python executable: {sys.executable}")
|
||
print(f"PATH environment variable:")
|
||
for i, path in enumerate(os.environ.get('PATH', '').split(os.pathsep)):
|
||
print(f" {i}: {path}")
|
||
|
||
raise RuntimeError("""
|
||
Cannot find slangc compiler. Please try one of the following:
|
||
Set SLANGC_PATH environment variable to the full path of slangc.exe
|
||
Ensure slangc.exe is in your PATH and restart your terminal
|
||
Install Slang to a standard location like C:\\Program Files\\Slang\\bin\\
|
||
Run the script from the same terminal where 'slangc --version' works
|
||
You can also specify the path directly when creating the compiler:
|
||
compiler = SDL3GPUSlangCompiler()
|
||
compiler.slangc_path = r'C:\\path\\to\\slangc.exe'
|
||
""")
|
||
|
||
slangc_path = SlangcFinder.find_slangc()
|
||
|
||
# 如果是windows系统,检查DXC编译器
|
||
if os.name == 'nt':
|
||
# 查找dxcapsviewer.exe路径, 避免查找dxc时找到Vulkan SDK的dxc.exe
|
||
dxcapsviewer_path = shutil.which('dxcapsviewer.exe')
|
||
if dxcapsviewer_path:
|
||
# dxc.exe一般与dxcapsviewer.exe在同一目录
|
||
dxc_path = os.path.join(os.path.dirname(dxcapsviewer_path), 'dxc.exe')
|
||
# 检查dxc.exe是否存在
|
||
if not os.path.isfile(dxc_path):
|
||
print("Warning: dxc.exe not found in the same directory as dxcapsviewer.exe.")
|
||
dxc_path = None
|
||
else:
|
||
# 如果没有找到dxcapsviewer.exe,尝试直接查找dxc.exe
|
||
dxc_path = shutil.which('dxc.exe')
|
||
|
||
# 检查是否有dxil.dll
|
||
if dxc_path:
|
||
dxil_dll_path = os.path.join(os.path.dirname(dxc_path), 'dxil.dll')
|
||
if not os.path.isfile(dxil_dll_path):
|
||
print("Warning: dxil.dll not found in the same directory as dxc.exe.")
|
||
dxc_path = None |