forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go2make.go
215 lines (189 loc) · 5.28 KB
/
go2make.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
goflag "flag"
"fmt"
"go/build"
"io"
"os"
"sort"
"strings"
"github.com/spf13/pflag"
)
var flPrune = pflag.StringSlice("prune", nil, "sub-packages to prune (recursive, may be specified multiple times)")
var flDebug = pflag.BoolP("debug", "d", false, "enable debugging output")
var flHelp = pflag.BoolP("help", "h", false, "print help and exit")
func main() {
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
pflag.Usage = func() { help(os.Stderr) }
pflag.Parse()
debug("PWD", getwd())
build.Default.BuildTags = []string{"ignore_autogenerated"}
build.Default.UseAllFiles = false
if *flHelp {
help(os.Stdout)
os.Exit(0)
}
if len(pflag.Args()) == 0 {
help(os.Stderr)
os.Exit(1)
}
for _, in := range pflag.Args() {
if strings.HasSuffix(in, "/...") {
// Recurse.
debug("starting", in)
pkgName := strings.TrimSuffix(in, "/...")
if err := WalkPkg(pkgName, visitPkg); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
} else {
// Import one package.
if err := saveImport(in); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
}
}
}
func help(out io.Writer) {
fmt.Fprintf(out, "Usage: %s [FLAG...] <PKG...>\n", os.Args[0])
fmt.Fprintf(out, "\n")
fmt.Fprintf(out, "go2make calculates all of the dependencies of a set of Go packages and prints\n")
fmt.Fprintf(out, "them as variable definitions suitable for use as a Makefile.\n")
fmt.Fprintf(out, "\n")
fmt.Fprintf(out, "Package specifications may be simple (e.g. 'example.com/txt/color') or\n")
fmt.Fprintf(out, "recursive (e.g. 'example.com/txt/...')\n")
fmt.Fprintf(out, " Example:\n")
fmt.Fprintf(out, " $ %s ./example.com/pretty\n", os.Args[0])
fmt.Fprintf(out, " example.com/txt/split := \\\n")
fmt.Fprintf(out, " /go/src/example.com/txt/split/ \\\n")
fmt.Fprintf(out, " /go/src/example.com/txt/split/split.go \\\n")
fmt.Fprintf(out, " ./example.com/pretty := \\\n")
fmt.Fprintf(out, " /go/src/example.com/pretty/ \\\n")
fmt.Fprintf(out, " /go/src/example.com/pretty/print.go \\\n")
fmt.Fprintf(out, " /go/src/example.com/txt/split/ \\\n")
fmt.Fprintf(out, " /go/src/example.com/txt/split/split.go\n")
fmt.Fprintf(out, "\n")
fmt.Fprintf(out, " Flags:\n")
pflag.PrintDefaults()
}
func debug(items ...interface{}) {
if *flDebug {
x := []interface{}{"DBG:"}
x = append(x, items...)
fmt.Println(x...)
}
}
func visitPkg(importPath, absPath string) error {
debug("visit", importPath)
return saveImport(importPath)
}
func prune(pkgName string) bool {
for _, pr := range *flPrune {
if pr == pkgName {
return true
}
}
return false
}
// cache keeps track of which packages we have already loaded.
var cache = map[string]*build.Package{}
func saveImport(pkgName string) error {
if cache[pkgName] != nil {
return nil
}
if prune(pkgName) {
debug("prune", pkgName)
return ErrSkipPkg
}
pkg, err := loadPackage(pkgName)
if err != nil {
return err
}
debug("save", pkgName)
cache[pkgName] = pkg
debug("recurse", pkgName)
defer func() { debug("done ", pkgName) }()
if !pkg.Goroot && (len(pkg.GoFiles)+len(pkg.Imports) > 0) {
// Process deps of this package before the package itself.
for _, impName := range pkg.Imports {
if impName == "C" {
continue
}
debug("depends on", impName)
saveImport(impName)
}
// Emit a variable for each package.
var buf bytes.Buffer
buf.WriteString(pkgName)
buf.WriteString(" := ")
// Packages depend on their own directories, their own files, and
// transitive list of all deps' directories and files.
all := map[string]struct{}{}
all[pkg.Dir+"/"] = struct{}{}
filesForPkg(pkg, all)
for _, imp := range pkg.Imports {
pkg := cache[imp]
if pkg == nil || pkg.Goroot {
continue
}
all[pkg.Dir+"/"] = struct{}{}
filesForPkg(pkg, all)
}
// Sort and de-dup them.
files := flatten(all)
for _, f := range files {
buf.WriteString(" \\\n ")
buf.WriteString(f)
}
fmt.Println(buf.String())
}
return nil
}
func filesForPkg(pkg *build.Package, all map[string]struct{}) {
for _, file := range pkg.GoFiles {
if pkg.Dir != "." {
file = pkg.Dir + "/" + file
}
all[file] = struct{}{}
}
}
func flatten(all map[string]struct{}) []string {
list := make([]string, 0, len(all))
for k := range all {
list = append(list, k)
}
sort.Strings(list)
return list
}
func loadPackage(pkgName string) (*build.Package, error) {
debug("load", pkgName)
pkg, err := build.Import(pkgName, getwd(), 0)
if err != nil {
// We can ignore NoGoError. Anything else is real.
if _, ok := err.(*build.NoGoError); !ok {
return nil, err
}
}
return pkg, nil
}
func getwd() string {
pwd, err := os.Getwd()
if err != nil {
panic(fmt.Sprintf("can't get working directory: %v", err))
}
return pwd
}