模拟器构建入口改VS版本自适应(新增build_sim,兼容VS2019/2022/2026);文档同步并补SDL2 SDK下载步骤

This commit is contained in:
2026-09-21 02:30:16 +08:00
parent d0ec983cdd
commit bad5ceac0d
9 changed files with 246 additions and 51 deletions
+14 -12
View File
@@ -12,6 +12,7 @@
```
simulator/
├── CMakeLists.txt # 构建脚本(CMake + MSVC,x64)
├── build_sim.bat / .ps1 # 一键构建入口(自动探测 VS2019/2022/2026 生成器,x64;.bat 绕过执行策略)
├── main.c # 入口:SDL 初始化 → LVGL → 菜单 → 主循环
├── sim/ # 平台层(PC 专用实现;MCU 端对应实现在各 BSP 目录)
│ ├── sim_port.c / .h # 显示/输入端口(SDL 窗口 + 键盘 + mono 主题),替代 MCU 端 ui_port.c
@@ -49,6 +50,7 @@ simulator/
| `sim/cfg_file_store_sim.c` | 通用配置文件存储后端:本地文件 `sim_config.ini`(`fopen`/`rename` 原子写),替代 MCU 端 `App/cfg_file_store_mcu.c`(FatFS);旧档 `sim_netcfg.bin` 首启自动迁移进 `sim_config.ini` |
| `lv_conf.h` | 模拟器专用 LVGL 配置。与 MCU 端 `Lib/lv_conf.h` 的关键差异见 §3.3 |
| `dbg_log.h` | MCU 版 `dbg_log.h` 依赖 `sys_clock.h`(RTC 时间戳),PC 无法编译。此 stub 把 `DBG_INFO/ERROR/DEBUG` 映射到 `printf`;因 `simulator/` 在 include 路径最前,**优先生效**覆盖 MCU 版 |
| `build_sim.bat` / `build_sim.ps1` | 一键构建入口:vswhere 探测本机 VS 大版本(16=2019 / 17=2022 / 18=2026,取已装最高)→ 映射 CMake 生成器;`build/` 缓存生成器与本机不符(换机/升级 VS)时自动删除重建(带重试,抗文件锁)。**构建入口一律走它,文档/命令中勿写死生成器版本**;`.bat` 仅负责绕过 PowerShell 执行策略并转交 `.ps1` |
| `CMakeLists.txt` | 收集 LVGL 源码(`../Lib/lvgl`)+ 共享 UI 源码(`../Drivers/BSP/UI`)+ 通用配置文件引擎(`../App/cfg_file.c`、`../Drivers/BSP/NETCFG` 的 net_cfg/net_cfg_legacy)+ `sim/` 平台层,链接预编译 SDL2,产出 `lvgl_sim.exe` |
| `i1_to_png.py` | 把 `--screenshot` 产出的 I1(1bpp) 原始位图转成 PNG,便于离线比对设计稿。详见 §3.5 |
| `tests/` | 无头回归脚本(菜单 8 状态像素回归、内容页文本断言 + 黄金参考回归);`ref/` 为**本地生成物、不入库**(首次 `--update` 生成)。详见 §3.6 |
@@ -118,7 +120,7 @@ LVGL 场景(同一份 ui_menu.c)
### 2.4 关键设计决策
1. **SDL2**:LVGL 官方 PC 端口同款显示方案;使用仓库内预编译 SDK(`SDL2/SDL2-2.32.10/`),无需系统安装
2. **CMake + MSVC**:`Visual Studio 16 2019` 生成器,`-A x64`;MSVC 下加 `/utf-8` 消除中文注释的 C4819 警告
2. **CMake + MSVC**:`build_sim.ps1` 按本机 VS 版本自动选生成器(16=2019 / 17=2022 / 18=2026;不同机器版本不同,**勿写死**),`-A x64`;MSVC 下加 `/utf-8` 消除中文注释的 C4819 警告
3. **共享 UI 源码**:`ui_menu.c` / `ui_font_cn_12.c` 直接编译 `Drivers/BSP/UI/` 原件,改一处两端同时生效
4. **纯键盘导航**:不创建鼠标/触摸设备,与 MCU 端 keypad 操作语义一致(↑↓←→ / Enter / Esc)
5. **UI 为主**:不含 CH395F 网络 / NAND 存储 / ADC 等硬件业务;通用配置文件引擎(`cfg_file`)与 `net_cfg` 核心为纯软件,配置加载/保存/旧档迁移可在模拟器离线验证(`sim_config.ini` / `sim_netcfg.bin`)
@@ -132,27 +134,27 @@ LVGL 场景(同一份 ui_menu.c)
| 依赖 | 版本 | 说明 |
|------|------|------|
| CMake | ≥ 3.15 | 本机验证 4.3.0-rc1 |
| MSVC | VS2019 BuildTools | `Visual Studio 16 2019` 生成器,Hostx64/x64 |
| MSVC | VS2019 / 2022 / 2026 任一 | `build_sim.ps1` 自动探测生成器(16=2019 / 17=2022 / 18=2026,取已装最高),Hostx64/x64 |
| SDL2 | 2.32.10 | 已内置于 `SDL2/`,无需安装 |
| 操作系统 | Windows | 已验证 win32 |
### 3.2 构建命令
```powershell
# 首次(配置 + 构建)
cd simulator
cmake -B build -G "Visual Studio 16 2019" -A x64
cmake --build build --config Debug
# 日常(仅改 .c/.h 时增量编译)
cmake --build build --config Debug
# 一键构建(仓库根目录执行;自动探测本机 VS2019/2022/2026 生成器,
# build/ 缓存生成器与本机不符时自动删除重建)
& "simulator\build_sim.bat"
# 运行
.\build\Debug\lvgl_sim.exe
# 或:cmake --build build --config Debug --target run
.\simulator\build\Debug\lvgl_sim.exe
# 或:cmake --build simulator\build --config Debug --target run
# 全量重建
& "simulator\build_sim.bat" -Clean
```
> 改 `CMakeLists.txt` 后需重新 `cmake -B build`;遇到奇怪问题先删 `build/` 全量重建。
> 改 `CMakeLists.txt` 后再跑一次 `build_sim.bat` 即可(缓存不匹配会自动重建);遇到奇怪问题加 `-Clean` 全量重建。
> 手动 CMake 等效:`cd simulator; cmake -B build -G "Visual Studio <NN> <YYYY>" -A x64; cmake --build build --config Debug`,生成器号须与本机 VS 大版本一致(16=2019 / 17=2022 / 18=2026)。
### 3.3 `lv_conf.h` 与 MCU 端的关键差异
+24
View File
@@ -0,0 +1,24 @@
@echo off
rem ============================================================
rem LVGL PC simulator one-shot build (VS2019/2022/2026 auto-detect, x64)
rem Usage (from repo root, any shell):
rem & "simulator\build_sim.bat" incremental build
rem & "simulator\build_sim.bat" -Clean full rebuild
rem & "simulator\build_sim.bat" -Config Release Release build
rem Logic lives in build_sim.ps1 (vswhere probe -> generator mapping);
rem this bat only bypasses the PowerShell execution policy and
rem forwards the arguments. Exit code = build result (0 ok / 1 fail / 3 env).
rem NOTE: keep this file PURE ASCII - cmd parses .bat in the ANSI
rem codepage; UTF-8 Chinese here corrupts the command lines.
rem ============================================================
setlocal
where pwsh >nul 2>nul
if %errorlevel%==0 (
pwsh -NoProfile -ExecutionPolicy Bypass -File "%~dp0build_sim.ps1" %*
goto :done
)
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0build_sim.ps1" %*
:done
exit /b %errorlevel%
+138
View File
@@ -0,0 +1,138 @@
#Requires -Version 5.1
<#
.SYNOPSIS
LVGL PC 模拟器一键构建(Visual Studio 版本自适应,x64)。
.DESCRIPTION
不同开发机安装的 VS 版本不同,CMake 生成器必须与本机 VS 大版本一致,
因此构建入口不写死生成器:本脚本用 vswhere 自动探测本机 Visual Studio
(VS / BuildTools 均可),按大版本映射 CMake 生成器,取已安装的最高版本:
16 = "Visual Studio 16 2019"
17 = "Visual Studio 17 2022"
18 = "Visual Studio 18 2026"
若 build/ 目录缓存的生成器与探测结果不一致(换机/升级 VS 后),
自动删除 build/ 重新配置,避免 MSBuild 报生成器不匹配。
退出码:0=成功;1=构建失败;3=环境预检失败(无 cmake / 无 VS / VS 版本过新)。
.PARAMETER Config
构建配置,默认 Debug(可选 Release)。
.PARAMETER Clean
强制删除 build/ 后全量重建。
.EXAMPLE
& 'simulator\build_sim.ps1' # 仓库根目录执行,日常增量构建
& 'simulator\build_sim.ps1' -Clean # 全量重建
& 'simulator\build_sim.ps1' -Config Release # Release 构建
#>
param(
[ValidateSet('Debug', 'Release')]
[string]$Config = 'Debug',
[switch]$Clean
)
$ErrorActionPreference = 'Stop'
# 本脚本位于 simulator/ 目录
$simDir = $PSScriptRoot
$buildDir = Join-Path $simDir 'build'
# --- 1. 环境预检 -----------------------------------------------------------
$cmake = Get-Command cmake -ErrorAction SilentlyContinue
if (-not $cmake) {
Write-Host '[build_sim] 预检失败:PATH 中找不到 cmake' -ForegroundColor Red
exit 3
}
# vswhere 随 Visual Studio / BuildTools 自带
$pf86 = ${env:ProgramFiles(x86)}
if (-not $pf86) { $pf86 = 'C:\Program Files (x86)' }
$vswhere = Join-Path $pf86 'Microsoft Visual Studio\Installer\vswhere.exe'
if (-not (Test-Path $vswhere)) {
Write-Host '[build_sim] 预检失败:未找到 vswhere(本机未安装 Visual Studio / BuildTools)' -ForegroundColor Red
exit 3
}
# --- 2. 探测 VS 大版本 → 选择生成器 ----------------------------------------
$raw = & $vswhere -products * -format json
$instances = @($raw | ConvertFrom-Json) # 单实例时 PS5.1 返回裸对象,@() 统一成数组
$instances = $instances | Where-Object { $_ -and $_.installationVersion }
if (-not $instances) {
Write-Host '[build_sim] 预检失败:vswhere 未返回任何 VS 实例' -ForegroundColor Red
exit 3
}
$best = $instances |
Sort-Object { [version]$_.installationVersion } |
Select-Object -Last 1
$major = [version]$best.installationVersion
switch ($major.Major) {
16 { $generator = 'Visual Studio 16 2019' }
17 { $generator = 'Visual Studio 17 2022' }
18 { $generator = 'Visual Studio 18 2026' }
default {
Write-Host "[build_sim] 预检失败:不支持的 VS 大版本 $($major.Major)($($best.installationPath)),仅支持 16/17/18" -ForegroundColor Red
exit 3
}
}
Write-Host "[build_sim] 探测到 VS $($best.installationVersion) -> 生成器 '$generator'"
# 删除 build/ 目录(带重试:抗杀软瞬时锁/文件占用;彻底失败则提示用户)
function Remove-BuildDir([string]$Dir) {
for ($i = 1; $i -le 3; $i++) {
try {
Remove-Item $Dir -Recurse -Force -ErrorAction Stop
return $true
} catch {
if ($i -lt 3) {
Write-Host "[build_sim] build/ 删除失败(可能有文件被占用),重试 $i/3 ..."
Start-Sleep -Seconds 2
}
}
}
Write-Host '[build_sim] 无法删除 build/:若模拟器窗口仍开着,请先关闭再重试' -ForegroundColor Red
return $false
}
# --- 3. 生成器一致性检查(缓存不匹配则删 build/ 重建)-----------------------
if ($Clean -and (Test-Path $buildDir)) {
if (-not (Remove-BuildDir $buildDir)) { exit 3 }
Write-Host '[build_sim] -Clean:已删除 build/'
}
$cache = Join-Path $buildDir 'CMakeCache.txt'
if (Test-Path $cache) {
# CMake 缓存中 CMAKE_GENERATOR 的类型随版本为 STRING 或 INTERNAL,统一按行首匹配
$cached = (Select-String -Path $cache -Pattern '^CMAKE_GENERATOR:' |
Select-Object -First 1).Line -replace '^CMAKE_GENERATOR:[A-Z]+=', ''
if ($cached -cne $generator) {
Write-Host "[build_sim] build/ 由 '$cached' 配置,与本机 '$generator' 不符 -> 删除重建"
if (-not (Remove-BuildDir $buildDir)) { exit 3 }
}
}
# --- 4. 配置 + 构建 ---------------------------------------------------------
if (-not (Test-Path (Join-Path $buildDir 'CMakeCache.txt'))) {
Write-Host '[build_sim] CMake 配置 ...'
& cmake -S $simDir -B $buildDir -G $generator -A x64
if ($LASTEXITCODE -ne 0) {
Write-Host '[build_sim] CMake 配置失败' -ForegroundColor Red
exit $LASTEXITCODE
}
}
Write-Host "[build_sim] 构建($Config)..."
& cmake --build $buildDir --config $Config
if ($LASTEXITCODE -ne 0) {
Write-Host '[build_sim] 构建失败' -ForegroundColor Red
exit $LASTEXITCODE
}
$exe = Join-Path $buildDir (Join-Path $Config 'lvgl_sim.exe')
if (Test-Path $exe) {
Write-Host "[build_sim] OK:$exe" -ForegroundColor Green
exit 0
}
Write-Host "[build_sim] 构建退出码 0 但未找到 $exe" -ForegroundColor Red
exit 1