build.ps1 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. param(
  2. [Parameter(Position=0)]
  3. [string]$Target = "build",
  4. [Parameter()]
  5. [string]$Tags
  6. )
  7. $ErrorActionPreference = "Stop"
  8. function Assert-ExitCode {
  9. if ($LASTEXITCODE -ne 0) {
  10. throw "Command failed with exit code $LASTEXITCODE"
  11. }
  12. }
  13. # Variables
  14. $NAME = "sing-box"
  15. $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"
  16. if (-not $Tags) {
  17. $Tags = $DEFAULT_TAGS
  18. }
  19. function Get-Version {
  20. $gohostos = (go env GOHOSTOS)
  21. $gohostarch = (go env GOHOSTARCH)
  22. $env:CGO_ENABLED = "0"
  23. $env:GOOS = $gohostos
  24. $env:GOARCH = $gohostarch
  25. try {
  26. $version = (go run github.com/sagernet/sing-box/cmd/internal/read_tag@latest)
  27. Assert-ExitCode
  28. return $version
  29. }
  30. finally {
  31. Remove-Item Env:\CGO_ENABLED -ErrorAction SilentlyContinue
  32. Remove-Item Env:\GOOS -ErrorAction SilentlyContinue
  33. Remove-Item Env:\GOARCH -ErrorAction SilentlyContinue
  34. }
  35. }
  36. function Invoke-Build {
  37. $version = Get-Version
  38. $ldflags = "-X 'github.com/sagernet/sing-box/constant.Version=$version' -X 'internal/godebug.defaultGODEBUG=multipathtcp=0' -s -w -buildid= -checklinkname=0"
  39. $env:GOTOOLCHAIN = "local"
  40. try {
  41. Write-Host "go build -v -trimpath -ldflags `"$ldflags`" -tags `"$Tags`" ./cmd/sing-box"
  42. & go build -v -trimpath -ldflags $ldflags -tags $Tags ./cmd/sing-box
  43. Assert-ExitCode
  44. }
  45. finally {
  46. Remove-Item Env:\GOTOOLCHAIN -ErrorAction SilentlyContinue
  47. }
  48. }
  49. function Invoke-Fmt {
  50. & gofumpt -l -w .
  51. Assert-ExitCode
  52. & gofmt -s -w .
  53. Assert-ExitCode
  54. & gci.exe write --custom-order -s standard -s "prefix(github.com/sagernet/)" -s "default" .
  55. Assert-ExitCode
  56. }
  57. function Invoke-FmtInstall {
  58. & go install -v mvdan.cc/gofumpt@latest
  59. Assert-ExitCode
  60. & go install -v github.com/daixiang0/gci@latest
  61. Assert-ExitCode
  62. }
  63. function Invoke-Lint {
  64. try {
  65. $env:GOOS = "linux"
  66. & golangci-lint run ./...
  67. Assert-ExitCode
  68. $env:GOOS = "android"
  69. & golangci-lint run ./...
  70. Assert-ExitCode
  71. $env:GOOS = "windows"
  72. & golangci-lint run ./...
  73. Assert-ExitCode
  74. $env:GOOS = "darwin"
  75. & golangci-lint run ./...
  76. Assert-ExitCode
  77. $env:GOOS = "freebsd"
  78. & golangci-lint run ./...
  79. Assert-ExitCode
  80. }
  81. finally {
  82. Remove-Item Env:\GOOS -ErrorAction SilentlyContinue
  83. }
  84. }
  85. function Invoke-LintInstall {
  86. & go install -v github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest
  87. Assert-ExitCode
  88. }
  89. function Invoke-Clean {
  90. $gopath = (go env GOPATH)
  91. Remove-Item -Path "bin", "dist", "sing-box", "sing-box.exe" -Recurse -Force -ErrorAction SilentlyContinue
  92. Remove-Item -Path "$gopath/sing-box", "$gopath/sing-box.exe" -Force -ErrorAction SilentlyContinue
  93. }
  94. function Invoke-Install {
  95. $prefix = (go env GOPATH)
  96. $version = Get-Version
  97. $ldflags = "-X 'github.com/sagernet/sing-box/constant.Version=$version' -X 'internal/godebug.defaultGODEBUG=multipathtcp=0' -s -w -buildid= -checklinkname=0"
  98. $env:GOTOOLCHAIN = "local"
  99. try {
  100. $outPath = Join-Path (Join-Path $prefix "bin") "$NAME.exe"
  101. Write-Host "go build -o `"$outPath`" -v -trimpath -ldflags `"$ldflags`" -tags `"$Tags`" ./cmd/sing-box"
  102. & go build -o $outPath -v -trimpath -ldflags $ldflags -tags $Tags ./cmd/sing-box
  103. Assert-ExitCode
  104. }
  105. finally {
  106. Remove-Item Env:\GOTOOLCHAIN -ErrorAction SilentlyContinue
  107. }
  108. }
  109. function Invoke-LibAndroid {
  110. & go run ./cmd/internal/build_libbox -target android
  111. Assert-ExitCode
  112. }
  113. function Invoke-LibInstall {
  114. & go install -v github.com/sagernet/gomobile/cmd/[email protected]
  115. Assert-ExitCode
  116. & go install -v github.com/sagernet/gomobile/cmd/[email protected]
  117. Assert-ExitCode
  118. }
  119. function Invoke-LibWindows {
  120. & go run ./cmd/internal/build_libbox -target windows
  121. Assert-ExitCode
  122. }
  123. function Invoke-Docs {
  124. & ".\venv\Scripts\mkdocs.exe" serve
  125. Assert-ExitCode
  126. }
  127. function Invoke-DocsInstall {
  128. & py -m venv venv
  129. Assert-ExitCode
  130. & ".\venv\Scripts\pip.exe" install --force-reinstall 'mkdocs-material==9.*' 'mkdocs-static-i18n==1.2.*'
  131. Assert-ExitCode
  132. }
  133. function Show-Help {
  134. Write-Host "Usage: .\build.ps1 <target> [-Tags <tags>]"
  135. Write-Host ""
  136. Write-Host "Targets:"
  137. Write-Host " build Build sing-box (default)"
  138. Write-Host " install Install sing-box to GOPATH/bin"
  139. Write-Host " fmt Format code"
  140. Write-Host " fmt_install Install formatting tools"
  141. Write-Host " lint Run linter"
  142. Write-Host " lint_install Install linter"
  143. Write-Host " clean Clean build artifacts"
  144. Write-Host " lib_install Install gomobile tools"
  145. Write-Host " lib_android Build Android library"
  146. Write-Host " lib_windows Build Windows library"
  147. Write-Host " docs Serve documentation locally"
  148. Write-Host " docs_install Install documentation tools"
  149. Write-Host " help Show this help"
  150. Write-Host ""
  151. Write-Host "Options:"
  152. Write-Host " -Tags <tags> Override build tags"
  153. }
  154. switch ($Target) {
  155. "build" { Invoke-Build }
  156. "install" { Invoke-Install }
  157. "fmt" { Invoke-Fmt }
  158. "fmt_install" { Invoke-FmtInstall }
  159. "lint" { Invoke-Lint }
  160. "lint_install" { Invoke-LintInstall }
  161. "clean" { Invoke-Clean }
  162. "lib_install" { Invoke-LibInstall }
  163. "lib_android" { Invoke-LibAndroid }
  164. "lib_windows" { Invoke-LibWindows }
  165. "docs" { Invoke-Docs }
  166. "docs_install" { Invoke-DocsInstall }
  167. "help" { Show-Help }
  168. default {
  169. Write-Host "Unknown target: $Target" -ForegroundColor Red
  170. Write-Host ""
  171. Show-Help
  172. exit 1
  173. }
  174. }