Files
chengnan/build-windows.ps1
2026-05-30 17:24:05 +08:00

101 lines
3.0 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
param(
[Parameter(Mandatory = $true)]
[string]$QtPrefix,
[string]$BuildDir = "build-windows",
[string]$DistDir = "dist-windows\chengnan",
[string]$Generator = "Visual Studio 17 2022",
[string]$Arch = "x64",
[ValidateSet("Debug", "Release", "RelWithDebInfo", "MinSizeRel")]
[string]$Config = "Release"
)
$ErrorActionPreference = "Stop"
function Resolve-FullPath([string]$PathValue) {
if ([System.IO.Path]::IsPathRooted($PathValue)) {
return [System.IO.Path]::GetFullPath($PathValue)
}
return [System.IO.Path]::GetFullPath((Join-Path (Get-Location) $PathValue))
}
function Require-Command([string]$Name) {
$command = Get-Command $Name -ErrorAction SilentlyContinue
if (-not $command) {
throw "找不到命令:$Name。请先安装并加入 PATH。"
}
return $command.Source
}
$ProjectDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $ProjectDir
$QtPrefix = Resolve-FullPath $QtPrefix
$BuildPath = Resolve-FullPath $BuildDir
$DistPath = Resolve-FullPath $DistDir
$QtBin = Join-Path $QtPrefix "bin"
$WinDeployQt = Join-Path $QtBin "windeployqt.exe"
if (-not (Test-Path $QtPrefix)) {
throw "QtPrefix 不存在:$QtPrefix"
}
if (-not (Test-Path $WinDeployQt)) {
throw "找不到 windeployqt$WinDeployQt。请确认 -QtPrefix 指向 Qt 套件目录,例如 C:\Qt\6.7.2\msvc2019_64。"
}
Require-Command "cmake" | Out-Null
$env:PATH = "$QtBin;$env:PATH"
$env:CMAKE_PREFIX_PATH = $QtPrefix
Write-Host "== chengnan Windows 构建 ==" -ForegroundColor Cyan
Write-Host "项目目录:$ProjectDir"
Write-Host "Qt 目录:$QtPrefix"
Write-Host "构建目录:$BuildPath"
Write-Host "发布目录:$DistPath"
Write-Host "生成器:$Generator"
Write-Host "配置:$Config"
$configureArgs = @(
"-S", $ProjectDir,
"-B", $BuildPath,
"-G", $Generator,
"-DCMAKE_PREFIX_PATH=$QtPrefix"
)
if ($Generator -like "Visual Studio*") {
$configureArgs += @("-A", $Arch)
} else {
$configureArgs += "-DCMAKE_BUILD_TYPE=$Config"
}
Write-Host "`n== 配置 CMake ==" -ForegroundColor Cyan
& cmake @configureArgs
Write-Host "`n== 编译 ==" -ForegroundColor Cyan
& cmake --build $BuildPath --config $Config --parallel
$ExeCandidates = @(
(Join-Path $BuildPath "$Config\chengnan.exe"),
(Join-Path $BuildPath "chengnan.exe")
)
$ExePath = $ExeCandidates | Where-Object { Test-Path $_ } | Select-Object -First 1
if (-not $ExePath) {
throw "构建完成但找不到 chengnan.exe。已检查$($ExeCandidates -join ', ')"
}
Write-Host "`n== 准备发布目录 ==" -ForegroundColor Cyan
if (Test-Path $DistPath) {
Remove-Item -Recurse -Force $DistPath
}
New-Item -ItemType Directory -Force -Path $DistPath | Out-Null
Copy-Item $ExePath $DistPath
Write-Host "`n== 收集 Qt 运行库 ==" -ForegroundColor Cyan
$DistExe = Join-Path $DistPath "chengnan.exe"
& $WinDeployQt --release --no-translations $DistExe
Write-Host "`n构建完成:$DistExe" -ForegroundColor Green
Write-Host "请整体复制目录:$DistPath"