cmd/gobind: load from export data, not source

Replace the vendored version of x/tools/go/loader with the standard
library's go/importer package. This reads the export data from
$GOPATH/pkg/pkgname.a instead of parsing and type checking the source
code. The "go install" subcommand is invoked just prior to reading the
export data to make sure the export data is up to date.

This has the advantage of relying entirely on the go tool for correctly
resolving and parsing dependencies of the package being bound. (For
example, a bound package can now depend on cgo.) It also removes a class
of bugs where the version of the loader we depend on can get out of sync
with the go tool. (For example, gobind now correctly handles vendor
dependencies.)

As a bonus, for packages with significant dependencies this approach
should also be noticeably faster as we do not need to parse and
typecheck all of the dependencies.

Change-Id: If9a431c137eae2071c1d89be88a4a6a61d6812fa
Reviewed-on: https://go-review.googlesource.com/16911
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/cmd/gobind/gen.go b/cmd/gobind/gen.go
index f151719..21f3b37 100644
--- a/cmd/gobind/gen.go
+++ b/cmd/gobind/gen.go
@@ -6,7 +6,6 @@
 
 import (
 	"go/ast"
-	"go/build"
 	"go/parser"
 	"go/scanner"
 	"go/token"
@@ -18,37 +17,9 @@
 	"unicode/utf8"
 
 	"golang.org/x/mobile/bind"
-	"golang.org/x/mobile/internal/loader"
 )
 
-func genPkg(pkg *build.Package) {
-	files := parseFiles(pkg.Dir, pkg.GoFiles)
-	if len(files) == 0 {
-		return // some error has been reported
-	}
-
-	conf := loader.Config{
-		Fset:        fset,
-		AllowErrors: true,
-	}
-	conf.TypeChecker.IgnoreFuncBodies = true
-	conf.TypeChecker.FakeImportC = true
-	conf.TypeChecker.DisableUnusedImportCheck = true
-	var tcErrs []error
-	conf.TypeChecker.Error = func(err error) {
-		tcErrs = append(tcErrs, err)
-	}
-	conf.CreateFromFiles(pkg.ImportPath, files...)
-	program, err := conf.Load()
-	if err != nil {
-		for _, err := range tcErrs {
-			errorf("%v", err)
-		}
-		errorf("%v", err)
-		return
-	}
-	p := program.Created[0].Pkg
-
+func genPkg(p *types.Package) {
 	fname := defaultFileName(*lang, p)
 	switch *lang {
 	case "java":
diff --git a/cmd/gobind/main.go b/cmd/gobind/main.go
index ad3709a..8dd5172 100644
--- a/cmd/gobind/main.go
+++ b/cmd/gobind/main.go
@@ -7,9 +7,11 @@
 import (
 	"flag"
 	"fmt"
-	"go/build"
+	"go/importer"
 	"log"
 	"os"
+	"os/exec"
+	"strings"
 )
 
 var (
@@ -32,14 +34,22 @@
 		log.Fatalf("Invalid option -prefix for gobind -lang=%s", *lang)
 	}
 
-	cwd, err := os.Getwd()
-	if err != nil {
-		log.Fatal(err)
+	// Make sure the export data for the packages being compiled is up to
+	// date. Also use the go tool to provide good error messages for any
+	// type checking errors in the provided packages.
+	cmd := exec.Command("go", "install")
+	cmd.Stdout = os.Stdout
+	cmd.Stderr = os.Stderr
+	cmd.Args = append(cmd.Args, flag.Args()...)
+	if err := cmd.Run(); err != nil {
+		fmt.Fprintf(os.Stderr, "%s failed: %v", strings.Join(cmd.Args, " "), err)
+		os.Exit(1)
 	}
+
 	for _, arg := range flag.Args() {
-		pkg, err := build.Import(arg, cwd, 0)
+		pkg, err := importer.Default().Import(arg)
 		if err != nil {
-			fmt.Fprintf(os.Stderr, "%s: %v\n", arg, err)
+			fmt.Fprintf(os.Stderr, "could not import package %s: %v", arg, err)
 			os.Exit(1)
 		}
 		genPkg(pkg)