cmd/gomobile: don't build apps that doesn't import the app package

In order to keep consistency with target=android, this CL turns
off gomobile build support for target=ios for programs that don't
import golang.org/x/mobile/app.

Change-Id: I423b042144aecfdc127726d0b97733c4d6532a81
Reviewed-on: https://go-review.googlesource.com/11985
Reviewed-by: David Crawshaw <crawshaw@golang.org>
diff --git a/cmd/gomobile/build.go b/cmd/gomobile/build.go
index 92fd5c5..00cc31a 100644
--- a/cmd/gomobile/build.go
+++ b/cmd/gomobile/build.go
@@ -89,6 +89,9 @@
 			if pkg.Name != "main" {
 				return fmt.Errorf("cannot build non-main packages")
 			}
+			if err := importsApp(pkg); err != nil {
+				return err
+			}
 			return goIOSBuild(pkg.ImportPath)
 		}
 		return fmt.Errorf("-target=ios requires darwin host")
@@ -101,16 +104,8 @@
 		return goAndroidBuild(pkg.ImportPath, "")
 	}
 
-	// Building a program, make sure it is appropriate for mobile.
-	importsApp := false
-	for _, path := range pkg.Imports {
-		if path == "golang.org/x/mobile/app" {
-			importsApp = true
-			break
-		}
-	}
-	if !importsApp {
-		return fmt.Errorf(`%s does not import "golang.org/x/mobile/app"`, pkg.ImportPath)
+	if err := importsApp(pkg); err != nil {
+		return err
 	}
 
 	if buildN {
@@ -297,6 +292,16 @@
 	return nil
 }
 
+func importsApp(pkg *build.Package) error {
+	// Building a program, make sure it is appropriate for mobile.
+	for _, path := range pkg.Imports {
+		if path == "golang.org/x/mobile/app" {
+			return nil
+		}
+	}
+	return fmt.Errorf(`%s does not import "golang.org/x/mobile/app"`, pkg.ImportPath)
+}
+
 var xout io.Writer = os.Stderr
 
 func printcmd(format string, args ...interface{}) {