cmd/gomobile: support vendored "golang.org/x/mobile/..." packages

gomobile checks "golang.org/x/mobile/..." packages are included in
the build product using nm.
If "golang/x/mobile/..." package is placed under vendor directory,
this check fails and it is treated as a build failure.

Fixes golang/go#22152

Change-Id: Ie0e05c3e0340b3608de5c68bb3f852d4ef7cdeb1
Reviewed-on: https://go-review.googlesource.com/102095
Reviewed-by: Elias Naur <elias.naur@gmail.com>
diff --git a/cmd/gomobile/build.go b/cmd/gomobile/build.go
index 2617944..2fa32d7 100644
--- a/cmd/gomobile/build.go
+++ b/cmd/gomobile/build.go
@@ -134,7 +134,7 @@
 	return nil
 }
 
-var nmRE = regexp.MustCompile(`[0-9a-f]{8} t (golang.org/x.*/[^.]*)`)
+var nmRE = regexp.MustCompile(`[0-9a-f]{8} t (?:.*/vendor/)?(golang.org/x.*/[^.]*)`)
 
 func extractPkgs(nm string, path string) (map[string]bool, error) {
 	if buildN {
diff --git a/cmd/gomobile/build_test.go b/cmd/gomobile/build_test.go
index 989acc0..b09a167 100644
--- a/cmd/gomobile/build_test.go
+++ b/cmd/gomobile/build_test.go
@@ -146,3 +146,33 @@
 		}
 	}
 }
+
+func TestRegexImportGolangXPackage(t *testing.T) {
+	tests := []struct {
+		in      string
+		want    string
+		wantLen int
+	}{
+		{"ffffffff t golang.org/x/mobile", "golang.org/x/mobile", 2},
+		{"ffffffff t github.com/example/repo/vendor/golang.org/x/mobile", "golang.org/x/mobile", 2},
+		{"ffffffff t github.com/example/golang.org/x/mobile", "", 0},
+		{"ffffffff t github.com/example/repo", "", 0},
+		{"ffffffff t github.com/example/repo/vendor", "", 0},
+	}
+
+	for _, tc := range tests {
+		res := nmRE.FindStringSubmatch(tc.in)
+		if len(res) != tc.wantLen {
+			t.Errorf("nmRE returned unexpected result for %q: want len(res) = %d, got %d",
+				tc.in, tc.wantLen, len(res))
+			continue
+		}
+		if tc.wantLen == 0 {
+			continue
+		}
+		if res[1] != tc.want {
+			t.Errorf("nmRE returned unexpected result. want (%v), got (%v)",
+				tc.want, res[1])
+		}
+	}
+}