cmd/gomobile: test for XCode in gomobile init

Previously, gomobile init assumed that the XCode developer tools
were available when running on darwin. That is not always the case,
in particular for Android developers on macOS.

Replace the GOOS check with an explicit check for the xcrun binary.

Change-Id: Ie5ae917288932cc641a17f904ed9822a105367cc
Reviewed-on: https://go-review.googlesource.com/35852
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/cmd/gomobile/build.go b/cmd/gomobile/build.go
index 3b36502..c1e9957 100644
--- a/cmd/gomobile/build.go
+++ b/cmd/gomobile/build.go
@@ -14,7 +14,6 @@
 	"os"
 	"os/exec"
 	"regexp"
-	"runtime"
 	"strings"
 )
 
@@ -110,8 +109,8 @@
 		}
 	case "darwin":
 		// TODO: use targetArchs?
-		if runtime.GOOS != "darwin" {
-			return fmt.Errorf("-target=ios requires darwin host")
+		if !xcodeAvailable() {
+			return fmt.Errorf("-target=ios requires XCode")
 		}
 		if pkg.Name != "main" {
 			if err := goBuild(pkg.ImportPath, darwinArmEnv); err != nil {
diff --git a/cmd/gomobile/env.go b/cmd/gomobile/env.go
index fbddaba..e96848b 100644
--- a/cmd/gomobile/env.go
+++ b/cmd/gomobile/env.go
@@ -149,7 +149,7 @@
 		}
 	}
 
-	if runtime.GOOS != "darwin" {
+	if !xcodeAvailable() {
 		return nil
 	}
 
@@ -378,3 +378,8 @@
 		minGoVer:   go1_6,
 	},
 }
+
+func xcodeAvailable() bool {
+	_, err := exec.LookPath("xcrun")
+	return err == nil
+}
diff --git a/cmd/gomobile/init.go b/cmd/gomobile/init.go
index 5b8a5e0..120a9bb 100644
--- a/cmd/gomobile/init.go
+++ b/cmd/gomobile/init.go
@@ -315,8 +315,8 @@
 }
 
 func installDarwin() error {
-	if goos != "darwin" {
-		return nil // Only build iOS compilers on OS X.
+	if !xcodeAvailable() {
+		return nil
 	}
 	if err := installStd(darwinArmEnv); err != nil {
 		return err
diff --git a/cmd/gomobile/version.go b/cmd/gomobile/version.go
index 5ce4551..08bd317 100644
--- a/cmd/gomobile/version.go
+++ b/cmd/gomobile/version.go
@@ -51,7 +51,7 @@
 
 	// Supported platforms
 	platforms := "android"
-	if goos == "darwin" {
+	if xcodeAvailable() {
 		platforms = "android,ios"
 	}