Skip to content

Commit 45c4267

Browse files
committed
Clean up
1 parent 6e4c23b commit 45c4267

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

cmd/gomobile/bind_iosapp.go

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11-
"os"
1211
"os/exec"
1312
"path/filepath"
1413
"strconv"
@@ -59,13 +58,15 @@ func goAppleBind(gobind string, pkgs []*packages.Package, targets []targetInfo)
5958
var waitGroup sync.WaitGroup
6059
waitGroup.Add(len(targets))
6160

61+
var parallelBuildError error
62+
6263
for _, target := range targets {
6364
go func(target targetInfo, targetBuildResultMutex *sync.Mutex) {
6465
defer waitGroup.Done()
6566

66-
err, buildResult := buildTargetArch(target, gobind, pkgs, title, name, modulesUsed)
67+
buildResult, err := buildTargetArch(target, gobind, pkgs, title, name, modulesUsed)
6768
if err != nil {
68-
fmt.Errorf("%v", err)
69+
parallelBuildError = fmt.Errorf("cannot build %s [%s]: %v", target.platform, target.arch, err)
6970
return
7071
}
7172

@@ -82,6 +83,10 @@ func goAppleBind(gobind string, pkgs []*packages.Package, targets []targetInfo)
8283

8384
waitGroup.Wait()
8485

86+
if parallelBuildError != nil {
87+
return parallelBuildError
88+
}
89+
8590
// Finally combine all frameworks to an XCFramework
8691
xcframeworkArgs := []string{"-create-xcframework"}
8792

@@ -95,10 +100,14 @@ func goAppleBind(gobind string, pkgs []*packages.Package, targets []targetInfo)
95100
} else if len(buildResults) == 1 {
96101
refinedBuildResults = append(refinedBuildResults, buildResults[0])
97102
} else {
98-
fmt.Errorf("unexpected number of build results", len(buildResults))
103+
err = fmt.Errorf("unexpected number of build results: %v", len(buildResults))
99104
}
100105
}
101106

107+
if err != nil {
108+
return err
109+
}
110+
102111
for _, result := range refinedBuildResults {
103112
xcframeworkArgs = append(xcframeworkArgs, "-framework", result.frameworkPath)
104113
}
@@ -144,11 +153,11 @@ func mergeArchsForSinglePlatform(from string, to string) error {
144153
return nil
145154
}
146155

