2
0
mirror of synced 2025-02-23 23:08:14 +00:00

cmd/gobind: remove go/build and go/import usages from main.go

This change removes the usages of go/build and go/import, that don't
work with Go module.

Before this change:

  1) Package information is retrieved by go/packages
  2) Files for reverse bindings are generated
  3) Package information is retrieved by go/imports

After this change, only 3) will be changed:

  3) Package information is retrieved by go/packages again only when
     reverse binding is used.

This is preparation to support Go modules.

Updates golang/go#27234

Change-Id: I1997f71f23455fdd3b3c6b2643d35ec33ad147a3
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/203399
Run-TryBot: Hajime Hoshi <hajimehoshi@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
Hajime Hoshi 2019-10-25 19:55:23 +09:00
parent 73ccc5ba04
commit 0945064e01

View File

@ -9,8 +9,6 @@ import (
"flag"
"fmt"
"go/ast"
"go/build"
"go/importer"
"go/types"
"io/ioutil"
"log"
@ -74,6 +72,7 @@ func run() {
if err != nil {
log.Fatal(err)
}
jrefs, err := importers.AnalyzePackages(allPkg, "Java/")
if err != nil {
log.Fatal(err)
@ -102,21 +101,7 @@ func run() {
}
}
ctx := build.Default
if *tags != "" {
ctx.BuildTags = append(ctx.BuildTags, strings.Split(*tags, ",")...)
}
// Determine GOPATH from go env GOPATH in case the default $HOME/go GOPATH
// is in effect.
if out, err := exec.Command(goBin(), "env", "GOPATH").Output(); err != nil {
log.Fatal(err)
} else {
ctx.GOPATH = string(bytes.TrimSpace(out))
}
if len(classes) > 0 || len(otypes) > 0 {
// After generation, reverse bindings needs to be in the GOPATH
// for user packages to build.
srcDir := *outdir
if srcDir == "" {
srcDir, err = ioutil.TempDir(os.TempDir(), "gobind-")
@ -130,10 +115,6 @@ func run() {
log.Fatal(err)
}
}
if ctx.GOPATH != "" {
ctx.GOPATH = string(filepath.ListSeparator) + ctx.GOPATH
}
ctx.GOPATH = srcDir + ctx.GOPATH
if len(classes) > 0 {
if err := genJavaPackages(srcDir, classes, jrefs.Embedders); err != nil {
log.Fatal(err)
@ -144,24 +125,34 @@ func run() {
log.Fatal(err)
}
}
// Add a new directory to GOPATH where the file for reverse bindings exist, and recreate allPkg.
// It is because the current allPkg did not solve imports for reverse bindings.
var gopath string
if out, err := exec.Command(goBin(), "env", "GOPATH").Output(); err != nil {
log.Fatal(err)
} else {
gopath = string(bytes.TrimSpace(out))
}
if gopath != "" {
gopath = string(filepath.ListSeparator) + gopath
}
gopath = srcDir + gopath
cfg.Env = append(os.Environ(), "GOPATH="+gopath)
allPkg, err = packages.Load(cfg, flag.Args()...)
if err != nil {
log.Fatal(err)
}
}
typePkgs := make([]*types.Package, len(allPkg))
astPkgs := make([][]*ast.File, len(allPkg))
// The "source" go/importer package implicitly uses build.Default.
oldCtx := build.Default
build.Default = ctx
defer func() {
build.Default = oldCtx
}()
imp := importer.For("source", nil)
for i, pkg := range allPkg {
var err error
typePkgs[i], err = imp.Import(pkg.PkgPath)
if err != nil {
errorf("%v\n", err)
if len(pkg.Errors) > 0 {
errorf("%v", pkg.Errors)
return
}
typePkgs[i] = pkg.Types
astPkgs[i] = pkg.Syntax
}
for _, l := range langs {