277 lines
7.9 KiB
PowerShell
277 lines
7.9 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
|
|
[string[]]$Targets = @("all"),
|
|
|
|
[string]$GoBin = "",
|
|
[string]$DistDir = "",
|
|
[string]$CgoEnabledValue = "0",
|
|
[string]$Version = "",
|
|
[string]$BuildTagsOthers = "",
|
|
[string]$BuildTagsWindows = "",
|
|
[switch]$Help
|
|
)
|
|
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
$RootDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
$MainPkg = "./cmd/sing-box"
|
|
$DefaultTargets = @(
|
|
"linux-amd64",
|
|
"linux-arm64",
|
|
"linux-armv7",
|
|
"windows-amd64",
|
|
"windows-arm64",
|
|
"darwin-amd64",
|
|
"darwin-arm64"
|
|
)
|
|
|
|
function Show-Usage {
|
|
@'
|
|
Usage:
|
|
.\building-windows.ps1
|
|
.\building-windows.ps1 all
|
|
.\building-windows.ps1 linux-amd64
|
|
.\building-windows.ps1 linux-amd64 windows-amd64
|
|
.\building-windows.ps1 current
|
|
|
|
Optional parameters:
|
|
-GoBin <path> Go binary path
|
|
-DistDir <path> Output directory, default: .\dist
|
|
-CgoEnabledValue <0|1> CGO_ENABLED value, default: 0
|
|
-Version <string> Embedded version, default: git describe --tags --always
|
|
-BuildTagsOthers <string> Override non-Windows build tags
|
|
-BuildTagsWindows <string> Override Windows build tags
|
|
'@ | Write-Host
|
|
}
|
|
|
|
function Require-File {
|
|
param([string]$Path)
|
|
|
|
if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) {
|
|
throw "Missing required file: $Path"
|
|
}
|
|
}
|
|
|
|
function Read-TrimmedFile {
|
|
param([string]$Path)
|
|
|
|
return ((Get-Content -LiteralPath $Path -Raw) -replace "`r", "").Trim()
|
|
}
|
|
|
|
function Resolve-GoBinary {
|
|
param([string]$RequestedGoBin)
|
|
|
|
if ($RequestedGoBin) {
|
|
if (-not (Test-Path -LiteralPath $RequestedGoBin -PathType Leaf)) {
|
|
throw "Go binary not found: $RequestedGoBin"
|
|
}
|
|
return (Resolve-Path -LiteralPath $RequestedGoBin).Path
|
|
}
|
|
|
|
$command = Get-Command go -ErrorAction SilentlyContinue
|
|
if ($command) {
|
|
return $command.Source
|
|
}
|
|
|
|
$defaultPath = "C:\Program Files\Go\bin\go.exe"
|
|
if (Test-Path -LiteralPath $defaultPath -PathType Leaf) {
|
|
return $defaultPath
|
|
}
|
|
|
|
throw "Go binary not found. Please install Go or pass -GoBin."
|
|
}
|
|
|
|
function Resolve-Version {
|
|
param(
|
|
[string]$RequestedVersion,
|
|
[string]$RepoRoot
|
|
)
|
|
|
|
if ($RequestedVersion) {
|
|
return $RequestedVersion
|
|
}
|
|
|
|
$gitCommand = Get-Command git -ErrorAction SilentlyContinue
|
|
if ($gitCommand) {
|
|
Push-Location $RepoRoot
|
|
try {
|
|
$described = git describe --tags --always 2>$null
|
|
if ($LASTEXITCODE -eq 0 -and $described) {
|
|
return $described.Trim()
|
|
}
|
|
$commit = git rev-parse --short HEAD 2>$null
|
|
if ($LASTEXITCODE -eq 0 -and $commit) {
|
|
return $commit.Trim()
|
|
}
|
|
}
|
|
finally {
|
|
Pop-Location
|
|
}
|
|
}
|
|
|
|
return "custom"
|
|
}
|
|
|
|
function Get-TargetConfig {
|
|
param([string]$Target)
|
|
|
|
switch ($Target) {
|
|
"linux-amd64" {
|
|
return @{
|
|
GOOS = "linux"
|
|
GOARCH = "amd64"
|
|
Output = "sing-box-linux-amd64"
|
|
Tags = $script:ResolvedBuildTagsOthers
|
|
}
|
|
}
|
|
"linux-arm64" {
|
|
return @{
|
|
GOOS = "linux"
|
|
GOARCH = "arm64"
|
|
Output = "sing-box-linux-arm64"
|
|
Tags = $script:ResolvedBuildTagsOthers
|
|
}
|
|
}
|
|
"linux-armv7" {
|
|
return @{
|
|
GOOS = "linux"
|
|
GOARCH = "arm"
|
|
GOARM = "7"
|
|
Output = "sing-box-linux-armv7"
|
|
Tags = $script:ResolvedBuildTagsOthers
|
|
}
|
|
}
|
|
"windows-amd64" {
|
|
return @{
|
|
GOOS = "windows"
|
|
GOARCH = "amd64"
|
|
Output = "sing-box-windows-amd64.exe"
|
|
Tags = $script:ResolvedBuildTagsWindows
|
|
}
|
|
}
|
|
"windows-arm64" {
|
|
return @{
|
|
GOOS = "windows"
|
|
GOARCH = "arm64"
|
|
Output = "sing-box-windows-arm64.exe"
|
|
Tags = $script:ResolvedBuildTagsWindows
|
|
}
|
|
}
|
|
"darwin-amd64" {
|
|
return @{
|
|
GOOS = "darwin"
|
|
GOARCH = "amd64"
|
|
Output = "sing-box-darwin-amd64"
|
|
Tags = $script:ResolvedBuildTagsOthers
|
|
}
|
|
}
|
|
"darwin-arm64" {
|
|
return @{
|
|
GOOS = "darwin"
|
|
GOARCH = "arm64"
|
|
Output = "sing-box-darwin-arm64"
|
|
Tags = $script:ResolvedBuildTagsOthers
|
|
}
|
|
}
|
|
"current" {
|
|
$goos = (& $script:ResolvedGoBin env GOOS).Trim()
|
|
$goarch = (& $script:ResolvedGoBin env GOARCH).Trim()
|
|
$output = "sing-box-$goos-$goarch"
|
|
$tags = $script:ResolvedBuildTagsOthers
|
|
if ($goos -eq "windows") {
|
|
$output += ".exe"
|
|
$tags = $script:ResolvedBuildTagsWindows
|
|
}
|
|
return @{
|
|
GOOS = $goos
|
|
GOARCH = $goarch
|
|
Output = $output
|
|
Tags = $tags
|
|
}
|
|
}
|
|
default {
|
|
throw "Unsupported target: $Target"
|
|
}
|
|
}
|
|
}
|
|
|
|
function Invoke-BuildTarget {
|
|
param([string]$Target)
|
|
|
|
$config = Get-TargetConfig -Target $Target
|
|
$outputPath = Join-Path $script:ResolvedDistDir $config.Output
|
|
|
|
Write-Host "==> Building $Target" -ForegroundColor Cyan
|
|
|
|
Push-Location $RootDir
|
|
try {
|
|
$env:CGO_ENABLED = $CgoEnabledValue
|
|
$env:GOOS = $config.GOOS
|
|
$env:GOARCH = $config.GOARCH
|
|
|
|
if ($config.ContainsKey("GOARM")) {
|
|
$env:GOARM = $config.GOARM
|
|
} else {
|
|
Remove-Item Env:GOARM -ErrorAction SilentlyContinue
|
|
}
|
|
|
|
& $script:ResolvedGoBin build -v -p 1 -trimpath `
|
|
-ldflags "-X 'github.com/sagernet/sing-box/constant.Version=$script:ResolvedVersion' $script:ResolvedLdflagsShared -s -w -buildid=" `
|
|
-tags $config.Tags `
|
|
-o $outputPath `
|
|
$MainPkg
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "Build failed: $Target"
|
|
}
|
|
}
|
|
finally {
|
|
Pop-Location
|
|
}
|
|
}
|
|
|
|
if ($Help) {
|
|
Show-Usage
|
|
exit 0
|
|
}
|
|
|
|
if ($Targets.Count -eq 1 -and ($Targets[0] -eq "-h" -or $Targets[0] -eq "--help")) {
|
|
Show-Usage
|
|
exit 0
|
|
}
|
|
|
|
$releaseTagsOthersPath = Join-Path $RootDir "release\DEFAULT_BUILD_TAGS_OTHERS"
|
|
$releaseTagsWindowsPath = Join-Path $RootDir "release\DEFAULT_BUILD_TAGS_WINDOWS"
|
|
$releaseLdflagsPath = Join-Path $RootDir "release\LDFLAGS"
|
|
|
|
Require-File -Path $releaseTagsOthersPath
|
|
Require-File -Path $releaseTagsWindowsPath
|
|
Require-File -Path $releaseLdflagsPath
|
|
|
|
$script:ResolvedGoBin = Resolve-GoBinary -RequestedGoBin $GoBin
|
|
$script:ResolvedDistDir = if ($DistDir) { $DistDir } else { Join-Path $RootDir "dist" }
|
|
$script:ResolvedDistDir = [System.IO.Path]::GetFullPath($script:ResolvedDistDir)
|
|
$script:ResolvedVersion = Resolve-Version -RequestedVersion $Version -RepoRoot $RootDir
|
|
$script:ResolvedBuildTagsOthers = if ($BuildTagsOthers) { $BuildTagsOthers } else { Read-TrimmedFile -Path $releaseTagsOthersPath }
|
|
$script:ResolvedBuildTagsWindows = if ($BuildTagsWindows) { $BuildTagsWindows } else { Read-TrimmedFile -Path $releaseTagsWindowsPath }
|
|
$script:ResolvedLdflagsShared = Read-TrimmedFile -Path $releaseLdflagsPath
|
|
|
|
New-Item -ItemType Directory -Force -Path $script:ResolvedDistDir | Out-Null
|
|
|
|
$resolvedTargets = @()
|
|
if ($Targets.Count -eq 0 -or ($Targets.Count -eq 1 -and $Targets[0] -eq "all")) {
|
|
$resolvedTargets = $DefaultTargets
|
|
} else {
|
|
$resolvedTargets = $Targets
|
|
}
|
|
|
|
foreach ($target in $resolvedTargets) {
|
|
Invoke-BuildTarget -Target $target
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "Build completed." -ForegroundColor Green
|
|
Write-Host "Output directory: $script:ResolvedDistDir"
|