147-
func buildTargetArch(t targetInfo, gobindCommandPath string, pkgs []*packages.Package, title string, name string, modulesUsed bool) (err error, buildResult *archBuildResult) {
156+
func buildTargetArch(t targetInfo, gobindCommandPath string, pkgs []*packages.Package, title string, name string, modulesUsed bool) (buildResult *archBuildResult, err error) {
148157
// Catalyst support requires iOS 13+
149158
v, _ := strconv.ParseFloat(buildIOSVersion, 64)
150159
if t.platform == "maccatalyst" && v < 13.0 {
151-
return errors.New("catalyst requires -iosversion=13 or higher"), nil
160+
return nil, errors.New("catalyst requires -iosversion=13 or higher")
152161
}
153162

154163
outDir := filepath.Join(tmpdir, t.platform, t.arch) // adding arch
@@ -172,9 +181,7 @@ func buildTargetArch(t targetInfo, gobindCommandPath string, pkgs []*packages.Pa
172181
cmd.Args = append(cmd.Args, p.PkgPath)
173182
}
174183
if err := runCmd(cmd); err != nil {
175-
fmt.Errorf("%v", err)
176-
os.Exit(1)
177-
return err, nil
184+
return nil, err
178185
}
179186

180187
env := appleEnv[t.String()][:]
@@ -193,45 +200,45 @@ func buildTargetArch(t targetInfo, gobindCommandPath string, pkgs []*packages.Pa
193200
env = append(env, gopath)
194201

195202
if err := writeGoMod(outDir, t.platform, t.arch); err != nil {
196-
return err, nil
203+
return nil, err
197204
}
198205

199206
// Run `go mod tidy` to force to create go.sum.
200207
// Without go.sum, `go build` fails as of Go 1.16.
201208
if modulesUsed {
202209
if err := goModTidyAt(outSrcDir, env); err != nil {
203-
return err, nil
210+
return nil, err
204211
}
205212
}
206213

207214
staticLibPath, err := goAppleBindArchive(name+"-"+t.platform+"-"+t.arch, env, outSrcDir)
208215
if err != nil {
209-
return fmt.Errorf("%s/%s: %v", t.platform, t.arch, err), nil
216+
return nil, fmt.Errorf("%s/%s: %v", t.platform, t.arch, err)
210217
}
211218

212219
versionsDir := filepath.Join(frameworkDir, "Versions")
213220
versionsADir := filepath.Join(versionsDir, "A")
214221
titlePath := filepath.Join(versionsADir, title)
215222
versionsAHeadersDir := filepath.Join(versionsADir, "Headers")
216223
if err := mkdir(versionsAHeadersDir); err != nil {
217-
return err, nil
224+
return nil, err
218225
}
219226
if err := symlink("A", filepath.Join(versionsDir, "Current")); err != nil {
220-
return err, nil
227+
return nil, err
221228
}
222229
if err := symlink("Versions/Current/Headers", filepath.Join(frameworkDir, "Headers")); err != nil {
223-
return err, nil
230+
return nil, err
224231
}
225232
if err := symlink(filepath.Join("Versions/Current", title), filepath.Join(frameworkDir, title)); err != nil {
226-
return err, nil
233+
return nil, err
227234
}
228235

229236
lipoCmd := exec.Command(
230237
"xcrun",
231238
"lipo", staticLibPath, "-create", "-o", titlePath,
232239
)
233240
if err := runCmd(lipoCmd); err != nil {
234-
return err, nil
241+
return nil, err
235242
}
236243

237244
// Copy header file next to output archive.
@@ -243,7 +250,7 @@ func buildTargetArch(t targetInfo, gobindCommandPath string, pkgs []*packages.Pa
243250
filepath.Join(gobindDir, bindPrefix+title+".objc.h"),
244251
)
245252
if err != nil {
246-
return err, nil
253+
return nil, err
247254
}
248255
} else {
249256
for _, fileBase := range fileBases {
@@ -253,15 +260,15 @@ func buildTargetArch(t targetInfo, gobindCommandPath string, pkgs []*packages.Pa
253260
filepath.Join(gobindDir, fileBase+".objc.h"),
254261
)
255262
if err != nil {
256-
return err, nil
263+
return nil, err
257264
}
258265
}
259266
err := copyFile(
260267
filepath.Join(versionsAHeadersDir, "ref.h"),
261268
filepath.Join(gobindDir, "ref.h"),
262269
)
263270
if err != nil {
264-
return err, nil
271+
return nil, err
265272
}
266273
headerFiles = append(headerFiles, title+".h")
267274
err = writeFile(filepath.Join(versionsAHeadersDir, title+".h"), func(w io.Writer) error {
@@ -270,24 +277,24 @@ func buildTargetArch(t targetInfo, gobindCommandPath string, pkgs []*packages.Pa
270277
})
271278
})
272279
if err != nil {
273-
return err, nil
280+
return nil, err
274281
}
275282
}
276283

277284
if err := mkdir(filepath.Join(versionsADir, "Resources")); err != nil {
278-
return err, nil
285+
return nil, err
279286
}
280287

281288
if err := symlink("Versions/Current/Resources", filepath.Join(frameworkDir, "Resources")); err != nil {
282-
return err, nil
289+
return nil, err
283290
}
284291

285292
err = writeFile(filepath.Join(frameworkDir, "Resources", "Info.plist"), func(w io.Writer) error {
286293
_, err := w.Write([]byte(appleBindInfoPlist))
287294
return err
288295
})
289296
if err != nil {
290-
return err, nil
297+
return nil, err
291298
}
292299

293300
var mmVals = struct {
@@ -301,17 +308,17 @@ func buildTargetArch(t targetInfo, gobindCommandPath string, pkgs []*packages.Pa
301308
return appleModuleMapTmpl.Execute(w, mmVals)
302309
})
303310
if err != nil {
304-
return err, nil
311+
return nil, err
305312
}
306313
err = symlink(filepath.Join("Versions/Current/Modules"), filepath.Join(frameworkDir, "Modules"))
307314
if err != nil {
308-
return err, nil
315+
return nil, err
309316
}
310-
return err, &archBuildResult{
317+
return &archBuildResult{
311318
titlePath: titlePath,
312319
targetInfo: t,
313320
frameworkPath: frameworkDir,
314-
}
321+
}, err
315322
}
316323

317324
var appleBindHeaderTmpl = template.Must(template.New("apple.h").Parse(`

0 commit comments

Comments
 (0)