cmd/gomobile: make gomobile-init support Go modules

Before this change, all the gomobile commands forced Go modules
to be off internally, regardless of the current Go modules state.

After this change, gomobile-init command follows the current Go
modules state. The other gomobile commands are not changed.

This is also a preparation to support Go modules in gomobile-bind
and gomobile-build.

Updates golang/go#27234

Change-Id: I3d9eedd667cba4d30de3ac0543f037be36cd3e51
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/210477
Run-TryBot: Hajime Hoshi <hajimehoshi@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
diff --git a/cmd/gomobile/bind_androidapp.go b/cmd/gomobile/bind_androidapp.go
index 0ccb079..31b2e56 100644
--- a/cmd/gomobile/bind_androidapp.go
+++ b/cmd/gomobile/bind_androidapp.go
@@ -58,6 +58,8 @@
 		// Add the generated packages to GOPATH for reverse bindings.
 		gopath := fmt.Sprintf("GOPATH=%s%c%s", tmpdir, filepath.ListSeparator, goEnv("GOPATH"))
 		env = append(env, gopath)
+		// gomobile-bind does not support modules yet.
+		env = append(env, "GO111MODULE=off")
 		toolchain := ndk.Toolchain(arch)
 
 		err := goBuildAt(
diff --git a/cmd/gomobile/bind_iosapp.go b/cmd/gomobile/bind_iosapp.go
index 4140731..924a474 100644
--- a/cmd/gomobile/bind_iosapp.go
+++ b/cmd/gomobile/bind_iosapp.go
@@ -178,6 +178,8 @@
 func goIOSBindArchive(name string, env []string, gosrc string) (string, error) {
 	arch := getenv(env, "GOARCH")
 	archive := filepath.Join(tmpdir, name+"-"+arch+".a")
+	// gobind-bind does not support modules yet.
+	env = append(env, "GO111MODULE=off")
 	err := goBuildAt(gosrc, "./gobind", env, "-buildmode=c-archive", "-o", archive)
 	if err != nil {
 		return "", err
diff --git a/cmd/gomobile/build.go b/cmd/gomobile/build.go
index f4b8c09..3c33571 100644
--- a/cmd/gomobile/build.go
+++ b/cmd/gomobile/build.go
@@ -119,6 +119,8 @@
 		if pkg.Name != "main" {
 			for _, arch := range targetArchs {
 				env := androidEnv[arch]
+				// gomobile-build does not support Go modules yet.
+				env = append(env, "GO111MODULE=off")
 				if err := goBuild(pkg.PkgPath, env); err != nil {
 					return nil, err
 				}
@@ -136,6 +138,8 @@
 		if pkg.Name != "main" {
 			for _, arch := range targetArchs {
 				env := darwinEnv[arch]
+				// gomobile-build does not support Go modules yet.
+				env = append(env, "GO111MODULE=off")
 				if err := goBuild(pkg.PkgPath, env); err != nil {
 					return nil, err
 				}
@@ -331,8 +335,6 @@
 	cmd.Args = append(cmd.Args, args...)
 	cmd.Args = append(cmd.Args, srcs...)
 	cmd.Env = append([]string{}, env...)
-	// gomobile does not support modules yet.
-	cmd.Env = append(cmd.Env, "GO111MODULE=off")
 	cmd.Dir = at
 	return runCmd(cmd)
 }
diff --git a/cmd/gomobile/build_androidapp.go b/cmd/gomobile/build_androidapp.go
index 1df9891..e538aa0 100644
--- a/cmd/gomobile/build_androidapp.go
+++ b/cmd/gomobile/build_androidapp.go
@@ -70,6 +70,8 @@
 
 	for _, arch := range androidArchs {
 		env := androidEnv[arch]
+		// gomobile-build does not support Go modules yet.
+		env = append(env, "GO111MODULE=off")
 		toolchain := ndk.Toolchain(arch)
 		libPath := "lib/" + toolchain.abi + "/lib" + libName + ".so"
 		libAbsPath := filepath.Join(tmpdir, libPath)
diff --git a/cmd/gomobile/build_iosapp.go b/cmd/gomobile/build_iosapp.go
index a0103de..b9db63a 100644
--- a/cmd/gomobile/build_iosapp.go
+++ b/cmd/gomobile/build_iosapp.go
@@ -73,7 +73,10 @@
 	for _, arch := range archs {
 		path := filepath.Join(tmpdir, arch)
 		// Disable DWARF; see golang.org/issues/25148.
-		if err := goBuild(src, darwinEnv[arch], "-ldflags=-w", "-o="+path); err != nil {
+		env := darwinEnv[arch]
+		// gomobile-build does not support Go modules yet.
+		env = append(env, "GO111MODULE=off")
+		if err := goBuild(src, env, "-ldflags=-w", "-o="+path); err != nil {
 			return nil, err
 		}
 		if nmpkgs == nil {
diff --git a/cmd/gomobile/init_test.go b/cmd/gomobile/init_test.go
index e01daad..42a8a5a 100644
--- a/cmd/gomobile/init_test.go
+++ b/cmd/gomobile/init_test.go
@@ -106,7 +106,7 @@
 rm -r -f "$GOMOBILE"
 mkdir -p $GOMOBILE
 WORK={{.GOPATH}}/pkg/gomobile/work
-GO111MODULE=off go install -x golang.org/x/mobile/cmd/gobind
+go install -x golang.org/x/mobile/cmd/gobind
 cp $OPENAL_PATH/include/AL/al.h $GOMOBILE/include/AL/al.h
 mkdir -p $GOMOBILE/include/AL
 cp $OPENAL_PATH/include/AL/alc.h $GOMOBILE/include/AL/alc.h