libgo: update to Go 1.6.1 release
[official-gcc.git] / libgo / go / go / build / deps_test.go
blobc7cd8804da7b75e68488e2a3bfcdd90455b426ac
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.
8 package build
10 import (
11 "bytes"
12 "fmt"
13 "io/ioutil"
14 "os"
15 "path/filepath"
16 "runtime"
17 "sort"
18 "strconv"
19 "strings"
20 "testing"
23 // pkgDeps defines the expected dependencies between packages in
24 // the Go source tree. It is a statement of policy.
25 // Changes should not be made to this map without prior discussion.
27 // The map contains two kinds of entries:
28 // 1) Lower-case keys are standard import paths and list the
29 // allowed imports in that package.
30 // 2) Upper-case keys define aliases for package sets, which can then
31 // be used as dependencies by other rules.
33 // DO NOT CHANGE THIS DATA TO FIX BUILDS.
35 var pkgDeps = map[string][]string{
36 // L0 is the lowest level, core, nearly unavoidable packages.
37 "errors": {},
38 "io": {"errors", "sync"},
39 "runtime": {"unsafe", "runtime/internal/atomic", "runtime/internal/sys"},
40 "runtime/internal/sys": {},
41 "runtime/internal/atomic": {"unsafe", "runtime/internal/sys"},
42 "internal/race": {"runtime", "unsafe"},
43 "sync": {"internal/race", "runtime", "sync/atomic", "unsafe"},
44 "sync/atomic": {"unsafe"},
45 "unsafe": {},
47 "L0": {
48 "errors",
49 "io",
50 "runtime",
51 "runtime/internal/atomic",
52 "sync",
53 "sync/atomic",
54 "unsafe",
57 // L1 adds simple functions and strings processing,
58 // but not Unicode tables.
59 "math": {"unsafe"},
60 "math/cmplx": {"math"},
61 "math/rand": {"L0", "math"},
62 "sort": {},
63 "strconv": {"L0", "unicode/utf8", "math"},
64 "unicode/utf16": {},
65 "unicode/utf8": {},
67 "L1": {
68 "L0",
69 "math",
70 "math/cmplx",
71 "math/rand",
72 "sort",
73 "strconv",
74 "unicode/utf16",
75 "unicode/utf8",
78 // L2 adds Unicode and strings processing.
79 "bufio": {"L0", "unicode/utf8", "bytes"},
80 "bytes": {"L0", "unicode", "unicode/utf8"},
81 "path": {"L0", "unicode/utf8", "strings"},
82 "strings": {"L0", "unicode", "unicode/utf8"},
83 "unicode": {},
85 "L2": {
86 "L1",
87 "bufio",
88 "bytes",
89 "path",
90 "strings",
91 "unicode",
94 // L3 adds reflection and some basic utility packages
95 // and interface definitions, but nothing that makes
96 // system calls.
97 "crypto": {"L2", "hash"}, // interfaces
98 "crypto/cipher": {"L2", "crypto/subtle"}, // interfaces
99 "crypto/subtle": {},
100 "encoding/base32": {"L2"},
101 "encoding/base64": {"L2"},
102 "encoding/binary": {"L2", "reflect"},
103 "hash": {"L2"}, // interfaces
104 "hash/adler32": {"L2", "hash"},
105 "hash/crc32": {"L2", "hash"},
106 "hash/crc64": {"L2", "hash"},
107 "hash/fnv": {"L2", "hash"},
108 "image": {"L2", "image/color"}, // interfaces
109 "image/color": {"L2"}, // interfaces
110 "image/color/palette": {"L2", "image/color"},
111 "reflect": {"L2"},
113 "L3": {
114 "L2",
115 "crypto",
116 "crypto/cipher",
117 "crypto/subtle",
118 "encoding/base32",
119 "encoding/base64",
120 "encoding/binary",
121 "hash",
122 "hash/adler32",
123 "hash/crc32",
124 "hash/crc64",
125 "hash/fnv",
126 "image",
127 "image/color",
128 "image/color/palette",
129 "reflect",
132 // End of linear dependency definitions.
134 // Operating system access.
135 "syscall": {"L0", "internal/race", "internal/syscall/windows/sysdll", "unicode/utf16"},
136 "internal/syscall/unix": {"L0", "syscall"},
137 "internal/syscall/windows": {"L0", "syscall", "internal/syscall/windows/sysdll"},
138 "internal/syscall/windows/registry": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
139 "time": {"L0", "syscall", "internal/syscall/windows/registry"},
140 "os": {"L1", "os", "syscall", "time", "internal/syscall/windows"},
141 "path/filepath": {"L2", "os", "syscall"},
142 "io/ioutil": {"L2", "os", "path/filepath", "time"},
143 "os/exec": {"L2", "os", "path/filepath", "syscall"},
144 "os/signal": {"L2", "os", "syscall"},
146 // OS enables basic operating system functionality,
147 // but not direct use of package syscall, nor os/signal.
148 "OS": {
149 "io/ioutil",
150 "os",
151 "os/exec",
152 "path/filepath",
153 "time",
156 // Formatted I/O: few dependencies (L1) but we must add reflect.
157 "fmt": {"L1", "os", "reflect"},
158 "log": {"L1", "os", "fmt", "time"},
160 // Packages used by testing must be low-level (L2+fmt).
161 "regexp": {"L2", "regexp/syntax"},
162 "regexp/syntax": {"L2"},
163 "runtime/debug": {"L2", "fmt", "io/ioutil", "os", "time"},
164 "runtime/pprof": {"L2", "fmt", "text/tabwriter"},
165 "runtime/trace": {"L0"},
166 "text/tabwriter": {"L2"},
168 "testing": {"L2", "flag", "fmt", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"},
169 "testing/iotest": {"L2", "log"},
170 "testing/quick": {"L2", "flag", "fmt", "reflect"},
171 "internal/testenv": {"L2", "os", "testing"},
173 // L4 is defined as L3+fmt+log+time, because in general once
174 // you're using L3 packages, use of fmt, log, or time is not a big deal.
175 "L4": {
176 "L3",
177 "fmt",
178 "log",
179 "time",
182 // Go parser.
183 "go/ast": {"L4", "OS", "go/scanner", "go/token"},
184 "go/doc": {"L4", "go/ast", "go/token", "regexp", "text/template"},
185 "go/parser": {"L4", "OS", "go/ast", "go/scanner", "go/token"},
186 "go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"},
187 "go/scanner": {"L4", "OS", "go/token"},
188 "go/token": {"L4"},
190 "GOPARSER": {
191 "go/ast",
192 "go/doc",
193 "go/parser",
194 "go/printer",
195 "go/scanner",
196 "go/token",
199 "go/format": {"L4", "GOPARSER", "internal/format"},
200 "internal/format": {"L4", "GOPARSER"},
202 // Go type checking.
203 "go/constant": {"L4", "go/token", "math/big"},
204 "go/importer": {"L4", "go/internal/gcimporter", "go/internal/gccgoimporter", "go/types"},
205 "go/internal/gcimporter": {"L4", "OS", "go/build", "go/constant", "go/token", "go/types", "text/scanner"},
206 "go/internal/gccgoimporter": {"L4", "OS", "debug/elf", "go/constant", "go/token", "go/types", "text/scanner"},
207 "go/types": {"L4", "GOPARSER", "container/heap", "go/constant"},
209 // One of a kind.
210 "archive/tar": {"L4", "OS", "syscall"},
211 "archive/zip": {"L4", "OS", "compress/flate"},
212 "container/heap": {"sort"},
213 "compress/bzip2": {"L4"},
214 "compress/flate": {"L4"},
215 "compress/gzip": {"L4", "compress/flate"},
216 "compress/lzw": {"L4"},
217 "compress/zlib": {"L4", "compress/flate"},
218 "database/sql": {"L4", "container/list", "database/sql/driver"},
219 "database/sql/driver": {"L4", "time"},
220 "debug/dwarf": {"L4"},
221 "debug/elf": {"L4", "OS", "debug/dwarf", "compress/zlib"},
222 "debug/gosym": {"L4"},
223 "debug/macho": {"L4", "OS", "debug/dwarf"},
224 "debug/pe": {"L4", "OS", "debug/dwarf"},
225 "debug/plan9obj": {"L4", "OS"},
226 "encoding": {"L4"},
227 "encoding/ascii85": {"L4"},
228 "encoding/asn1": {"L4", "math/big"},
229 "encoding/csv": {"L4"},
230 "encoding/gob": {"L4", "OS", "encoding"},
231 "encoding/hex": {"L4"},
232 "encoding/json": {"L4", "encoding"},
233 "encoding/pem": {"L4"},
234 "encoding/xml": {"L4", "encoding"},
235 "flag": {"L4", "OS"},
236 "go/build": {"L4", "OS", "GOPARSER"},
237 "html": {"L4"},
238 "image/draw": {"L4", "image/internal/imageutil"},
239 "image/gif": {"L4", "compress/lzw", "image/color/palette", "image/draw"},
240 "image/internal/imageutil": {"L4"},
241 "image/jpeg": {"L4", "image/internal/imageutil"},
242 "image/png": {"L4", "compress/zlib"},
243 "index/suffixarray": {"L4", "regexp"},
244 "internal/singleflight": {"sync"},
245 "internal/trace": {"L4", "OS"},
246 "math/big": {"L4"},
247 "mime": {"L4", "OS", "syscall", "internal/syscall/windows/registry"},
248 "mime/quotedprintable": {"L4"},
249 "net/internal/socktest": {"L4", "OS", "syscall"},
250 "net/url": {"L4"},
251 "text/scanner": {"L4", "OS"},
252 "text/template/parse": {"L4"},
254 "html/template": {
255 "L4", "OS", "encoding/json", "html", "text/template",
256 "text/template/parse",
258 "text/template": {
259 "L4", "OS", "net/url", "text/template/parse",
262 // Cgo.
263 // If you add a dependency on CGO, you must add the package to
264 // cgoPackages in cmd/dist/test.go.
265 "runtime/cgo": {"L0", "C"},
266 "CGO": {"C", "runtime/cgo"},
268 // Fake entry to satisfy the pseudo-import "C"
269 // that shows up in programs that use cgo.
270 "C": {},
272 // Race detector/MSan uses cgo.
273 "runtime/race": {"C"},
274 "runtime/msan": {"C"},
276 // Plan 9 alone needs io/ioutil and os.
277 "os/user": {"L4", "CGO", "io/ioutil", "os", "syscall"},
279 // Basic networking.
280 // Because net must be used by any package that wants to
281 // do networking portably, it must have a small dependency set: just L0+basic os.
282 "net": {"L0", "CGO", "math/rand", "os", "sort", "syscall", "time", "internal/syscall/windows", "internal/singleflight", "internal/race"},
284 // NET enables use of basic network-related packages.
285 "NET": {
286 "net",
287 "mime",
288 "net/textproto",
289 "net/url",
292 // Uses of networking.
293 "log/syslog": {"L4", "OS", "net"},
294 "net/mail": {"L4", "NET", "OS", "mime"},
295 "net/textproto": {"L4", "OS", "net"},
297 // Core crypto.
298 "crypto/aes": {"L3"},
299 "crypto/des": {"L3"},
300 "crypto/hmac": {"L3"},
301 "crypto/md5": {"L3"},
302 "crypto/rc4": {"L3"},
303 "crypto/sha1": {"L3"},
304 "crypto/sha256": {"L3"},
305 "crypto/sha512": {"L3"},
307 "CRYPTO": {
308 "crypto/aes",
309 "crypto/des",
310 "crypto/hmac",
311 "crypto/md5",
312 "crypto/rc4",
313 "crypto/sha1",
314 "crypto/sha256",
315 "crypto/sha512",
318 // Random byte, number generation.
319 // This would be part of core crypto except that it imports
320 // math/big, which imports fmt.
321 "crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall", "internal/syscall/unix"},
323 // Mathematical crypto: dependencies on fmt (L4) and math/big.
324 // We could avoid some of the fmt, but math/big imports fmt anyway.
325 "crypto/dsa": {"L4", "CRYPTO", "math/big"},
326 "crypto/ecdsa": {"L4", "CRYPTO", "crypto/elliptic", "math/big", "encoding/asn1"},
327 "crypto/elliptic": {"L4", "CRYPTO", "math/big"},
328 "crypto/rsa": {"L4", "CRYPTO", "crypto/rand", "math/big"},
330 "CRYPTO-MATH": {
331 "CRYPTO",
332 "crypto/dsa",
333 "crypto/ecdsa",
334 "crypto/elliptic",
335 "crypto/rand",
336 "crypto/rsa",
337 "encoding/asn1",
338 "math/big",
341 // SSL/TLS.
342 "crypto/tls": {
343 "L4", "CRYPTO-MATH", "OS",
344 "container/list", "crypto/x509", "encoding/pem", "net", "syscall",
346 "crypto/x509": {
347 "L4", "CRYPTO-MATH", "OS", "CGO",
348 "crypto/x509/pkix", "encoding/pem", "encoding/hex", "net", "syscall",
350 "crypto/x509/pkix": {"L4", "CRYPTO-MATH"},
352 // Simple net+crypto-aware packages.
353 "mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto", "mime/quotedprintable"},
354 "net/smtp": {"L4", "CRYPTO", "NET", "crypto/tls"},
356 // HTTP, kingpin of dependencies.
357 "net/http": {
358 "L4", "NET", "OS",
359 "compress/gzip", "crypto/tls", "mime/multipart", "runtime/debug",
360 "net/http/internal",
361 "internal/golang.org/x/net/http2/hpack",
363 "net/http/internal": {"L4"},
365 // HTTP-using packages.
366 "expvar": {"L4", "OS", "encoding/json", "net/http"},
367 "net/http/cgi": {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"},
368 "net/http/cookiejar": {"L4", "NET", "net/http"},
369 "net/http/fcgi": {"L4", "NET", "OS", "net/http", "net/http/cgi"},
370 "net/http/httptest": {"L4", "NET", "OS", "crypto/tls", "flag", "net/http", "net/http/internal"},
371 "net/http/httputil": {"L4", "NET", "OS", "net/http", "net/http/internal"},
372 "net/http/pprof": {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"},
373 "net/rpc": {"L4", "NET", "encoding/gob", "html/template", "net/http"},
374 "net/rpc/jsonrpc": {"L4", "NET", "encoding/json", "net/rpc"},
377 // isMacro reports whether p is a package dependency macro
378 // (uppercase name).
379 func isMacro(p string) bool {
380 return 'A' <= p[0] && p[0] <= 'Z'
383 func allowed(pkg string) map[string]bool {
384 m := map[string]bool{}
385 var allow func(string)
386 allow = func(p string) {
387 if m[p] {
388 return
390 m[p] = true // set even for macros, to avoid loop on cycle
392 // Upper-case names are macro-expanded.
393 if isMacro(p) {
394 for _, pp := range pkgDeps[p] {
395 allow(pp)
399 for _, pp := range pkgDeps[pkg] {
400 allow(pp)
402 return m
405 var bools = []bool{false, true}
406 var geese = []string{"android", "darwin", "dragonfly", "freebsd", "linux", "nacl", "netbsd", "openbsd", "plan9", "solaris", "windows"}
407 var goarches = []string{"386", "amd64", "arm", "arm64"}
409 type osPkg struct {
410 goos, pkg string
413 // allowedErrors are the operating systems and packages known to contain errors
414 // (currently just "no Go source files")
415 var allowedErrors = map[osPkg]bool{
416 osPkg{"windows", "log/syslog"}: true,
417 osPkg{"plan9", "log/syslog"}: true,
420 // listStdPkgs returns the same list of packages as "go list std".
421 func listStdPkgs(goroot string) ([]string, error) {
422 // Based on cmd/go's matchPackages function.
423 var pkgs []string
425 src := filepath.Join(goroot, "src") + string(filepath.Separator)
426 walkFn := func(path string, fi os.FileInfo, err error) error {
427 if err != nil || !fi.IsDir() || path == src {
428 return nil
431 base := filepath.Base(path)
432 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
433 return filepath.SkipDir
436 name := filepath.ToSlash(path[len(src):])
437 if name == "builtin" || name == "cmd" || strings.Contains(name, ".") {
438 return filepath.SkipDir
441 pkgs = append(pkgs, name)
442 return nil
444 if err := filepath.Walk(src, walkFn); err != nil {
445 return nil, err
447 return pkgs, nil
450 func TestDependencies(t *testing.T) {
451 iOS := runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")
452 if runtime.GOOS == "nacl" || iOS {
453 // Tests run in a limited file system and we do not
454 // provide access to every source file.
455 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
458 ctxt := Default
459 all, err := listStdPkgs(ctxt.GOROOT)
460 if err != nil {
461 t.Fatal(err)
463 sort.Strings(all)
465 for _, pkg := range all {
466 imports, err := findImports(pkg)
467 if err != nil {
468 t.Error(err)
469 continue
471 ok := allowed(pkg)
472 var bad []string
473 for _, imp := range imports {
474 if !ok[imp] {
475 bad = append(bad, imp)
478 if bad != nil {
479 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
484 var buildIgnore = []byte("\n// +build ignore")
486 func findImports(pkg string) ([]string, error) {
487 dir := filepath.Join(Default.GOROOT, "src", pkg)
488 files, err := ioutil.ReadDir(dir)
489 if err != nil {
490 return nil, err
492 var imports []string
493 var haveImport = map[string]bool{}
494 for _, file := range files {
495 name := file.Name()
496 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
497 continue
499 f, err := os.Open(filepath.Join(dir, name))
500 if err != nil {
501 return nil, err
503 var imp []string
504 data, err := readImports(f, false, &imp)
505 f.Close()
506 if err != nil {
507 return nil, fmt.Errorf("reading %v: %v", name, err)
509 if bytes.Contains(data, buildIgnore) {
510 continue
512 for _, quoted := range imp {
513 path, err := strconv.Unquote(quoted)
514 if err != nil {
515 continue
517 if !haveImport[path] {
518 haveImport[path] = true
519 imports = append(imports, path)
523 sort.Strings(imports)
524 return imports, nil