Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9e324c

Browse files
committed
cmd/gomobile: remove go/build usages from build.go
This CL is a pure refactoring. This removes a global variable ctx, which is a build.Default. Before this change, ctx was used to keep build tags and its state affected go command executions. As the variable is mutable, the code was not readable. This changes introduces another global variable buildTags instead, but this is more consistent with other build flags, and this is immutable. Updates golang/go#27234 Change-Id: Id8d0c779de21b249e96febd2f40833cd0c84534f Reviewed-on: https://go-review.googlesource.com/c/mobile/+/208060 Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent 08e574b commit d9e324c

File tree

6 files changed

+42
-53
lines changed

6 files changed

+42
-53
lines changed

cmd/gomobile/bind.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"golang.org/x/tools/go/packages"
1919
)
2020

21-
// ctx in build.go
22-
2321
var cmdBind = &command{
2422
run: runBind,
2523
Name: "bind",
@@ -81,12 +79,6 @@ func runBind(cmd *command) error {
8179
return fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
8280
}
8381

84-
// TODO(hajimehoshi): ctx is now used only for recording build tags in bind. Remove this.
85-
oldCtx := ctx
86-
defer func() {
87-
ctx = oldCtx
88-
}()
89-
9082
if bindJavaPkg != "" && targetOS != "android" {
9183
return fmt.Errorf("-javapkg is supported only for android target")
9284
}
@@ -99,9 +91,6 @@ func runBind(cmd *command) error {
9991
return err
10092
}
10193
}
102-
if targetOS == "darwin" {
103-
ctx.BuildTags = append(ctx.BuildTags, "ios")
104-
}
10594

