|
1 | 1 | # ============================================================ |
2 | 2 | # Windows Example Launcher Generator |
3 | 3 | # ============================================================ |
4 | | -# 为每个 example 可执行文件生成 Windows 启动脚本 (.bat) |
| 4 | +# 为每个 example 可执行文件生成 Windows 启动脚本 (.ps1) |
5 | 5 | # |
6 | 6 | # 功能: |
7 | | -# - 自动生成 .bat 启动脚本 |
| 7 | +# - 自动生成 .ps1 启动脚本 |
8 | 8 | # - 设置 PATH 指向共享的 runtimes/ 目录 |
9 | | -# - 提供环境隔离保护 |
| 9 | +# - 设置 QT_PLUGIN_PATH |
10 | 10 | # - Linux 下跳过 |
11 | 11 | # ============================================================ |
12 | 12 |
|
@@ -50,32 +50,53 @@ function(cf_generate_launcher_script TARGET_NAME CATEGORY OUTPUT_DIR) |
50 | 50 |
|
51 | 51 | # 获取可执行文件名 (带扩展名) |
52 | 52 | set(EXE_NAME "${TARGET_NAME}.exe") |
53 | | - set(BAT_NAME "${TARGET_NAME}.bat") |
| 53 | + set(PS1_NAME "${TARGET_NAME}.ps1") |
54 | 54 |
|
55 | 55 | # 启动脚本内容 |
56 | | - set(LAUNCHER_CONTENT "@echo off |
57 | | -REM Launcher for ${TARGET_NAME} |
58 | | -REM This script sets up the PATH to find shared Qt DLLs in ../runtimes/ |
| 56 | + set(LAUNCHER_CONTENT "# Launcher for ${TARGET_NAME} |
| 57 | +# This script sets up the PATH to find shared Qt DLLs in ../../runtimes/ |
59 | 58 |
|
60 | | -setlocal |
| 59 | +\$ScriptDir = Split-Path -Parent \$MyInvocation.MyCommand.Path |
| 60 | +# From examples/{category}/ go up two levels to reach build_develop/, then into runtimes/ |
| 61 | +\$RuntimesDir = Join-Path \$ScriptDir \"..\\..\\runtimes\" -Resolve |
61 | 62 |
|
62 | | -REM Get the directory where this script is located |
63 | | -set \"SCRIPT_DIR=%~dp0\" |
| 63 | +Write-Host \"[Launcher] ScriptDir: \$ScriptDir\" -ForegroundColor Cyan |
| 64 | +Write-Host \"[Launcher] RuntimesDir: \$RuntimesDir\" -ForegroundColor Cyan |
| 65 | +Write-Host \"[Launcher] Exe: ${EXE_NAME}\" -ForegroundColor Cyan |
64 | 66 |
|
65 | | -REM Add runtimes directory to PATH (relative to script location) |
66 | | -REM The runtimes/ directory is at the same level as examples/ |
67 | | -set \"PATH=%SCRIPT_DIR%..\\runtimes;%PATH%\" |
| 67 | +if (-not (Test-Path \$RuntimesDir)) { |
| 68 | + Write-Host \"[Launcher] ERROR: RuntimesDir not found!\" -ForegroundColor Red |
| 69 | + exit 1 |
| 70 | +} |
68 | 71 |
|
69 | | -REM Launch the executable |
70 | | -REM %* passes all command line arguments to the executable |
71 | | -start \"\" \"%SCRIPT_DIR%%EXE_NAME%\" %*\ |
| 72 | +# Clear Qt environment to avoid conflicts |
| 73 | +\$env:QT_PLUGIN_PATH = \$null |
| 74 | +\$env:QML2_IMPORT_PATH = \$null |
72 | 75 |
|
73 | | -endlocal") |
| 76 | +# Set PATH with runtimes FIRST to avoid loading wrong DLLs |
| 77 | +\$env:PATH=\"\$RuntimesDir;\$env:PATH\" |
74 | 78 |
|
75 | | - # 写入 .bat 文件 |
76 | | - file(WRITE "${OUTPUT_DIR}/${BAT_NAME}" "${LAUNCHER_CONTENT}") |
| 79 | +# List Qt DLLs being loaded |
| 80 | +Write-Host \"[Launcher] Qt DLLs in runtimes:\" -ForegroundColor Cyan |
| 81 | +Get-ChildItem \$RuntimesDir -Filter \"Qt6*.dll\" | ForEach-Object { Write-Host \" \$($_.Name)\" } |
77 | 82 |
|
78 | | - log_info("Launcher" "Generated: ${OUTPUT_DIR}/${BAT_NAME}") |
| 83 | +# Set Qt plugin path |
| 84 | +\$env:QT_PLUGIN_PATH=\"\$RuntimesDir\" |
| 85 | +
|
| 86 | +Write-Host \"[Launcher] Starting executable...\" -ForegroundColor Green |
| 87 | +
|
| 88 | +# Launch the executable |
| 89 | +\$exePath = Join-Path \$ScriptDir \"${EXE_NAME}\" |
| 90 | +Write-Host \"[Launcher] Exe path: \$exePath\" -ForegroundColor Cyan |
| 91 | +
|
| 92 | +Start-Process -FilePath \$exePath -ArgumentList \$args -Wait |
| 93 | +Write-Host \"[Launcher] Exit code: \$LASTEXITCODE\" -ForegroundColor Yellow |
| 94 | +") |
| 95 | + |
| 96 | + # 写入 .ps1 文件 |
| 97 | + file(WRITE "${OUTPUT_DIR}/${PS1_NAME}" "${LAUNCHER_CONTENT}") |
| 98 | + |
| 99 | + log_info("Launcher" "Generated: ${OUTPUT_DIR}/${PS1_NAME}") |
79 | 100 | endfunction() |
80 | 101 |
|
81 | 102 | # ============================================================ |
|
0 commit comments