90 lines
3.5 KiB
Python
90 lines
3.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'
|
|
""") |