param([string]$Configuration = "Release") $ErrorActionPreference = "Stop" $Root = $PSScriptRoot $Build = Join-Path -Path $Root -ChildPath "build" $PackageName = "海宁读卡助手_1.0.0" $DistRoot = Join-Path -Path $Root -ChildPath "dist" $Dist = Join-Path -Path $DistRoot -ChildPath $PackageName $MSBuild = "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe" $VCVars = "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" $CMakeCommand = Get-Command cmake -ErrorAction SilentlyContinue if ($CMakeCommand) { $CMake = $CMakeCommand.Source } else { $VsCMake = "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" if (-not (Test-Path -LiteralPath $VsCMake)) { throw "CMake was not found. Install Visual Studio C++ Desktop Development and CMake." } $CMake = $VsCMake } if (-not (Test-Path -LiteralPath $MSBuild)) { throw "Visual Studio 2017 MSBuild was not found. Install the Visual C++ Build Tools workload." } $env:Path = (Split-Path -Parent $MSBuild) + ";" + $env:Path if (-not (Test-Path -LiteralPath $VCVars)) { throw "Visual Studio 2017 C++ environment setup was not found. Install the Visual C++ Build Tools workload." } # Import the x86 compiler, SDK, include, and library paths into this PowerShell process. $VCVarsCommand = 'call "' + $VCVars + '" x86 >nul && set' cmd.exe /c $VCVarsCommand | ForEach-Object { if ($_ -match '^([^=]+)=(.*)$') { Set-Item -Path ("env:" + $matches[1]) -Value $matches[2] } } $UcrtLib = "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\ucrt\x86" if (Test-Path -LiteralPath $UcrtLib) { $env:LIB = $env:LIB + ";" + $UcrtLib } # VS2017's bundled CMake predates the -S/-B flags, so configure from build/. # Build a 32-bit v141 application. v141 is the appropriate target for # Windows 7; the optional v141_xp toolset requires an obsolete XP SDK and # prevents CMake's compiler probe from locating the modern UCRT libraries. New-Item -ItemType Directory -Force -Path $Build | Out-Null $Cache = Join-Path -Path $Build -ChildPath "CMakeCache.txt" if ((Test-Path -LiteralPath $Cache) -and ((Select-String -LiteralPath $Cache -Pattern 'CMAKE_CXX_COMPILER:FILEPATH=CMAKE_CXX_COMPILER-NOTFOUND' -Quiet) -or (Test-Path -LiteralPath (Join-Path -Path $Build -ChildPath "CMakeFiles\CMakeError.log")))) { Remove-Item -LiteralPath $Cache -Force $CMakeFiles = Join-Path -Path $Build -ChildPath "CMakeFiles" if (Test-Path -LiteralPath $CMakeFiles) { Remove-Item -LiteralPath $CMakeFiles -Recurse -Force } } Push-Location $Build try { # Force static CRT even for CMake's Debug compiler probe. & $CMake -G "Visual Studio 15 2017" -A Win32 -T v141 ("-DCMAKE_MAKE_PROGRAM=" + $MSBuild) "-DCMAKE_C_FLAGS_DEBUG=/MTd" "-DCMAKE_CXX_FLAGS_DEBUG=/MTd" $Root if ($LASTEXITCODE -ne 0) { throw "CMake configuration failed." } & $CMake --build . --config $Configuration if ($LASTEXITCODE -ne 0) { throw "CMake build failed." } } finally { Pop-Location } New-Item -ItemType Directory -Force -Path $Dist | Out-Null $ConfigurationDir = Join-Path -Path $Build -ChildPath $Configuration $Binary = Join-Path -Path $ConfigurationDir -ChildPath "HainingCardReader.exe" if (-not (Test-Path -LiteralPath $Binary)) { throw "Build output was not found: $Binary" } $ExeDestination = Join-Path -Path $Dist -ChildPath "HainingCardReader.exe" $ConfigDestination = Join-Path -Path $Dist -ChildPath "config.json" $PackageSource = Join-Path -Path $Root -ChildPath "package" $PackageDestination = Join-Path -Path $Dist -ChildPath "package" try { Copy-Item -LiteralPath $Binary -Destination $ExeDestination -Force } catch { throw "Cannot replace $ExeDestination. Close the running HainingCardReader.exe, then run 一键打包.bat again." } # Runtime defaults are compiled into readcard-c.exe. Remove a stale config # from older packages so users do not accidentally override the embedded values. if (Test-Path -LiteralPath $ConfigDestination) { Remove-Item -LiteralPath $ConfigDestination -Force } Copy-Item -LiteralPath $PackageSource -Destination $PackageDestination -Recurse -Force Write-Host "Build completed: $Dist"