Skip to content

Commit

Permalink
cmd/gomobile: remove go/build usages from build.go
Browse files Browse the repository at this point in the history
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

Conflict: cmd/gomobile/build.go
Cherry picked from github.com/golang/mobile
  • Loading branch information
hajimehoshi authored and andydotxyz committed Mar 5, 2020
1 parent 9070acd commit 469edaa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
10 changes: 7 additions & 3 deletions cmd/fyne/internal/mobile/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"golang.org/x/tools/go/packages"
)

// ctx, pkg, tmpdir in build.go
// pkg, tmpdir in build.go

func copyFile(dst, src string) error {
if buildX {
Expand Down Expand Up @@ -68,8 +68,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
}
65 changes: 31 additions & 34 deletions cmd/fyne/internal/mobile/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"golang.org/x/tools/go/packages"
)

var ctx = build.Default
var tmpdir string

var cmdBuild = &command{
Expand Down Expand Up @@ -93,21 +92,6 @@ func runBuildImpl(cmd *command) (*packages.Package, error) { cleanup, err := bui
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")

if buildRelease {
targetArchs = []string{"arm", "arm64"}
ctx.GOARCH = targetArchs[0]
}
}

var buildPath string
switch len(args) {
case 0:
Expand Down Expand Up @@ -157,6 +141,10 @@ func runBuildImpl(cmd *command) (*packages.Package, error) { cleanup, err := bui
if !xcodeAvailable() {
return nil, fmt.Errorf("-target=ios requires XCode")
}
if buildRelease {
targetArchs = []string{"arm", "arm64"}
}

if pkg.Name != "main" {
for _, arch := range targetArchs {
env := darwinEnv[arch]
Expand Down Expand Up @@ -247,21 +235,22 @@ func printcmd(format string, args ...interface{}) {

// "Build flags", used by multiple commands.
var (
buildA bool // -a
buildI bool // -i
buildN bool // -n
buildV bool // -v
buildX bool // -x
buildO string // -o
buildGcflags string // -gcflags
buildLdflags string // -ldflags
buildRelease bool // -release
buildTarget string // -target
buildTrimpath bool // -trimpath
buildWork bool // -work
buildBundleID string // -bundleid
buildIOSVersion string // -iosversion
buildAndroidAPI int // -androidapi
buildA bool // -a
buildI bool // -i
buildN bool // -n
buildV bool // -v
buildX bool // -x
buildO string // -o
buildGcflags string // -gcflags
buildLdflags string // -ldflags
buildRelease bool // -release
buildTarget string // -target
buildTrimpath bool // -trimpath
buildWork bool // -work
buildBundleID string // -bundleid
buildIOSVersion string // -iosversion
buildAndroidAPI int // -androidapi
buildTags stringsFlag // -tags
)

func RunNewBuild(target, appID, icon, name string, release bool) error {
Expand All @@ -288,7 +277,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) {
Expand Down Expand Up @@ -323,8 +312,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")
Expand Down

0 comments on commit 469edaa

Please sign in to comment.