Files
chengnan/docs/windows/build-windows.ps1
2026-05-29 22:29:38 +08:00

87 lines
3.2 KiB
PowerShell
Raw 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(
[ValidateSet("msvc", "mingw")]
[string]$Toolchain = "msvc",
[string]$BuildDir = "$PSScriptRoot\..\build-windows",
[switch]$Deploy
)
$ErrorActionPreference = "Stop"
$ProjectRoot = Resolve-Path "$PSScriptRoot\.."
$BuildDir = [System.IO.Path]::GetFullPath($BuildDir)
$ThirdParty = Join-Path $ProjectRoot "third_party"
$Tools = Join-Path $ProjectRoot "tools"
$CMakeExe = Join-Path $Tools "cmake-current-windows\bin\cmake.exe"
if (!(Test-Path $CMakeExe)) {
$CMakeExe = Join-Path $Tools "cmake-windows\cmake-3.31.6-windows-x86_64\bin\cmake.exe"
}
if (!(Test-Path $CMakeExe)) {
throw "未找到项目内 Windows CMake$CMakeExe"
}
$NinjaExe = Join-Path $ThirdParty "qt-tools\Tools\Ninja\ninja.exe"
if (!(Test-Path $NinjaExe)) {
$NinjaExe = Join-Path $ThirdParty "qt-tools\Tools\ninja\ninja.exe"
}
if ($Toolchain -eq "msvc") {
$QtPrefix = Join-Path $ThirdParty "qt-windows-msvc"
$OpenCvPrefix = Join-Path $ThirdParty "opencv-windows\x64\vc16\lib"
$OpenCvBin = Join-Path $ThirdParty "opencv-windows\x64\vc16\bin"
$Generator = "Ninja"
if (!(Test-Path $QtPrefix)) { throw "未找到 Qt MSVC$QtPrefix" }
if (!(Test-Path $OpenCvPrefix)) { throw "未找到 OpenCV MSVC$OpenCvPrefix" }
if (!(Get-Command cl.exe -ErrorAction SilentlyContinue)) {
throw "未检测到 cl.exe。请在 x64 Native Tools Command Prompt for VS 中运行本脚本,或先运行 vcvars64.bat。"
}
} else {
$QtPrefix = Join-Path $ThirdParty "qt-windows"
$OpenCvPrefix = Join-Path $ThirdParty "opencv-windows\x64\vc16\lib"
$OpenCvBin = Join-Path $ThirdParty "opencv-windows\x64\vc16\bin"
$MingwBin = Join-Path $ThirdParty "qt-tools\Tools\mingw1120_64\bin"
$Generator = "Ninja"
if (!(Test-Path $QtPrefix)) { throw "未找到 Qt MinGW$QtPrefix" }
if (!(Test-Path $MingwBin)) { throw "未找到项目内 MinGW$MingwBin" }
Write-Warning "OpenCV 官方 Windows 预编译包是 MSVC vc16不适配 MinGW 链接。若要 MinGW 构建,需要另备 MinGW 版 OpenCV 二进制包。"
$env:PATH = "$MingwBin;$env:PATH"
$env:CC = Join-Path $MingwBin "gcc.exe"
$env:CXX = Join-Path $MingwBin "g++.exe"
}
if (!(Test-Path $NinjaExe)) {
throw "未找到项目内 Ninja$NinjaExe"
}
$env:PATH = "$QtPrefix\bin;$OpenCvBin;$(Split-Path $NinjaExe);$env:PATH"
$PrefixPath = "$QtPrefix;$OpenCvPrefix"
Write-Host "ProjectRoot: $ProjectRoot"
Write-Host "Toolchain: $Toolchain"
Write-Host "QtPrefix: $QtPrefix"
Write-Host "OpenCV: $OpenCvPrefix"
Write-Host "BuildDir: $BuildDir"
Write-Host "Ninja: $NinjaExe"
& $CMakeExe -S $ProjectRoot -B $BuildDir -G $Generator `
-DCMAKE_BUILD_TYPE=Release `
-DCMAKE_MAKE_PROGRAM="$NinjaExe" `
-DCMAKE_PREFIX_PATH="$PrefixPath"
& $CMakeExe --build $BuildDir --config Release
if ($Deploy) {
$Exe = Join-Path $BuildDir "chengnan.exe"
$DeployDir = Join-Path $BuildDir "deploy"
New-Item -ItemType Directory -Force -Path $DeployDir | Out-Null
Copy-Item $Exe $DeployDir -Force
& (Join-Path $QtPrefix "bin\windeployqt.exe") --release --no-translations (Join-Path $DeployDir "chengnan.exe")
Copy-Item (Join-Path $OpenCvBin "*.dll") $DeployDir -Force
Write-Host "已部署到:$DeployDir"
}
Write-Host "构建完成。"