2014-03-25 16:26:38 -06:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-03-26 15:47:52 -06:00
|
|
|
"go/build"
|
2016-03-17 13:25:34 -06:00
|
|
|
"go/types"
|
2014-03-26 15:47:52 -06:00
|
|
|
"io/ioutil"
|
2014-03-25 16:26:38 -06:00
|
|
|
"os"
|
2014-03-26 10:54:20 -06:00
|
|
|
"path/filepath"
|
2014-03-25 16:26:38 -06:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-03-26 15:47:52 -06:00
|
|
|
source = flag.String("s", "", "only consider packages from src, where src is one of the supported compilers")
|
2014-03-25 16:26:38 -06:00
|
|
|
verbose = flag.Bool("v", false, "verbose mode")
|
|
|
|
)
|
|
|
|
|
2014-03-26 15:47:52 -06:00
|
|
|
// lists of registered sources and corresponding importers
|
2014-03-25 16:26:38 -06:00
|
|
|
var (
|
2014-03-26 15:47:52 -06:00
|
|
|
sources []string
|
|
|
|
importers []types.Importer
|
2014-03-25 16:26:38 -06:00
|
|
|
importFailed = errors.New("import failed")
|
|
|
|
)
|
|
|
|
|
|
|
|
func usage() {
|
|
|
|
fmt.Fprintln(os.Stderr, "usage: godex [flags] {path|qualifiedIdent}")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func report(msg string) {
|
|
|
|
fmt.Fprintln(os.Stderr, "error: "+msg)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Usage = usage
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if flag.NArg() == 0 {
|
|
|
|
report("no package name, path, or file provided")
|
|
|
|
}
|
|
|
|
|
2016-03-17 13:25:34 -06:00
|
|
|
var imp types.Importer = new(tryImporters)
|
2014-03-25 16:26:38 -06:00
|
|
|
if *source != "" {
|
2014-03-26 10:54:20 -06:00
|
|
|
imp = lookup(*source)
|
2014-03-25 16:26:38 -06:00
|
|
|
if imp == nil {
|
2014-03-26 10:54:20 -06:00
|
|
|
report("source (-s argument) must be one of: " + strings.Join(sources, ", "))
|
2014-03-25 16:26:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, arg := range flag.Args() {
|
2014-03-26 10:54:20 -06:00
|
|
|
path, name := splitPathIdent(arg)
|
2014-03-26 15:47:52 -06:00
|
|
|
logf("\tprocessing %q: path = %q, name = %s\n", arg, path, name)
|
|
|
|
|
|
|
|
// generate possible package path prefixes
|
2014-03-28 15:59:25 -06:00
|
|
|
// (at the moment we do this for each argument - should probably cache the generated prefixes)
|
2014-03-26 15:47:52 -06:00
|
|
|
prefixes := make(chan string)
|
2014-03-28 15:59:25 -06:00
|
|
|
go genPrefixes(prefixes, !filepath.IsAbs(path) && !build.IsLocalImport(path))
|
2014-03-25 16:26:38 -06:00
|
|
|
|
|
|
|
// import package
|
2016-03-17 13:25:34 -06:00
|
|
|
pkg, err := tryPrefixes(prefixes, path, imp)
|
2014-03-25 16:26:38 -06:00
|
|
|
if err != nil {
|
2014-03-26 15:47:52 -06:00
|
|
|
logf("\t=> ignoring %q: %s\n", path, err)
|
2014-03-25 16:26:38 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter objects if needed
|
2014-03-28 13:21:51 -06:00
|
|
|
var filter func(types.Object) bool
|
2014-03-25 16:26:38 -06:00
|
|
|
if name != "" {
|
|
|
|
filter = func(obj types.Object) bool {
|
|
|
|
// TODO(gri) perhaps use regular expression matching here?
|
2014-03-28 13:21:51 -06:00
|
|
|
return obj.Name() == name
|
2014-03-25 16:26:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// print contents
|
|
|
|
print(os.Stdout, pkg, filter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-26 15:47:52 -06:00
|
|
|
func logf(format string, args ...interface{}) {
|
|
|
|
if *verbose {
|
|
|
|
fmt.Fprintf(os.Stderr, format, args...)
|
2014-03-25 16:26:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-26 10:54:20 -06:00
|
|
|
// splitPathIdent splits a path.name argument into its components.
|
|
|
|
// All but the last path element may contain dots.
|
|
|
|
func splitPathIdent(arg string) (path, name string) {
|
|
|
|
if i := strings.LastIndex(arg, "."); i >= 0 {
|
2014-03-26 15:47:52 -06:00
|
|
|
if j := strings.LastIndex(arg, "/"); j < i {
|
2014-03-26 10:54:20 -06:00
|
|
|
// '.' is not part of path
|
|
|
|
path = arg[:i]
|
|
|
|
name = arg[i+1:]
|
|
|
|
return
|
|
|
|
}
|
2014-03-25 16:26:38 -06:00
|
|
|
}
|
2014-03-26 10:54:20 -06:00
|
|
|
path = arg
|
|
|
|
return
|
2014-03-25 16:26:38 -06:00
|
|
|
}
|
|
|
|
|
2014-03-26 15:47:52 -06:00
|
|
|
// tryPrefixes tries to import the package given by (the possibly partial) path using the given importer imp
|
|
|
|
// by prepending all possible prefixes to path. It returns with the first package that it could import, or
|
|
|
|
// with an error.
|
2016-03-17 13:25:34 -06:00
|
|
|
func tryPrefixes(prefixes chan string, path string, imp types.Importer) (pkg *types.Package, err error) {
|
2014-03-26 15:47:52 -06:00
|
|
|
for prefix := range prefixes {
|
2014-04-18 10:44:23 -06:00
|
|
|
actual := path
|
|
|
|
if prefix == "" {
|
|
|
|
// don't use filepath.Join as it will sanitize the path and remove
|
|
|
|
// a leading dot and then the path is not recognized as a relative
|
|
|
|
// package path by the importers anymore
|
|
|
|
logf("\ttrying no prefix\n")
|
|
|
|
} else {
|
|
|
|
actual = filepath.Join(prefix, path)
|
|
|
|
logf("\ttrying prefix %q\n", prefix)
|
|
|
|
}
|
2016-03-17 13:25:34 -06:00
|
|
|
pkg, err = imp.Import(actual)
|
2014-03-26 15:47:52 -06:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2014-04-18 10:44:23 -06:00
|
|
|
logf("\t=> importing %q failed: %s\n", actual, err)
|
2014-03-26 15:47:52 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-17 13:25:34 -06:00
|
|
|
// tryImporters is an importer that tries all registered importers
|
2014-03-26 15:47:52 -06:00
|
|
|
// successively until one of them succeeds or all of them failed.
|
2016-03-17 13:25:34 -06:00
|
|
|
type tryImporters struct{}
|
|
|
|
|
|
|
|
func (t *tryImporters) Import(path string) (pkg *types.Package, err error) {
|
2014-03-26 15:47:52 -06:00
|
|
|
for i, imp := range importers {
|
|
|
|
logf("\t\ttrying %s import\n", sources[i])
|
2016-03-17 13:25:34 -06:00
|
|
|
pkg, err = imp.Import(path)
|
2014-03-26 15:47:52 -06:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
logf("\t\t=> %s import failed: %s\n", sources[i], err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-17 13:25:34 -06:00
|
|
|
type protector struct {
|
|
|
|
imp types.Importer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *protector) Import(path string) (pkg *types.Package, err error) {
|
|
|
|
defer func() {
|
|
|
|
if recover() != nil {
|
|
|
|
pkg = nil
|
|
|
|
err = importFailed
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return p.imp.Import(path)
|
|
|
|
}
|
|
|
|
|
2014-03-26 15:47:52 -06:00
|
|
|
// protect protects an importer imp from panics and returns the protected importer.
|
|
|
|
func protect(imp types.Importer) types.Importer {
|
2016-03-17 13:25:34 -06:00
|
|
|
return &protector{imp}
|
2014-03-26 15:47:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// register registers an importer imp for a given source src.
|
2014-03-26 10:54:20 -06:00
|
|
|
func register(src string, imp types.Importer) {
|
|
|
|
if lookup(src) != nil {
|
|
|
|
panic(src + " importer already registered")
|
2014-03-25 16:26:38 -06:00
|
|
|
}
|
2014-03-26 10:54:20 -06:00
|
|
|
sources = append(sources, src)
|
2014-03-26 15:47:52 -06:00
|
|
|
importers = append(importers, protect(imp))
|
2014-03-25 16:26:38 -06:00
|
|
|
}
|
|
|
|
|
2014-03-26 15:47:52 -06:00
|
|
|
// lookup returns the importer imp for a given source src.
|
2014-03-26 10:54:20 -06:00
|
|
|
func lookup(src string) types.Importer {
|
|
|
|
for i, s := range sources {
|
|
|
|
if s == src {
|
|
|
|
return importers[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-03-25 16:26:38 -06:00
|
|
|
}
|
2014-03-26 15:47:52 -06:00
|
|
|
|
2014-03-28 15:59:25 -06:00
|
|
|
func genPrefixes(out chan string, all bool) {
|
|
|
|
out <- ""
|
|
|
|
if all {
|
|
|
|
platform := build.Default.GOOS + "_" + build.Default.GOARCH
|
|
|
|
dirnames := append([]string{build.Default.GOROOT}, filepath.SplitList(build.Default.GOPATH)...)
|
|
|
|
for _, dirname := range dirnames {
|
|
|
|
walkDir(filepath.Join(dirname, "pkg", platform), "", out)
|
|
|
|
}
|
2014-03-26 15:47:52 -06:00
|
|
|
}
|
|
|
|
close(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func walkDir(dirname, prefix string, out chan string) {
|
|
|
|
fiList, err := ioutil.ReadDir(dirname)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, fi := range fiList {
|
|
|
|
if fi.IsDir() && !strings.HasPrefix(fi.Name(), ".") {
|
|
|
|
prefix := filepath.Join(prefix, fi.Name())
|
|
|
|
out <- prefix
|
|
|
|
walkDir(filepath.Join(dirname, fi.Name()), prefix, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|