param( [string]$OutputPath = "", [ValidateSet("tera-api", "tera-accessd", "tera-hwd", "tera-netd", "tera-timed", "tera-alarmd", "tera-snmpd", "tera-logd", "tera-updated", "tera-seriald", "tera-sessiond", "tera-web", "tera-serial-check", "tera-serial-line-check")] [string]$Command = "tera-api" ) $ErrorActionPreference = "Stop" $scriptApiRoot = Resolve-Path (Join-Path $PSScriptRoot "..") $workingApiRoot = Resolve-Path (Get-Location) if (Test-Path -LiteralPath (Join-Path $workingApiRoot "go.mod")) { $apiRoot = $workingApiRoot } elseif (Test-Path -LiteralPath (Join-Path $scriptApiRoot "go.mod")) { $apiRoot = $scriptApiRoot } else { throw "Run this script from services\tera-api, or place it under services\tera-api\scripts." } $repoRoot = Resolve-Path (Join-Path $apiRoot "..\..") $goRoot = Join-Path $repoRoot ".tools\go1.26.4" $portableGo = Join-Path $goRoot "bin\go.exe" $configuredGoRoot = [string]$env:TERA_GO_ROOT if (-not [string]::IsNullOrWhiteSpace($configuredGoRoot)) { $configuredGo = Join-Path $configuredGoRoot "bin\go.exe" if (Test-Path -LiteralPath $configuredGo) { $goRoot = (Resolve-Path -LiteralPath $configuredGoRoot).Path $portableGo = $configuredGo } } $goCommand = Get-Command go -ErrorAction SilentlyContinue if (Test-Path -LiteralPath $portableGo) { $goExe = $portableGo } elseif ($goCommand) { $goExe = $goCommand.Source $goRoot = (& $goExe env GOROOT).Trim() } else { throw "Go 1.25.0 or newer is required to build $Command." } if ($OutputPath -eq "") { $OutputPath = Join-Path ([System.IO.Path]::GetTempPath()) "$Command-linux-arm64" } if (-not [System.IO.Path]::IsPathRooted($OutputPath)) { $OutputPath = [System.IO.Path]::GetFullPath((Join-Path (Get-Location).Path $OutputPath)) } $outputParent = Split-Path -Parent $OutputPath if ($outputParent -ne "") { New-Item -ItemType Directory -Force $outputParent | Out-Null } Push-Location $apiRoot try { $env:GOROOT = $goRoot if ([string]::IsNullOrWhiteSpace($env:GOPATH)) { $env:GOPATH = Join-Path $repoRoot ".tools\gopath" } if ([string]::IsNullOrWhiteSpace($env:GOCACHE)) { $env:GOCACHE = Join-Path $repoRoot ".tools\go-build" } if ([string]::IsNullOrWhiteSpace($env:GOMODCACHE)) { $env:GOMODCACHE = Join-Path $env:GOPATH "pkg\mod" } $env:GOOS = "linux" $env:GOARCH = "arm64" $env:CGO_ENABLED = "0" New-Item -ItemType Directory -Force $env:GOPATH, $env:GOCACHE, $env:GOMODCACHE | Out-Null & $goExe build -buildvcs=false -trimpath -mod=vendor -o $OutputPath "./cmd/$Command" if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } } finally { Pop-Location } $artifact = Get-Item -LiteralPath $OutputPath if ($artifact.Length -le 0) { throw "Linux ARM64 build output is empty: $OutputPath" } Write-Host "[check-linux-arm64-build] Built $($artifact.FullName) ($($artifact.Length) bytes)"