启用多核编译

This commit is contained in:
CN-JS-HuiBai
2026-04-15 20:13:53 +08:00
parent 20d3cb46d4
commit d45e84f837
3 changed files with 59 additions and 7 deletions

View File

@@ -22,6 +22,22 @@ SERVICE_NAME="singbox"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
LEGACY_SERVICE_NAMES=("ganclient" "sing-box") LEGACY_SERVICE_NAMES=("ganclient" "sing-box")
resolve_build_jobs() {
if [[ -n "${GO_BUILD_JOBS:-}" ]]; then
echo "$GO_BUILD_JOBS"
return
fi
if command -v nproc >/dev/null 2>&1; then
nproc
return
fi
if command -v getconf >/dev/null 2>&1; then
getconf _NPROCESSORS_ONLN
return
fi
echo "1"
}
echo -e "${GREEN}Welcome to singbox Installation Script${NC}" echo -e "${GREEN}Welcome to singbox Installation Script${NC}"
# Check root # Check root
@@ -71,6 +87,7 @@ install_go() {
# Build sing-box # Build sing-box
build_sing_box() { build_sing_box() {
echo -e "${YELLOW}Building sing-box from source...${NC}" echo -e "${YELLOW}Building sing-box from source...${NC}"
BUILD_JOBS="$(resolve_build_jobs)"
# Check if we are in the source directory # Check if we are in the source directory
if [[ ! -f "go.mod" ]]; then if [[ ! -f "go.mod" ]]; then
@@ -101,12 +118,11 @@ build_sing_box() {
fi fi
echo -e "${YELLOW}Starting compilation (this may take a few minutes)...${NC}" echo -e "${YELLOW}Starting compilation (this may take a few minutes)...${NC}"
echo -e "${YELLOW}Note: Using -p 1 to save memory and avoid silent crashes.${NC}" echo -e "${YELLOW}Using Go parallel build jobs: ${BUILD_JOBS}${NC}"
# Use -o to be explicit about output location # Use -o to be explicit about output location
# Use -p 1 to limit parallel builds (prevent OOM spikes)
# Redirect stderr to stdout to see errors clearly # Redirect stderr to stdout to see errors clearly
if ! go build -v -p 1 -trimpath \ if ! go build -v -p "$BUILD_JOBS" -trimpath \
-o "$BINARY_PATH" \ -o "$BINARY_PATH" \
-ldflags "-X 'github.com/sagernet/sing-box/constant.Version=$VERSION' -s -w" \ -ldflags "-X 'github.com/sagernet/sing-box/constant.Version=$VERSION' -s -w" \
-tags "$TAGS" \ -tags "$TAGS" \

View File

@@ -7,6 +7,7 @@ DIST_DIR="${DIST_DIR:-$ROOT_DIR/dist}"
MAIN_PKG="./cmd/sing-box" MAIN_PKG="./cmd/sing-box"
GO_BIN="${GO_BIN:-go}" GO_BIN="${GO_BIN:-go}"
CGO_ENABLED_VALUE="${CGO_ENABLED_VALUE:-0}" CGO_ENABLED_VALUE="${CGO_ENABLED_VALUE:-0}"
BUILD_JOBS="${GO_BUILD_JOBS:-}"
DEFAULT_TARGETS=( DEFAULT_TARGETS=(
"linux-amd64" "linux-amd64"
@@ -31,6 +32,7 @@ Environment variables:
GO_BIN Go binary path, default: go GO_BIN Go binary path, default: go
DIST_DIR Output directory, default: ./dist DIST_DIR Output directory, default: ./dist
CGO_ENABLED_VALUE CGO_ENABLED value, default: 0 CGO_ENABLED_VALUE CGO_ENABLED value, default: 0
GO_BUILD_JOBS Go build parallel jobs, default: detected CPU core count
VERSION Embedded version string, default: git describe --tags --always VERSION Embedded version string, default: git describe --tags --always
BUILD_TAGS_OTHERS Override tags for non-Windows builds BUILD_TAGS_OTHERS Override tags for non-Windows builds
BUILD_TAGS_WINDOWS Override tags for Windows builds BUILD_TAGS_WINDOWS Override tags for Windows builds
@@ -62,6 +64,22 @@ resolve_version() {
printf '%s' "custom" printf '%s' "custom"
} }
resolve_build_jobs() {
if [[ -n "$BUILD_JOBS" ]]; then
printf '%s' "$BUILD_JOBS"
return
fi
if command -v nproc >/dev/null 2>&1; then
nproc
return
fi
if command -v getconf >/dev/null 2>&1; then
getconf _NPROCESSORS_ONLN
return
fi
printf '%s' "1"
}
build_target() { build_target() {
local target="$1" local target="$1"
local goos goarch goarm="" output tags local goos goarch goarm="" output tags
@@ -127,7 +145,7 @@ build_target() {
;; ;;
esac esac
echo "==> Building $target" echo "==> Building $target (jobs: $RESOLVED_BUILD_JOBS)"
( (
cd "$ROOT_DIR" cd "$ROOT_DIR"
export CGO_ENABLED="$CGO_ENABLED_VALUE" export CGO_ENABLED="$CGO_ENABLED_VALUE"
@@ -139,7 +157,7 @@ build_target() {
unset GOARM 2>/dev/null || true unset GOARM 2>/dev/null || true
fi fi
"$GO_BIN" build -v -p 1 -trimpath \ "$GO_BIN" build -v -p "$RESOLVED_BUILD_JOBS" -trimpath \
-ldflags "-X 'github.com/sagernet/sing-box/constant.Version=$VERSION_VALUE' $LDFLAGS_SHARED -s -w -buildid=" \ -ldflags "-X 'github.com/sagernet/sing-box/constant.Version=$VERSION_VALUE' $LDFLAGS_SHARED -s -w -buildid=" \
-tags "$tags" \ -tags "$tags" \
-o "$output" \ -o "$output" \
@@ -165,6 +183,7 @@ BUILD_TAGS_OTHERS="${BUILD_TAGS_OTHERS:-$(trim_file "$ROOT_DIR/release/DEFAULT_B
BUILD_TAGS_WINDOWS="${BUILD_TAGS_WINDOWS:-$(trim_file "$ROOT_DIR/release/DEFAULT_BUILD_TAGS_WINDOWS")}" BUILD_TAGS_WINDOWS="${BUILD_TAGS_WINDOWS:-$(trim_file "$ROOT_DIR/release/DEFAULT_BUILD_TAGS_WINDOWS")}"
LDFLAGS_SHARED="$(trim_file "$ROOT_DIR/release/LDFLAGS")" LDFLAGS_SHARED="$(trim_file "$ROOT_DIR/release/LDFLAGS")"
VERSION_VALUE="$(resolve_version)" VERSION_VALUE="$(resolve_version)"
RESOLVED_BUILD_JOBS="$(resolve_build_jobs)"
mkdir -p "$DIST_DIR" mkdir -p "$DIST_DIR"

View File

@@ -6,6 +6,7 @@ param(
[string]$GoBin = "", [string]$GoBin = "",
[string]$DistDir = "", [string]$DistDir = "",
[string]$CgoEnabledValue = "0", [string]$CgoEnabledValue = "0",
[string]$BuildJobs = "",
[string]$Version = "", [string]$Version = "",
[string]$BuildTagsOthers = "", [string]$BuildTagsOthers = "",
[string]$BuildTagsWindows = "", [string]$BuildTagsWindows = "",
@@ -40,6 +41,7 @@ Optional parameters:
-GoBin <path> Go binary path -GoBin <path> Go binary path
-DistDir <path> Output directory, default: .\dist -DistDir <path> Output directory, default: .\dist
-CgoEnabledValue <0|1> CGO_ENABLED value, default: 0 -CgoEnabledValue <0|1> CGO_ENABLED value, default: 0
-BuildJobs <int> Go build parallel jobs, default: GO_BUILD_JOBS or CPU core count
-Version <string> Embedded version, default: git describe --tags --always -Version <string> Embedded version, default: git describe --tags --always
-BuildTagsOthers <string> Override non-Windows build tags -BuildTagsOthers <string> Override non-Windows build tags
-BuildTagsWindows <string> Override Windows build tags -BuildTagsWindows <string> Override Windows build tags
@@ -114,6 +116,20 @@ function Resolve-Version {
return "custom" return "custom"
} }
function Resolve-BuildJobs {
param([string]$RequestedBuildJobs)
if ($RequestedBuildJobs) {
return $RequestedBuildJobs
}
if ($env:GO_BUILD_JOBS) {
return $env:GO_BUILD_JOBS
}
return [string][Environment]::ProcessorCount
}
function Get-TargetConfig { function Get-TargetConfig {
param([string]$Target) param([string]$Target)
@@ -203,7 +219,7 @@ function Invoke-BuildTarget {
$config = Get-TargetConfig -Target $Target $config = Get-TargetConfig -Target $Target
$outputPath = Join-Path $script:ResolvedDistDir $config.Output $outputPath = Join-Path $script:ResolvedDistDir $config.Output
Write-Host "==> Building $Target" -ForegroundColor Cyan Write-Host "==> Building $Target (jobs: $script:ResolvedBuildJobs)" -ForegroundColor Cyan
Push-Location $RootDir Push-Location $RootDir
try { try {
@@ -217,7 +233,7 @@ function Invoke-BuildTarget {
Remove-Item Env:GOARM -ErrorAction SilentlyContinue Remove-Item Env:GOARM -ErrorAction SilentlyContinue
} }
& $script:ResolvedGoBin build -v -p 1 -trimpath ` & $script:ResolvedGoBin build -v -p $script:ResolvedBuildJobs -trimpath `
-ldflags "-X 'github.com/sagernet/sing-box/constant.Version=$script:ResolvedVersion' $script:ResolvedLdflagsShared -s -w -buildid=" ` -ldflags "-X 'github.com/sagernet/sing-box/constant.Version=$script:ResolvedVersion' $script:ResolvedLdflagsShared -s -w -buildid=" `
-tags $config.Tags ` -tags $config.Tags `
-o $outputPath ` -o $outputPath `
@@ -253,6 +269,7 @@ Require-File -Path $releaseLdflagsPath
$script:ResolvedGoBin = Resolve-GoBinary -RequestedGoBin $GoBin $script:ResolvedGoBin = Resolve-GoBinary -RequestedGoBin $GoBin
$script:ResolvedDistDir = if ($DistDir) { $DistDir } else { Join-Path $RootDir "dist" } $script:ResolvedDistDir = if ($DistDir) { $DistDir } else { Join-Path $RootDir "dist" }
$script:ResolvedDistDir = [System.IO.Path]::GetFullPath($script:ResolvedDistDir) $script:ResolvedDistDir = [System.IO.Path]::GetFullPath($script:ResolvedDistDir)
$script:ResolvedBuildJobs = Resolve-BuildJobs -RequestedBuildJobs $BuildJobs
$script:ResolvedVersion = Resolve-Version -RequestedVersion $Version -RepoRoot $RootDir $script:ResolvedVersion = Resolve-Version -RequestedVersion $Version -RepoRoot $RootDir
$script:ResolvedBuildTagsOthers = if ($BuildTagsOthers) { $BuildTagsOthers } else { Read-TrimmedFile -Path $releaseTagsOthersPath } $script:ResolvedBuildTagsOthers = if ($BuildTagsOthers) { $BuildTagsOthers } else { Read-TrimmedFile -Path $releaseTagsOthersPath }
$script:ResolvedBuildTagsWindows = if ($BuildTagsWindows) { $BuildTagsWindows } else { Read-TrimmedFile -Path $releaseTagsWindowsPath } $script:ResolvedBuildTagsWindows = if ($BuildTagsWindows) { $BuildTagsWindows } else { Read-TrimmedFile -Path $releaseTagsWindowsPath }