diff --git a/building-install.sh b/building-install.sh index 53f814a5..17a69da7 100644 --- a/building-install.sh +++ b/building-install.sh @@ -22,6 +22,22 @@ SERVICE_NAME="singbox" SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" 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}" # Check root @@ -71,6 +87,7 @@ install_go() { # Build sing-box build_sing_box() { echo -e "${YELLOW}Building sing-box from source...${NC}" + BUILD_JOBS="$(resolve_build_jobs)" # Check if we are in the source directory if [[ ! -f "go.mod" ]]; then @@ -101,12 +118,11 @@ build_sing_box() { fi 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 -p 1 to limit parallel builds (prevent OOM spikes) # 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" \ -ldflags "-X 'github.com/sagernet/sing-box/constant.Version=$VERSION' -s -w" \ -tags "$TAGS" \ diff --git a/building-linux.sh b/building-linux.sh index cf8fc5a9..9831fc83 100644 --- a/building-linux.sh +++ b/building-linux.sh @@ -7,6 +7,7 @@ DIST_DIR="${DIST_DIR:-$ROOT_DIR/dist}" MAIN_PKG="./cmd/sing-box" GO_BIN="${GO_BIN:-go}" CGO_ENABLED_VALUE="${CGO_ENABLED_VALUE:-0}" +BUILD_JOBS="${GO_BUILD_JOBS:-}" DEFAULT_TARGETS=( "linux-amd64" @@ -31,6 +32,7 @@ Environment variables: GO_BIN Go binary path, default: go DIST_DIR Output directory, default: ./dist 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 BUILD_TAGS_OTHERS Override tags for non-Windows builds BUILD_TAGS_WINDOWS Override tags for Windows builds @@ -62,6 +64,22 @@ resolve_version() { 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() { local target="$1" local goos goarch goarm="" output tags @@ -127,7 +145,7 @@ build_target() { ;; esac - echo "==> Building $target" + echo "==> Building $target (jobs: $RESOLVED_BUILD_JOBS)" ( cd "$ROOT_DIR" export CGO_ENABLED="$CGO_ENABLED_VALUE" @@ -139,7 +157,7 @@ build_target() { unset GOARM 2>/dev/null || true 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=" \ -tags "$tags" \ -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")}" LDFLAGS_SHARED="$(trim_file "$ROOT_DIR/release/LDFLAGS")" VERSION_VALUE="$(resolve_version)" +RESOLVED_BUILD_JOBS="$(resolve_build_jobs)" mkdir -p "$DIST_DIR" diff --git a/building-windows.ps1 b/building-windows.ps1 index 91cdc630..5a760766 100644 --- a/building-windows.ps1 +++ b/building-windows.ps1 @@ -6,6 +6,7 @@ param( [string]$GoBin = "", [string]$DistDir = "", [string]$CgoEnabledValue = "0", + [string]$BuildJobs = "", [string]$Version = "", [string]$BuildTagsOthers = "", [string]$BuildTagsWindows = "", @@ -40,6 +41,7 @@ Optional parameters: -GoBin Go binary path -DistDir Output directory, default: .\dist -CgoEnabledValue <0|1> CGO_ENABLED value, default: 0 + -BuildJobs Go build parallel jobs, default: GO_BUILD_JOBS or CPU core count -Version Embedded version, default: git describe --tags --always -BuildTagsOthers Override non-Windows build tags -BuildTagsWindows Override Windows build tags @@ -114,6 +116,20 @@ function Resolve-Version { 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 { param([string]$Target) @@ -203,7 +219,7 @@ function Invoke-BuildTarget { $config = Get-TargetConfig -Target $Target $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 try { @@ -217,7 +233,7 @@ function Invoke-BuildTarget { 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=" ` -tags $config.Tags ` -o $outputPath ` @@ -253,6 +269,7 @@ 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:ResolvedBuildJobs = Resolve-BuildJobs -RequestedBuildJobs $BuildJobs $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 }