Skip to content

Commit 618cd00

Browse files
hajimehoshiandydotxyz
authored andcommitted
cmd/gomobile: remove a global variable
This CL is a pure refactoring to remove a global variable.. Cherry picked from github.com/golang/gomobile.
1 parent aa78092 commit 618cd00

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

cmd/fyne/internal/mobile/build.go

+27-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
)
2020

2121
var ctx = build.Default
22-
var pkg *build.Package // TODO(crawshaw): remove global pkg variable
2322
var tmpdir string
2423

2524
var cmdBuild = &command{
@@ -72,17 +71,23 @@ const (
7271
)
7372

7473
func runBuild(cmd *command) (err error) {
75-
cleanup, err := buildEnvInit()
74+
_, err = runBuildImpl(cmd)
75+
return
76+
}
77+
78+
// runBuildImpl builds a package for mobiles based on the given commands.
79+
// runBuildImpl returns a built package information and an error if exists.
80+
func runBuildImpl(cmd *command) (*build.Package, error) { cleanup, err := buildEnvInit()
7681
if err != nil {
77-
return err
82+
return nil, err
7883
}
7984
defer cleanup()
8085

8186
args := cmd.Flag.Args()
8287

8388
targetOS, targetArchs, err := parseBuildTarget(buildTarget)
8489
if err != nil {
85-
return fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
90+
return nil, fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
8691
}
8792

8893
oldCtx := ctx
@@ -94,8 +99,14 @@ func runBuild(cmd *command) (err error) {
9499

95100
if ctx.GOOS == "darwin" {
96101
ctx.BuildTags = append(ctx.BuildTags, "ios")
102+
103+
if buildRelease {
104+
targetArchs = []string{"arm", "arm64"}
105+
ctx.GOARCH = targetArchs[0]
106+
}
97107
}
98108

109+
var pkg *build.Package
99110
switch len(args) {
100111
case 0:
101112
pkg, err = ctx.ImportDir(cwd, build.ImportComment)
@@ -106,14 +117,14 @@ func runBuild(cmd *command) (err error) {
106117
os.Exit(1)
107118
}
108119
if err != nil {
109-
return err
120+
return nil, err
110121
}
111122

112123
if pkg.Name != "main" && buildO != "" {
113-
return fmt.Errorf("cannot set -o when building non-main package")
124+
return nil, fmt.Errorf("cannot set -o when building non-main package")
114125
}
115126
if buildBundleID == "" {
116-
return fmt.Errorf("value for -appID is required for a mobile package")
127+
return nil, fmt.Errorf("value for -appID is required for a mobile package")
117128
}
118129

119130
var nmpkgs map[string]bool
@@ -123,39 +134,39 @@ func runBuild(cmd *command) (err error) {
123134
for _, arch := range targetArchs {
124135
env := androidEnv[arch]
125136
if err := goBuild(pkg.ImportPath, env); err != nil {
126-
return err
137+
return nil, err
127138
}
128139
}
129-
return nil
140+
return pkg, nil
130141
}
131142
nmpkgs, err = goAndroidBuild(pkg, buildBundleID, targetArchs, cmd.IconPath, cmd.AppName)
132143
if err != nil {
133-
return err
144+
return nil, err
134145
}
135146
case "darwin":
136147
if !xcodeAvailable() {
137-
return fmt.Errorf("-target=ios requires XCode")
148+
return nil, fmt.Errorf("-target=ios requires XCode")
138149
}
139150
if pkg.Name != "main" {
140151
for _, arch := range targetArchs {
141152
env := darwinEnv[arch]
142153
if err := goBuild(pkg.ImportPath, env); err != nil {
143-
return err
154+
return nil, err
144155
}
145156
}
146-
return nil
157+
return pkg, nil
147158
}
148159
nmpkgs, err = goIOSBuild(pkg, buildBundleID, targetArchs, cmd.AppName)
149160
if err != nil {
150-
return err
161+
return nil, err
151162
}
152163
}
153164

154165
if !nmpkgs["golang.org/x/mobile/app"] {
155-
return fmt.Errorf(`%s does not import "golang.org/x/mobile/app"`, pkg.ImportPath)
166+
return nil, fmt.Errorf(`%s does not import "golang.org/x/mobile/app"`, pkg.ImportPath)
156167
}
157168

158-
return nil
169+
return pkg, nil
159170
}
160171

161172
var nmRE = regexp.MustCompile(`[0-9a-f]{8} t (?:.*/vendor/)?(golang.org/x.*/[^.]*)`)

0 commit comments

Comments
 (0)