cmd/gobind: update export data for imported packages

CL 30093 removed the go install step from gobind to avoid errors from
circular dependencies with Java reverse wrapper. However, if a
dependency is either never installed or outdated, gobind will fail.

Reinstate the go install step, but ignore errors from the go install
tool.

Tested manually by wiping the $GOPATH/pkg directory and running the
cmd/gobind tests.

Updates golang/go#19046

Change-Id: I31832eccab09b2a7cf29e5d5bc1cc76963b7c2ef
Reviewed-on: https://go-review.googlesource.com/37326
Reviewed-by: David Crawshaw <crawshaw@golang.org>
diff --git a/cmd/gobind/main.go b/cmd/gobind/main.go
index 62f19cd..928a3ac 100644
--- a/cmd/gobind/main.go
+++ b/cmd/gobind/main.go
@@ -16,7 +16,9 @@
 	"io/ioutil"
 	"log"
 	"os"
+	"os/exec"
 	"path/filepath"
+	"strings"
 
 	"golang.org/x/mobile/internal/importers"
 	"golang.org/x/mobile/internal/importers/java"
@@ -86,6 +88,19 @@
 		}
 	}
 
+	// Make sure the export data for any imported packages are up to date.
+	cmd := exec.Command("go", "install")
+	cmd.Args = append(cmd.Args, flag.Args()...)
+	cmd.Env = append(os.Environ(), "GOPATH="+ctx.GOPATH)
+	if err := cmd.Run(); err != nil {
+		// Only report I/O errors. Errors from go install is expected for as-yet
+		// undefined Java wrappers.
+		if _, ok := err.(*exec.ExitError); !ok {
+			fmt.Fprintf(os.Stderr, "%s failed: %v", strings.Join(cmd.Args, " "), err)
+			os.Exit(1)
+		}
+	}
+
 	typePkgs := make([]*types.Package, len(allPkg))
 	fset := token.NewFileSet()
 	conf := &types.Config{