Skip to content

Commit

Permalink
Add parallelism limit
Browse files Browse the repository at this point in the history
  • Loading branch information
imWildCat committed Jan 20, 2022
1 parent 6d124e1 commit fd0e949
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
13 changes: 12 additions & 1 deletion cmd/gomobile/bind.go
Expand Up @@ -126,10 +126,13 @@ func runBind(cmd *command) error {
case isAndroidPlatform(targets[0].platform):
return goAndroidBind(gobind, pkgs, targets)
case isApplePlatform(targets[0].platform):
if buildWorkerError := validateBuildWorkers(buildWorkers); buildWorkerError != nil {
return buildWorkerError
}
if !xcodeAvailable() {
return fmt.Errorf("-target=%q requires Xcode", buildTarget)
}
return goAppleBind(gobind, pkgs, targets)
return goAppleBind(gobind, pkgs, targets, buildWorkers)
default:
return fmt.Errorf(`invalid -target=%q`, buildTarget)
}
Expand Down Expand Up @@ -321,3 +324,11 @@ func areGoModulesUsed() (bool, error) {
}
return true, nil
}

func validateBuildWorkers(workers int) error {
if workers < 1 {
return fmt.Errorf("invalid workers %d: must be >= 1", workers)
} else {
return nil
}
}
18 changes: 12 additions & 6 deletions cmd/gomobile/bind_iosapp.go
Expand Up @@ -23,7 +23,7 @@ type archBuildResult struct {
frameworkPath string
}

func goAppleBind(gobind string, pkgs []*packages.Package, targets []targetInfo) error {
func goAppleBind(gobind string, pkgs []*packages.Package, targets []targetInfo, buildWorkers int) error {
var name string
var title string

Expand Down Expand Up @@ -55,15 +55,20 @@ func goAppleBind(gobind string, pkgs []*packages.Package, targets []targetInfo)
var waitGroup sync.WaitGroup
waitGroup.Add(len(targets))

var parallelBuildError error
var parallelBuildErrorBuffer = make(chan error, len(targets))
semophore := make(chan struct{}, buildWorkers)

for _, target := range targets {
go func(target targetInfo) {
defer waitGroup.Done()
semophore <- struct{}{}
defer func() {
<-semophore
waitGroup.Done()
}()

buildResult, err := buildTargetArch(target, gobind, pkgs, title, name, modulesUsed)
if err != nil {
parallelBuildError = fmt.Errorf("cannot build %s [%s]: %v", target.platform, target.arch, err)
parallelBuildErrorBuffer <- fmt.Errorf("cannot build %s [%s]: %v", target.platform, target.arch, err)
return
}

Expand All @@ -75,9 +80,10 @@ func goAppleBind(gobind string, pkgs []*packages.Package, targets []targetInfo)
}

waitGroup.Wait()
close(parallelBuildErrorBuffer)

if parallelBuildError != nil {
return parallelBuildError
for buildErr := range parallelBuildErrorBuffer {
return buildErr
}

// Finally combine all frameworks to an XCFramework
Expand Down
3 changes: 3 additions & 0 deletions cmd/gomobile/build.go
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"

Expand Down Expand Up @@ -243,6 +244,7 @@ var (
buildTarget string // -target
buildTrimpath bool // -trimpath
buildWork bool // -work
buildWorkers int // -j
buildBundleID string // -bundleid
buildIOSVersion string // -iosversion
buildAndroidAPI int // -androidapi
Expand All @@ -262,6 +264,7 @@ func addBuildFlags(cmd *command) {
cmd.flag.BoolVar(&buildI, "i", false, "")
cmd.flag.BoolVar(&buildTrimpath, "trimpath", false, "")
cmd.flag.Var(&buildTags, "tags", "")
cmd.flag.IntVar(&buildWorkers, "j", runtime.NumCPU(), "")
}

func addBuildFlagsNVXWork(cmd *command) {
Expand Down

0 comments on commit fd0e949

Please sign in to comment.