build: GL 渲染层与客户端挂 gl 构建标签(无 C 工具链也可构建测试);构建脚本自动定位 zig/gcc

This commit is contained in:
NianGao Dev
2026-08-15 23:00:55 +08:00
parent 509c6f228c
commit 5fa057632d
8 changed files with 258 additions and 229 deletions

View File

@@ -1,39 +1,42 @@
# 年糕历险记 构建脚本(Windows)
# 用法:pwsh -File scripts/build.ps1 [-Release]
# 自动准备便携 MinGW-w64(GLFW 需要 CGO),然后构建 client/server。
# 自动定位 C 工具链(tools/ 下的 zig 或 mingw64/w64devkit),构建 GL 客户端与服务器。
param([switch]$Release)
$ErrorActionPreference = "Stop"
$root = Split-Path $PSScriptRoot -Parent
$tools = Join-Path $root "tools"
$mingw = Join-Path $tools "mingw64"
$gcc = Join-Path $mingw "bin\gcc.exe"
if (-not (Test-Path $gcc)) {
Write-Host "== 未找到 C 编译器(GLFW 需要),下载便携 MinGW-w64 =="
New-Item -ItemType Directory -Force -Path $tools | Out-Null
$url = "https://github.com/brechtsanders/winlibs_mingw/releases/download/14.2.0posix-19.1.1-12.0.0-ucrt-r1/winlibs-x86_64-posix-seh-gcc-14.2.0-mingw-w64ucrt-12.0.0-r1.zip"
$zip = Join-Path $env:TEMP "winlibs-mingw.zip"
Invoke-WebRequest -Uri $url -OutFile $zip -UseBasicParsing
Expand-Archive -Path $zip -DestinationPath $tools -Force
Remove-Item $zip -Force
if (-not (Test-Path $gcc)) {
throw "MinGW 准备失败:请手动安装 mingw-w64 并重试,或检查网络。"
# 定位 C 编译器(GLFW 需要 CGO):优先 zig(zig 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/releases(winlibs zip)"
exit 1
}
}
$env:CC = $cc
$env:CGO_ENABLED = "1"
$env:CC = $gcc
$env:Path = "$(Join-Path $mingw 'bin');$env:Path"
$tags = ""
$ldflags = "-s -w"
if (-not $Release) { $tags = "-tags dev" }
$tags = "gl"
if (-not $Release) { $tags = "$tags dev" }
Push-Location $root
try {
New-Item -ItemType Directory -Force -Path bin | Out-Null
go build $tags -trimpath -ldflags $ldflags -o bin/mc-client.exe ./cmd/client
go build $tags -trimpath -ldflags $ldflags -o bin/mc-server.exe ./cmd/server
go build -tags $tags -trimpath -ldflags "-s -w" -o bin/mc-client.exe ./cmd/client
go build -trimpath -ldflags "-s -w" -o bin/mc-server.exe ./cmd/server
} finally {
Pop-Location
}