Files
ngzz-mc/scripts/build.ps1
2026-09-19 14:21:08 +08:00

81 lines
3.3 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.
# 年糕历险记 构建脚本Windows
# 用法pwsh -File scripts/build.ps1 [-Release]
# 自动定位 C 工具链tools/ 下的 zig 或 mingw64/w64devkit构建 GL 客户端与服务器。
param([switch]$Release)
$ErrorActionPreference = "Stop"
$root = Split-Path $PSScriptRoot -Parent
$tools = Join-Path $root "tools"
# 定位 C 编译器GLFW 需要 CGO优先 zigzig cc其次 mingw64/w64devkit 的 gcc
$cc = $null
$zig = Get-ChildItem $tools -Recurse -Filter zig.exe -ErrorAction SilentlyContinue | Select-Object -First 1
if ($zig) {
$cc = "$($zig.FullName) cc"
Write-Host "== 使用 zig 作为 C 编译器: $($zig.FullName) =="
} else {
$gcc = Get-ChildItem $tools -Recurse -Filter gcc.exe -ErrorAction SilentlyContinue | Select-Object -First 1
if ($gcc) {
$cc = $gcc.FullName
$env:Path = "$(Split-Path $gcc.FullName);$env:Path"
Write-Host "== 使用 gcc: $($gcc.FullName) =="
} else {
Write-Host "== 未找到 C 工具链:请把 zig或 MinGW放到 tools/ 后重试。"
Write-Host " 下载https://ziglang.org/download/zig-x86_64-windows-*.zip"
Write-Host " 或: https://github.com/brechtsanders/winlibs_mingw/releaseswinlibs zip"
exit 1
}
}
$env:CC = $cc
$env:CGO_ENABLED = "1"
# 双击 bat 时 Explorer 的 PATH 可能没有 go按常见位置补上
if (-not (Get-Command go -ErrorAction SilentlyContinue)) {
$goExe = $null
$exact = @()
if ($env:GOROOT) { $exact += (Join-Path $env:GOROOT "bin\go.exe") }
$exact += "C:\Program Files\Go\bin\go.exe"
foreach ($p in $exact) {
if (Test-Path -LiteralPath $p) { $goExe = $p; break }
}
if (-not $goExe) {
$globs = @(
(Join-Path $env:USERPROFILE "sdk\go*\bin\go.exe"),
"D:\*\sdk\go*\bin\go.exe"
)
$hit = Get-ChildItem -Path $globs -ErrorAction SilentlyContinue | Select-Object -First 1
if ($hit) { $goExe = $hit.FullName }
}
if ($goExe) {
$env:Path = "$(Split-Path $goExe);$env:Path"
Write-Host "== 使用 go: $goExe =="
} else {
Write-Host "== 未找到 go请安装 Go 或把 go.exe 加入 PATH。"
exit 1
}
}
$tags = "gl"
if (-not $Release) { $tags = "$tags dev" }
Push-Location $root
try {
New-Item -ItemType Directory -Force -Path bin | Out-Null
# exe 图标(原版素材 logo缺失时自动生成 .sysomake_icon.ps1
if (-not (Test-Path (Join-Path $root "cmd\client\icon.syso"))) {
& (Join-Path $PSScriptRoot "make_icon.ps1")
if ($LASTEXITCODE -ne 0) { throw "图标生成失败make_icon.ps1" }
}
go build -tags $tags -trimpath -ldflags "-s -w -H windowsgui" -o bin/mc-client.exe ./cmd/client
go build -trimpath -ldflags "-s -w" -o bin/mc-server.exe ./cmd/server
# 资源入 bin/(可执行文件目录解析:双击/快捷方式运行无需依赖工作目录)
Copy-Item (Join-Path $root "config") (Join-Path $root "bin") -Recurse -Force
Copy-Item (Join-Path $root "assets") (Join-Path $root "bin") -Recurse -Force
Copy-Item (Join-Path $root "mods") (Join-Path $root "bin") -Recurse -Force
Copy-Item (Join-Path $root "resourcepacks") (Join-Path $root "bin") -Recurse -Force
} finally {
Pop-Location
}
Write-Host "== 构建完成bin/mc-client.exe bin/mc-server.exe含资源 =="