| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- param(
- [Parameter(Position=0)]
- [string]$Target = "build",
- [Parameter()]
- [string]$Tags
- )
- $ErrorActionPreference = "Stop"
- function Assert-ExitCode {
- if ($LASTEXITCODE -ne 0) {
- throw "Command failed with exit code $LASTEXITCODE"
- }
- }
- # Variables
- $NAME = "sing-box"
- $DEFAULT_TAGS = "with_gvisor,with_quic,with_dhcp,with_wireguard,with_utls,with_acme,with_clash_api,with_tailscale,with_ccm,with_ocm,badlinkname,tfogo_checklinkname0"
- if (-not $Tags) {
- $Tags = $DEFAULT_TAGS
- }
- function Get-Version {
- $gohostos = (go env GOHOSTOS)
- $gohostarch = (go env GOHOSTARCH)
- $env:CGO_ENABLED = "0"
- $env:GOOS = $gohostos
- $env:GOARCH = $gohostarch
- try {
- $version = (go run github.com/sagernet/sing-box/cmd/internal/read_tag@latest)
- Assert-ExitCode
- return $version
- }
- finally {
- Remove-Item Env:\CGO_ENABLED -ErrorAction SilentlyContinue
- Remove-Item Env:\GOOS -ErrorAction SilentlyContinue
- Remove-Item Env:\GOARCH -ErrorAction SilentlyContinue
- }
- }
- function Invoke-Build {
- $version = Get-Version
- $ldflags = "-X 'github.com/sagernet/sing-box/constant.Version=$version' -X 'internal/godebug.defaultGODEBUG=multipathtcp=0' -s -w -buildid= -checklinkname=0"
- $env:GOTOOLCHAIN = "local"
- try {
- Write-Host "go build -v -trimpath -ldflags `"$ldflags`" -tags `"$Tags`" ./cmd/sing-box"
- & go build -v -trimpath -ldflags $ldflags -tags $Tags ./cmd/sing-box
- Assert-ExitCode
- }
- finally {
- Remove-Item Env:\GOTOOLCHAIN -ErrorAction SilentlyContinue
- }
- }
- function Invoke-Fmt {
- & gofumpt -l -w .
- Assert-ExitCode
- & gofmt -s -w .
- Assert-ExitCode
- & gci.exe write --custom-order -s standard -s "prefix(github.com/sagernet/)" -s "default" .
- Assert-ExitCode
- }
- function Invoke-FmtInstall {
- & go install -v mvdan.cc/gofumpt@latest
- Assert-ExitCode
- & go install -v github.com/daixiang0/gci@latest
- Assert-ExitCode
- }
- function Invoke-Lint {
- try {
- $env:GOOS = "linux"
- & golangci-lint run ./...
- Assert-ExitCode
- $env:GOOS = "android"
- & golangci-lint run ./...
- Assert-ExitCode
- $env:GOOS = "windows"
- & golangci-lint run ./...
- Assert-ExitCode
- $env:GOOS = "darwin"
- & golangci-lint run ./...
- Assert-ExitCode
- $env:GOOS = "freebsd"
- & golangci-lint run ./...
- Assert-ExitCode
- }
- finally {
- Remove-Item Env:\GOOS -ErrorAction SilentlyContinue
- }
- }
- function Invoke-LintInstall {
- & go install -v github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest
- Assert-ExitCode
- }
- function Invoke-Clean {
- $gopath = (go env GOPATH)
- Remove-Item -Path "bin", "dist", "sing-box", "sing-box.exe" -Recurse -Force -ErrorAction SilentlyContinue
- Remove-Item -Path "$gopath/sing-box", "$gopath/sing-box.exe" -Force -ErrorAction SilentlyContinue
- }
- function Invoke-Install {
- $prefix = (go env GOPATH)
- $version = Get-Version
- $ldflags = "-X 'github.com/sagernet/sing-box/constant.Version=$version' -X 'internal/godebug.defaultGODEBUG=multipathtcp=0' -s -w -buildid= -checklinkname=0"
- $env:GOTOOLCHAIN = "local"
- try {
- $outPath = Join-Path (Join-Path $prefix "bin") "$NAME.exe"
- Write-Host "go build -o `"$outPath`" -v -trimpath -ldflags `"$ldflags`" -tags `"$Tags`" ./cmd/sing-box"
- & go build -o $outPath -v -trimpath -ldflags $ldflags -tags $Tags ./cmd/sing-box
- Assert-ExitCode
- }
- finally {
- Remove-Item Env:\GOTOOLCHAIN -ErrorAction SilentlyContinue
- }
- }
- function Invoke-LibAndroid {
- & go run ./cmd/internal/build_libbox -target android
- Assert-ExitCode
- }
- function Invoke-LibInstall {
- & go install -v github.com/sagernet/gomobile/cmd/[email protected]
- Assert-ExitCode
- & go install -v github.com/sagernet/gomobile/cmd/[email protected]
- Assert-ExitCode
- }
- function Invoke-LibWindows {
- & go run ./cmd/internal/build_libbox -target windows
- Assert-ExitCode
- }
- function Invoke-Docs {
- & ".\venv\Scripts\mkdocs.exe" serve
- Assert-ExitCode
- }
- function Invoke-DocsInstall {
- & py -m venv venv
- Assert-ExitCode
- & ".\venv\Scripts\pip.exe" install --force-reinstall 'mkdocs-material==9.*' 'mkdocs-static-i18n==1.2.*'
- Assert-ExitCode
- }
- function Show-Help {
- Write-Host "Usage: .\build.ps1 <target> [-Tags <tags>]"
- Write-Host ""
- Write-Host "Targets:"
- Write-Host " build Build sing-box (default)"
- Write-Host " install Install sing-box to GOPATH/bin"
- Write-Host " fmt Format code"
- Write-Host " fmt_install Install formatting tools"
- Write-Host " lint Run linter"
- Write-Host " lint_install Install linter"
- Write-Host " clean Clean build artifacts"
- Write-Host " lib_install Install gomobile tools"
- Write-Host " lib_android Build Android library"
- Write-Host " lib_windows Build Windows library"
- Write-Host " docs Serve documentation locally"
- Write-Host " docs_install Install documentation tools"
- Write-Host " help Show this help"
- Write-Host ""
- Write-Host "Options:"
- Write-Host " -Tags <tags> Override build tags"
- }
- switch ($Target) {
- "build" { Invoke-Build }
- "install" { Invoke-Install }
- "fmt" { Invoke-Fmt }
- "fmt_install" { Invoke-FmtInstall }
- "lint" { Invoke-Lint }
- "lint_install" { Invoke-LintInstall }
- "clean" { Invoke-Clean }
- "lib_install" { Invoke-LibInstall }
- "lib_android" { Invoke-LibAndroid }
- "lib_windows" { Invoke-LibWindows }
- "docs" { Invoke-Docs }
- "docs_install" { Invoke-DocsInstall }
- "help" { Show-Help }
- default {
- Write-Host "Unknown target: $Target" -ForegroundColor Red
- Write-Host ""
- Show-Help
- exit 1
- }
- }
|