10695
var gobind string
10796
if !buildN {
@@ -232,8 +221,12 @@ func writeFile(filename string, generate func(io.Writer) error) error {
232221
func packagesConfig(targetOS string) *packages.Config {
233222
config := &packages.Config{}
234223
config.Env = append(os.Environ(), "GOARCH=arm", "GOOS="+targetOS)
235-
if len(ctx.BuildTags) > 0 {
236-
config.BuildFlags = []string{"-tags=" + strings.Join(ctx.BuildTags, ",")}
224+
tags := buildTags
225+
if targetOS == "darwin" {
226+
tags = append(tags, "ios")
227+
}
228+
if len(tags) > 0 {
229+
config.BuildFlags = []string{"-tags=" + strings.Join(tags, ",")}
237230
}
238231
return config
239232
}

cmd/gomobile/bind_androidapp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func goAndroidBind(gobind string, pkgs []*packages.Package, androidArchs []strin
3131
)
3232
cmd.Env = append(cmd.Env, "GOOS=android")
3333
cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
34-
if len(ctx.BuildTags) > 0 {
35-
cmd.Args = append(cmd.Args, "-tags="+strings.Join(ctx.BuildTags, ","))
34+
if len(buildTags) > 0 {
35+
cmd.Args = append(cmd.Args, "-tags="+strings.Join(buildTags, ","))
3636
}
3737
if bindJavaPkg != "" {
3838
cmd.Args = append(cmd.Args, "-javapkg="+bindJavaPkg)

cmd/gomobile/bind_iosapp.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ func goIOSBind(gobind string, pkgs []*packages.Package, archs []string) error {
2424
)
2525
cmd.Env = append(cmd.Env, "GOOS=darwin")
2626
cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
27-
if len(ctx.BuildTags) > 0 {
28-
cmd.Args = append(cmd.Args, "-tags="+strings.Join(ctx.BuildTags, ","))
29-
}
27+
tags := append(buildTags, "ios")
28+
cmd.Args = append(cmd.Args, "-tags="+strings.Join(tags, ","))
3029
if bindPrefix != "" {
3130
cmd.Args = append(cmd.Args, "-prefix="+bindPrefix)
3231
}

cmd/gomobile/build.go

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package main
99
import (
1010
"bufio"
1111
"fmt"
12-
"go/build"
1312
"io"
1413
"os"
1514
"os/exec"
@@ -20,7 +19,6 @@ import (
2019
"golang.org/x/tools/go/packages"
2120
)
2221

23-
var ctx = build.Default
2422
var tmpdir string
2523

2624
var cmdBuild = &command{
@@ -89,16 +87,6 @@ func runBuildImpl(cmd *command) (*packages.Package, error) {
8987
return nil, fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
9088
}
9189

92-
// TODO(hajimehoshi): ctx is now used only for recording build tags in build. Remove this.
93-
oldCtx := ctx
94-
defer func() {
95-
ctx = oldCtx
96-
}()
97-
98-
if targetOS == "darwin" {
99-
ctx.BuildTags = append(ctx.BuildTags, "ios")
100-
}
101-
10290
var buildPath string
10391
switch len(args) {
10492
case 0:
@@ -228,20 +216,21 @@ func printcmd(format string, args ...interface{}) {
228216

229217
// "Build flags", used by multiple commands.
230218
var (
231-
buildA bool // -a
232-
buildI bool // -i
233-
buildN bool // -n
234-
buildV bool // -v
235-
buildX bool // -x
236-
buildO string // -o
237-
buildGcflags string // -gcflags
238-
buildLdflags string // -ldflags
239-
buildTarget string // -target
240-
buildTrimpath bool // -trimpath
241-
buildWork bool // -work
242-
buildBundleID string // -bundleid
243-
buildIOSVersion string // -iosversion
244-
buildAndroidAPI int // -androidapi
219+
buildA bool // -a
220+
buildI bool // -i
221+
buildN bool // -n
222+
buildV bool // -v
223+
buildX bool // -x
224+
buildO string // -o
225+
buildGcflags string // -gcflags
226+
buildLdflags string // -ldflags
227+
buildTarget string // -target
228+
buildTrimpath bool // -trimpath
229+
buildWork bool // -work
230+
buildBundleID string // -bundleid
231+
buildIOSVersion string // -iosversion
232+
buildAndroidAPI int // -androidapi
233+
buildTags stringsFlag // -tags
245234
)
246235

247236
func addBuildFlags(cmd *command) {
@@ -256,7 +245,7 @@ func addBuildFlags(cmd *command) {
256245
cmd.flag.BoolVar(&buildA, "a", false, "")
257246
cmd.flag.BoolVar(&buildI, "i", false, "")
258247
cmd.flag.BoolVar(&buildTrimpath, "trimpath", false, "")
259-
cmd.flag.Var((*stringsFlag)(&ctx.BuildTags), "tags", "")
248+
cmd.flag.Var(&buildTags, "tags", "")
260249
}
261250

262251
func addBuildFlagsNVXWork(cmd *command) {
@@ -299,8 +288,16 @@ func goCmd(subcmd string, srcs []string, env []string, args ...string) error {
299288
goBin(),
300289
subcmd,
301290
)
302-
if len(ctx.BuildTags) > 0 {
303-
cmd.Args = append(cmd.Args, "-tags", strings.Join(ctx.BuildTags, " "))
291+
tags := buildTags
292+
targetOS, _, err := parseBuildTarget(buildTarget)
293+
if err != nil {
294+
return err
295+
}
296+
if targetOS == "darwin" {
297+
tags = append(tags, "ios")
298+
}
299+
if len(tags) > 0 {
300+
cmd.Args = append(cmd.Args, "-tags", strings.Join(tags, " "))
304301
}
305302
if buildV {
306303
cmd.Args = append(cmd.Args, "-v")

cmd/gomobile/build_darwin_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func TestIOSBuild(t *testing.T) {
2323
buildTarget = "ios"
2424
buildBundleID = "org.golang.todo"
2525
gopath = filepath.SplitList(goEnv("GOPATH"))[0]
26-
oldTags := ctx.BuildTags
27-
ctx.BuildTags = []string{"tag1"}
26+
oldTags := buildTags
27+
buildTags = []string{"tag1"}
2828
defer func() {
29-
ctx.BuildTags = oldTags
29+
buildTags = oldTags
3030
}()
3131
tests := []struct {
3232
pkg string

cmd/gomobile/build_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ func TestAndroidBuild(t *testing.T) {
8686
os.Setenv("HOMEDRIVE", "C:")
8787
}
8888
cmdBuild.flag.Parse([]string{"golang.org/x/mobile/example/basic"})
89-
oldTags := ctx.BuildTags
90-
ctx.BuildTags = []string{"tag1"}
89+
oldTags := buildTags
90+
buildTags = []string{"tag1"}
9191
defer func() {
92-
ctx.BuildTags = oldTags
92+
buildTags = oldTags
9393
}()
9494
err := runBuild(cmdBuild)
9595
if err != nil {

0 commit comments

Comments
 (0)