1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file exercises the import parser but also checks that
6 // some low-level packages do not have new dependencies added.
16 // pkgDeps defines the expected dependencies between packages in
17 // the Go source tree. It is a statement of policy.
18 // Changes should not be made to this map without prior discussion.
20 // The map contains two kinds of entries:
21 // 1) Lower-case keys are standard import paths and list the
22 // allowed imports in that package.
23 // 2) Upper-case keys define aliases for package sets, which can then
24 // be used as dependencies by other rules.
26 // DO NOT CHANGE THIS DATA TO FIX BUILDS.
28 var pkgDeps
= map[string][]string{
29 // L0 is the lowest level, core, nearly unavoidable packages.
31 "io": {"errors", "sync"},
32 "runtime": {"unsafe"},
33 "sync": {"runtime", "sync/atomic", "unsafe"},
34 "sync/atomic": {"unsafe"},
46 // L1 adds simple functions and strings processing,
47 // but not Unicode tables.
49 "math/cmplx": {"math"},
50 "math/rand": {"L0", "math"},
52 "strconv": {"L0", "unicode/utf8", "math"},
67 // L2 adds Unicode and strings processing.
68 "bufio": {"L0", "unicode/utf8", "bytes"},
69 "bytes": {"L0", "unicode", "unicode/utf8"},
70 "path": {"L0", "unicode/utf8", "strings"},
71 "strings": {"L0", "unicode", "unicode/utf8"},
83 // L3 adds reflection and some basic utility packages
84 // and interface definitions, but nothing that makes
86 "crypto": {"L2", "hash"}, // interfaces
87 "crypto/cipher": {"L2", "crypto/subtle"}, // interfaces
89 "encoding/base32": {"L2"},
90 "encoding/base64": {"L2"},
91 "encoding/binary": {"L2", "reflect"},
92 "hash": {"L2"}, // interfaces
93 "hash/adler32": {"L2", "hash"},
94 "hash/crc32": {"L2", "hash"},
95 "hash/crc64": {"L2", "hash"},
96 "hash/fnv": {"L2", "hash"},
97 "image": {"L2", "image/color"}, // interfaces
98 "image/color": {"L2"}, // interfaces
99 "image/color/palette": {"L2", "image/color"},
117 "image/color/palette",
121 // End of linear dependency definitions.
123 // Operating system access.
124 "syscall": {"L0", "unicode/utf16"},
125 "time": {"L0", "syscall"},
126 "os": {"L1", "os", "syscall", "time"},
127 "path/filepath": {"L2", "os", "syscall"},
128 "io/ioutil": {"L2", "os", "path/filepath", "time"},
129 "os/exec": {"L2", "os", "path/filepath", "syscall"},
130 "os/signal": {"L2", "os", "syscall"},
132 // OS enables basic operating system functionality,
133 // but not direct use of package syscall, nor os/signal.
142 // Formatted I/O: few dependencies (L1) but we must add reflect.
143 "fmt": {"L1", "os", "reflect"},
144 "log": {"L1", "os", "fmt", "time"},
146 // Packages used by testing must be low-level (L2+fmt).
147 "regexp": {"L2", "regexp/syntax"},
148 "regexp/syntax": {"L2"},
149 "runtime/debug": {"L2", "fmt", "io/ioutil", "os", "time"},
150 "runtime/pprof": {"L2", "fmt", "text/tabwriter"},
151 "text/tabwriter": {"L2"},
153 "testing": {"L2", "flag", "fmt", "os", "runtime/pprof", "time"},
154 "testing/iotest": {"L2", "log"},
155 "testing/quick": {"L2", "flag", "fmt", "reflect"},
157 // L4 is defined as L3+fmt+log+time, because in general once
158 // you're using L3 packages, use of fmt, log, or time is not a big deal.
167 "go/ast": {"L4", "OS", "go/scanner", "go/token"},
168 "go/doc": {"L4", "go/ast", "go/token", "regexp", "text/template"},
169 "go/parser": {"L4", "OS", "go/ast", "go/scanner", "go/token"},
170 "go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"},
171 "go/scanner": {"L4", "OS", "go/token"},
184 "archive/tar": {"L4", "OS", "syscall"},
185 "archive/zip": {"L4", "OS", "compress/flate"},
186 "compress/bzip2": {"L4"},
187 "compress/flate": {"L4"},
188 "compress/gzip": {"L4", "compress/flate"},
189 "compress/lzw": {"L4"},
190 "compress/zlib": {"L4", "compress/flate"},
191 "database/sql": {"L4", "container/list", "database/sql/driver"},
192 "database/sql/driver": {"L4", "time"},
193 "debug/dwarf": {"L4"},
194 "debug/elf": {"L4", "OS", "debug/dwarf"},
195 "debug/gosym": {"L4"},
196 "debug/macho": {"L4", "OS", "debug/dwarf"},
197 "debug/pe": {"L4", "OS", "debug/dwarf"},
199 "encoding/ascii85": {"L4"},
200 "encoding/asn1": {"L4", "math/big"},
201 "encoding/csv": {"L4"},
202 "encoding/gob": {"L4", "OS", "encoding"},
203 "encoding/hex": {"L4"},
204 "encoding/json": {"L4", "encoding"},
205 "encoding/pem": {"L4"},
206 "encoding/xml": {"L4", "encoding"},
207 "flag": {"L4", "OS"},
208 "go/build": {"L4", "OS", "GOPARSER"},
210 "image/draw": {"L4"},
211 "image/gif": {"L4", "compress/lzw", "image/color/palette", "image/draw"},
212 "image/jpeg": {"L4"},
213 "image/png": {"L4", "compress/zlib"},
214 "index/suffixarray": {"L4", "regexp"},
216 "mime": {"L4", "OS", "syscall"},
218 "text/scanner": {"L4", "OS"},
219 "text/template/parse": {"L4"},
222 "L4", "OS", "encoding/json", "html", "text/template",
223 "text/template/parse",
226 "L4", "OS", "net/url", "text/template/parse",
230 "runtime/cgo": {"L0", "C"},
231 "CGO": {"C", "runtime/cgo"},
233 // Fake entry to satisfy the pseudo-import "C"
234 // that shows up in programs that use cgo.
237 // Plan 9 alone needs io/ioutil and os.
238 "os/user": {"L4", "CGO", "io/ioutil", "os", "syscall"},
241 // Because net must be used by any package that wants to
242 // do networking portably, it must have a small dependency set: just L1+basic os.
243 "net": {"L1", "CGO", "os", "syscall", "time"},
245 // NET enables use of basic network-related packages.
253 // Uses of networking.
254 "log/syslog": {"L4", "OS", "net"},
255 "net/mail": {"L4", "NET", "OS"},
256 "net/textproto": {"L4", "OS", "net"},
259 "crypto/aes": {"L3"},
260 "crypto/des": {"L3"},
261 "crypto/hmac": {"L3"},
262 "crypto/md5": {"L3"},
263 "crypto/rc4": {"L3"},
264 "crypto/sha1": {"L3"},
265 "crypto/sha256": {"L3"},
266 "crypto/sha512": {"L3"},
279 // Random byte, number generation.
280 // This would be part of core crypto except that it imports
281 // math/big, which imports fmt.
282 "crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall"},
284 // Mathematical crypto: dependencies on fmt (L4) and math/big.
285 // We could avoid some of the fmt, but math/big imports fmt anyway.
286 "crypto/dsa": {"L4", "CRYPTO", "math/big"},
287 "crypto/ecdsa": {"L4", "CRYPTO", "crypto/elliptic", "math/big"},
288 "crypto/elliptic": {"L4", "CRYPTO", "math/big"},
289 "crypto/rsa": {"L4", "CRYPTO", "crypto/rand", "math/big"},
304 "L4", "CRYPTO-MATH", "CGO", "OS",
305 "container/list", "crypto/x509", "encoding/pem", "net", "syscall",
308 "L4", "CRYPTO-MATH", "OS", "CGO",
309 "crypto/x509/pkix", "encoding/pem", "encoding/hex", "net", "syscall",
311 "crypto/x509/pkix": {"L4", "CRYPTO-MATH"},
313 // Simple net+crypto-aware packages.
314 "mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto"},
315 "net/smtp": {"L4", "CRYPTO", "NET", "crypto/tls"},
317 // HTTP, kingpin of dependencies.
320 "compress/gzip", "crypto/tls", "mime/multipart", "runtime/debug",
323 // HTTP-using packages.
324 "expvar": {"L4", "OS", "encoding/json", "net/http"},
325 "net/http/cgi": {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"},
326 "net/http/fcgi": {"L4", "NET", "OS", "net/http", "net/http/cgi"},
327 "net/http/httptest": {"L4", "NET", "OS", "crypto/tls", "flag", "net/http"},
328 "net/http/httputil": {"L4", "NET", "OS", "net/http"},
329 "net/http/pprof": {"L4", "OS", "html/template", "net/http", "runtime/pprof"},
330 "net/rpc": {"L4", "NET", "encoding/gob", "net/http", "text/template"},
331 "net/rpc/jsonrpc": {"L4", "NET", "encoding/json", "net/rpc"},
334 // isMacro reports whether p is a package dependency macro
336 func isMacro(p
string) bool {
337 return 'A' <= p
[0] && p
[0] <= 'Z'
340 func allowed(pkg
string) map[string]bool {
341 m
:= map[string]bool{}
342 var allow
func(string)
343 allow
= func(p
string) {
347 m
[p
] = true // set even for macros, to avoid loop on cycle
349 // Upper-case names are macro-expanded.
351 for _
, pp
:= range pkgDeps
[p
] {
356 for _
, pp
:= range pkgDeps
[pkg
] {
362 var bools
= []bool{false, true}
363 var geese
= []string{"darwin", "dragonfly", "freebsd", "linux", "nacl", "netbsd", "openbsd", "plan9", "solaris", "windows"}
364 var goarches
= []string{"386", "amd64", "arm", "arm64"}
370 // allowedErrors are the operating systems and packages known to contain errors
371 // (currently just "no Go source files")
372 var allowedErrors
= map[osPkg
]bool{
373 osPkg
{"windows", "log/syslog"}: true,
374 osPkg
{"plan9", "log/syslog"}: true,
377 func TestDependencies(t
*testing
.T
) {
378 if runtime
.GOOS
== "nacl" {
379 // NaCl tests run in a limited file system and we do not
380 // provide access to every source file.
381 t
.Skip("skipping on NaCl")
385 for k
:= range pkgDeps
{
391 test
:= func(mustImport
bool) {
392 for _
, pkg
:= range all
{
396 if pkg
== "runtime/cgo" && !ctxt
.CgoEnabled
{
399 p
, err
:= ctxt
.Import(pkg
, "", 0)
401 if allowedErrors
[osPkg
{ctxt
.GOOS
, pkg
}] {
404 if !ctxt
.CgoEnabled
&& pkg
== "runtime/cgo" {
407 // Some of the combinations we try might not
408 // be reasonable (like arm,plan9,cgo), so ignore
409 // errors for the auto-generated combinations.
413 t
.Errorf("%s/%s/cgo=%v %v", ctxt
.GOOS
, ctxt
.GOARCH
, ctxt
.CgoEnabled
, err
)
418 for _
, imp
:= range p
.Imports
{
420 bad
= append(bad
, imp
)
424 t
.Errorf("%s/%s/cgo=%v unexpected dependency: %s imports %v", ctxt
.GOOS
, ctxt
.GOARCH
, ctxt
.CgoEnabled
, pkg
, bad
)
431 t
.Logf("skipping other systems")
435 for _
, ctxt
.GOOS
= range geese
{
436 for _
, ctxt
.GOARCH
= range goarches
{
437 for _
, ctxt
.CgoEnabled
= range bools
{