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 <hyangah@gmail.com>
This commit is contained in:
Hajime Hoshi 2019-11-20 16:55:32 +09:00
parent 08e574b148
commit d9e324ca8c
6 changed files with 42 additions and 53 deletions

View File

@ -18,8 +18,6 @@ import (
"golang.org/x/tools/go/packages"
)
// ctx in build.go
var cmdBind = &command{
run: runBind,
Name: "bind",
@ -81,12 +79,6 @@ func runBind(cmd *command) error {
return fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
}
// TODO(hajimehoshi): ctx is now used only for recording build tags in bind. Remove this.
oldCtx := ctx
defer func() {
ctx = oldCtx
}()
if bindJavaPkg != "" && targetOS != "android" {
return fmt.Errorf("-javapkg is supported only for android target")
}
@ -99,9 +91,6 @@ func runBind(cmd *command) error {
return err
}
}
if targetOS == "darwin" {
ctx.BuildTags = append(ctx.BuildTags, "ios")
}
var gobind string
if !buildN {
@ -232,8 +221,12 @@ func writeFile(filename string, generate func(io.Writer) error) error {
func packagesConfig(targetOS string) *packages.Config {
config := &packages.Config{}
config.Env = append(os.Environ(), "GOARCH=arm", "GOOS="+targetOS)
if len(ctx.BuildTags) > 0 {
config.BuildFlags = []string{"-tags=" + strings.Join(ctx.BuildTags, ",")}
tags := buildTags
if targetOS == "darwin" {
tags = append(tags, "ios")
}
if len(tags) > 0 {
config.BuildFlags = []string{"-tags=" + strings.Join(tags, ",")}
}
return config
}

View File

@ -31,8 +31,8 @@ func goAndroidBind(gobind string, pkgs []*packages.Package, androidArchs []strin
)
cmd.Env = append(cmd.Env, "GOOS=android")
cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
if len(ctx.BuildTags) > 0 {
cmd.Args = append(cmd.Args, "-tags="+strings.Join(ctx.BuildTags, ","))
if len(buildTags) > 0 {
cmd.Args = append(cmd.Args, "-tags="+strings.Join(buildTags, ","))
}
if bindJavaPkg != "" {
cmd.Args = append(cmd.Args, "-javapkg="+bindJavaPkg)

View File

@ -24,9 +24,8 @@ func goIOSBind(gobind string, pkgs []*packages.Package, archs []string) error {
)
cmd.Env = append(cmd.Env, "GOOS=darwin")
cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
if len(ctx.BuildTags) > 0 {
cmd.Args = append(cmd.Args, "-tags="+strings.Join(ctx.BuildTags, ","))
}
tags := append(buildTags, "ios")
cmd.Args = append(cmd.Args, "-tags="+strings.Join(tags, ","))
if bindPrefix != "" {
cmd.Args = append(cmd.Args, "-prefix="+bindPrefix)
}

View File

@ -9,7 +9,6 @@ package main
import (
"bufio"
"fmt"
"go/build"
"io"
"os"
"os/exec"
@ -20,7 +19,6 @@ import (
"golang.org/x/tools/go/packages"
)
var ctx = build.Default
var tmpdir string
var cmdBuild = &command{
@ -89,16 +87,6 @@ func runBuildImpl(cmd *command) (*packages.Package, error) {
return nil, fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
}
// TODO(hajimehoshi): ctx is now used only for recording build tags in build. Remove this.
oldCtx := ctx
defer func() {
ctx = oldCtx
}()
if targetOS == "darwin" {
ctx.BuildTags = append(ctx.BuildTags, "ios")
}
var buildPath string
switch len(args) {
case 0:
@ -242,6 +230,7 @@ var (
buildBundleID string // -bundleid
buildIOSVersion string // -iosversion
buildAndroidAPI int // -androidapi
buildTags stringsFlag // -tags
)
func addBuildFlags(cmd *command) {
@ -256,7 +245,7 @@ func addBuildFlags(cmd *command) {
cmd.flag.BoolVar(&buildA, "a", false, "")
cmd.flag.BoolVar(&buildI, "i", false, "")
cmd.flag.BoolVar(&buildTrimpath, "trimpath", false, "")
cmd.flag.Var((*stringsFlag)(&ctx.BuildTags), "tags", "")
cmd.flag.Var(&buildTags, "tags", "")
}
func addBuildFlagsNVXWork(cmd *command) {
@ -299,8 +288,16 @@ func goCmd(subcmd string, srcs []string, env []string, args ...string) error {
goBin(),
subcmd,
)
if len(ctx.BuildTags) > 0 {
cmd.Args = append(cmd.Args, "-tags", strings.Join(ctx.BuildTags, " "))
tags := buildTags
targetOS, _, err := parseBuildTarget(buildTarget)
if err != nil {
return err
}
if targetOS == "darwin" {
tags = append(tags, "ios")
}
if len(tags) > 0 {
cmd.Args = append(cmd.Args, "-tags", strings.Join(tags, " "))
}
if buildV {
cmd.Args = append(cmd.Args, "-v")

View File

@ -23,10 +23,10 @@ func TestIOSBuild(t *testing.T) {
buildTarget = "ios"
buildBundleID = "org.golang.todo"
gopath = filepath.SplitList(goEnv("GOPATH"))[0]
oldTags := ctx.BuildTags
ctx.BuildTags = []string{"tag1"}
oldTags := buildTags
buildTags = []string{"tag1"}
defer func() {
ctx.BuildTags = oldTags
buildTags = oldTags
}()
tests := []struct {
pkg string

View File

@ -86,10 +86,10 @@ func TestAndroidBuild(t *testing.T) {
os.Setenv("HOMEDRIVE", "C:")
}
cmdBuild.flag.Parse([]string{"golang.org/x/mobile/example/basic"})
oldTags := ctx.BuildTags
ctx.BuildTags = []string{"tag1"}
oldTags := buildTags
buildTags = []string{"tag1"}
defer func() {
ctx.BuildTags = oldTags
buildTags = oldTags
}()
err := runBuild(cmdBuild)
if err != nil {