cmd/gomobile: remove a global variable

This CL is a pure refactoring to remove a global variable.

Change-Id: I996c6a15354163e90148bc2540bda6b56296271c
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/208057
Run-TryBot: Hajime Hoshi <hajimehoshi@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/cmd/gomobile/build.go b/cmd/gomobile/build.go
index 545234d..9a980ff 100644
--- a/cmd/gomobile/build.go
+++ b/cmd/gomobile/build.go
@@ -18,7 +18,6 @@
 )
 
 var ctx = build.Default
-var pkg *build.Package // TODO(crawshaw): remove global pkg variable
 var tmpdir string
 
 var cmdBuild = &command{
@@ -67,9 +66,16 @@
 }
 
 func runBuild(cmd *command) (err error) {
+	_, err = runBuildImpl(cmd)
+	return
+}
+
+// runBuildImpl builds a package for mobiles based on the given commands.
+// runBuildImpl returns a built package information and an error if exists.
+func runBuildImpl(cmd *command) (*build.Package, error) {
 	cleanup, err := buildEnvInit()
 	if err != nil {
-		return err
+		return nil, err
 	}
 	defer cleanup()
 
@@ -77,7 +83,7 @@
 
 	targetOS, targetArchs, err := parseBuildTarget(buildTarget)
 	if err != nil {
-		return fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
+		return nil, fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
 	}
 
 	oldCtx := ctx
@@ -91,6 +97,7 @@
 		ctx.BuildTags = append(ctx.BuildTags, "ios")
 	}
 
+	var pkg *build.Package
 	switch len(args) {
 	case 0:
 		pkg, err = ctx.ImportDir(cwd, build.ImportComment)
@@ -101,11 +108,11 @@
 		os.Exit(1)
 	}
 	if err != nil {
-		return err
+		return nil, err
 	}
 
 	if pkg.Name != "main" && buildO != "" {
-		return fmt.Errorf("cannot set -o when building non-main package")
+		return nil, fmt.Errorf("cannot set -o when building non-main package")
 	}
 
 	var nmpkgs map[string]bool
@@ -115,42 +122,42 @@
 			for _, arch := range targetArchs {
 				env := androidEnv[arch]
 				if err := goBuild(pkg.ImportPath, env); err != nil {
-					return err
+					return nil, err
 				}
 			}
-			return nil
+			return pkg, nil
 		}
 		nmpkgs, err = goAndroidBuild(pkg, targetArchs)
 		if err != nil {
-			return err
+			return nil, err
 		}
 	case "darwin":
 		if !xcodeAvailable() {
-			return fmt.Errorf("-target=ios requires XCode")
+			return nil, fmt.Errorf("-target=ios requires XCode")
 		}
 		if pkg.Name != "main" {
 			for _, arch := range targetArchs {
 				env := darwinEnv[arch]
 				if err := goBuild(pkg.ImportPath, env); err != nil {
-					return err
+					return nil, err
 				}
 			}
-			return nil
+			return pkg, nil
 		}
 		if buildBundleID == "" {
-			return fmt.Errorf("-target=ios requires -bundleid set")
+			return nil, fmt.Errorf("-target=ios requires -bundleid set")
 		}
 		nmpkgs, err = goIOSBuild(pkg, buildBundleID, targetArchs)
 		if err != nil {
-			return err
+			return nil, err
 		}
 	}
 
 	if !nmpkgs["golang.org/x/mobile/app"] {
-		return fmt.Errorf(`%s does not import "golang.org/x/mobile/app"`, pkg.ImportPath)
+		return nil, fmt.Errorf(`%s does not import "golang.org/x/mobile/app"`, pkg.ImportPath)
 	}
 
-	return nil
+	return pkg, nil
 }
 
 var nmRE = regexp.MustCompile(`[0-9a-f]{8} t (?:.*/vendor/)?(golang.org/x.*/[^.]*)`)
diff --git a/cmd/gomobile/install.go b/cmd/gomobile/install.go
index 86edaed..05ff14b 100644
--- a/cmd/gomobile/install.go
+++ b/cmd/gomobile/install.go
@@ -33,7 +33,8 @@
 	if !strings.HasPrefix(buildTarget, "android") {
 		return fmt.Errorf("install is not supported for -target=%s", buildTarget)
 	}
-	if err := runBuild(cmd); err != nil {
+	pkg, err := runBuildImpl(cmd)
+	if err != nil {
 		return err
 	}